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 "mesh_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 speed on the tunnel limited to the slowest peer?
407 * Is the tunnel bufferless (minimum latency)?
412 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
417 * Packet ID of the last bck packet sent (unique counter per hop).
422 * SKIP value for this tunnel.
427 * Force sending ACK? Flag to allow duplicate ACK on POLL.
432 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
433 * Contains the Flow Control info: FWD ACK value received,
434 * last BCK ACK sent, PID and SKIP values.
436 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
439 * Last ACK sent towards the origin (for traffic towards leaf node).
441 uint32_t last_fwd_ack;
444 * BCK ACK value received from the hop towards the owner of the tunnel,
445 * (previous node / owner): up to what message PID can we sent back to him.
450 * How many messages are in the forward queue (towards leaves).
452 unsigned int fwd_queue_n;
455 * How many messages do we accept in the forward queue.
457 unsigned int fwd_queue_max;
460 * How many messages are in the backward queue (towards origin).
462 unsigned int bck_queue_n;
465 * How many messages do we accept in the backward queue.
467 unsigned int bck_queue_max;
470 * Task to poll peer in case of a stall.
472 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
475 * Last time the tunnel was used
477 struct GNUNET_TIME_Absolute timestamp;
480 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
481 * containing peers added by id or by type, not intermediate peers.
483 struct GNUNET_CONTAINER_MultiHashMap *peers;
486 * Number of peers that are connected and potentially ready to receive data
488 unsigned int peers_ready;
491 * Client owner of the tunnel, if any
493 struct MeshClient *owner;
496 * Clients that have been informed about and want to stay in the tunnel.
498 struct MeshClient **clients;
501 * Flow control info for each client.
503 struct MeshTunnelClientInfo *clients_fc;
506 * Number of elements in clients/clients_fc
508 unsigned int nclients;
511 * Clients that have been informed but requested to leave the tunnel.
513 struct MeshClient **ignore;
516 * Number of elements in clients
518 unsigned int nignore;
523 GNUNET_PEER_Id *blacklisted;
526 * Number of elements in blacklisted
528 unsigned int nblacklisted;
531 * Bloomfilter (for peer identities) to stop circular routes
533 char bloomfilter[MESH_BLOOM_SIZE];
538 struct MeshTunnelTree *tree;
541 * Application type we are looking for in this tunnel
543 GNUNET_MESH_ApplicationType type;
546 * Used to search peers offering a service
548 struct GNUNET_DHT_GetHandle *dht_get_type;
551 * Handle for the regex search for a connect_by_string
553 struct MeshRegexSearchInfo *regex_search;
556 * Task to keep the used paths alive
558 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
561 * Task to destroy the tunnel after timeout
563 * FIXME: merge the two? a tunnel will have either
564 * a path refresh OR a timeout, never both!
566 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
569 * Flag to signal the destruction of the tunnel.
570 * If this is set GNUNET_YES the tunnel will be destroyed
571 * when the queue is empty.
576 * Total messages pending for this tunnels, payload or not.
578 unsigned int pending_messages;
581 * If the tunnel is empty, destoy it.
583 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
588 * Info about a child node in a tunnel, needed to perform flow control.
590 struct MeshTunnelChildInfo
593 * ID of the child node.
613 * Maximum PID allowed (FWD ACK received).
618 * Last ACK sent to that child (BCK ACK).
623 * Circular buffer pointing to MeshPeerQueue elements for all
624 * payload traffic going to this child.
625 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
627 struct MeshPeerQueue **send_buffer;
630 * Index of the oldest element in the send_buffer.
632 unsigned int send_buffer_start;
635 * How many elements are already in the buffer.
637 unsigned int send_buffer_n;
640 * Tunnel this info is about
642 struct MeshTunnel *t;
645 * Task to poll peer in case of a stall.
647 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
650 * Time to use for next polling call.
652 struct GNUNET_TIME_Relative fc_poll_time;
657 * Info about a leaf client of a tunnel, needed to perform flow control.
659 struct MeshTunnelClientInfo
662 * PID of the last packet sent to the client (FWD).
667 * PID of the last packet received from the client (BCK).
672 * Maximum PID allowed (FWD ACK received).
677 * Last ACK sent to that child (BCK ACK).
685 * Info collected during iteration of child nodes in order to get the ACK value
688 struct MeshTunnelChildIteratorContext
691 * Tunnel whose info is being collected.
693 struct MeshTunnel *t;
696 * Is this context initialized? Is the value in max_child_ack valid?
701 * Maximum child ACK so far.
703 uint32_t max_child_ack;
706 * Number of children nodes
708 unsigned int nchildren;
713 * Info needed to work with tunnel paths and peers
720 struct MeshTunnel *t;
723 * Neighbouring peer to whom we send the packet to
725 struct MeshPeerInfo *peer;
730 struct MeshPeerPath *path;
735 * Struct containing information about a client of the service
742 struct MeshClient *next;
747 struct MeshClient *prev;
750 * Tunnels that belong to this client, indexed by local id
752 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
755 * Tunnels this client has accepted, indexed by incoming local id
757 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
760 * Tunnels this client has rejected, indexed by incoming local id
762 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
764 * Handle to communicate with the client
766 struct GNUNET_SERVER_Client *handle;
769 * Applications that this client has claimed to provide: H(app) = app.
771 struct GNUNET_CONTAINER_MultiHashMap *apps;
774 * Messages that this client has declared interest in
776 struct GNUNET_CONTAINER_MultiHashMap *types;
779 * Whether the client is active or shutting down (don't send confirmations
780 * to a client that is shutting down.
785 * ID of the client, mainly for debug messages
790 * Regular expressions describing the services offered by this client.
792 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
795 * Number of regular expressions in regexes.
797 unsigned int n_regex;
800 * Task to refresh all regular expresions in the DHT.
802 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
805 * Tmp store for partially retrieved regex.
812 /******************************************************************************/
813 /************************ DEBUG FUNCTIONS ****************************/
814 /******************************************************************************/
818 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
819 * @param cls string to print
820 * @param success GNUNET_OK if the PUT was transmitted,
821 * GNUNET_NO on timeout,
822 * GNUNET_SYSERR on disconnect from service
823 * after the PUT message was transmitted
824 * (so we don't know if it was received or not)
829 mesh_debug (void *cls, int success)
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
837 unsigned int debug_fwd_ack;
838 unsigned int debug_bck_ack;
842 /******************************************************************************/
843 /*********************** GLOBAL VARIABLES ****************************/
844 /******************************************************************************/
846 /************************** Configuration parameters **************************/
849 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
851 static struct GNUNET_TIME_Relative refresh_path_time;
854 * How often to PUT local application numbers in the DHT.
856 static struct GNUNET_TIME_Relative app_announce_time;
859 * How often to PUT own ID in the DHT.
861 static struct GNUNET_TIME_Relative id_announce_time;
864 * Maximum time allowed to connect to a peer found by string.
866 static struct GNUNET_TIME_Relative connect_timeout;
869 * Default TTL for payload packets.
871 static unsigned long long default_ttl;
874 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
876 static unsigned long long dht_replication_level;
879 * How many tunnels are we willing to maintain.
880 * Local tunnels are always allowed, even if there are more tunnels than max.
882 static unsigned long long max_tunnels;
885 * How many messages *in total* are we willing to queue, divided by number of
886 * tunnels to get tunnel queue size.
888 static unsigned long long max_msgs_queue;
891 * How many peers do we want to remember?
893 static unsigned long long max_peers;
896 /*************************** Static global variables **************************/
899 * Hostkey generation context
901 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
904 * DLL with all the clients, head.
906 static struct MeshClient *clients;
909 * DLL with all the clients, tail.
911 static struct MeshClient *clients_tail;
914 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
916 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
919 * Number of tunnels known.
921 static unsigned long long n_tunnels;
924 * Tunnels incoming, indexed by MESH_TunnelNumber
925 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
927 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
930 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
932 static struct GNUNET_CONTAINER_MultiHashMap *peers;
935 * Handle to communicate with transport
937 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
940 * Handle to communicate with core.
942 static struct GNUNET_CORE_Handle *core_handle;
947 static struct GNUNET_DHT_Handle *dht_handle;
952 static struct GNUNET_SERVER_Handle *server_handle;
955 * Handle to the statistics service.
957 static struct GNUNET_STATISTICS_Handle *stats;
960 * Notification context, to send messages to local clients.
962 static struct GNUNET_SERVER_NotificationContext *nc;
965 * Local peer own ID (memory efficient handle).
967 static GNUNET_PEER_Id myid;
970 * Local peer own ID (full value).
972 static struct GNUNET_PeerIdentity my_full_id;
977 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
982 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
985 * Tunnel ID for the next created tunnel (global tunnel number).
987 static MESH_TunnelNumber next_tid;
990 * Tunnel ID for the next incoming tunnel (local tunnel number).
992 static MESH_TunnelNumber next_local_tid;
995 * All application types provided by this peer: H(app) = *Client.
997 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1000 * All message types clients of this peer are interested in.
1002 static struct GNUNET_CONTAINER_MultiHashMap *types;
1005 * Task to periodically announce provided applications.
1007 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1010 * Task to periodically announce itself in the network.
1012 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1015 * Next ID to assign to a client.
1017 unsigned int next_client_id;
1020 /******************************************************************************/
1021 /*********************** DECLARATIONS **************************/
1022 /******************************************************************************/
1025 * Function to process paths received for a new peer addition. The recorded
1026 * paths form the initial tunnel, which can be optimized later.
1027 * Called on each result obtained for the DHT search.
1029 * @param cls closure
1030 * @param exp when will this value expire
1031 * @param key key of the result
1032 * @param type type of the result
1033 * @param size number of bytes in data
1034 * @param data pointer to the result data
1037 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1038 const struct GNUNET_HashCode * key,
1039 const struct GNUNET_PeerIdentity *get_path,
1040 unsigned int get_path_length,
1041 const struct GNUNET_PeerIdentity *put_path,
1042 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1043 size_t size, const void *data);
1047 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1048 * and insert it in the appropiate structures if the peer is not known yet.
1050 * @param peer Full identity of the peer.
1052 * @return Existing or newly created peer info.
1054 static struct MeshPeerInfo *
1055 peer_info_get (const struct GNUNET_PeerIdentity *peer);
1059 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1060 * and insert it in the appropiate structures if the peer is not known yet.
1062 * @param peer Short identity of the peer.
1064 * @return Existing or newly created peer info.
1066 static struct MeshPeerInfo *
1067 peer_info_get_short (const GNUNET_PEER_Id peer);
1071 * Try to establish a new connection to this peer.
1072 * Use the best path for the given tunnel.
1073 * If the peer doesn't have any path to it yet, try to get one.
1074 * If the peer already has some path, send a CREATE PATH towards it.
1076 * @param peer PeerInfo of the peer.
1077 * @param t Tunnel for which to create the path, if possible.
1080 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1084 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1085 * to obtain a local peer -> destination path and interning the peer ids.
1087 * @return Newly allocated and created path
1089 static struct MeshPeerPath *
1090 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1091 unsigned int get_path_length,
1092 const struct GNUNET_PeerIdentity *put_path,
1093 unsigned int put_path_length);
1097 * Adds a path to the peer_infos of all the peers in the path
1099 * @param p Path to process.
1100 * @param confirmed Whether we know if the path works or not.
1103 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1107 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1108 * needed rescources.
1109 * If peer already exists, reevaluate shortest path and change if different.
1111 * @param t Tunnel we want to add a new peer to
1112 * @param peer PeerInfo of the peer being added
1116 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1120 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1121 * that are no longer needed, as well as nodes of no longer reachable peers.
1122 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1124 * @param t Tunnel from which to remove the path.
1125 * @param peer Short id of the peer which should be removed.
1128 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1132 * Search for a tunnel by global ID using full PeerIdentities.
1134 * @param oid owner of the tunnel.
1135 * @param tid global tunnel number.
1137 * @return tunnel handler, NULL if doesn't exist.
1139 static struct MeshTunnel *
1140 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1144 * Delete an active client from the tunnel.
1150 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1153 * Notify a tunnel that a connection has broken that affects at least
1154 * some of its peers.
1156 * @param t Tunnel affected.
1157 * @param p1 Peer that got disconnected from p2.
1158 * @param p2 Peer that got disconnected from p1.
1160 * @return Short ID of the peer disconnected (either p1 or p2).
1161 * 0 if the tunnel remained unaffected.
1163 static GNUNET_PEER_Id
1164 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1169 * Get the current ack value for a tunnel, for data going from root to leaves,
1170 * taking in account the tunnel mode and the status of all children and clients.
1174 * @return Maximum PID allowed.
1177 tunnel_get_fwd_ack (struct MeshTunnel *t);
1181 * Add a client to a tunnel, initializing all needed data structures.
1183 * @param t Tunnel to which add the client.
1184 * @param c Client which to add to the tunnel.
1187 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1191 * @brief Queue and pass message to core when possible.
1193 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1194 * and accounts for it. In case the queue is full, the message is dropped and
1197 * Otherwise, message is treated as internal and allowed to go regardless of
1200 * @param cls Closure (@c type dependant). It will be used by queue_send to
1201 * build the message to be sent if not already prebuilt.
1202 * @param type Type of the message, 0 for a raw message.
1203 * @param size Size of the message.
1204 * @param dst Neighbor to send message to.
1205 * @param t Tunnel this message belongs to.
1208 queue_add (void *cls, uint16_t type, size_t size,
1209 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1213 * Free a transmission that was already queued with all resources
1214 * associated to the request.
1216 * @param queue Queue handler to cancel.
1217 * @param clear_cls Is it necessary to free associated cls?
1220 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1224 * @brief Get the next transmittable message from the queue.
1226 * This will be the head, except in the case of being a data packet
1227 * not allowed by the destination peer.
1229 * @param peer Destination peer.
1231 * @return The next viable MeshPeerQueue element to send to that peer.
1232 * NULL when there are no transmittable messages.
1234 struct MeshPeerQueue *
1235 queue_get_next (const struct MeshPeerInfo *peer);
1239 * Core callback to write a queued packet to core buffer
1241 * @param cls Closure (peer info).
1242 * @param size Number of bytes available in buf.
1243 * @param buf Where the to write the message.
1245 * @return number of bytes written to buf
1248 queue_send (void *cls, size_t size, void *buf);
1250 /******************************************************************************/
1251 /************************ REGEX INTEGRATION ****************************/
1252 /******************************************************************************/
1255 * Cancel a mesh regex search and free resources.
1258 regex_cancel_search (struct MeshRegexSearchInfo *regex_search)
1260 DEBUG_REGEX ("Search for %s canelled.\n", regex_search->description);
1261 GNUNET_REGEX_search_cancel (regex_search->search_handle);
1262 if (0 < regex_search->n_peers)
1263 GNUNET_free (regex_search->peers);
1264 if (GNUNET_SCHEDULER_NO_TASK != regex_search->timeout)
1266 GNUNET_SCHEDULER_cancel(regex_search->timeout);
1268 GNUNET_free (regex_search);
1273 * Function called if the connect attempt to a peer found via
1274 * connect_by_string times out. Try to connect to another peer, if any.
1275 * Otherwise try to reconnect to the same peer.
1277 * @param cls Closure (info about regex search).
1278 * @param tc TaskContext.
1281 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1283 struct MeshRegexSearchInfo *info = cls;
1284 struct MeshPeerInfo *peer_info;
1288 DEBUG_REGEX ("Regex connect timeout\n");
1289 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1290 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1292 DEBUG_REGEX (" due to shutdown\n");
1297 DEBUG_REGEX (" timed out: %u\n", old);
1299 if (0 < info->n_peers)
1301 // Select next peer, put current in that spot.
1302 id = info->peers[info->i_peer];
1303 info->peers[info->i_peer] = info->peer;
1304 info->i_peer = (info->i_peer + 1) % info->n_peers;
1308 // Try to connect to same peer again.
1311 DEBUG_REGEX (" trying: %u\n", id);
1313 peer_info = peer_info_get_short(id);
1314 tunnel_add_peer (info->t, peer_info);
1316 tunnel_delete_peer (info->t, old);
1317 peer_info_connect (peer_info, info->t);
1318 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1319 ®ex_connect_timeout,
1321 DEBUG_REGEX ("Regex connect timeout END\n");
1326 * Function to process DHT string to regex matching.
1327 * Called on each result obtained for the DHT search.
1329 * @param cls Closure provided in GNUNET_REGEX_search.
1330 * @param id Peer providing a regex that matches the string.
1331 * @param get_path Path of the get request.
1332 * @param get_path_length Lenght of get_path.
1333 * @param put_path Path of the put request.
1334 * @param put_path_length Length of the put_path.
1337 regex_found_handler (void *cls,
1338 const struct GNUNET_PeerIdentity *id,
1339 const struct GNUNET_PeerIdentity *get_path,
1340 unsigned int get_path_length,
1341 const struct GNUNET_PeerIdentity *put_path,
1342 unsigned int put_path_length)
1344 struct MeshRegexSearchInfo *info = cls;
1345 struct MeshPeerPath *p;
1346 struct MeshPeerInfo *peer_info;
1348 DEBUG_REGEX ("Got regex results from DHT!\n");
1349 DEBUG_REGEX (" for %s\n", info->description);
1351 peer_info = peer_info_get (id);
1352 p = path_build_from_dht (get_path, get_path_length,
1353 put_path, put_path_length);
1354 path_add_to_peers (p, GNUNET_NO);
1357 tunnel_add_peer (info->t, peer_info);
1358 peer_info_connect (peer_info, info->t);
1359 if (0 == info->peer)
1361 info->peer = peer_info->id;
1365 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
1368 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1371 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1372 ®ex_connect_timeout,
1380 * Store the regular expression describing a local service into the DHT.
1382 * @param regex The regular expresion.
1385 regex_put (struct MeshRegexDescriptor *regex)
1387 DEBUG_REGEX (" regex_put (%s) start\n", regex->regex);
1388 if (NULL == regex->h)
1390 DEBUG_REGEX (" first put, creating DFA\n");
1391 regex->h = GNUNET_REGEX_announce (dht_handle,
1399 DEBUG_REGEX (" not first put, using cached data\n");
1400 GNUNET_REGEX_reannounce (regex->h);
1402 DEBUG_REGEX (" regex_put (%s) end\n", regex->regex);
1407 * Periodically announce what applications are provided by local clients
1410 * @param cls closure
1411 * @param tc task context
1414 regex_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1416 struct MeshClient *c = cls;
1419 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1420 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1422 DEBUG_REGEX ("Starting announce for regex\n");
1423 for (i = 0; i < c->n_regex; i++)
1424 regex_put (&c->regexes[i]);
1425 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1428 DEBUG_REGEX ("Finished announce for regex\n");
1432 /******************************************************************************/
1433 /************************ PERIODIC FUNCTIONS ****************************/
1434 /******************************************************************************/
1437 * Announce iterator over for each application provided by the peer
1439 * @param cls closure
1440 * @param key current key code
1441 * @param value value in the hash map
1442 * @return GNUNET_YES if we should continue to
1447 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1449 struct PBlock block;
1450 struct MeshClient *c;
1452 block.id = my_full_id;
1453 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1454 GNUNET_assert(NULL != c);
1455 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1456 if (0 == block.type)
1461 block.type = htonl (block.type);
1462 DEBUG_DHT ("Putting APP key: %s\n", GNUNET_h2s (key));
1463 GNUNET_break (NULL !=
1464 GNUNET_DHT_put (dht_handle, key,
1465 dht_replication_level,
1466 GNUNET_DHT_RO_RECORD_ROUTE |
1467 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1468 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1470 (const char *) &block,
1471 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1472 app_announce_time, NULL, NULL));
1478 * Periodically announce what applications are provided by local clients
1481 * @param cls closure
1482 * @param tc task context
1485 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1487 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1489 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1493 DEBUG_DHT ("Starting PUT for apps\n");
1495 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1497 announce_applications_task =
1498 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1500 DEBUG_DHT ("Finished PUT for apps\n");
1505 * Periodically announce self id in the DHT
1507 * @param cls closure
1508 * @param tc task context
1511 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1513 struct PBlock block;
1515 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1517 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1521 * - Set data expiration in function of X
1522 * - Adapt X to churn
1524 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1526 block.id = my_full_id;
1527 block.type = htonl (0);
1528 GNUNET_DHT_put (dht_handle, /* DHT handle */
1529 &my_full_id.hashPubKey, /* Key to use */
1530 dht_replication_level, /* Replication level */
1531 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1532 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1533 sizeof (block), /* Size of the data */
1534 (const char *) &block, /* Data itself */
1535 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1536 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1537 NULL, /* Continuation */
1538 NULL); /* Continuation closure */
1540 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1544 /******************************************************************************/
1545 /****************** GENERAL HELPER FUNCTIONS ************************/
1546 /******************************************************************************/
1549 * Decrements the reference counter and frees all resources if needed
1551 * @param mesh_data Data Descriptor used in a multicast message.
1552 * Freed no longer needed (last message).
1555 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1557 if (0 == --(mesh_data->reference_counter))
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1560 GNUNET_free (mesh_data->data);
1561 GNUNET_free (mesh_data);
1567 * Check if client has registered with the service and has not disconnected
1569 * @param client the client to check
1571 * @return non-NULL if client exists in the global DLL
1573 static struct MeshClient *
1574 client_get (struct GNUNET_SERVER_Client *client)
1576 struct MeshClient *c;
1581 if (c->handle == client)
1590 * Checks if a given client has subscribed to certain message type
1592 * @param message_type Type of message to check
1593 * @param c Client to check
1595 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1597 * FIXME: use of crypto_hash slows it down
1598 * The hash function alone takes 8-10us out of the ~55us for the whole
1599 * process of retransmitting the message from one local client to another.
1600 * Find faster implementation!
1603 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1605 struct GNUNET_HashCode hc;
1607 if (NULL == c->types)
1610 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1611 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1616 * Check whether client wants traffic from a tunnel.
1618 * @param c Client to check.
1619 * @param t Tunnel to be found.
1621 * @return GNUNET_YES if client knows tunnel.
1623 * TODO look in client hashmap
1626 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1630 for (i = 0; i < t->nclients; i++)
1631 if (t->clients[i] == c)
1638 * Check whether client has been informed about a tunnel.
1640 * @param c Client to check.
1641 * @param t Tunnel to be found.
1643 * @return GNUNET_YES if client knows tunnel.
1645 * TODO look in client hashmap
1648 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1652 for (i = 0; i < t->nignore; i++)
1653 if (t->ignore[i] == c)
1655 return client_wants_tunnel(c, t);
1660 * Marks a client as uninterested in traffic from the tunnel, updating both
1661 * client and tunnel to reflect this.
1663 * @param c Client that doesn't want traffic anymore.
1664 * @param t Tunnel which should be ignored.
1666 * FIXME when to delete an incoming tunnel?
1669 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1671 struct GNUNET_HashCode hash;
1673 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1674 GNUNET_break (GNUNET_YES ==
1675 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1677 GNUNET_break (GNUNET_YES ==
1678 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1679 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1680 tunnel_delete_active_client (t, c);
1681 GNUNET_array_append (t->ignore, t->nignore, c);
1686 * Deletes a tunnel from a client (either owner or destination). To be used on
1687 * tunnel destroy, otherwise, use client_ignore_tunnel.
1689 * @param c Client whose tunnel to delete.
1690 * @param t Tunnel which should be deleted.
1693 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1695 struct GNUNET_HashCode hash;
1699 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1700 GNUNET_assert (GNUNET_YES ==
1701 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1707 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1709 GNUNET_assert (GNUNET_YES ==
1710 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1714 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1722 * Notify the owner of a tunnel that a peer has disconnected.
1724 * @param c Client (owner of tunnel).
1725 * @param t Tunnel this message is about.
1726 * @param peer_id Short ID of the disconnected peer.
1729 client_notify_peer_disconnected (struct MeshClient *c,
1730 struct MeshTunnel *t,
1731 GNUNET_PEER_Id peer_id)
1733 struct GNUNET_MESH_PeerControl msg;
1735 if (NULL == t->owner || NULL == nc)
1738 msg.header.size = htons (sizeof (msg));
1739 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1740 msg.tunnel_id = htonl (t->local_tid);
1741 GNUNET_PEER_resolve (peer_id, &msg.peer);
1742 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1743 &msg.header, GNUNET_NO);
1748 * Send the message to all clients that have subscribed to its type
1750 * @param msg Pointer to the message itself
1751 * @param payload Pointer to the payload of the message.
1752 * @param t The tunnel to whose clients this message goes.
1754 * @return number of clients this message was sent to
1757 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1758 const struct GNUNET_MessageHeader *payload,
1759 struct MeshTunnel *t)
1761 struct MeshClient *c;
1762 MESH_TunnelNumber *tid;
1765 char cbuf[htons (msg->size)];
1767 type = ntohs (payload->type);
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1770 GNUNET_MESH_DEBUG_M2S (type));
1772 memcpy (cbuf, msg, sizeof (cbuf));
1773 switch (htons (msg->type))
1775 struct GNUNET_MESH_Unicast *uc;
1776 struct GNUNET_MESH_Multicast *mc;
1777 struct GNUNET_MESH_ToOrigin *to;
1779 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1780 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1783 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1784 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1787 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1788 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1796 for (count = 0, c = clients; c != NULL; c = c->next)
1798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1799 if (client_is_subscribed (type, c))
1801 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1805 *tid = htonl (t->local_tid);
1809 if (GNUNET_NO == client_knows_tunnel (c, t))
1811 /* This client doesn't know the tunnel */
1812 struct GNUNET_MESH_TunnelNotification tmsg;
1813 struct GNUNET_HashCode hash;
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1816 tmsg.header.size = htons (sizeof (tmsg));
1817 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1818 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1819 tmsg.tunnel_id = htonl (t->local_tid_dest);
1821 if (GNUNET_YES == t->speed_min)
1822 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
1823 if (GNUNET_YES == t->nobuffer)
1824 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1825 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1826 &tmsg.header, GNUNET_NO);
1827 tunnel_add_client (t, c);
1828 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1830 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1831 c->incoming_tunnels, &hash, t,
1832 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1834 *tid = htonl (t->local_tid_dest);
1837 /* Check if the client wants to get traffic from the tunnel */
1838 if (GNUNET_NO == client_wants_tunnel(c, t))
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1842 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1843 (struct GNUNET_MessageHeader
1844 *) cbuf, GNUNET_NO);
1853 * Notify the client that owns the tunnel that a peer has connected to it
1854 * (the requested path to it has been confirmed).
1856 * @param t Tunnel whose owner to notify
1857 * @param id Short id of the peer that has connected
1860 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1862 struct GNUNET_MESH_PeerControl pc;
1864 if (NULL == t->owner || GNUNET_YES == t->destroy)
1867 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1868 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1869 pc.tunnel_id = htonl (t->local_tid);
1870 GNUNET_PEER_resolve (id, &pc.peer);
1871 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1877 * Notify all clients (not depending on registration status) that the incoming
1878 * tunnel is no longer valid.
1880 * @param t Tunnel that was destroyed.
1883 send_clients_tunnel_destroy (struct MeshTunnel *t)
1885 struct GNUNET_MESH_TunnelMessage msg;
1887 msg.header.size = htons (sizeof (msg));
1888 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1889 msg.tunnel_id = htonl (t->local_tid_dest);
1890 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1895 * Notify clients of tunnel disconnections, if needed.
1896 * In case the origin disconnects, the destination clients get a tunnel destroy
1897 * notification. If the last destination disconnects (only one remaining client
1898 * in tunnel), the origin gets a (local ID) peer disconnected.
1899 * Note that the function must be called BEFORE removing the client from
1902 * @param t Tunnel that was destroyed.
1903 * @param c Client that disconnected.
1906 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1912 struct GNUNET_MESH_TunnelMessage msg;
1914 msg.header.size = htons (sizeof (msg));
1915 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1916 msg.tunnel_id = htonl (t->local_tid_dest);
1917 for (i = 0; i < t->nclients; i++)
1918 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1919 &msg.header, GNUNET_NO);
1921 // FIXME when to disconnect an incoming tunnel?
1922 else if (1 == t->nclients && NULL != t->owner)
1924 struct GNUNET_MESH_PeerControl msg;
1926 msg.header.size = htons (sizeof (msg));
1927 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1928 msg.tunnel_id = htonl (t->local_tid);
1929 msg.peer = my_full_id;
1930 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1931 &msg.header, GNUNET_NO);
1937 * Iterator over all the peers to remove the oldest not-used entry.
1939 * @param cls Closure (unsued).
1940 * @param key ID of the peer.
1941 * @param value Peer_Info of the peer.
1946 peer_info_timeout (void *cls,
1947 const struct GNUNET_HashCode *key,
1954 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1955 * and insert it in the appropiate structures if the peer is not known yet.
1957 * @param peer Full identity of the peer.
1959 * @return Existing or newly created peer info.
1961 static struct MeshPeerInfo *
1962 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1964 struct MeshPeerInfo *peer_info;
1966 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1967 if (NULL == peer_info)
1970 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1971 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1973 GNUNET_CONTAINER_multihashmap_iterate (peers,
1977 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1978 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1979 peer_info->id = GNUNET_PEER_intern (peer);
1981 peer_info->last_contact = GNUNET_TIME_absolute_get();
1988 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1989 * and insert it in the appropiate structures if the peer is not known yet.
1991 * @param peer Short identity of the peer.
1993 * @return Existing or newly created peer info.
1995 static struct MeshPeerInfo *
1996 peer_info_get_short (const GNUNET_PEER_Id peer)
1998 struct GNUNET_PeerIdentity id;
2000 GNUNET_PEER_resolve (peer, &id);
2001 return peer_info_get (&id);
2006 * Iterator to remove the tunnel from the list of tunnels a peer participates
2009 * @param cls Closure (tunnel info)
2010 * @param key GNUNET_PeerIdentity of the peer (unused)
2011 * @param value PeerInfo of the peer
2013 * @return always GNUNET_YES, to keep iterating
2016 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2018 struct MeshTunnel *t = cls;
2019 struct MeshPeerInfo *peer = value;
2022 for (i = 0; i < peer->ntunnels; i++)
2025 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2028 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2030 GNUNET_realloc (peer->tunnels,
2031 peer->ntunnels * sizeof(struct MeshTunnel *));
2040 * Core callback to write a pre-constructed data packet to core buffer
2042 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2043 * @param size Number of bytes available in buf.
2044 * @param buf Where the to write the message.
2046 * @return number of bytes written to buf
2049 send_core_data_raw (void *cls, size_t size, void *buf)
2051 struct MeshTransmissionDescriptor *info = cls;
2052 struct GNUNET_MessageHeader *msg;
2055 GNUNET_assert (NULL != info);
2056 GNUNET_assert (NULL != info->mesh_data);
2057 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2058 total_size = ntohs (msg->size);
2060 if (total_size > size)
2065 memcpy (buf, msg, total_size);
2066 data_descriptor_decrement_rc (info->mesh_data);
2073 * Sends an already built non-multicast message to a peer,
2074 * properly registrating all used resources.
2076 * @param message Message to send. Function makes a copy of it.
2077 * @param peer Short ID of the neighbor whom to send the message.
2078 * @param t Tunnel on which this message is transmitted.
2081 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2082 const struct GNUNET_PeerIdentity *peer,
2083 struct MeshTunnel *t)
2085 struct MeshTransmissionDescriptor *info;
2086 struct MeshPeerInfo *neighbor;
2087 struct MeshPeerPath *p;
2091 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2093 size = ntohs (message->size);
2094 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2095 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2096 info->mesh_data->data = GNUNET_malloc (size);
2097 memcpy (info->mesh_data->data, message, size);
2098 type = ntohs(message->type);
2101 struct GNUNET_MESH_Unicast *m;
2102 struct GNUNET_MESH_ToOrigin *to;
2104 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2105 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2106 m->ttl = htonl (ntohl (m->ttl) - 1);
2108 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2109 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2111 to->pid = htonl(t->bck_pid);
2113 info->mesh_data->data_len = size;
2114 info->mesh_data->reference_counter = 1;
2115 info->mesh_data->total_out = 1;
2116 neighbor = peer_info_get (peer);
2117 for (p = neighbor->path_head; NULL != p; p = p->next)
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2128 " %s IS NOT DIRECTLY CONNECTED\n",
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2133 for (p = neighbor->path_head; NULL != p; p = p->next)
2135 struct GNUNET_PeerIdentity debug_id;
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 " path with %u hops through:\n",
2141 for (i = 0; i < p->length; i++)
2143 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2146 i, GNUNET_i2s(&debug_id));
2150 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2152 " no direct connection to %s\n",
2154 GNUNET_free (info->mesh_data->data);
2155 GNUNET_free (info->mesh_data);
2159 info->peer = neighbor;
2160 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2171 * Sends a CREATE PATH message for a path to a peer, properly registrating
2172 * all used resources.
2174 * @param peer PeerInfo of the final peer for whom this path is being created.
2175 * @param p Path itself.
2176 * @param t Tunnel for which the path is created.
2179 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2180 struct MeshTunnel *t)
2182 struct GNUNET_PeerIdentity id;
2183 struct MeshPathInfo *path_info;
2184 struct MeshPeerInfo *neighbor;
2190 p = tree_get_path_to_peer (t->tree, peer->id);
2197 for (i = 0; i < p->length; i++)
2199 if (p->peers[i] == myid)
2202 if (i >= p->length - 1)
2208 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2210 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2211 path_info->path = p;
2213 neighbor = peer_info_get (&id);
2214 path_info->peer = neighbor;
2215 queue_add (path_info,
2216 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2217 sizeof (struct GNUNET_MESH_ManipulatePath) +
2218 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2225 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2227 * @param t Tunnel whose path to destroy.
2228 * @param destination Short ID of the peer to whom the path to destroy.
2231 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2233 struct MeshPeerPath *p;
2236 p = tree_get_path_to_peer (t->tree, destination);
2242 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2243 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2245 struct GNUNET_MESH_ManipulatePath *msg;
2246 struct GNUNET_PeerIdentity *pi;
2250 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2251 msg->header.size = htons (size);
2252 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2253 msg->tid = htonl (t->id.tid);
2254 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2255 for (i = 0; i < p->length; i++)
2257 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2259 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2266 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2268 * @param t Tunnel which to confirm.
2271 send_path_ack (struct MeshTunnel *t)
2273 struct MeshTransmissionDescriptor *info;
2274 struct GNUNET_PeerIdentity id;
2275 GNUNET_PEER_Id peer;
2277 peer = tree_get_predecessor (t->tree);
2278 GNUNET_PEER_resolve (peer, &id);
2279 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2280 info->origin = &t->id;
2281 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2282 GNUNET_assert (NULL != info->peer);
2285 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2286 sizeof (struct GNUNET_MESH_PathACK),
2293 * Try to establish a new connection to this peer.
2294 * Use the best path for the given tunnel.
2295 * If the peer doesn't have any path to it yet, try to get one.
2296 * If the peer already has some path, send a CREATE PATH towards it.
2298 * @param peer PeerInfo of the peer.
2299 * @param t Tunnel for which to create the path, if possible.
2302 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2304 struct MeshPeerPath *p;
2305 struct MeshPathInfo *path_info;
2307 if (NULL != peer->path_head)
2309 p = tree_get_path_to_peer (t->tree, peer->id);
2316 // FIXME always send create path to self
2319 send_create_path (peer, p, t);
2323 struct GNUNET_HashCode hash;
2326 send_client_peer_connected (t, myid);
2327 t->local_tid_dest = next_local_tid++;
2328 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2331 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2332 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2339 else if (NULL == peer->dhtget)
2341 struct GNUNET_PeerIdentity id;
2343 GNUNET_PEER_resolve (peer->id, &id);
2344 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2345 path_info->peer = peer;
2347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2349 peer->dhtgetcls = path_info;
2350 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2351 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2352 &id.hashPubKey, /* key to search */
2353 dht_replication_level, /* replication level */
2354 GNUNET_DHT_RO_RECORD_ROUTE |
2355 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2356 NULL, /* xquery */ // FIXME BLOOMFILTER
2357 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2358 &dht_get_id_handler, path_info);
2360 /* Otherwise, there is no path but the DHT get is already started. */
2365 * Task to delay the connection of a peer
2367 * @param cls Closure (path info with tunnel and peer to connect).
2368 * Will be free'd on exection.
2369 * @param tc TaskContext
2372 peer_info_connect_task (void *cls,
2373 const struct GNUNET_SCHEDULER_TaskContext *tc)
2375 struct MeshPathInfo *path_info = cls;
2377 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2379 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2384 peer_info_connect (path_info->peer, path_info->t);
2390 * Destroy the peer_info and free any allocated resources linked to it
2392 * @param pi The peer_info to destroy.
2394 * @return GNUNET_OK on success
2397 peer_info_destroy (struct MeshPeerInfo *pi)
2399 struct GNUNET_PeerIdentity id;
2400 struct MeshPeerPath *p;
2401 struct MeshPeerPath *nextp;
2403 GNUNET_PEER_resolve (pi->id, &id);
2404 GNUNET_PEER_change_rc (pi->id, -1);
2407 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2411 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2413 if (NULL != pi->dhtget)
2415 GNUNET_DHT_get_stop (pi->dhtget);
2416 GNUNET_free (pi->dhtgetcls);
2422 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2426 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2428 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2436 * Remove all paths that rely on a direct connection between p1 and p2
2437 * from the peer itself and notify all tunnels about it.
2439 * @param peer PeerInfo of affected peer.
2440 * @param p1 GNUNET_PEER_Id of one peer.
2441 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2444 * TODO: optimize (see below)
2447 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2450 struct MeshPeerPath *p;
2451 struct MeshPeerPath *aux;
2452 struct MeshPeerInfo *peer_d;
2454 unsigned int destroyed;
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2461 p = peer->path_head;
2465 for (i = 0; i < (p->length - 1); i++)
2467 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2468 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2470 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2481 for (i = 0; i < peer->ntunnels; i++)
2483 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2487 * Problem: one or more peers have been deleted from the tunnel tree.
2488 * We don't know who they are to try to add them again.
2489 * We need to try to find a new path for each of the disconnected peers.
2490 * Some of them might already have a path to reach them that does not
2491 * involve p1 and p2. Adding all anew might render in a better tree than
2492 * the trivial immediate fix.
2494 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2495 * its children will be reachable trough him.
2497 peer_d = peer_info_get_short (d);
2500 for (p = peer_d->path_head; NULL != p; p = p->next)
2502 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2510 /* No callback, as peer will be already disconnected and a connection
2511 * scheduled by tunnel_notify_connection_broken.
2513 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2517 peer_info_connect (peer_d, peer->tunnels[i]);
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2525 * Add the path to the peer and update the path used to reach it in case this
2528 * @param peer_info Destination peer to add the path to.
2529 * @param path New path to add. Last peer must be the peer in arg 1.
2530 * Path will be either used of freed if already known.
2531 * @param trusted Do we trust that this path is real?
2534 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2537 struct MeshPeerPath *aux;
2541 if ((NULL == peer_info) || (NULL == path))
2544 path_destroy (path);
2547 if (path->peers[path->length - 1] != peer_info->id)
2550 path_destroy (path);
2553 if (path->length <= 2 && GNUNET_NO == trusted)
2555 /* Only allow CORE to tell us about direct paths */
2556 path_destroy (path);
2559 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2560 for (l = 1; l < path->length; l++)
2562 if (path->peers[l] == myid)
2564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2565 for (l2 = 0; l2 < path->length - l; l2++)
2567 path->peers[l2] = path->peers[l + l2];
2572 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2577 struct GNUNET_PeerIdentity id;
2579 GNUNET_PEER_resolve (peer_info->id, &id);
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2581 path->length, GNUNET_i2s (&id));
2584 l = path_get_length (path);
2591 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2592 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2594 l2 = path_get_length (aux);
2597 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2598 peer_info->path_tail, aux, path);
2603 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2605 path_destroy (path);
2610 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2617 * Add the path to the origin peer and update the path used to reach it in case
2618 * this is the shortest.
2619 * The path is given in peer_info -> destination, therefore we turn the path
2620 * upside down first.
2622 * @param peer_info Peer to add the path to, being the origin of the path.
2623 * @param path New path to add after being inversed.
2624 * @param trusted Do we trust that this path is real?
2627 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2628 struct MeshPeerPath *path, int trusted)
2631 peer_info_add_path (peer_info, path, trusted);
2636 * Function called if the connection to the peer has been stalled for a while,
2637 * possibly due to a missed ACK. Poll the peer about its ACK status.
2639 * @param cls Closure (cinfo).
2640 * @param tc TaskContext.
2643 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2645 struct MeshTunnelChildInfo *cinfo = cls;
2646 struct GNUNET_MESH_Poll msg;
2647 struct GNUNET_PeerIdentity id;
2648 struct MeshTunnel *t;
2650 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2651 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2657 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2658 msg.header.size = htons (sizeof (msg));
2659 msg.tid = htonl (t->id.tid);
2660 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2661 msg.last_ack = htonl (cinfo->fwd_ack);
2663 GNUNET_PEER_resolve (cinfo->id, &id);
2664 send_prebuilt_message (&msg.header, &id, cinfo->t);
2665 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2667 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2668 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2669 &tunnel_poll, cinfo);
2674 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2675 * to obtain a local peer -> destination path and interning the peer ids.
2677 * @return Newly allocated and created path
2679 static struct MeshPeerPath *
2680 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2681 unsigned int get_path_length,
2682 const struct GNUNET_PeerIdentity *put_path,
2683 unsigned int put_path_length)
2685 struct MeshPeerPath *p;
2691 GNUNET_PEER_change_rc (myid, 1);
2692 i = get_path_length;
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2694 for (i--; i >= 0; i--)
2696 id = GNUNET_PEER_intern (&get_path[i]);
2697 if (p->length > 0 && id == p->peers[p->length - 1])
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2700 GNUNET_PEER_change_rc (id, -1);
2704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2705 GNUNET_i2s (&get_path[i]));
2707 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2708 p->peers[p->length - 1] = id;
2711 i = put_path_length;
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2713 for (i--; i >= 0; i--)
2715 id = GNUNET_PEER_intern (&put_path[i]);
2718 /* PUT path went through us, so discard the path up until now and start
2719 * from here to get a much shorter (and loop-free) path.
2724 if (p->length > 0 && id == p->peers[p->length - 1])
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2727 GNUNET_PEER_change_rc (id, -1);
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2732 GNUNET_i2s (&put_path[i]));
2734 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2735 p->peers[p->length - 1] = id;
2739 if (get_path_length > 0)
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2741 GNUNET_i2s (&get_path[0]));
2742 if (put_path_length > 0)
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2744 GNUNET_i2s (&put_path[0]));
2745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2747 for (i = 0; i < p->length; i++)
2749 struct GNUNET_PeerIdentity peer_id;
2751 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2753 GNUNET_i2s (&peer_id));
2761 * Adds a path to the peer_infos of all the peers in the path
2763 * @param p Path to process.
2764 * @param confirmed Whether we know if the path works or not.
2767 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2771 /* TODO: invert and add */
2772 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2773 for (i++; i < p->length; i++)
2775 struct MeshPeerInfo *aux;
2776 struct MeshPeerPath *copy;
2778 aux = peer_info_get_short (p->peers[i]);
2779 copy = path_duplicate (p);
2780 copy->length = i + 1;
2781 peer_info_add_path (aux, copy, GNUNET_NO);
2787 * Send keepalive packets for a peer
2789 * @param cls Closure (tunnel for which to send the keepalive).
2790 * @param tc Notification context.
2792 * TODO: implement explicit multicast keepalive?
2795 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2799 * Search for a tunnel among the incoming tunnels
2801 * @param tid the local id of the tunnel
2803 * @return tunnel handler, NULL if doesn't exist
2805 static struct MeshTunnel *
2806 tunnel_get_incoming (MESH_TunnelNumber tid)
2808 struct GNUNET_HashCode hash;
2810 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2811 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2812 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2817 * Search for a tunnel among the tunnels for a client
2819 * @param c the client whose tunnels to search in
2820 * @param tid the local id of the tunnel
2822 * @return tunnel handler, NULL if doesn't exist
2824 static struct MeshTunnel *
2825 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2827 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2829 return tunnel_get_incoming (tid);
2833 struct GNUNET_HashCode hash;
2835 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2836 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2842 * Search for a tunnel by global ID using PEER_ID
2844 * @param pi owner of the tunnel
2845 * @param tid global tunnel number
2847 * @return tunnel handler, NULL if doesn't exist
2849 static struct MeshTunnel *
2850 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2852 struct MESH_TunnelID id;
2853 struct GNUNET_HashCode hash;
2858 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2859 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2864 * Search for a tunnel by global ID using full PeerIdentities
2866 * @param oid owner of the tunnel
2867 * @param tid global tunnel number
2869 * @return tunnel handler, NULL if doesn't exist
2871 static struct MeshTunnel *
2872 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2874 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2879 * Delete an active client from the tunnel.
2885 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2889 for (i = 0; i < t->nclients; i++)
2891 if (t->clients[i] == c)
2893 t->clients[i] = t->clients[t->nclients - 1];
2894 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2895 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2897 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2905 * Delete an ignored client from the tunnel.
2911 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2915 for (i = 0; i < t->nignore; i++)
2917 if (t->ignore[i] == c)
2919 t->ignore[i] = t->ignore[t->nignore - 1];
2920 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2928 * Delete a client from the tunnel. It should be only done on
2929 * client disconnection, otherwise use client_ignore_tunnel.
2935 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2937 tunnel_delete_ignored_client (t, c);
2938 tunnel_delete_active_client (t, c);
2943 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
2945 * Destroys queue elements of all waiting transmissions and frees all memory
2946 * used by the struct and its elements.
2948 * @param cls Closure (tunnel info).
2949 * @param key Hash of GNUNET_PEER_Id (unused).
2950 * @param value MeshTunnelChildInfo of the child.
2952 * @return always GNUNET_YES, to keep iterating
2955 tunnel_destroy_child (void *cls,
2956 const struct GNUNET_HashCode * key,
2959 struct MeshTunnelChildInfo *cinfo = value;
2960 struct MeshTunnel *t = cls;
2961 struct MeshPeerQueue *q;
2965 for (c = 0; c < cinfo->send_buffer_n; c++)
2967 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
2968 q = cinfo->send_buffer[i];
2969 cinfo->send_buffer[i] = NULL;
2971 queue_destroy (q, GNUNET_YES);
2974 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
2976 GNUNET_free_non_null (cinfo->send_buffer);
2977 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
2979 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
2980 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2982 GNUNET_free (cinfo);
2988 * Callback used to notify a client owner of a tunnel that a peer has
2989 * disconnected, most likely because of a path change.
2991 * @param cls Closure (tunnel this notification is about).
2992 * @param peer_id Short ID of disconnected peer.
2995 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2997 struct MeshTunnel *t = cls;
2998 struct MeshPeerInfo *peer;
2999 struct MeshPathInfo *path_info;
3001 client_notify_peer_disconnected (t->owner, t, peer_id);
3003 peer = peer_info_get_short (peer_id);
3004 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3005 path_info->peer = peer;
3007 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3013 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3014 * needed rescources.
3015 * If peer already exists, reevaluate shortest path and change if different.
3017 * @param t Tunnel we want to add a new peer to
3018 * @param peer PeerInfo of the peer being added
3022 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3024 struct GNUNET_PeerIdentity id;
3025 struct MeshPeerPath *best_p;
3026 struct MeshPeerPath *p;
3027 unsigned int best_cost;
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3031 GNUNET_PEER_resolve (peer->id, &id);
3033 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3035 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3036 GNUNET_assert (GNUNET_OK ==
3037 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3039 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3042 if (NULL != (p = peer->path_head))
3045 best_cost = tree_get_path_cost (t->tree, p);
3048 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3055 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3056 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3057 t->path_refresh_task =
3058 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3062 /* Start a DHT get */
3063 peer_info_connect (peer, t);
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3069 * Add a path to a tunnel which we don't own, just to remember the next hop.
3070 * If destination node was already in the tunnel, the first hop information
3071 * will be replaced with the new path.
3073 * @param t Tunnel we want to add a new peer to
3074 * @param p Path to add
3075 * @param own_pos Position of local node in path.
3079 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3080 unsigned int own_pos)
3082 struct GNUNET_PeerIdentity id;
3084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3085 GNUNET_assert (0 != own_pos);
3086 tree_add_path (t->tree, p, NULL, NULL);
3087 if (own_pos < p->length - 1)
3089 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3090 tree_update_first_hops (t->tree, myid, &id);
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3096 * Add a client to a tunnel, initializing all needed data structures.
3098 * @param t Tunnel to which add the client.
3099 * @param c Client which to add to the tunnel.
3102 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3104 struct MeshTunnelClientInfo clinfo;
3106 GNUNET_array_append (t->clients, t->nclients, c);
3107 clinfo.fwd_ack = t->fwd_pid + 1;
3108 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3109 clinfo.fwd_pid = t->fwd_pid;
3110 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3112 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3117 * Notifies a tunnel that a connection has broken that affects at least
3118 * some of its peers. Sends a notification towards the root of the tree.
3119 * In case the peer is the owner of the tree, notifies the client that owns
3120 * the tunnel and tries to reconnect.
3122 * @param t Tunnel affected.
3123 * @param p1 Peer that got disconnected from p2.
3124 * @param p2 Peer that got disconnected from p1.
3126 * @return Short ID of the peer disconnected (either p1 or p2).
3127 * 0 if the tunnel remained unaffected.
3129 static GNUNET_PEER_Id
3130 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3136 tree_notify_connection_broken (t->tree, p1, p2,
3137 &tunnel_notify_client_peer_disconnected,
3139 if (myid != p1 && myid != p2)
3145 if (tree_get_predecessor (t->tree) != 0)
3147 /* We are the peer still connected, notify owner of the disconnection. */
3148 struct GNUNET_MESH_PathBroken msg;
3149 struct GNUNET_PeerIdentity neighbor;
3151 msg.header.size = htons (sizeof (msg));
3152 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3153 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3154 msg.tid = htonl (t->id.tid);
3155 msg.peer1 = my_full_id;
3156 GNUNET_PEER_resolve (pid, &msg.peer2);
3157 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3158 send_prebuilt_message (&msg.header, &neighbor, t);
3166 * Send a multicast packet to a neighbor.
3168 * @param cls Closure (Info about the multicast packet)
3169 * @param neighbor_id Short ID of the neighbor to send the packet to.
3172 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3174 struct MeshData *mdata = cls;
3175 struct MeshTransmissionDescriptor *info;
3176 struct GNUNET_PeerIdentity neighbor;
3177 struct GNUNET_MessageHeader *msg;
3179 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3181 info->mesh_data = mdata;
3182 (mdata->reference_counter) ++;
3183 info->destination = neighbor_id;
3184 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3186 GNUNET_i2s (&neighbor));
3187 info->peer = peer_info_get (&neighbor);
3188 GNUNET_assert (NULL != info->peer);
3189 msg = (struct GNUNET_MessageHeader *) mdata->data;
3192 info->mesh_data->data_len,
3199 * Queue a message in a tunnel in multicast, sending a copy to each child node
3200 * down the local one in the tunnel tree.
3202 * @param t Tunnel in which to send the data.
3203 * @param msg Message to be sent.
3206 tunnel_send_multicast (struct MeshTunnel *t,
3207 const struct GNUNET_MessageHeader *msg)
3209 struct MeshData *mdata;
3211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3212 " sending a multicast packet...\n");
3214 mdata = GNUNET_malloc (sizeof (struct MeshData));
3215 mdata->data_len = ntohs (msg->size);
3217 mdata->data = GNUNET_malloc (mdata->data_len);
3218 memcpy (mdata->data, msg, mdata->data_len);
3219 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3221 struct GNUNET_MESH_Multicast *mcast;
3223 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3224 if (t->fwd_queue_n >= t->fwd_queue_max)
3227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3229 " message from %s!\n",
3230 GNUNET_i2s(&mcast->oid));
3231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3232 " message at %s!\n",
3233 GNUNET_i2s(&my_full_id));
3234 GNUNET_free (mdata->data);
3235 GNUNET_free (mdata);
3239 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3241 ntohl (mcast->ttl));
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3248 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3249 if (mdata->reference_counter == 0)
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3252 " no one to send data to\n");
3253 GNUNET_free (mdata->data);
3254 GNUNET_free (mdata);
3255 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3260 mdata->total_out = mdata->reference_counter;
3262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3263 " sending a multicast packet done\n");
3269 * Increase the SKIP value of all peers that
3270 * have not received a unicast message.
3272 * @param cls Closure (ID of the peer that HAS received the message).
3273 * @param key ID of the neighbor.
3274 * @param value Information about the neighbor.
3276 * @return GNUNET_YES to keep iterating.
3279 tunnel_add_skip (void *cls,
3280 const struct GNUNET_HashCode * key,
3283 struct GNUNET_PeerIdentity *neighbor = cls;
3284 struct MeshTunnelChildInfo *cinfo = value;
3286 /* TODO compare only pointers? key == neighbor? */
3287 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3297 * @brief Get neighbor's Flow Control information.
3299 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3300 * descendant of the local node in a certain tunnel.
3301 * If the info is not yet there (recently created path), creates the data struct
3302 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3305 * @param t Tunnel related.
3306 * @param peer Neighbor whose Flow Control info is needed.
3308 * @return Neighbor's Flow Control info.
3310 static struct MeshTunnelChildInfo *
3311 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3312 const struct GNUNET_PeerIdentity *peer)
3314 struct MeshTunnelChildInfo *cinfo;
3316 if (NULL == t->children_fc)
3319 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3325 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3326 cinfo->id = GNUNET_PEER_intern (peer);
3327 cinfo->skip = t->fwd_pid;
3330 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3331 cinfo->fwd_ack = t->fwd_pid + delta;
3332 cinfo->bck_ack = delta;
3333 cinfo->bck_pid = -1;
3335 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3336 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3338 cinfo->send_buffer =
3339 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3341 GNUNET_assert (GNUNET_OK ==
3342 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3345 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3352 * Get the Flow Control info of a client.
3354 * @param t Tunnel on which to look.
3355 * @param c Client whose ACK to get.
3357 * @return ACK value.
3359 static struct MeshTunnelClientInfo *
3360 tunnel_get_client_fc (struct MeshTunnel *t,
3361 struct MeshClient *c)
3365 for (i = 0; i < t->nclients; i++)
3367 if (t->clients[i] != c)
3369 return &t->clients_fc[i];
3372 return NULL; // avoid compiler / coverity complaints
3377 * Iterator to get the appropiate ACK value from all children nodes.
3379 * @param cls Closue (tunnel).
3380 * @param id Id of the child node.
3383 tunnel_get_child_fwd_ack (void *cls,
3386 struct GNUNET_PeerIdentity peer_id;
3387 struct MeshTunnelChildInfo *cinfo;
3388 struct MeshTunnelChildIteratorContext *ctx = cls;
3389 struct MeshTunnel *t = ctx->t;
3392 GNUNET_PEER_resolve (id, &peer_id);
3393 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3394 ack = cinfo->fwd_ack;
3397 if (GNUNET_NO == ctx->init)
3399 ctx->max_child_ack = ack;
3400 ctx->init = GNUNET_YES;
3403 if (GNUNET_YES == t->speed_min)
3405 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3409 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3416 * Get the maximum PID allowed to transmit to any
3417 * tunnel child of the local peer, depending on the tunnel
3418 * buffering/speed settings.
3422 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3425 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3427 struct MeshTunnelChildIteratorContext ctx;
3429 ctx.max_child_ack = 0;
3431 ctx.init = GNUNET_NO;
3432 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3434 if (0 == ctx.nchildren)
3436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3437 " tunnel has no children, no FWD ACK\n");
3441 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3442 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3444 return (int64_t) ctx.max_child_ack;
3449 * Set the FWD ACK value of a client in a particular tunnel.
3451 * @param t Tunnel affected.
3452 * @param c Client whose ACK to set.
3453 * @param ack ACK value.
3456 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3457 struct MeshClient *c,
3462 for (i = 0; i < t->nclients; i++)
3464 if (t->clients[i] != c)
3466 t->clients_fc[i].fwd_ack = ack;
3474 * Get the highest ACK value of all clients in a particular tunnel,
3475 * according to the buffering/speed settings.
3477 * @param t Tunnel on which to look.
3479 * @return Corresponding ACK value (max uint32_t).
3480 * If no clients are suscribed, -1LL.
3483 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3488 if (0 == t->nclients)
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3491 " tunnel has no clients, no FWD ACK\n");
3495 for (ack = -1LL, i = 0; i < t->nclients; i++)
3498 (GNUNET_YES == t->speed_min &&
3499 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3500 (GNUNET_NO == t->speed_min &&
3501 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3503 ack = t->clients_fc[i].fwd_ack;
3507 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3508 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3510 return (uint32_t) ack;
3515 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3516 * mode and the status of all children nodes.
3520 * @return Maximum PID allowed.
3523 tunnel_get_fwd_ack (struct MeshTunnel *t)
3527 uint32_t buffer_free;
3531 count = t->fwd_pid - t->skip;
3532 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3533 child_ack = tunnel_get_children_fwd_ack (t);
3534 client_ack = tunnel_get_clients_fwd_ack (t);
3535 if (GNUNET_YES == t->nobuffer)
3538 if (-1LL == child_ack)
3539 child_ack = client_ack;
3540 if (-1LL == child_ack)
3543 client_ack = child_ack = ack;
3548 ack = count + buffer_free; // Overflow? OK!
3550 if (-1LL == child_ack)
3552 // Node has no children, child_ack AND core buffer are irrelevant.
3553 if (-1LL == client_ack) // No children AND no clients? Not good!
3555 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3559 return (uint32_t) client_ack;
3561 if (-1LL == client_ack)
3565 if (GNUNET_YES == t->speed_min)
3567 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3568 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3572 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3573 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3576 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3577 count, buffer_free, child_ack, client_ack, ack);
3583 * Build a local ACK message and send it to a local client.
3585 * @param t Tunnel on which to send the ACK.
3586 * @param c Client to whom send the ACK.
3587 * @param ack Value of the ACK.
3590 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3592 struct GNUNET_MESH_LocalAck msg;
3594 msg.header.size = htons (sizeof (msg));
3595 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3596 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3597 msg.max_pid = htonl (ack);
3598 GNUNET_SERVER_notification_context_unicast(nc,
3605 * Build an ACK message and queue it to send to the given peer.
3607 * @param t Tunnel on which to send the ACK.
3608 * @param peer Peer to whom send the ACK.
3609 * @param ack Value of the ACK.
3612 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3614 struct GNUNET_MESH_ACK msg;
3616 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3617 msg.header.size = htons (sizeof (msg));
3618 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3619 msg.pid = htonl (ack);
3620 msg.tid = htonl (t->id.tid);
3622 send_prebuilt_message (&msg.header, peer, t);
3627 * Notify a the owner of a tunnel about how many more
3628 * payload packages will we accept on a given tunnel.
3630 * @param t Tunnel on which to send the ACK.
3633 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3638 "Sending client FWD ACK on tunnel %X\n",
3641 ack = tunnel_get_fwd_ack (t);
3643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3644 if (t->last_fwd_ack == ack)
3646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3651 t->last_fwd_ack = ack;
3652 send_local_ack (t, t->owner, ack);
3657 * Send an ACK informing the predecessor about the available buffer space.
3658 * In case there is no predecessor, inform the owning client.
3659 * If buffering is off, send only on behalf of children or self if endpoint.
3660 * If buffering is on, send when sent to children and buffer space is free.
3661 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3662 * the ACK itself goes "back" (towards root).
3664 * @param t Tunnel on which to send the ACK.
3665 * @param type Type of message that triggered the ACK transmission.
3668 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3670 struct GNUNET_PeerIdentity id;
3673 if (NULL != t->owner)
3675 tunnel_send_client_fwd_ack (t);
3678 /* Is it after unicast / multicast retransmission? */
3681 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3682 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3684 "ACK due to FWD DATA retransmission\n");
3685 if (GNUNET_YES == t->nobuffer)
3687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3691 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3692 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3694 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3695 t->force_ack = GNUNET_YES;
3701 /* Check if we need to transmit the ACK */
3702 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3703 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
3704 GNUNET_NO == t->force_ack)
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3708 " t->qmax: %u, t->qn: %u\n",
3709 t->fwd_queue_max, t->fwd_queue_n);
3710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3711 " t->pid: %u, t->ack: %u\n",
3712 t->fwd_pid, t->last_fwd_ack);
3716 /* Ok, ACK might be necessary, what PID to ACK? */
3717 ack = tunnel_get_fwd_ack (t);
3719 /* If speed_min and not all children have ack'd, dont send yet */
3720 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3726 t->last_fwd_ack = ack;
3727 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3728 send_ack (t, &id, ack);
3730 t->force_ack = GNUNET_NO;
3735 * Iterator to send a child node a BCK ACK to allow him to send more
3738 * @param cls Closure (tunnel).
3739 * @param id Id of the child node.
3742 tunnel_send_child_bck_ack (void *cls,
3745 struct MeshTunnel *t = cls;
3746 struct MeshTunnelChildInfo *cinfo;
3747 struct GNUNET_PeerIdentity peer;
3750 GNUNET_PEER_resolve (id, &peer);
3751 cinfo = tunnel_get_neighbor_fc (t, &peer);
3752 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
3754 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
3756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3757 " Not sending ACK, not needed\n");
3760 cinfo->bck_ack = ack;
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3763 " Sending BCK ACK %u (last sent: %u)\n",
3764 ack, cinfo->bck_ack);
3765 send_ack (t, &peer, ack);
3770 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3772 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3774 * FIXME fc: what happens if we have 2 clients but q_size is 1?
3775 * - implement a size 1 buffer in each client_fc AND children_fc
3776 * to hold at least 1 message per "child".
3777 * problem: violates no buffer policy
3778 * - ack 0 and make "children" poll for transmission slots
3779 * problem: big overhead, extra latency even in low traffic
3782 * @param t Tunnel on which to send the BCK ACKs.
3785 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3788 unsigned int tunnel_delta;
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3792 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
3793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3795 /* Find client whom to allow to send to origin (with lowest buffer space) */
3796 for (i = 0; i < t->nclients; i++)
3798 struct MeshTunnelClientInfo *clinfo;
3801 clinfo = &t->clients_fc[i];
3802 delta = clinfo->bck_ack - clinfo->bck_pid;
3803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3804 t->clients[i]->id, delta);
3806 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3807 (GNUNET_YES == t->nobuffer && 0 == delta))
3811 ack = clinfo->bck_pid;
3812 ack += t->nobuffer ? 1 : tunnel_delta;
3813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3814 " sending ack to client %u: %u\n",
3815 t->clients[i]->id, ack);
3816 send_local_ack (t, t->clients[i], ack);
3817 clinfo->bck_ack = ack;
3821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3822 " not sending ack to client %u (td %u, d %u)\n",
3823 t->clients[i]->id, tunnel_delta, delta);
3830 * Send an ACK informing the children nodes and destination clients about
3831 * the available buffer space.
3832 * If buffering is off, send only on behalf of root (can be self).
3833 * If buffering is on, send when sent to predecessor and buffer space is free.
3834 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3835 * the ACK itself goes "forward" (towards children/clients).
3837 * @param t Tunnel on which to send the ACK.
3838 * @param type Type of message that triggered the ACK transmission.
3841 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3844 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3845 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3846 /* Is it after data to_origin retransmission? */
3849 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3850 if (GNUNET_YES == t->nobuffer)
3852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3853 " Not sending ACK, nobuffer\n");
3857 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3858 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3860 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3861 t->force_ack = GNUNET_YES;
3867 tunnel_send_clients_bck_ack (t);
3868 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3869 t->force_ack = GNUNET_NO;
3874 * @brief Re-initiate traffic to this peer if necessary.
3876 * Check if there is traffic queued towards this peer
3877 * and the core transmit handle is NULL (traffic was stalled).
3878 * If so, call core tmt rdy.
3880 * @param cls Closure (unused)
3881 * @param peer_id Short ID of peer to which initiate traffic.
3884 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3886 struct MeshPeerInfo *peer;
3887 struct GNUNET_PeerIdentity id;
3888 struct MeshPeerQueue *q;
3891 peer = peer_info_get_short(peer_id);
3892 if (NULL != peer->core_transmit)
3895 q = queue_get_next(peer);
3898 /* Might br multicast traffic already sent to this particular peer but
3899 * not to other children in this tunnel.
3900 * This way t->queue_n would be > 0 but the queue of this particular peer
3906 GNUNET_PEER_resolve (peer->id, &id);
3907 peer->core_transmit =
3908 GNUNET_CORE_notify_transmit_ready(core_handle,
3911 GNUNET_TIME_UNIT_FOREVER_REL,
3921 * @brief Allow transmission of FWD traffic on this tunnel
3923 * Check if there is traffic queued towards any children
3924 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3926 * @param t Tunnel on which to unlock FWD traffic.
3929 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3931 if (0 == t->fwd_queue_n)
3934 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3939 * @brief Allow transmission of BCK traffic on this tunnel
3941 * Check if there is traffic queued towards the root of the tree
3942 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3944 * @param t Tunnel on which to unlock BCK traffic.
3947 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3949 if (0 == t->bck_queue_n)
3952 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3957 * Send a message to all peers in this tunnel that the tunnel is no longer
3960 * @param t The tunnel whose peers to notify.
3961 * @param parent ID of the parent, in case the tree is already destroyed.
3964 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
3966 struct GNUNET_MESH_TunnelDestroy msg;
3967 struct GNUNET_PeerIdentity id;
3969 msg.header.size = htons (sizeof (msg));
3970 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3971 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3972 msg.tid = htonl (t->id.tid);
3973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3974 " sending tunnel destroy for tunnel: %s [%X]\n",
3975 GNUNET_i2s (&msg.oid), t->id.tid);
3976 if (tree_count_children(t->tree) > 0)
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to children\n");
3979 tunnel_send_multicast (t, &msg.header);
3982 parent = tree_get_predecessor (t->tree);
3983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
3987 GNUNET_PEER_resolve (parent, &id);
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3989 " sending back to %s\n",
3991 send_prebuilt_message (&msg.header, &id, t);
3996 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3998 * @param cls Closure (Tunnel which to cancel).
3999 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4002 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4004 struct MeshTunnel *t = cls;
4005 struct MeshPeerInfo *peer_info;
4006 struct MeshPeerQueue *pq;
4007 struct MeshPeerQueue *next;
4009 peer_info = peer_info_get_short (neighbor_id);
4010 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4013 if (pq->tunnel == t)
4015 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4016 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4017 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4019 // Should have been removed on destroy children
4022 queue_destroy (pq, GNUNET_YES);
4025 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4027 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4028 peer_info->core_transmit = NULL;
4033 * Destroy the tunnel and free any allocated resources linked to it.
4035 * @param t the tunnel to destroy
4037 * @return GNUNET_OK on success
4040 tunnel_destroy (struct MeshTunnel *t)
4042 struct MeshClient *c;
4043 struct GNUNET_HashCode hash;
4054 struct GNUNET_PeerIdentity id;
4056 GNUNET_PEER_resolve (t->id.oid, &id);
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4058 GNUNET_i2s (&id), t->id.tid);
4060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4064 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4065 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4073 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4075 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4082 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4083 for (i = 0; i < t->nclients; i++)
4087 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4093 for (i = 0; i < t->nignore; i++)
4097 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4104 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
4105 GNUNET_free_non_null (t->clients);
4106 GNUNET_free_non_null (t->ignore);
4107 GNUNET_free_non_null (t->clients_fc);
4109 if (NULL != t->peers)
4111 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4113 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4116 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4117 &tunnel_destroy_child,
4119 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4120 t->children_fc = NULL;
4122 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4123 tree_destroy (t->tree);
4125 if (NULL != t->regex_search)
4126 GNUNET_REGEX_search_cancel (t->regex_search->search_handle);
4127 if (NULL != t->dht_get_type)
4128 GNUNET_DHT_get_stop (t->dht_get_type);
4129 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4130 GNUNET_SCHEDULER_cancel (t->timeout_task);
4131 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4132 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4135 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4140 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
4143 * Tunnel is empty: destroy it.
4145 * @param cls Closure (Tunnel).
4146 * @param tc TaskContext.
4149 tunnel_destroy_empty_delayed (void *cls,
4150 const struct GNUNET_SCHEDULER_TaskContext *tc)
4152 struct MeshTunnel *t = cls;
4154 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
4155 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4158 if (0 != t->nclients ||
4159 0 != tree_count_children (t->tree))
4164 struct GNUNET_PeerIdentity id;
4166 GNUNET_PEER_resolve (t->id.oid, &id);
4167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4168 "executing destruction of empty tunnel %s [%X]\n",
4169 GNUNET_i2s (&id), t->id.tid);
4173 tunnel_send_destroy (t, 0);
4174 if (0 == t->pending_messages)
4177 t->destroy = GNUNET_YES;
4182 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4184 * @param t Tunnel to destroy if empty.
4187 tunnel_destroy_empty (struct MeshTunnel *t)
4189 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4191 0 != tree_count_children (t->tree))
4193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4195 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
4201 struct GNUNET_PeerIdentity id;
4203 GNUNET_PEER_resolve (t->id.oid, &id);
4204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4205 "scheduling destruction of empty tunnel %s [%X]\n",
4206 GNUNET_i2s (&id), t->id.tid);
4210 t->delayed_destroy =
4211 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4212 &tunnel_destroy_empty_delayed,
4218 * Create a new tunnel
4220 * @param owner Who is the owner of the tunnel (short ID).
4221 * @param tid Tunnel Number of the tunnel.
4222 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4223 * @param local Tunnel Number for the tunnel, for the client point of view.
4225 * @return A new initialized tunnel. NULL on error.
4227 static struct MeshTunnel *
4228 tunnel_new (GNUNET_PEER_Id owner,
4229 MESH_TunnelNumber tid,
4230 struct MeshClient *client,
4231 MESH_TunnelNumber local)
4233 struct MeshTunnel *t;
4234 struct GNUNET_HashCode hash;
4236 if (n_tunnels >= max_tunnels && NULL == client)
4239 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4242 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4243 t->bck_queue_max = t->fwd_queue_max;
4244 t->tree = tree_new (owner);
4246 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4247 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4248 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4249 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4250 t->local_tid = local;
4251 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4253 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4255 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4257 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4258 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4265 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4272 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4274 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4275 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4279 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4288 * Callback when removing children from a tunnel tree. Notify owner.
4290 * @param cls Closure (tunnel).
4291 * @param peer_id Short ID of the peer deleted.
4294 tunnel_child_removed (void *cls, GNUNET_PEER_Id peer_id)
4296 struct MeshTunnel *t = cls;
4298 client_notify_peer_disconnected (t->owner, t, peer_id);
4302 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4303 * that are no longer needed, as well as nodes of no longer reachable peers.
4304 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4306 * @param t Tunnel from which to remove the path.
4307 * @param peer Short id of the peer which should be removed.
4310 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4314 r = tree_del_peer (t->tree, peer, &tunnel_child_removed, t);
4317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4318 "Tunnel %u [%u] has no more nodes\n",
4319 t->id.oid, t->id.tid);
4325 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4326 * client when the client disconnects. If the client is not the owner, the
4327 * owner will get notified if no more clients are in the tunnel and the client
4328 * get removed from the tunnel's list.
4330 * @param cls closure (client that is disconnecting)
4331 * @param key the hash of the local tunnel id (used to access the hashmap)
4332 * @param value the value stored at the key (tunnel to destroy)
4334 * @return GNUNET_OK, keep iterating.
4337 tunnel_destroy_iterator (void *cls,
4338 const struct GNUNET_HashCode * key,
4341 struct MeshTunnel *t = value;
4342 struct MeshClient *c = cls;
4344 send_client_tunnel_disconnect (t, c);
4347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4348 tunnel_delete_client (t, c);
4349 client_delete_tunnel (c, t);
4350 tunnel_destroy_empty (t);
4353 tunnel_send_destroy (t, 0);
4355 t->destroy = GNUNET_YES;
4362 * Timeout function, destroys tunnel if called
4364 * @param cls Closure (tunnel to destroy).
4365 * @param tc TaskContext
4368 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4370 struct MeshTunnel *t = cls;
4371 struct GNUNET_PeerIdentity id;
4373 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4374 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4376 GNUNET_PEER_resolve(t->id.oid, &id);
4377 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4378 "Tunnel %s [%X] timed out. Destroying.\n",
4379 GNUNET_i2s(&id), t->id.tid);
4380 send_clients_tunnel_destroy (t);
4385 * Resets the tunnel timeout. Starts it if no timeout was running.
4387 * @param t Tunnel whose timeout to reset.
4389 * TODO use heap to improve efficiency of scheduler.
4392 tunnel_reset_timeout (struct MeshTunnel *t)
4394 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4395 GNUNET_SCHEDULER_cancel (t->timeout_task);
4397 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4398 (refresh_path_time, 4), &tunnel_timeout, t);
4402 /******************************************************************************/
4403 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4404 /******************************************************************************/
4407 * Function to send a create path packet to a peer.
4409 * @param cls closure
4410 * @param size number of bytes available in buf
4411 * @param buf where the callee should write the message
4412 * @return number of bytes written to buf
4415 send_core_path_create (void *cls, size_t size, void *buf)
4417 struct MeshPathInfo *info = cls;
4418 struct GNUNET_MESH_ManipulatePath *msg;
4419 struct GNUNET_PeerIdentity *peer_ptr;
4420 struct MeshTunnel *t = info->t;
4421 struct MeshPeerPath *p = info->path;
4426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4428 sizeof (struct GNUNET_MESH_ManipulatePath) +
4429 p->length * sizeof (struct GNUNET_PeerIdentity);
4431 if (size < size_needed || NULL == buf)
4436 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4437 msg->header.size = htons (size_needed);
4438 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4439 msg->tid = ntohl (t->id.tid);
4442 if (GNUNET_YES == t->speed_min)
4443 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4444 if (GNUNET_YES == t->nobuffer)
4445 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4446 msg->opt = htonl(opt);
4449 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4450 for (i = 0; i < p->length; i++)
4452 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4459 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4465 * Fill the core buffer
4467 * @param cls closure (data itself)
4468 * @param size number of bytes available in buf
4469 * @param buf where the callee should write the message
4471 * @return number of bytes written to buf
4474 send_core_data_multicast (void *cls, size_t size, void *buf)
4476 struct MeshTransmissionDescriptor *info = cls;
4479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4480 GNUNET_assert (NULL != info);
4481 GNUNET_assert (NULL != info->peer);
4482 total_size = info->mesh_data->data_len;
4483 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4485 if (total_size > size)
4490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4491 memcpy (buf, info->mesh_data->data, total_size);
4494 struct GNUNET_MESH_Multicast *mc;
4495 struct GNUNET_MessageHeader *mh;
4498 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4500 mc = (struct GNUNET_MESH_Multicast *) mh;
4501 mh = (struct GNUNET_MessageHeader *) &mc[1];
4502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4503 " multicast, payload type %s\n",
4504 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4506 " multicast, payload size %u\n", ntohs (mh->size));
4510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4511 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4515 data_descriptor_decrement_rc (info->mesh_data);
4516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4524 * Creates a path ack message in buf and frees all unused resources.
4526 * @param cls closure (MeshTransmissionDescriptor)
4527 * @param size number of bytes available in buf
4528 * @param buf where the callee should write the message
4529 * @return number of bytes written to buf
4532 send_core_path_ack (void *cls, size_t size, void *buf)
4534 struct MeshTransmissionDescriptor *info = cls;
4535 struct GNUNET_MESH_PathACK *msg = buf;
4537 GNUNET_assert (NULL != info);
4538 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4543 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4544 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4545 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4546 msg->tid = htonl (info->origin->tid);
4547 msg->peer_id = my_full_id;
4550 /* TODO add signature */
4552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4553 return sizeof (struct GNUNET_MESH_PathACK);
4558 * Free a transmission that was already queued with all resources
4559 * associated to the request.
4561 * @param queue Queue handler to cancel.
4562 * @param clear_cls Is it necessary to free associated cls?
4565 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4567 struct MeshTransmissionDescriptor *dd;
4568 struct MeshPathInfo *path_info;
4569 struct MeshTunnelChildInfo *cinfo;
4570 struct GNUNET_PeerIdentity id;
4574 if (GNUNET_YES == clear_cls)
4576 switch (queue->type)
4578 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4579 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4580 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4582 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4583 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4584 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4585 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4586 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4587 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4589 " prebuilt message\n");
4590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4592 GNUNET_MESH_DEBUG_M2S(queue->type));
4594 data_descriptor_decrement_rc (dd->mesh_data);
4596 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4598 path_info = queue->cls;
4599 path_destroy (path_info->path);
4603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4604 " type %s unknown!\n",
4605 GNUNET_MESH_DEBUG_M2S(queue->type));
4607 GNUNET_free_non_null (queue->cls);
4609 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4610 queue->peer->queue_tail,
4613 /* Delete from child_fc in the appropiate tunnel */
4614 max = queue->tunnel->fwd_queue_max;
4615 GNUNET_PEER_resolve (queue->peer->id, &id);
4616 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4619 for (i = 0; i < cinfo->send_buffer_n; i++)
4622 i2 = (cinfo->send_buffer_start + i) % max;
4623 if (cinfo->send_buffer[i2] == queue)
4625 /* Found corresponding entry in the send_buffer. Move all others back. */
4630 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4632 j2 = (cinfo->send_buffer_start + j) % max;
4633 j3 = (cinfo->send_buffer_start + j + 1) % max;
4634 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4637 cinfo->send_buffer[j3] = NULL;
4638 cinfo->send_buffer_n--;
4643 GNUNET_free (queue);
4648 * @brief Get the next transmittable message from the queue.
4650 * This will be the head, except in the case of being a data packet
4651 * not allowed by the destination peer.
4653 * @param peer Destination peer.
4655 * @return The next viable MeshPeerQueue element to send to that peer.
4656 * NULL when there are no transmittable messages.
4658 struct MeshPeerQueue *
4659 queue_get_next (const struct MeshPeerInfo *peer)
4661 struct MeshPeerQueue *q;
4662 struct MeshTunnel *t;
4663 struct MeshTransmissionDescriptor *info;
4664 struct MeshTunnelChildInfo *cinfo;
4665 struct GNUNET_MESH_Unicast *ucast;
4666 struct GNUNET_MESH_ToOrigin *to_orig;
4667 struct GNUNET_MESH_Multicast *mcast;
4668 struct GNUNET_PeerIdentity id;
4672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4673 for (q = peer->queue_head; NULL != q; q = q->next)
4677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4679 GNUNET_MESH_DEBUG_M2S(q->type));
4682 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4683 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4684 pid = ntohl (ucast->pid);
4685 GNUNET_PEER_resolve (info->peer->id, &id);
4686 cinfo = tunnel_get_neighbor_fc(t, &id);
4687 ack = cinfo->fwd_ack;
4689 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4690 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4691 pid = ntohl (to_orig->pid);
4694 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4695 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4696 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4698 // Not a multicast payload: multicast control traffic (destroy, etc)
4701 pid = ntohl (mcast->pid);
4702 GNUNET_PEER_resolve (info->peer->id, &id);
4703 cinfo = tunnel_get_neighbor_fc(t, &id);
4704 ack = cinfo->fwd_ack;
4707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4712 "********* ACK: %u, PID: %u\n",
4714 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4723 "********* NEXT!\n");
4726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4727 "********* nothing found\n");
4733 * Core callback to write a queued packet to core buffer
4735 * @param cls Closure (peer info).
4736 * @param size Number of bytes available in buf.
4737 * @param buf Where the to write the message.
4739 * @return number of bytes written to buf
4742 queue_send (void *cls, size_t size, void *buf)
4744 struct MeshPeerInfo *peer = cls;
4745 struct GNUNET_MessageHeader *msg;
4746 struct MeshPeerQueue *queue;
4747 struct MeshTunnel *t;
4748 struct MeshTunnelChildInfo *cinfo;
4749 struct GNUNET_PeerIdentity dst_id;
4752 peer->core_transmit = NULL;
4755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4756 queue = queue_get_next (peer);
4758 /* Queue has no internal mesh traffic nor sendable payload */
4761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4762 if (NULL == peer->queue_head)
4763 GNUNET_break (0); // Should've been canceled
4766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4768 GNUNET_PEER_resolve (peer->id, &dst_id);
4769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4770 "********* towards %s\n",
4771 GNUNET_i2s(&dst_id));
4772 /* Check if buffer size is enough for the message */
4773 if (queue->size > size)
4775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4776 "********* not enough room, reissue\n");
4777 peer->core_transmit =
4778 GNUNET_CORE_notify_transmit_ready (core_handle,
4781 GNUNET_TIME_UNIT_FOREVER_REL,
4788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4791 GNUNET_assert (0 < t->pending_messages);
4792 t->pending_messages--;
4793 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4797 "********* unicast: t->q (%u/%u)\n",
4798 t->fwd_queue_n, t->fwd_queue_max);
4800 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4807 switch (queue->type)
4810 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4811 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4812 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4813 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4814 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4816 "********* raw: %s\n",
4817 GNUNET_MESH_DEBUG_M2S (queue->type));
4819 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4820 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4821 data_size = send_core_data_raw (queue->cls, size, buf);
4822 msg = (struct GNUNET_MessageHeader *) buf;
4823 switch (ntohs (msg->type)) // Type of preconstructed message
4825 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4826 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4828 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4829 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4835 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4838 struct MeshTransmissionDescriptor *info = queue->cls;
4840 if ((1 == info->mesh_data->reference_counter
4841 && GNUNET_YES == t->speed_min)
4843 (info->mesh_data->total_out == info->mesh_data->reference_counter
4844 && GNUNET_NO == t->speed_min))
4846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4847 "********* considered sent\n");
4852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4853 "********* NOT considered sent yet\n");
4854 t->pending_messages++;
4857 data_size = send_core_data_multicast(queue->cls, size, buf);
4858 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4860 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4862 data_size = send_core_path_create (queue->cls, size, buf);
4864 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4866 data_size = send_core_path_ack (queue->cls, size, buf);
4868 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4870 data_size = send_core_data_multicast (queue->cls, size, buf);
4874 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4875 "********* type unknown: %u\n",
4879 switch (queue->type)
4881 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4882 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4883 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4884 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4885 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4888 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4889 "at pos %u (%p) != %p\n",
4890 cinfo->send_buffer_start,
4891 cinfo->send_buffer[cinfo->send_buffer_start],
4894 if (cinfo->send_buffer_n > 0)
4896 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4897 cinfo->send_buffer_n--;
4898 cinfo->send_buffer_start++;
4899 cinfo->send_buffer_start %= t->fwd_queue_max;
4910 /* Free queue, but cls was freed by send_core_* */
4911 queue_destroy (queue, GNUNET_NO);
4913 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4919 /* If more data in queue, send next */
4920 queue = queue_get_next(peer);
4923 struct GNUNET_PeerIdentity id;
4925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4926 GNUNET_PEER_resolve (peer->id, &id);
4927 peer->core_transmit =
4928 GNUNET_CORE_notify_transmit_ready(core_handle,
4931 GNUNET_TIME_UNIT_FOREVER_REL,
4939 if (NULL != peer->queue_head)
4941 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4942 "********* %s stalled\n",
4943 GNUNET_i2s(&my_full_id));
4945 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4946 // FIXME unify bck/fwd structures, bck does not have cinfo right now
4947 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
4949 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
4950 &tunnel_poll, cinfo);
4954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4960 * @brief Queue and pass message to core when possible.
4962 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
4963 * and accounts for it. In case the queue is full, the message is dropped and
4966 * Otherwise, message is treated as internal and allowed to go regardless of
4969 * @param cls Closure (@c type dependant). It will be used by queue_send to
4970 * build the message to be sent if not already prebuilt.
4971 * @param type Type of the message, 0 for a raw message.
4972 * @param size Size of the message.
4973 * @param dst Neighbor to send message to.
4974 * @param t Tunnel this message belongs to.
4977 queue_add (void *cls, uint16_t type, size_t size,
4978 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4980 struct MeshPeerQueue *queue;
4981 struct MeshTunnelChildInfo *cinfo;
4982 struct GNUNET_PeerIdentity id;
4988 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4989 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4991 n = &t->fwd_queue_n;
4992 max = &t->fwd_queue_max;
4994 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4996 n = &t->bck_queue_n;
4997 max = &t->bck_queue_max;
5004 GNUNET_STATISTICS_update(stats,
5005 "# messages dropped (buffer full)",
5007 return; // Drop message
5011 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5017 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5018 GNUNET_PEER_resolve (dst->id, &id);
5019 if (NULL == dst->core_transmit)
5021 dst->core_transmit =
5022 GNUNET_CORE_notify_transmit_ready (core_handle,
5025 GNUNET_TIME_UNIT_FOREVER_REL,
5031 t->pending_messages++;
5032 if (NULL == n) // Is this internal mesh traffic?
5035 // It's payload, keep track of buffer per peer.
5036 cinfo = tunnel_get_neighbor_fc(t, &id);
5037 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5038 if (NULL != cinfo->send_buffer[i])
5040 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5041 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5042 cinfo->send_buffer_start++;
5043 cinfo->send_buffer_start %= t->fwd_queue_max;
5047 cinfo->send_buffer_n++;
5049 cinfo->send_buffer[i] = queue;
5050 if (cinfo->send_buffer_n > t->fwd_queue_max)
5053 cinfo->send_buffer_n = t->fwd_queue_max;
5058 /******************************************************************************/
5059 /******************** MESH NETWORK HANDLERS **************************/
5060 /******************************************************************************/
5064 * Core handler for path creation
5066 * @param cls closure
5067 * @param message message
5068 * @param peer peer identity this notification is about
5069 * @param atsi performance data
5070 * @param atsi_count number of records in 'atsi'
5072 * @return GNUNET_OK to keep the connection open,
5073 * GNUNET_SYSERR to close it (signal serious error)
5076 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5077 const struct GNUNET_MessageHeader *message,
5078 const struct GNUNET_ATS_Information *atsi,
5079 unsigned int atsi_count)
5081 unsigned int own_pos;
5084 MESH_TunnelNumber tid;
5085 struct GNUNET_MESH_ManipulatePath *msg;
5086 struct GNUNET_PeerIdentity *pi;
5087 struct GNUNET_HashCode hash;
5088 struct MeshPeerPath *path;
5089 struct MeshPeerInfo *dest_peer_info;
5090 struct MeshPeerInfo *orig_peer_info;
5091 struct MeshTunnel *t;
5093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5094 "Received a path create msg [%s]\n",
5095 GNUNET_i2s (&my_full_id));
5096 size = ntohs (message->size);
5097 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5099 GNUNET_break_op (0);
5103 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5104 if (size % sizeof (struct GNUNET_PeerIdentity))
5106 GNUNET_break_op (0);
5109 size /= sizeof (struct GNUNET_PeerIdentity);
5112 GNUNET_break_op (0);
5115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5116 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5118 tid = ntohl (msg->tid);
5119 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5121 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5122 t = tunnel_get (pi, tid);
5123 if (NULL == t) // FIXME only for INCOMING tunnels?
5127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5128 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5131 // FIXME notify failure
5134 opt = ntohl (msg->opt);
5135 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5136 GNUNET_YES : GNUNET_NO;
5137 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5139 t->nobuffer = GNUNET_YES;
5140 t->last_fwd_ack = t->fwd_pid + 1;
5142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5143 " speed_min: %d, nobuffer:%d\n",
5144 t->speed_min, t->nobuffer);
5146 if (GNUNET_YES == t->nobuffer)
5148 t->bck_queue_max = 1;
5149 t->fwd_queue_max = 1;
5152 // FIXME only assign a local tid if a local client is interested (on demand)
5153 while (NULL != tunnel_get_incoming (next_local_tid))
5154 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5155 t->local_tid_dest = next_local_tid++;
5156 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5159 tunnel_reset_timeout (t);
5160 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5162 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5163 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5171 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5172 if (NULL == dest_peer_info)
5174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5175 " Creating PeerInfo for destination.\n");
5176 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5177 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5178 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5180 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5182 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5183 if (NULL == orig_peer_info)
5185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5186 " Creating PeerInfo for origin.\n");
5187 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5188 orig_peer_info->id = GNUNET_PEER_intern (pi);
5189 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5190 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5193 path = path_new (size);
5195 for (i = 0; i < size; i++)
5197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5198 GNUNET_i2s (&pi[i]));
5199 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5200 if (path->peers[i] == myid)
5203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5206 /* cannot be self, must be 'not found' */
5207 /* create path: self not found in path through self */
5208 GNUNET_break_op (0);
5209 path_destroy (path);
5213 path_add_to_peers (path, GNUNET_NO);
5214 tunnel_add_path (t, path, own_pos);
5215 if (own_pos == size - 1)
5217 /* It is for us! Send ack. */
5218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5219 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5220 if (NULL == t->peers)
5222 /* New tunnel! Notify clients on first payload message. */
5223 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5225 GNUNET_break (GNUNET_SYSERR !=
5226 GNUNET_CONTAINER_multihashmap_put (t->peers,
5227 &my_full_id.hashPubKey,
5230 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5235 struct MeshPeerPath *path2;
5237 /* It's for somebody else! Retransmit. */
5238 path2 = path_duplicate (path);
5239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5240 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5241 path2 = path_duplicate (path);
5242 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5243 send_create_path (dest_peer_info, path, t);
5250 * Core handler for path destruction
5252 * @param cls closure
5253 * @param message message
5254 * @param peer peer identity this notification is about
5255 * @param atsi performance data
5256 * @param atsi_count number of records in 'atsi'
5258 * @return GNUNET_OK to keep the connection open,
5259 * GNUNET_SYSERR to close it (signal serious error)
5262 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5263 const struct GNUNET_MessageHeader *message,
5264 const struct GNUNET_ATS_Information *atsi,
5265 unsigned int atsi_count)
5267 struct GNUNET_MESH_ManipulatePath *msg;
5268 struct GNUNET_PeerIdentity *pi;
5269 struct MeshPeerPath *path;
5270 struct MeshTunnel *t;
5271 unsigned int own_pos;
5275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5276 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5277 size = ntohs (message->size);
5278 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5280 GNUNET_break_op (0);
5284 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5285 if (size % sizeof (struct GNUNET_PeerIdentity))
5287 GNUNET_break_op (0);
5290 size /= sizeof (struct GNUNET_PeerIdentity);
5293 GNUNET_break_op (0);
5296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5298 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5299 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5301 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5303 t = tunnel_get (pi, ntohl (msg->tid));
5306 /* TODO notify back: we don't know this tunnel */
5307 GNUNET_break_op (0);
5310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5311 path = path_new (size);
5313 for (i = 0; i < size; i++)
5315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5316 GNUNET_i2s (&pi[i]));
5317 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5318 if (path->peers[i] == myid)
5321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5322 if (own_pos < path->length - 1)
5323 send_prebuilt_message (message, &pi[own_pos + 1], t);
5325 send_client_tunnel_disconnect(t, NULL);
5327 tunnel_delete_peer (t, path->peers[path->length - 1]);
5328 path_destroy (path);
5334 * Core handler for notifications of broken paths
5336 * @param cls closure
5337 * @param message message
5338 * @param peer peer identity this notification is about
5339 * @param atsi performance data
5340 * @param atsi_count number of records in 'atsi'
5342 * @return GNUNET_OK to keep the connection open,
5343 * GNUNET_SYSERR to close it (signal serious error)
5346 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5347 const struct GNUNET_MessageHeader *message,
5348 const struct GNUNET_ATS_Information *atsi,
5349 unsigned int atsi_count)
5351 struct GNUNET_MESH_PathBroken *msg;
5352 struct MeshTunnel *t;
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5355 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5356 msg = (struct GNUNET_MESH_PathBroken *) message;
5357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5358 GNUNET_i2s (&msg->peer1));
5359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5360 GNUNET_i2s (&msg->peer2));
5361 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5364 GNUNET_break_op (0);
5367 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5368 GNUNET_PEER_search (&msg->peer2));
5375 * Core handler for tunnel destruction
5377 * @param cls closure
5378 * @param message message
5379 * @param peer peer identity this notification is about
5380 * @param atsi performance data
5381 * @param atsi_count number of records in 'atsi'
5383 * @return GNUNET_OK to keep the connection open,
5384 * GNUNET_SYSERR to close it (signal serious error)
5387 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5388 const struct GNUNET_MessageHeader *message,
5389 const struct GNUNET_ATS_Information *atsi,
5390 unsigned int atsi_count)
5392 struct GNUNET_MESH_TunnelDestroy *msg;
5393 struct MeshTunnel *t;
5394 GNUNET_PEER_Id parent;
5397 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5399 "Got a TUNNEL DESTROY packet from %s\n",
5401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5402 " for tunnel %s [%u]\n",
5403 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5404 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5405 /* Check signature */
5408 /* Probably already got the message from another path,
5409 * destroyed the tunnel and retransmitted to children.
5412 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5416 parent = tree_get_predecessor (t->tree);
5417 pid = GNUNET_PEER_search (peer);
5422 tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
5423 nc = tree_count_children (t->tree);
5424 if (nc > 0 || NULL != t->owner || t->nclients > 0)
5426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5427 "still in use: %u cl, %u ch\n",
5432 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5434 /* Tunnel was incoming, notify clients */
5435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5436 t->local_tid, t->local_tid_dest);
5437 send_clients_tunnel_destroy (t);
5439 tunnel_send_destroy (t, parent);
5440 t->destroy = GNUNET_YES;
5441 // TODO: add timeout to destroy the tunnel anyway
5447 * Core handler for mesh network traffic going from the origin to a peer
5449 * @param cls closure
5450 * @param peer peer identity this notification is about
5451 * @param message message
5452 * @param atsi performance data
5453 * @param atsi_count number of records in 'atsi'
5454 * @return GNUNET_OK to keep the connection open,
5455 * GNUNET_SYSERR to close it (signal serious error)
5458 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5459 const struct GNUNET_MessageHeader *message,
5460 const struct GNUNET_ATS_Information *atsi,
5461 unsigned int atsi_count)
5463 struct GNUNET_MESH_Unicast *msg;
5464 struct GNUNET_PeerIdentity *neighbor;
5465 struct MeshTunnelChildInfo *cinfo;
5466 struct MeshTunnel *t;
5467 GNUNET_PEER_Id dest_id;
5472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5475 size = ntohs (message->size);
5477 sizeof (struct GNUNET_MESH_Unicast) +
5478 sizeof (struct GNUNET_MessageHeader))
5483 msg = (struct GNUNET_MESH_Unicast *) message;
5484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5485 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5487 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5490 /* TODO notify back: we don't know this tunnel */
5491 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5492 GNUNET_break_op (0);
5495 pid = ntohl (msg->pid);
5496 if (t->fwd_pid == pid)
5498 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5500 " Already seen pid %u, DROPPING!\n", pid);
5505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5506 " pid %u not seen yet, forwarding\n", pid);
5509 t->skip += (pid - t->fwd_pid) - 1;
5512 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5514 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5515 GNUNET_break_op (0);
5516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5517 "Received PID %u, ACK %u\n",
5518 pid, t->last_fwd_ack);
5522 tunnel_reset_timeout (t);
5523 dest_id = GNUNET_PEER_search (&msg->destination);
5524 if (dest_id == myid)
5526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5527 " it's for us! sending to clients...\n");
5528 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5529 send_subscribed_clients (message, &msg[1].header, t);
5530 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5533 ttl = ntohl (msg->ttl);
5534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5537 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5539 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5543 " not for us, retransmitting...\n");
5545 neighbor = tree_get_first_hop (t->tree, dest_id);
5546 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5547 cinfo->fwd_pid = pid;
5548 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5551 if (GNUNET_YES == t->nobuffer &&
5552 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5554 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5555 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5556 GNUNET_break_op (0);
5559 send_prebuilt_message (message, neighbor, t);
5560 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5566 * Core handler for mesh network traffic going from the origin to all peers
5568 * @param cls closure
5569 * @param message message
5570 * @param peer peer identity this notification is about
5571 * @param atsi performance data
5572 * @param atsi_count number of records in 'atsi'
5573 * @return GNUNET_OK to keep the connection open,
5574 * GNUNET_SYSERR to close it (signal serious error)
5576 * TODO: Check who we got this from, to validate route.
5579 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5580 const struct GNUNET_MessageHeader *message,
5581 const struct GNUNET_ATS_Information *atsi,
5582 unsigned int atsi_count)
5584 struct GNUNET_MESH_Multicast *msg;
5585 struct MeshTunnel *t;
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5591 size = ntohs (message->size);
5592 if (sizeof (struct GNUNET_MESH_Multicast) +
5593 sizeof (struct GNUNET_MessageHeader) > size)
5595 GNUNET_break_op (0);
5598 msg = (struct GNUNET_MESH_Multicast *) message;
5599 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5603 /* TODO notify that we dont know that tunnel */
5604 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5605 GNUNET_break_op (0);
5608 pid = ntohl (msg->pid);
5609 if (t->fwd_pid == pid)
5611 /* already seen this packet, drop */
5612 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5614 " Already seen pid %u, DROPPING!\n", pid);
5615 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5621 " pid %u not seen yet, forwarding\n", pid);
5623 t->skip += (pid - t->fwd_pid) - 1;
5625 tunnel_reset_timeout (t);
5627 /* Transmit to locally interested clients */
5628 if (NULL != t->peers &&
5629 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5631 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5632 send_subscribed_clients (message, &msg[1].header, t);
5633 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5636 if (ntohl (msg->ttl) == 0)
5638 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5640 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5643 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5644 tunnel_send_multicast (t, message);
5650 * Core handler for mesh network traffic toward the owner of a tunnel
5652 * @param cls closure
5653 * @param message message
5654 * @param peer peer identity this notification is about
5655 * @param atsi performance data
5656 * @param atsi_count number of records in 'atsi'
5658 * @return GNUNET_OK to keep the connection open,
5659 * GNUNET_SYSERR to close it (signal serious error)
5662 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5663 const struct GNUNET_MessageHeader *message,
5664 const struct GNUNET_ATS_Information *atsi,
5665 unsigned int atsi_count)
5667 struct GNUNET_MESH_ToOrigin *msg;
5668 struct GNUNET_PeerIdentity id;
5669 struct MeshPeerInfo *peer_info;
5670 struct MeshTunnel *t;
5671 struct MeshTunnelChildInfo *cinfo;
5672 GNUNET_PEER_Id predecessor;
5676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5678 size = ntohs (message->size);
5679 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5680 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5682 GNUNET_break_op (0);
5685 msg = (struct GNUNET_MESH_ToOrigin *) message;
5686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5687 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5688 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5689 pid = ntohl (msg->pid);
5693 /* TODO notify that we dont know this tunnel (whom)? */
5694 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5696 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
5697 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5701 cinfo = tunnel_get_neighbor_fc(t, peer);
5708 if (cinfo->bck_pid == pid)
5710 /* already seen this packet, drop */
5711 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5713 " Already seen pid %u, DROPPING!\n", pid);
5714 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5719 " pid %u not seen yet, forwarding\n", pid);
5720 cinfo->bck_pid = pid;
5722 if (NULL != t->owner)
5725 struct GNUNET_MESH_ToOrigin *copy;
5727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5728 " it's for us! sending to clients...\n");
5729 /* TODO signature verification */
5730 memcpy (cbuf, message, size);
5731 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5732 copy->tid = htonl (t->local_tid);
5734 copy->pid = htonl (t->bck_pid);
5735 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5736 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5737 ©->header, GNUNET_NO);
5738 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5742 " not for us, retransmitting...\n");
5744 peer_info = peer_info_get (&msg->oid);
5745 if (NULL == peer_info)
5747 /* unknown origin of tunnel */
5751 predecessor = tree_get_predecessor (t->tree);
5752 if (0 == predecessor)
5754 if (GNUNET_YES == t->destroy)
5756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5757 "to orig received on a dying tunnel %s [%X]\n",
5758 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5762 "unknown to origin at %s\n",
5763 GNUNET_i2s (&my_full_id));
5764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5767 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5768 "for tunnel %s [%X]\n",
5769 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5770 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5772 tree_debug (t->tree);
5775 GNUNET_PEER_resolve (predecessor, &id);
5776 send_prebuilt_message (message, &id, t);
5777 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5784 * Core handler for mesh network traffic point-to-point acks.
5786 * @param cls closure
5787 * @param message message
5788 * @param peer peer identity this notification is about
5789 * @param atsi performance data
5790 * @param atsi_count number of records in 'atsi'
5792 * @return GNUNET_OK to keep the connection open,
5793 * GNUNET_SYSERR to close it (signal serious error)
5796 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5797 const struct GNUNET_MessageHeader *message,
5798 const struct GNUNET_ATS_Information *atsi,
5799 unsigned int atsi_count)
5801 struct GNUNET_MESH_ACK *msg;
5802 struct MeshTunnel *t;
5805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5807 msg = (struct GNUNET_MESH_ACK *) message;
5809 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5813 /* TODO notify that we dont know this tunnel (whom)? */
5814 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5817 ack = ntohl (msg->pid);
5818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5820 /* Is this a forward or backward ACK? */
5821 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5823 struct MeshTunnelChildInfo *cinfo;
5826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5827 cinfo = tunnel_get_neighbor_fc (t, peer);
5828 cinfo->fwd_ack = ack;
5829 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5830 tunnel_unlock_fwd_queues (t);
5831 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
5833 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
5834 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
5835 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
5840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5842 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5843 tunnel_unlock_bck_queue (t);
5850 * Core handler for mesh network traffic point-to-point ack polls.
5852 * @param cls closure
5853 * @param message message
5854 * @param peer peer identity this notification is about
5855 * @param atsi performance data
5856 * @param atsi_count number of records in 'atsi'
5858 * @return GNUNET_OK to keep the connection open,
5859 * GNUNET_SYSERR to close it (signal serious error)
5862 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5863 const struct GNUNET_MessageHeader *message,
5864 const struct GNUNET_ATS_Information *atsi,
5865 unsigned int atsi_count)
5867 struct GNUNET_MESH_Poll *msg;
5868 struct MeshTunnel *t;
5870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5873 msg = (struct GNUNET_MESH_Poll *) message;
5875 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5879 /* TODO notify that we dont know this tunnel (whom)? */
5880 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5881 GNUNET_break_op (0);
5885 /* Is this a forward or backward ACK? */
5886 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5888 struct MeshTunnelChildInfo *cinfo;
5890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5891 cinfo = tunnel_get_neighbor_fc (t, peer);
5892 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5893 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5898 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5905 * Core handler for path ACKs
5907 * @param cls closure
5908 * @param message message
5909 * @param peer peer identity this notification is about
5910 * @param atsi performance data
5911 * @param atsi_count number of records in 'atsi'
5913 * @return GNUNET_OK to keep the connection open,
5914 * GNUNET_SYSERR to close it (signal serious error)
5917 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5918 const struct GNUNET_MessageHeader *message,
5919 const struct GNUNET_ATS_Information *atsi,
5920 unsigned int atsi_count)
5922 struct GNUNET_MESH_PathACK *msg;
5923 struct GNUNET_PeerIdentity id;
5924 struct MeshPeerInfo *peer_info;
5925 struct MeshPeerPath *p;
5926 struct MeshTunnel *t;
5928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5929 GNUNET_i2s (&my_full_id));
5930 msg = (struct GNUNET_MESH_PathACK *) message;
5931 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5934 /* TODO notify that we don't know the tunnel */
5935 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5937 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5941 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5943 peer_info = peer_info_get (&msg->peer_id);
5944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5945 GNUNET_i2s (&msg->peer_id));
5946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5949 if (NULL != t->regex_search && t->regex_search->peer == peer_info->id)
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5952 "connect_by_string completed, stopping search\n");
5953 regex_cancel_search (t->regex_search);
5954 t->regex_search = NULL;
5957 /* Add paths to peers? */
5958 p = tree_get_path_to_peer (t->tree, peer_info->id);
5961 path_add_to_peers (p, GNUNET_YES);
5969 /* Message for us? */
5970 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5973 if (NULL == t->owner)
5975 GNUNET_break_op (0);
5978 if (NULL != t->dht_get_type)
5980 GNUNET_DHT_get_stop (t->dht_get_type);
5981 t->dht_get_type = NULL;
5983 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5985 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5986 send_client_peer_connected (t, peer_info->id);
5988 if (NULL != peer_info->dhtget)
5990 GNUNET_DHT_get_stop (peer_info->dhtget);
5991 peer_info->dhtget = NULL;
5996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5997 " not for us, retransmitting...\n");
5998 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5999 peer_info = peer_info_get (&msg->oid);
6000 send_prebuilt_message (message, &id, t);
6006 * Core handler for mesh keepalives.
6008 * @param cls closure
6009 * @param message message
6010 * @param peer peer identity this notification is about
6011 * @param atsi performance data
6012 * @param atsi_count number of records in 'atsi'
6013 * @return GNUNET_OK to keep the connection open,
6014 * GNUNET_SYSERR to close it (signal serious error)
6016 * TODO: Check who we got this from, to validate route.
6019 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6020 const struct GNUNET_MessageHeader *message,
6021 const struct GNUNET_ATS_Information *atsi,
6022 unsigned int atsi_count)
6024 struct GNUNET_MESH_TunnelKeepAlive *msg;
6025 struct MeshTunnel *t;
6027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6030 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6031 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6035 /* TODO notify that we dont know that tunnel */
6036 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
6041 tunnel_reset_timeout (t);
6043 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6044 tunnel_send_multicast (t, message);
6051 * Functions to handle messages from core
6053 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6054 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6055 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6056 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6057 sizeof (struct GNUNET_MESH_PathBroken)},
6058 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6059 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6060 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6061 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6062 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6063 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6064 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6065 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6066 sizeof (struct GNUNET_MESH_ACK)},
6067 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6068 sizeof (struct GNUNET_MESH_Poll)},
6069 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6070 sizeof (struct GNUNET_MESH_PathACK)},
6076 /******************************************************************************/
6077 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6078 /******************************************************************************/
6081 * deregister_app: iterator for removing each application registered by a client
6083 * @param cls closure
6084 * @param key the hash of the application id (used to access the hashmap)
6085 * @param value the value stored at the key (client)
6087 * @return GNUNET_OK on success
6090 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6092 struct MeshClient *c = cls;
6094 GNUNET_break (GNUNET_YES ==
6095 GNUNET_CONTAINER_multihashmap_remove (applications, key, c));
6101 * notify_client_connection_failure: notify a client that the connection to the
6102 * requested remote peer is not possible (for instance, no route found)
6103 * Function called when the socket is ready to queue more data. "buf" will be
6104 * NULL and "size" zero if the socket was closed for writing in the meantime.
6106 * @param cls closure
6107 * @param size number of bytes available in buf
6108 * @param buf where the callee should write the message
6109 * @return number of bytes written to buf
6112 notify_client_connection_failure (void *cls, size_t size, void *buf)
6115 struct MeshPeerInfo *peer_info;
6116 struct GNUNET_MESH_PeerControl *msg;
6117 struct GNUNET_PeerIdentity id;
6119 if (0 == size && NULL == buf)
6121 // TODO retry? cancel?
6125 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6126 peer_info = (struct MeshPeerInfo *) cls;
6127 msg = (struct GNUNET_MESH_PeerControl *) buf;
6128 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6129 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6130 // msg->tunnel_id = htonl(peer_info->t->tid);
6131 GNUNET_PEER_resolve (peer_info->id, &id);
6132 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6140 * Send keepalive packets for a peer
6142 * @param cls Closure (tunnel for which to send the keepalive).
6143 * @param tc Notification context.
6146 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6148 struct MeshTunnel *t = cls;
6149 struct GNUNET_MESH_TunnelKeepAlive *msg;
6150 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6153 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6154 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6160 "sending keepalive for tunnel %d\n", t->id.tid);
6162 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6163 msg->header.size = htons (size);
6164 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6165 msg->oid = my_full_id;
6166 msg->tid = htonl (t->id.tid);
6167 tunnel_send_multicast (t, &msg->header);
6169 t->path_refresh_task =
6170 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6171 tunnel_reset_timeout(t);
6176 * Function to process paths received for a new peer addition. The recorded
6177 * paths form the initial tunnel, which can be optimized later.
6178 * Called on each result obtained for the DHT search.
6180 * @param cls closure
6181 * @param exp when will this value expire
6182 * @param key key of the result
6183 * @param get_path path of the get request
6184 * @param get_path_length lenght of get_path
6185 * @param put_path path of the put request
6186 * @param put_path_length length of the put_path
6187 * @param type type of the result
6188 * @param size number of bytes in data
6189 * @param data pointer to the result data
6191 * TODO: re-issue the request after certain time? cancel after X results?
6194 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6195 const struct GNUNET_HashCode * key,
6196 const struct GNUNET_PeerIdentity *get_path,
6197 unsigned int get_path_length,
6198 const struct GNUNET_PeerIdentity *put_path,
6199 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6200 size_t size, const void *data)
6202 struct MeshPathInfo *path_info = cls;
6203 struct MeshPeerPath *p;
6204 struct GNUNET_PeerIdentity pi;
6207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6208 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6211 p = path_build_from_dht (get_path, get_path_length,
6212 put_path, put_path_length);
6213 path_add_to_peers (p, GNUNET_NO);
6215 for (i = 0; i < path_info->peer->ntunnels; i++)
6217 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6218 peer_info_connect (path_info->peer, path_info->t);
6226 * Function to process paths received for a new peer addition. The recorded
6227 * paths form the initial tunnel, which can be optimized later.
6228 * Called on each result obtained for the DHT search.
6230 * @param cls closure
6231 * @param exp when will this value expire
6232 * @param key key of the result
6233 * @param get_path path of the get request
6234 * @param get_path_length lenght of get_path
6235 * @param put_path path of the put request
6236 * @param put_path_length length of the put_path
6237 * @param type type of the result
6238 * @param size number of bytes in data
6239 * @param data pointer to the result data
6242 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6243 const struct GNUNET_HashCode * key,
6244 const struct GNUNET_PeerIdentity *get_path,
6245 unsigned int get_path_length,
6246 const struct GNUNET_PeerIdentity *put_path,
6247 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6248 size_t size, const void *data)
6250 const struct PBlock *pb = data;
6251 const struct GNUNET_PeerIdentity *pi = &pb->id;
6252 struct MeshTunnel *t = cls;
6253 struct MeshPeerInfo *peer_info;
6254 struct MeshPeerPath *p;
6256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6257 if (size != sizeof (struct PBlock))
6259 GNUNET_break_op (0);
6262 if (ntohl(pb->type) != t->type)
6264 GNUNET_break_op (0);
6267 GNUNET_assert (NULL != t->owner);
6268 peer_info = peer_info_get (pi);
6269 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6271 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6273 p = path_build_from_dht (get_path, get_path_length, put_path,
6275 path_add_to_peers (p, GNUNET_NO);
6277 tunnel_add_peer (t, peer_info);
6278 peer_info_connect (peer_info, t);
6282 /******************************************************************************/
6283 /********************* MESH LOCAL HANDLES **************************/
6284 /******************************************************************************/
6288 * Handler for client disconnection
6290 * @param cls closure
6291 * @param client identification of the client; NULL
6292 * for the last call when the server is destroyed
6295 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6297 struct MeshClient *c;
6298 struct MeshClient *next;
6301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6311 if (c->handle != client)
6313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6319 GNUNET_SERVER_client_drop (c->handle);
6320 c->shutting_down = GNUNET_YES;
6321 GNUNET_assert (NULL != c->own_tunnels);
6322 GNUNET_assert (NULL != c->incoming_tunnels);
6323 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6324 &tunnel_destroy_iterator, c);
6325 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6326 &tunnel_destroy_iterator, c);
6327 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6328 &tunnel_destroy_iterator, c);
6329 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6330 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6331 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6333 /* deregister clients applications */
6334 if (NULL != c->apps)
6336 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c);
6337 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6339 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6340 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6342 GNUNET_SCHEDULER_cancel (announce_applications_task);
6343 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6345 if (NULL != c->types)
6346 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6347 for (i = 0; i < c->n_regex; i++)
6349 GNUNET_free (c->regexes[i].regex);
6350 if (NULL != c->regexes[i].h)
6351 GNUNET_REGEX_announce_cancel (c->regexes[i].h);
6353 GNUNET_free_non_null (c->regexes);
6354 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6355 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6357 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6360 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6369 * Handler for new clients
6371 * @param cls closure
6372 * @param client identification of the client
6373 * @param message the actual message, which includes messages the client wants
6376 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6377 const struct GNUNET_MessageHeader *message)
6379 struct GNUNET_MESH_ClientConnect *cc_msg;
6380 struct MeshClient *c;
6381 GNUNET_MESH_ApplicationType *a;
6388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6389 /* Check data sanity */
6390 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6391 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6392 ntypes = ntohs (cc_msg->types);
6393 napps = ntohs (cc_msg->applications);
6395 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6402 /* Create new client structure */
6403 c = GNUNET_malloc (sizeof (struct MeshClient));
6404 c->id = next_client_id++;
6405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6407 GNUNET_SERVER_client_keep (client);
6408 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6411 GNUNET_MESH_ApplicationType at;
6412 struct GNUNET_HashCode hc;
6414 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6415 for (i = 0; i < napps; i++)
6418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6419 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6420 /* store in clients hashmap */
6421 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6422 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6423 /* store in global hashmap, for announcements */
6424 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6425 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6427 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6428 announce_applications_task =
6429 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6435 struct GNUNET_HashCode hc;
6437 t = (uint16_t *) & a[napps];
6438 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6439 for (i = 0; i < ntypes; i++)
6442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6443 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6445 /* store in clients hashmap */
6446 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6448 /* store in global hashmap */
6449 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6450 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6454 " client has %u+%u subscriptions\n", napps, ntypes);
6456 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6457 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6458 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6459 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6460 GNUNET_SERVER_notification_context_add (nc, client);
6461 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6463 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6469 * Handler for clients announcing available services by a regular expression.
6471 * @param cls closure
6472 * @param client identification of the client
6473 * @param message the actual message, which includes messages the client wants
6476 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6477 const struct GNUNET_MessageHeader *message)
6479 const struct GNUNET_MESH_RegexAnnounce *msg;
6480 struct MeshRegexDescriptor rd;
6481 struct MeshClient *c;
6486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6488 /* Sanity check for client registration */
6489 if (NULL == (c = client_get (client)))
6492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6497 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6499 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6500 if (NULL != c->partial_regex)
6502 regex = c->partial_regex;
6503 offset = strlen (c->partial_regex);
6504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6505 " continuation, already have %u bytes\n",
6514 regex = GNUNET_realloc (regex, offset + len + 1);
6515 memcpy (®ex[offset], &msg[1], len);
6516 regex[offset + len] = '\0';
6517 if (0 == ntohs (msg->last))
6519 c->partial_regex = regex;
6520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6521 " not ended, stored %u bytes for later\n",
6523 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6527 rd.compression = ntohs (msg->compression_characters);
6529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " compr %u\n", ntohs (rd.compression));
6532 GNUNET_array_append (c->regexes, c->n_regex, rd);
6533 c->partial_regex = NULL;
6534 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6536 c->regex_announce_task = GNUNET_SCHEDULER_add_now (®ex_announce, c);
6542 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6548 * Handler for requests of new tunnels
6550 * @param cls closure
6551 * @param client identification of the client
6552 * @param message the actual message
6555 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6556 const struct GNUNET_MessageHeader *message)
6558 struct GNUNET_MESH_TunnelMessage *t_msg;
6559 struct MeshTunnel *t;
6560 struct MeshClient *c;
6561 MESH_TunnelNumber tid;
6563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6565 /* Sanity check for client registration */
6566 if (NULL == (c = client_get (client)))
6569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6574 /* Message sanity check */
6575 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6582 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6583 /* Sanity check for tunnel numbering */
6584 tid = ntohl (t_msg->tunnel_id);
6585 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6588 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6591 /* Sanity check for duplicate tunnel IDs */
6592 if (NULL != tunnel_get_by_local_id (c, tid))
6595 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6599 while (NULL != tunnel_get_by_pi (myid, next_tid))
6600 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6601 t = tunnel_new (myid, next_tid++, c, tid);
6604 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6606 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6609 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6611 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6612 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6615 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6621 * Handler for requests of deleting tunnels
6623 * @param cls closure
6624 * @param client identification of the client
6625 * @param message the actual message
6628 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6629 const struct GNUNET_MessageHeader *message)
6631 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6632 struct MeshClient *c;
6633 struct MeshTunnel *t;
6634 MESH_TunnelNumber tid;
6636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6637 "Got a DESTROY TUNNEL from client!\n");
6639 /* Sanity check for client registration */
6640 if (NULL == (c = client_get (client)))
6643 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6648 /* Message sanity check */
6649 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6652 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6656 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6658 /* Retrieve tunnel */
6659 tid = ntohl (tunnel_msg->tunnel_id);
6660 t = tunnel_get_by_local_id(c, tid);
6663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6668 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6670 client_ignore_tunnel (c, t);
6671 tunnel_destroy_empty (t);
6672 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6675 send_client_tunnel_disconnect (t, c);
6676 client_delete_tunnel (c, t);
6678 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6680 tunnel_send_destroy (t, 0);
6681 t->destroy = GNUNET_YES;
6682 /* The tunnel will be destroyed when the last message is transmitted. */
6683 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6689 * Handler for requests of seeting tunnel's speed.
6691 * @param cls Closure (unused).
6692 * @param client Identification of the client.
6693 * @param message The actual message.
6696 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6697 const struct GNUNET_MessageHeader *message)
6699 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6700 struct MeshClient *c;
6701 struct MeshTunnel *t;
6702 MESH_TunnelNumber tid;
6704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6705 "Got a SPEED request from client!\n");
6707 /* Sanity check for client registration */
6708 if (NULL == (c = client_get (client)))
6711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6717 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6719 /* Retrieve tunnel */
6720 tid = ntohl (tunnel_msg->tunnel_id);
6721 t = tunnel_get_by_local_id(c, tid);
6724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6726 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6730 switch (ntohs(message->type))
6732 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6733 t->speed_min = GNUNET_YES;
6735 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6736 t->speed_min = GNUNET_NO;
6741 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6746 * Handler for requests of seeting tunnel's buffering policy.
6748 * @param cls Closure (unused).
6749 * @param client Identification of the client.
6750 * @param message The actual message.
6753 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6754 const struct GNUNET_MessageHeader *message)
6756 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6757 struct MeshClient *c;
6758 struct MeshTunnel *t;
6759 MESH_TunnelNumber tid;
6761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6762 "Got a BUFFER request from client!\n");
6764 /* Sanity check for client registration */
6765 if (NULL == (c = client_get (client)))
6768 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6773 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6775 /* Retrieve tunnel */
6776 tid = ntohl (tunnel_msg->tunnel_id);
6777 t = tunnel_get_by_local_id(c, tid);
6780 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6782 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6786 switch (ntohs(message->type))
6788 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6789 t->nobuffer = GNUNET_NO;
6791 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6792 t->nobuffer = GNUNET_YES;
6798 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6803 * Handler for connection requests to new peers
6805 * @param cls closure
6806 * @param client identification of the client
6807 * @param message the actual message (PeerControl)
6810 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6811 const struct GNUNET_MessageHeader *message)
6813 struct GNUNET_MESH_PeerControl *peer_msg;
6814 struct MeshPeerInfo *peer_info;
6815 struct MeshClient *c;
6816 struct MeshTunnel *t;
6817 MESH_TunnelNumber tid;
6819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6820 /* Sanity check for client registration */
6821 if (NULL == (c = client_get (client)))
6824 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6829 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6831 /* Sanity check for message size */
6832 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6835 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6839 /* Tunnel exists? */
6840 tid = ntohl (peer_msg->tunnel_id);
6841 t = tunnel_get_by_local_id (c, tid);
6845 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6849 /* Does client own tunnel? */
6850 if (t->owner->handle != client)
6853 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6857 GNUNET_i2s (&peer_msg->peer));
6858 peer_info = peer_info_get (&peer_msg->peer);
6860 tunnel_add_peer (t, peer_info);
6861 peer_info_connect (peer_info, t);
6863 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6869 * Handler for disconnection requests of peers in a tunnel
6871 * @param cls closure
6872 * @param client identification of the client
6873 * @param message the actual message (PeerControl)
6876 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6877 const struct GNUNET_MessageHeader *message)
6879 struct GNUNET_MESH_PeerControl *peer_msg;
6880 struct MeshPeerInfo *peer_info;
6881 struct MeshClient *c;
6882 struct MeshTunnel *t;
6883 MESH_TunnelNumber tid;
6885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6886 /* Sanity check for client registration */
6887 if (NULL == (c = client_get (client)))
6890 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6895 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6897 /* Sanity check for message size */
6898 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6901 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6905 /* Tunnel exists? */
6906 tid = ntohl (peer_msg->tunnel_id);
6907 t = tunnel_get_by_local_id (c, tid);
6911 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6916 /* Does client own tunnel? */
6917 if (t->owner->handle != client)
6920 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6925 GNUNET_i2s (&peer_msg->peer));
6926 /* Is the peer in the tunnel? */
6928 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6929 if (NULL == peer_info)
6932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6936 /* Ok, delete peer from tunnel */
6937 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6938 &peer_msg->peer.hashPubKey);
6940 send_destroy_path (t, peer_info->id);
6941 tunnel_delete_peer (t, peer_info->id);
6942 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6947 * Handler for blacklist requests of peers in a tunnel
6949 * @param cls closure
6950 * @param client identification of the client
6951 * @param message the actual message (PeerControl)
6953 * FIXME implement DHT block bloomfilter
6956 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6957 const struct GNUNET_MessageHeader *message)
6959 struct GNUNET_MESH_PeerControl *peer_msg;
6960 struct MeshClient *c;
6961 struct MeshTunnel *t;
6962 MESH_TunnelNumber tid;
6964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6965 /* Sanity check for client registration */
6966 if (NULL == (c = client_get (client)))
6969 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6974 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6976 /* Sanity check for message size */
6977 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6980 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6984 /* Tunnel exists? */
6985 tid = ntohl (peer_msg->tunnel_id);
6986 t = tunnel_get_by_local_id (c, tid);
6990 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6995 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6996 GNUNET_PEER_intern(&peer_msg->peer));
7001 * Handler for unblacklist requests of peers in a tunnel
7003 * @param cls closure
7004 * @param client identification of the client
7005 * @param message the actual message (PeerControl)
7008 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7009 const struct GNUNET_MessageHeader *message)
7011 struct GNUNET_MESH_PeerControl *peer_msg;
7012 struct MeshClient *c;
7013 struct MeshTunnel *t;
7014 MESH_TunnelNumber tid;
7018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7019 /* Sanity check for client registration */
7020 if (NULL == (c = client_get (client)))
7023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7028 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7030 /* Sanity check for message size */
7031 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7034 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7038 /* Tunnel exists? */
7039 tid = ntohl (peer_msg->tunnel_id);
7040 t = tunnel_get_by_local_id (c, tid);
7044 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7049 /* if peer is not known, complain */
7050 pid = GNUNET_PEER_search (&peer_msg->peer);
7057 /* search and remove from list */
7058 for (i = 0; i < t->nblacklisted; i++)
7060 if (t->blacklisted[i] == pid)
7062 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7063 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7068 /* if peer hasn't been blacklisted, complain */
7074 * Handler for connection requests to new peers by type
7076 * @param cls closure
7077 * @param client identification of the client
7078 * @param message the actual message (ConnectPeerByType)
7081 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7082 const struct GNUNET_MessageHeader *message)
7084 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7085 struct MeshClient *c;
7086 struct MeshTunnel *t;
7087 struct GNUNET_HashCode hash;
7088 MESH_TunnelNumber tid;
7090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7091 /* Sanity check for client registration */
7092 if (NULL == (c = client_get (client)))
7095 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7100 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7102 /* Sanity check for message size */
7103 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7104 ntohs (connect_msg->header.size))
7107 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7111 /* Tunnel exists? */
7112 tid = ntohl (connect_msg->tunnel_id);
7113 t = tunnel_get_by_local_id (c, tid);
7117 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7121 /* Does client own tunnel? */
7122 if (t->owner->handle != client)
7125 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7129 /* Do WE have the service? */
7130 t->type = ntohl (connect_msg->type);
7131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7132 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7133 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7136 /* Yes! Fast forward, add ourselves to the tunnel and send the
7137 * good news to the client, and alert the destination client of
7138 * an incoming tunnel.
7140 * FIXME send a path create to self, avoid code duplication
7142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7143 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7144 peer_info_get (&my_full_id),
7145 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7148 send_client_peer_connected (t, myid);
7149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7150 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7152 t->local_tid_dest = next_local_tid++;
7153 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7154 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7155 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7159 /* Ok, lets find a peer offering the service */
7160 if (NULL != t->dht_get_type)
7162 GNUNET_DHT_get_stop (t->dht_get_type);
7164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7165 GNUNET_h2s (&hash));
7167 GNUNET_DHT_get_start (dht_handle,
7168 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7170 dht_replication_level,
7171 GNUNET_DHT_RO_RECORD_ROUTE |
7172 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7174 &dht_get_type_handler, t);
7176 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7182 * Handler for connection requests to new peers by a string service description.
7184 * @param cls closure
7185 * @param client identification of the client
7186 * @param message the actual message, which includes messages the client wants
7189 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7190 const struct GNUNET_MessageHeader *message)
7192 struct GNUNET_MESH_ConnectPeerByString *msg;
7193 struct MeshRegexSearchInfo *info;
7194 struct MeshTunnel *t;
7195 struct MeshClient *c;
7196 MESH_TunnelNumber tid;
7201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7202 "Connect by string started\n");
7203 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7204 size = htons (message->size);
7206 /* Sanity check for client registration */
7207 if (NULL == (c = client_get (client)))
7210 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7215 /* Message size sanity check */
7216 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7219 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7223 /* Tunnel exists? */
7224 tid = ntohl (msg->tunnel_id);
7225 t = tunnel_get_by_local_id (c, tid);
7229 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7233 /* Does client own tunnel? */
7234 if (t->owner->handle != client)
7237 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7242 " on tunnel %s [%u]\n",
7243 GNUNET_i2s(&my_full_id),
7246 /* Only one connect_by_string allowed at the same time! */
7247 /* FIXME: allow more, return handle at api level to cancel, document */
7248 if (NULL != t->regex_search)
7251 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7255 /* Find string itself */
7256 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7257 string = (const char *) &msg[1];
7259 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7261 info->description = GNUNET_strndup (string, len);
7262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7264 t->regex_search = info;
7266 info->search_handle = GNUNET_REGEX_search (dht_handle,
7268 ®ex_found_handler, info,
7271 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7277 * Handler for client traffic directed to one peer
7279 * @param cls closure
7280 * @param client identification of the client
7281 * @param message the actual message
7284 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7285 const struct GNUNET_MessageHeader *message)
7287 struct MeshClient *c;
7288 struct MeshTunnel *t;
7289 struct MeshPeerInfo *pi;
7290 struct GNUNET_MESH_Unicast *data_msg;
7291 MESH_TunnelNumber tid;
7294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7295 "Got a unicast request from a client!\n");
7297 /* Sanity check for client registration */
7298 if (NULL == (c = client_get (client)))
7301 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7306 data_msg = (struct GNUNET_MESH_Unicast *) message;
7308 /* Sanity check for message size */
7309 size = ntohs (message->size);
7310 if (sizeof (struct GNUNET_MESH_Unicast) +
7311 sizeof (struct GNUNET_MessageHeader) > size)
7314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7318 /* Tunnel exists? */
7319 tid = ntohl (data_msg->tid);
7320 t = tunnel_get_by_local_id (c, tid);
7324 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7328 /* Is it a local tunnel? Then, does client own the tunnel? */
7329 if (t->owner->handle != client)
7332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7336 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7337 &data_msg->destination.hashPubKey);
7338 /* Is the selected peer in the tunnel? */
7342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7346 /* PID should be as expected */
7347 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7351 "Unicast PID, expected %u, got %u\n",
7352 t->fwd_pid + 1, ntohl (data_msg->pid));
7353 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7357 /* Ok, everything is correct, send the message
7358 * (pretend we got it from a mesh peer)
7361 /* Work around const limitation */
7362 char buf[ntohs (message->size)] GNUNET_ALIGN;
7363 struct GNUNET_MESH_Unicast *copy;
7365 copy = (struct GNUNET_MESH_Unicast *) buf;
7366 memcpy (buf, data_msg, size);
7367 copy->oid = my_full_id;
7368 copy->tid = htonl (t->id.tid);
7369 copy->ttl = htonl (default_ttl);
7370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7371 " calling generic handler...\n");
7372 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7375 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7382 * Handler for client traffic directed to the origin
7384 * @param cls closure
7385 * @param client identification of the client
7386 * @param message the actual message
7389 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7390 const struct GNUNET_MessageHeader *message)
7392 struct GNUNET_MESH_ToOrigin *data_msg;
7393 struct MeshTunnelClientInfo *clinfo;
7394 struct MeshClient *c;
7395 struct MeshTunnel *t;
7396 MESH_TunnelNumber tid;
7399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7400 "Got a ToOrigin request from a client!\n");
7401 /* Sanity check for client registration */
7402 if (NULL == (c = client_get (client)))
7405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7410 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7412 /* Sanity check for message size */
7413 size = ntohs (message->size);
7414 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7415 sizeof (struct GNUNET_MessageHeader) > size)
7418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7422 /* Tunnel exists? */
7423 tid = ntohl (data_msg->tid);
7424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7425 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7431 t = tunnel_get_by_local_id (c, tid);
7434 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7441 /* It should be sent by someone who has this as incoming tunnel. */
7442 if (GNUNET_NO == client_knows_tunnel (c, t))
7445 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7449 /* PID should be as expected */
7450 clinfo = tunnel_get_client_fc (t, c);
7451 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7454 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7455 "To Origin PID, expected %u, got %u\n",
7456 clinfo->bck_pid + 1,
7457 ntohl (data_msg->pid));
7458 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7463 /* Ok, everything is correct, send the message
7464 * (pretend we got it from a mesh peer)
7467 char buf[ntohs (message->size)] GNUNET_ALIGN;
7468 struct GNUNET_MESH_ToOrigin *copy;
7470 /* Work around const limitation */
7471 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7472 memcpy (buf, data_msg, size);
7473 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7474 copy->tid = htonl (t->id.tid);
7475 copy->ttl = htonl (default_ttl);
7476 copy->pid = htonl (t->bck_pid + 1);
7478 copy->sender = my_full_id;
7479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7480 " calling generic handler...\n");
7481 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7483 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7490 * Handler for client traffic directed to all peers in a tunnel
7492 * @param cls closure
7493 * @param client identification of the client
7494 * @param message the actual message
7497 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7498 const struct GNUNET_MessageHeader *message)
7500 struct MeshClient *c;
7501 struct MeshTunnel *t;
7502 struct GNUNET_MESH_Multicast *data_msg;
7503 MESH_TunnelNumber tid;
7505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7506 "Got a multicast request from a client!\n");
7508 /* Sanity check for client registration */
7509 if (NULL == (c = client_get (client)))
7512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7517 data_msg = (struct GNUNET_MESH_Multicast *) message;
7519 /* Sanity check for message size */
7520 if (sizeof (struct GNUNET_MESH_Multicast) +
7521 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7528 /* Tunnel exists? */
7529 tid = ntohl (data_msg->tid);
7530 t = tunnel_get_by_local_id (c, tid);
7534 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7535 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7541 /* Does client own tunnel? */
7542 if (t->owner->handle != client)
7545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7549 /* PID should be as expected */
7550 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7553 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7554 "Multicast PID, expected %u, got %u\n",
7555 t->fwd_pid + 1, ntohl (data_msg->pid));
7556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7561 char buf[ntohs (message->size)] GNUNET_ALIGN;
7562 struct GNUNET_MESH_Multicast *copy;
7564 copy = (struct GNUNET_MESH_Multicast *) buf;
7565 memcpy (buf, message, ntohs (message->size));
7566 copy->oid = my_full_id;
7567 copy->tid = htonl (t->id.tid);
7568 copy->ttl = htonl (default_ttl);
7569 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7571 " calling generic handler...\n");
7572 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7575 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7581 * Handler for client's ACKs for payload traffic.
7583 * @param cls Closure (unused).
7584 * @param client Identification of the client.
7585 * @param message The actual message.
7588 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7589 const struct GNUNET_MessageHeader *message)
7591 struct GNUNET_MESH_LocalAck *msg;
7592 struct MeshTunnel *t;
7593 struct MeshClient *c;
7594 MESH_TunnelNumber tid;
7597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7598 /* Sanity check for client registration */
7599 if (NULL == (c = client_get (client)))
7602 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7607 msg = (struct GNUNET_MESH_LocalAck *) message;
7609 /* Tunnel exists? */
7610 tid = ntohl (msg->tunnel_id);
7611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7612 t = tunnel_get_by_local_id (c, tid);
7616 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7618 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7622 ack = ntohl (msg->max_pid);
7623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7625 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
7626 if (NULL != t->owner && t->owner->handle == client)
7628 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7630 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7634 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7635 tunnel_set_client_fwd_ack (t, c, ack);
7636 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7639 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7646 * Iterator over all peers to send a monitoring client info about a tunnel.
7648 * @param cls Closure (message being built).
7649 * @param key Key (hashed tunnel ID, unused).
7650 * @param value Peer info.
7652 * @return GNUNET_YES, to keep iterating.
7655 monitor_peers_iterator (void *cls,
7656 const struct GNUNET_HashCode * key,
7659 struct GNUNET_MESH_LocalMonitor *msg = cls;
7660 struct GNUNET_PeerIdentity *id;
7661 struct MeshPeerInfo *info = value;
7663 id = (struct GNUNET_PeerIdentity *) &msg[1];
7664 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
7667 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7668 "* sending info about peer %s [%u]\n",
7669 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
7677 * Iterator over all tunnels to send a monitoring client info about each tunnel.
7679 * @param cls Closure (client handle).
7680 * @param key Key (hashed tunnel ID, unused).
7681 * @param value Tunnel info.
7683 * @return GNUNET_YES, to keep iterating.
7686 monitor_all_tunnels_iterator (void *cls,
7687 const struct GNUNET_HashCode * key,
7690 struct GNUNET_SERVER_Client *client = cls;
7691 struct MeshTunnel *t = value;
7692 struct GNUNET_MESH_LocalMonitor *msg;
7695 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
7696 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
7697 npeers * sizeof (struct GNUNET_PeerIdentity));
7698 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
7699 msg->tunnel_id = htonl (t->id.tid);
7700 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
7701 npeers * sizeof (struct GNUNET_PeerIdentity));
7702 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
7704 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
7705 monitor_peers_iterator,
7708 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7709 "* sending info about tunnel %s [%u] (%u peers)\n",
7710 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
7712 if (msg->npeers != npeers)
7715 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7716 "Get tunnels fail: size %u - iter %u\n",
7717 npeers, msg->npeers);
7720 msg->npeers = htonl (npeers);
7721 GNUNET_SERVER_notification_context_unicast (nc, client,
7722 &msg->header, GNUNET_NO);
7728 * Handler for client's MONITOR request.
7730 * @param cls Closure (unused).
7731 * @param client Identification of the client.
7732 * @param message The actual message.
7735 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
7736 const struct GNUNET_MessageHeader *message)
7738 struct MeshClient *c;
7740 /* Sanity check for client registration */
7741 if (NULL == (c = client_get (client)))
7744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7748 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7749 "Received get tunnels request from client %u\n",
7751 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
7752 monitor_all_tunnels_iterator,
7754 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7755 "Get tunnels request from client %u completed\n",
7757 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7762 * Data needed to build a Monitor_Tunnel message.
7764 struct MeshMonitorTunnelContext
7767 * Partial message, including peer count.
7769 struct GNUNET_MESH_LocalMonitor *msg;
7772 * Hashmap with positions: peer->position.
7774 struct GNUNET_CONTAINER_MultiHashMap *lookup;
7777 * Index of the parent of each peer in the message, realtive to the absolute
7778 * order in the array (can be in a previous message).
7780 uint32_t parents[1024];
7783 * Peers visited so far in the tree, aka position of the current peer.
7785 unsigned int npeers;
7788 * Client requesting the info.
7790 struct MeshClient *c;
7795 * Send a client a message about the structure of a tunnel.
7797 * @param ctx Context of the tunnel iteration, with info regarding the state
7798 * of the execution and the number of peers visited for this message.
7801 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
7803 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
7804 struct GNUNET_PeerIdentity *pid;
7805 unsigned int *parent;
7808 size = sizeof (struct GNUNET_MESH_LocalMonitor);
7809 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
7810 resp->header.size = htons (size);
7811 pid = (struct GNUNET_PeerIdentity *) &resp[1];
7812 parent = (unsigned int *) &pid[resp->npeers];
7813 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
7814 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
7815 &resp->header, GNUNET_NO);
7819 * Iterator over a tunnel tree to build a message containing all peers
7820 * the in the tunnel, including relay nodes.
7822 * @param cls Closure (pointer to pointer of message being built).
7823 * @param peer Short ID of a peer.
7824 * @param parent Short ID of the @c peer 's parent.
7827 tunnel_tree_iterator (void *cls,
7828 GNUNET_PEER_Id peer,
7829 GNUNET_PEER_Id parent)
7831 struct MeshMonitorTunnelContext *ctx = cls;
7832 struct GNUNET_MESH_LocalMonitor *msg;
7833 struct GNUNET_PeerIdentity *pid;
7834 struct GNUNET_PeerIdentity ppid;
7837 pid = (struct GNUNET_PeerIdentity *) &msg[1];
7838 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
7839 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
7840 &pid[msg->npeers].hashPubKey,
7841 (void *) (long) ctx->npeers,
7842 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7843 GNUNET_PEER_resolve (parent, &ppid);
7844 ctx->parents[msg->npeers] =
7845 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
7851 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
7853 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
7856 send_client_tunnel_info (ctx);
7863 * Handler for client's MONITOR_TUNNEL request.
7865 * @param cls Closure (unused).
7866 * @param client Identification of the client.
7867 * @param message The actual message.
7870 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
7871 const struct GNUNET_MessageHeader *message)
7873 const struct GNUNET_MESH_LocalMonitor *msg;
7874 struct GNUNET_MESH_LocalMonitor *resp;
7875 struct MeshMonitorTunnelContext ctx;
7876 struct MeshClient *c;
7877 struct MeshTunnel *t;
7879 /* Sanity check for client registration */
7880 if (NULL == (c = client_get (client)))
7883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7887 msg = (struct GNUNET_MESH_LocalMonitor *) message;
7888 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7889 "Received tunnel info request from client %u for tunnel %s[%X]\n",
7892 ntohl (msg->tunnel_id));
7893 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
7896 /* We don't know the tunnel */
7897 struct GNUNET_MESH_LocalMonitor warn;
7900 warn.npeers = htonl (UINT_MAX);
7901 GNUNET_SERVER_notification_context_unicast (nc, client,
7904 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7908 /* Initialize context */
7909 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
7913 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7916 /* Collect and send information */
7917 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
7918 send_client_tunnel_info (&ctx);
7921 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
7924 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7925 "Monitor tunnel request from client %u completed\n",
7927 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7932 * Functions to handle messages from clients
7934 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7935 {&handle_local_new_client, NULL,
7936 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7937 {&handle_local_announce_regex, NULL,
7938 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7939 {&handle_local_tunnel_create, NULL,
7940 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7941 sizeof (struct GNUNET_MESH_TunnelMessage)},
7942 {&handle_local_tunnel_destroy, NULL,
7943 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7944 sizeof (struct GNUNET_MESH_TunnelMessage)},
7945 {&handle_local_tunnel_speed, NULL,
7946 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7947 sizeof (struct GNUNET_MESH_TunnelMessage)},
7948 {&handle_local_tunnel_speed, NULL,
7949 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7950 sizeof (struct GNUNET_MESH_TunnelMessage)},
7951 {&handle_local_tunnel_buffer, NULL,
7952 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7953 sizeof (struct GNUNET_MESH_TunnelMessage)},
7954 {&handle_local_tunnel_buffer, NULL,
7955 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7956 sizeof (struct GNUNET_MESH_TunnelMessage)},
7957 {&handle_local_connect_add, NULL,
7958 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7959 sizeof (struct GNUNET_MESH_PeerControl)},
7960 {&handle_local_connect_del, NULL,
7961 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7962 sizeof (struct GNUNET_MESH_PeerControl)},
7963 {&handle_local_blacklist, NULL,
7964 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7965 sizeof (struct GNUNET_MESH_PeerControl)},
7966 {&handle_local_unblacklist, NULL,
7967 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7968 sizeof (struct GNUNET_MESH_PeerControl)},
7969 {&handle_local_connect_by_type, NULL,
7970 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7971 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7972 {&handle_local_connect_by_string, NULL,
7973 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7974 {&handle_local_unicast, NULL,
7975 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7976 {&handle_local_to_origin, NULL,
7977 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7978 {&handle_local_multicast, NULL,
7979 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7980 {&handle_local_ack, NULL,
7981 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7982 sizeof (struct GNUNET_MESH_LocalAck)},
7983 {&handle_local_get_tunnels, NULL,
7984 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
7985 sizeof (struct GNUNET_MessageHeader)},
7986 {&handle_local_show_tunnel, NULL,
7987 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
7988 sizeof (struct GNUNET_MESH_LocalMonitor)},
7994 * To be called on core init/fail.
7996 * @param cls service closure
7997 * @param server handle to the server for this service
7998 * @param identity the public identity of this peer
8001 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8002 const struct GNUNET_PeerIdentity *identity)
8005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8006 core_handle = server;
8007 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8010 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8011 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8013 GNUNET_i2s (identity));
8014 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8016 GNUNET_i2s (&my_full_id));
8017 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8019 GNUNET_abort(); // Try harder
8026 * Method called whenever a given peer connects.
8028 * @param cls closure
8029 * @param peer peer identity this notification is about
8030 * @param atsi performance data for the connection
8031 * @param atsi_count number of records in 'atsi'
8034 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8035 const struct GNUNET_ATS_Information *atsi,
8036 unsigned int atsi_count)
8038 struct MeshPeerInfo *peer_info;
8039 struct MeshPeerPath *path;
8041 DEBUG_CONN ("Peer connected\n");
8042 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8043 peer_info = peer_info_get (peer);
8044 if (myid == peer_info->id)
8046 DEBUG_CONN (" (self)\n");
8051 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8053 path = path_new (2);
8054 path->peers[0] = myid;
8055 path->peers[1] = peer_info->id;
8056 GNUNET_PEER_change_rc (myid, 1);
8057 GNUNET_PEER_change_rc (peer_info->id, 1);
8058 peer_info_add_path (peer_info, path, GNUNET_YES);
8059 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8065 * Method called whenever a peer disconnects.
8067 * @param cls closure
8068 * @param peer peer identity this notification is about
8071 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8073 struct MeshPeerInfo *pi;
8074 struct MeshPeerQueue *q;
8075 struct MeshPeerQueue *n;
8077 DEBUG_CONN ("Peer disconnected\n");
8078 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8088 /* TODO try to reroute this traffic instead */
8089 queue_destroy(q, GNUNET_YES);
8092 if (NULL != pi->core_transmit)
8094 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8095 pi->core_transmit = NULL;
8097 peer_info_remove_path (pi, pi->id, myid);
8100 DEBUG_CONN (" (self)\n");
8102 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8107 /******************************************************************************/
8108 /************************ MAIN FUNCTIONS ****************************/
8109 /******************************************************************************/
8112 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8114 * @param cls closure
8115 * @param key current key code
8116 * @param value value in the hash map
8117 * @return GNUNET_YES if we should continue to iterate,
8121 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8123 struct MeshTunnel *t = value;
8130 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8132 * @param cls closure
8133 * @param key current key code
8134 * @param value value in the hash map
8135 * @return GNUNET_YES if we should continue to iterate,
8139 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8141 struct MeshPeerInfo *p = value;
8142 struct MeshPeerQueue *q;
8143 struct MeshPeerQueue *n;
8151 queue_destroy(q, GNUNET_YES);
8155 peer_info_destroy (p);
8161 * Task run during shutdown.
8167 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8171 if (core_handle != NULL)
8173 GNUNET_CORE_disconnect (core_handle);
8178 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
8181 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8182 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8183 if (dht_handle != NULL)
8185 GNUNET_DHT_disconnect (dht_handle);
8190 GNUNET_SERVER_notification_context_destroy (nc);
8193 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8195 GNUNET_SCHEDULER_cancel (announce_id_task);
8196 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8198 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8200 GNUNET_SCHEDULER_cancel (announce_applications_task);
8201 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8208 * Callback for hostkey read/generation.
8210 * @param cls Closure (Configuration handle).
8211 * @param pk The ECC private key.
8212 * @param emsg Error message, if any.
8215 key_generation_cb (void *cls,
8216 struct GNUNET_CRYPTO_EccPrivateKey *pk,
8219 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8220 struct MeshPeerInfo *peer;
8221 struct MeshPeerPath *p;
8226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8227 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8229 GNUNET_SCHEDULER_shutdown ();
8232 my_private_key = pk;
8233 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
8234 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8235 &my_full_id.hashPubKey);
8236 myid = GNUNET_PEER_intern (&my_full_id);
8237 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8238 "Mesh for peer [%s] starting\n",
8239 GNUNET_i2s(&my_full_id));
8241 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8242 NULL, /* Closure passed to MESH functions */
8243 &core_init, /* Call core_init once connected */
8244 &core_connect, /* Handle connects */
8245 &core_disconnect, /* remove peers on disconnects */
8246 NULL, /* Don't notify about all incoming messages */
8247 GNUNET_NO, /* For header only in notification */
8248 NULL, /* Don't notify about all outbound messages */
8249 GNUNET_NO, /* For header-only out notification */
8250 core_handlers); /* Register these handlers */
8252 if (core_handle == NULL)
8255 GNUNET_SCHEDULER_shutdown ();
8260 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8263 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8264 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8265 GNUNET_SERVER_disconnect_notify (server_handle,
8266 &handle_local_client_disconnect, NULL);
8270 clients_tail = NULL;
8273 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8274 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8276 /* Create a peer_info for the local peer */
8277 peer = peer_info_get (&my_full_id);
8280 GNUNET_PEER_change_rc (myid, 1);
8281 peer_info_add_path (peer, p, GNUNET_YES);
8282 GNUNET_SERVER_resume (server_handle);
8283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8288 * Process mesh requests.
8290 * @param cls closure
8291 * @param server the initialized server
8292 * @param c configuration to use
8295 run (void *cls, struct GNUNET_SERVER_Handle *server,
8296 const struct GNUNET_CONFIGURATION_Handle *c)
8300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8301 server_handle = server;
8304 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
8307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8309 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8310 "mesh", "peer/privatekey");
8311 GNUNET_SCHEDULER_shutdown ();
8316 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8317 &refresh_path_time))
8319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8321 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8322 "mesh", "refresh path time");
8323 GNUNET_SCHEDULER_shutdown ();
8328 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8329 &app_announce_time))
8331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8333 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8334 "mesh", "app announce time");
8335 GNUNET_SCHEDULER_shutdown ();
8338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8339 "APP_ANNOUNCE_TIME %llu ms\n",
8340 app_announce_time.rel_value);
8342 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8347 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8348 "mesh", "id announce time");
8349 GNUNET_SCHEDULER_shutdown ();
8354 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8359 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8360 "mesh", "connect timeout");
8361 GNUNET_SCHEDULER_shutdown ();
8366 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8371 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8372 "mesh", "max msgs queue");
8373 GNUNET_SCHEDULER_shutdown ();
8378 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8383 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8384 "mesh", "max tunnels");
8385 GNUNET_SCHEDULER_shutdown ();
8390 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8395 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8396 "mesh", "default ttl", 64);
8401 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8405 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8406 "mesh", "max peers", 1000);
8411 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8412 &dht_replication_level))
8414 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8416 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8417 "mesh", "dht replication level", 3);
8418 dht_replication_level = 3;
8421 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8422 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8423 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8424 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8425 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8427 dht_handle = GNUNET_DHT_connect (c, 64);
8428 if (NULL == dht_handle)
8432 stats = GNUNET_STATISTICS_create ("mesh", c);
8434 GNUNET_SERVER_suspend (server_handle);
8435 /* Scheduled the task to clean up when shutdown is called */
8436 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8438 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
8441 GNUNET_free (keyfile);
8446 * The main function for the mesh service.
8448 * @param argc number of arguments from the command line
8449 * @param argv command line arguments
8450 * @return 0 ok, 1 on error
8453 main (int argc, char *const *argv)
8458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8459 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8461 ret = (GNUNET_OK == r) ? 0 : 1;
8462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8466 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8467 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8468 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);