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
5070 * @return GNUNET_OK to keep the connection open,
5071 * GNUNET_SYSERR to close it (signal serious error)
5074 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5075 const struct GNUNET_MessageHeader *message)
5077 unsigned int own_pos;
5080 MESH_TunnelNumber tid;
5081 struct GNUNET_MESH_ManipulatePath *msg;
5082 struct GNUNET_PeerIdentity *pi;
5083 struct GNUNET_HashCode hash;
5084 struct MeshPeerPath *path;
5085 struct MeshPeerInfo *dest_peer_info;
5086 struct MeshPeerInfo *orig_peer_info;
5087 struct MeshTunnel *t;
5089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5090 "Received a path create msg [%s]\n",
5091 GNUNET_i2s (&my_full_id));
5092 size = ntohs (message->size);
5093 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5095 GNUNET_break_op (0);
5099 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5100 if (size % sizeof (struct GNUNET_PeerIdentity))
5102 GNUNET_break_op (0);
5105 size /= sizeof (struct GNUNET_PeerIdentity);
5108 GNUNET_break_op (0);
5111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5112 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5114 tid = ntohl (msg->tid);
5115 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5117 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5118 t = tunnel_get (pi, tid);
5119 if (NULL == t) // FIXME only for INCOMING tunnels?
5123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5124 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5127 // FIXME notify failure
5130 opt = ntohl (msg->opt);
5131 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5132 GNUNET_YES : GNUNET_NO;
5133 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5135 t->nobuffer = GNUNET_YES;
5136 t->last_fwd_ack = t->fwd_pid + 1;
5138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5139 " speed_min: %d, nobuffer:%d\n",
5140 t->speed_min, t->nobuffer);
5142 if (GNUNET_YES == t->nobuffer)
5144 t->bck_queue_max = 1;
5145 t->fwd_queue_max = 1;
5148 // FIXME only assign a local tid if a local client is interested (on demand)
5149 while (NULL != tunnel_get_incoming (next_local_tid))
5150 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5151 t->local_tid_dest = next_local_tid++;
5152 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5155 tunnel_reset_timeout (t);
5156 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5158 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5159 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5167 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5168 if (NULL == dest_peer_info)
5170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5171 " Creating PeerInfo for destination.\n");
5172 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5173 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5174 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5176 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5178 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5179 if (NULL == orig_peer_info)
5181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5182 " Creating PeerInfo for origin.\n");
5183 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5184 orig_peer_info->id = GNUNET_PEER_intern (pi);
5185 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5186 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5189 path = path_new (size);
5191 for (i = 0; i < size; i++)
5193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5194 GNUNET_i2s (&pi[i]));
5195 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5196 if (path->peers[i] == myid)
5199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5202 /* cannot be self, must be 'not found' */
5203 /* create path: self not found in path through self */
5204 GNUNET_break_op (0);
5205 path_destroy (path);
5209 path_add_to_peers (path, GNUNET_NO);
5210 tunnel_add_path (t, path, own_pos);
5211 if (own_pos == size - 1)
5213 /* It is for us! Send ack. */
5214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5215 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5216 if (NULL == t->peers)
5218 /* New tunnel! Notify clients on first payload message. */
5219 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5221 GNUNET_break (GNUNET_SYSERR !=
5222 GNUNET_CONTAINER_multihashmap_put (t->peers,
5223 &my_full_id.hashPubKey,
5226 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5231 struct MeshPeerPath *path2;
5233 /* It's for somebody else! Retransmit. */
5234 path2 = path_duplicate (path);
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5236 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5237 path2 = path_duplicate (path);
5238 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5239 send_create_path (dest_peer_info, path, t);
5246 * Core handler for path destruction
5248 * @param cls closure
5249 * @param message message
5250 * @param peer peer identity this notification is about
5252 * @return GNUNET_OK to keep the connection open,
5253 * GNUNET_SYSERR to close it (signal serious error)
5256 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5257 const struct GNUNET_MessageHeader *message)
5259 struct GNUNET_MESH_ManipulatePath *msg;
5260 struct GNUNET_PeerIdentity *pi;
5261 struct MeshPeerPath *path;
5262 struct MeshTunnel *t;
5263 unsigned int own_pos;
5267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5268 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5269 size = ntohs (message->size);
5270 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5272 GNUNET_break_op (0);
5276 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5277 if (size % sizeof (struct GNUNET_PeerIdentity))
5279 GNUNET_break_op (0);
5282 size /= sizeof (struct GNUNET_PeerIdentity);
5285 GNUNET_break_op (0);
5288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5290 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5291 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5293 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5295 t = tunnel_get (pi, ntohl (msg->tid));
5298 /* TODO notify back: we don't know this tunnel */
5299 GNUNET_break_op (0);
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5303 path = path_new (size);
5305 for (i = 0; i < size; i++)
5307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5308 GNUNET_i2s (&pi[i]));
5309 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5310 if (path->peers[i] == myid)
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5314 if (own_pos < path->length - 1)
5315 send_prebuilt_message (message, &pi[own_pos + 1], t);
5317 send_client_tunnel_disconnect(t, NULL);
5319 tunnel_delete_peer (t, path->peers[path->length - 1]);
5320 path_destroy (path);
5326 * Core handler for notifications of broken paths
5328 * @param cls closure
5329 * @param message message
5330 * @param peer peer identity this notification is about
5332 * @return GNUNET_OK to keep the connection open,
5333 * GNUNET_SYSERR to close it (signal serious error)
5336 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5337 const struct GNUNET_MessageHeader *message)
5339 struct GNUNET_MESH_PathBroken *msg;
5340 struct MeshTunnel *t;
5342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5343 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5344 msg = (struct GNUNET_MESH_PathBroken *) message;
5345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5346 GNUNET_i2s (&msg->peer1));
5347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5348 GNUNET_i2s (&msg->peer2));
5349 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5352 GNUNET_break_op (0);
5355 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5356 GNUNET_PEER_search (&msg->peer2));
5363 * Core handler for tunnel destruction
5365 * @param cls closure
5366 * @param message message
5367 * @param peer peer identity this notification is about
5369 * @return GNUNET_OK to keep the connection open,
5370 * GNUNET_SYSERR to close it (signal serious error)
5373 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5374 const struct GNUNET_MessageHeader *message)
5376 struct GNUNET_MESH_TunnelDestroy *msg;
5377 struct MeshTunnel *t;
5378 GNUNET_PEER_Id parent;
5381 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5383 "Got a TUNNEL DESTROY packet from %s\n",
5385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5386 " for tunnel %s [%u]\n",
5387 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5388 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5389 /* Check signature */
5392 /* Probably already got the message from another path,
5393 * destroyed the tunnel and retransmitted to children.
5396 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5400 parent = tree_get_predecessor (t->tree);
5401 pid = GNUNET_PEER_search (peer);
5406 tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
5407 nc = tree_count_children (t->tree);
5408 if (nc > 0 || NULL != t->owner || t->nclients > 0)
5410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5411 "still in use: %u cl, %u ch\n",
5416 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5418 /* Tunnel was incoming, notify clients */
5419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5420 t->local_tid, t->local_tid_dest);
5421 send_clients_tunnel_destroy (t);
5423 tunnel_send_destroy (t, parent);
5424 t->destroy = GNUNET_YES;
5425 // TODO: add timeout to destroy the tunnel anyway
5431 * Core handler for mesh network traffic going from the origin to a peer
5433 * @param cls closure
5434 * @param peer peer identity this notification is about
5435 * @param message message
5436 * @return GNUNET_OK to keep the connection open,
5437 * GNUNET_SYSERR to close it (signal serious error)
5440 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5441 const struct GNUNET_MessageHeader *message)
5443 struct GNUNET_MESH_Unicast *msg;
5444 struct GNUNET_PeerIdentity *neighbor;
5445 struct MeshTunnelChildInfo *cinfo;
5446 struct MeshTunnel *t;
5447 GNUNET_PEER_Id dest_id;
5452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5455 size = ntohs (message->size);
5457 sizeof (struct GNUNET_MESH_Unicast) +
5458 sizeof (struct GNUNET_MessageHeader))
5463 msg = (struct GNUNET_MESH_Unicast *) message;
5464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5465 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5467 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5470 /* TODO notify back: we don't know this tunnel */
5471 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5472 GNUNET_break_op (0);
5475 pid = ntohl (msg->pid);
5476 if (t->fwd_pid == pid)
5478 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5480 " Already seen pid %u, DROPPING!\n", pid);
5485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5486 " pid %u not seen yet, forwarding\n", pid);
5489 t->skip += (pid - t->fwd_pid) - 1;
5492 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5494 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5495 GNUNET_break_op (0);
5496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5497 "Received PID %u, ACK %u\n",
5498 pid, t->last_fwd_ack);
5502 tunnel_reset_timeout (t);
5503 dest_id = GNUNET_PEER_search (&msg->destination);
5504 if (dest_id == myid)
5506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5507 " it's for us! sending to clients...\n");
5508 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5509 send_subscribed_clients (message, &msg[1].header, t);
5510 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5513 ttl = ntohl (msg->ttl);
5514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5517 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5519 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5523 " not for us, retransmitting...\n");
5525 neighbor = tree_get_first_hop (t->tree, dest_id);
5526 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5527 cinfo->fwd_pid = pid;
5528 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5531 if (GNUNET_YES == t->nobuffer &&
5532 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5534 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5535 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5536 GNUNET_break_op (0);
5539 send_prebuilt_message (message, neighbor, t);
5540 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5546 * Core handler for mesh network traffic going from the origin to all peers
5548 * @param cls closure
5549 * @param message message
5550 * @param peer peer identity this notification is about
5551 * @return GNUNET_OK to keep the connection open,
5552 * GNUNET_SYSERR to close it (signal serious error)
5554 * TODO: Check who we got this from, to validate route.
5557 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5558 const struct GNUNET_MessageHeader *message)
5560 struct GNUNET_MESH_Multicast *msg;
5561 struct MeshTunnel *t;
5565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5567 size = ntohs (message->size);
5568 if (sizeof (struct GNUNET_MESH_Multicast) +
5569 sizeof (struct GNUNET_MessageHeader) > size)
5571 GNUNET_break_op (0);
5574 msg = (struct GNUNET_MESH_Multicast *) message;
5575 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5579 /* TODO notify that we dont know that tunnel */
5580 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5581 GNUNET_break_op (0);
5584 pid = ntohl (msg->pid);
5585 if (t->fwd_pid == pid)
5587 /* already seen this packet, drop */
5588 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5590 " Already seen pid %u, DROPPING!\n", pid);
5591 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5597 " pid %u not seen yet, forwarding\n", pid);
5599 t->skip += (pid - t->fwd_pid) - 1;
5601 tunnel_reset_timeout (t);
5603 /* Transmit to locally interested clients */
5604 if (NULL != t->peers &&
5605 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5607 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5608 send_subscribed_clients (message, &msg[1].header, t);
5609 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5612 if (ntohl (msg->ttl) == 0)
5614 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5615 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5616 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5619 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5620 tunnel_send_multicast (t, message);
5626 * Core handler for mesh network traffic toward the owner of a tunnel
5628 * @param cls closure
5629 * @param message message
5630 * @param peer peer identity this notification is about
5632 * @return GNUNET_OK to keep the connection open,
5633 * GNUNET_SYSERR to close it (signal serious error)
5636 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5637 const struct GNUNET_MessageHeader *message)
5639 struct GNUNET_MESH_ToOrigin *msg;
5640 struct GNUNET_PeerIdentity id;
5641 struct MeshPeerInfo *peer_info;
5642 struct MeshTunnel *t;
5643 struct MeshTunnelChildInfo *cinfo;
5644 GNUNET_PEER_Id predecessor;
5648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5650 size = ntohs (message->size);
5651 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5652 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5654 GNUNET_break_op (0);
5657 msg = (struct GNUNET_MESH_ToOrigin *) message;
5658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5659 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5660 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5661 pid = ntohl (msg->pid);
5665 /* TODO notify that we dont know this tunnel (whom)? */
5666 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5668 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
5669 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5673 cinfo = tunnel_get_neighbor_fc(t, peer);
5680 if (cinfo->bck_pid == pid)
5682 /* already seen this packet, drop */
5683 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5685 " Already seen pid %u, DROPPING!\n", pid);
5686 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5691 " pid %u not seen yet, forwarding\n", pid);
5692 cinfo->bck_pid = pid;
5694 if (NULL != t->owner)
5697 struct GNUNET_MESH_ToOrigin *copy;
5699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5700 " it's for us! sending to clients...\n");
5701 /* TODO signature verification */
5702 memcpy (cbuf, message, size);
5703 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5704 copy->tid = htonl (t->local_tid);
5706 copy->pid = htonl (t->bck_pid);
5707 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5708 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5709 ©->header, GNUNET_NO);
5710 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5714 " not for us, retransmitting...\n");
5716 peer_info = peer_info_get (&msg->oid);
5717 if (NULL == peer_info)
5719 /* unknown origin of tunnel */
5723 predecessor = tree_get_predecessor (t->tree);
5724 if (0 == predecessor)
5726 if (GNUNET_YES == t->destroy)
5728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5729 "to orig received on a dying tunnel %s [%X]\n",
5730 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5734 "unknown to origin at %s\n",
5735 GNUNET_i2s (&my_full_id));
5736 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5739 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5740 "for tunnel %s [%X]\n",
5741 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5742 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5744 tree_debug (t->tree);
5747 GNUNET_PEER_resolve (predecessor, &id);
5748 send_prebuilt_message (message, &id, t);
5749 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5756 * Core handler for mesh network traffic point-to-point acks.
5758 * @param cls closure
5759 * @param message message
5760 * @param peer peer identity this notification is about
5762 * @return GNUNET_OK to keep the connection open,
5763 * GNUNET_SYSERR to close it (signal serious error)
5766 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5767 const struct GNUNET_MessageHeader *message)
5769 struct GNUNET_MESH_ACK *msg;
5770 struct MeshTunnel *t;
5773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5775 msg = (struct GNUNET_MESH_ACK *) message;
5777 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5781 /* TODO notify that we dont know this tunnel (whom)? */
5782 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5785 ack = ntohl (msg->pid);
5786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5788 /* Is this a forward or backward ACK? */
5789 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5791 struct MeshTunnelChildInfo *cinfo;
5794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5795 cinfo = tunnel_get_neighbor_fc (t, peer);
5796 cinfo->fwd_ack = ack;
5797 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5798 tunnel_unlock_fwd_queues (t);
5799 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
5801 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
5802 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
5803 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
5808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5810 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5811 tunnel_unlock_bck_queue (t);
5818 * Core handler for mesh network traffic point-to-point ack polls.
5820 * @param cls closure
5821 * @param message message
5822 * @param peer peer identity this notification is about
5824 * @return GNUNET_OK to keep the connection open,
5825 * GNUNET_SYSERR to close it (signal serious error)
5828 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5829 const struct GNUNET_MessageHeader *message)
5831 struct GNUNET_MESH_Poll *msg;
5832 struct MeshTunnel *t;
5834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5837 msg = (struct GNUNET_MESH_Poll *) message;
5839 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5843 /* TODO notify that we dont know this tunnel (whom)? */
5844 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5845 GNUNET_break_op (0);
5849 /* Is this a forward or backward ACK? */
5850 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5852 struct MeshTunnelChildInfo *cinfo;
5854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5855 cinfo = tunnel_get_neighbor_fc (t, peer);
5856 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5857 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5862 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5869 * Core handler for path ACKs
5871 * @param cls closure
5872 * @param message message
5873 * @param peer peer identity this notification is about
5875 * @return GNUNET_OK to keep the connection open,
5876 * GNUNET_SYSERR to close it (signal serious error)
5879 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5880 const struct GNUNET_MessageHeader *message)
5882 struct GNUNET_MESH_PathACK *msg;
5883 struct GNUNET_PeerIdentity id;
5884 struct MeshPeerInfo *peer_info;
5885 struct MeshPeerPath *p;
5886 struct MeshTunnel *t;
5888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5889 GNUNET_i2s (&my_full_id));
5890 msg = (struct GNUNET_MESH_PathACK *) message;
5891 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5894 /* TODO notify that we don't know the tunnel */
5895 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5897 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5901 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5903 peer_info = peer_info_get (&msg->peer_id);
5904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5905 GNUNET_i2s (&msg->peer_id));
5906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5909 if (NULL != t->regex_search && t->regex_search->peer == peer_info->id)
5911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5912 "connect_by_string completed, stopping search\n");
5913 regex_cancel_search (t->regex_search);
5914 t->regex_search = NULL;
5917 /* Add paths to peers? */
5918 p = tree_get_path_to_peer (t->tree, peer_info->id);
5921 path_add_to_peers (p, GNUNET_YES);
5929 /* Message for us? */
5930 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5933 if (NULL == t->owner)
5935 GNUNET_break_op (0);
5938 if (NULL != t->dht_get_type)
5940 GNUNET_DHT_get_stop (t->dht_get_type);
5941 t->dht_get_type = NULL;
5943 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5945 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5946 send_client_peer_connected (t, peer_info->id);
5948 if (NULL != peer_info->dhtget)
5950 GNUNET_DHT_get_stop (peer_info->dhtget);
5951 peer_info->dhtget = NULL;
5956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5957 " not for us, retransmitting...\n");
5958 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5959 peer_info = peer_info_get (&msg->oid);
5960 send_prebuilt_message (message, &id, t);
5966 * Core handler for mesh keepalives.
5968 * @param cls closure
5969 * @param message message
5970 * @param peer peer identity this notification is about
5971 * @return GNUNET_OK to keep the connection open,
5972 * GNUNET_SYSERR to close it (signal serious error)
5974 * TODO: Check who we got this from, to validate route.
5977 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5978 const struct GNUNET_MessageHeader *message)
5980 struct GNUNET_MESH_TunnelKeepAlive *msg;
5981 struct MeshTunnel *t;
5983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5986 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5987 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5991 /* TODO notify that we dont know that tunnel */
5992 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
5997 tunnel_reset_timeout (t);
5999 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6000 tunnel_send_multicast (t, message);
6007 * Functions to handle messages from core
6009 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6010 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6011 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6012 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6013 sizeof (struct GNUNET_MESH_PathBroken)},
6014 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6015 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6016 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6017 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6018 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6019 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6020 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6021 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6022 sizeof (struct GNUNET_MESH_ACK)},
6023 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6024 sizeof (struct GNUNET_MESH_Poll)},
6025 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6026 sizeof (struct GNUNET_MESH_PathACK)},
6032 /******************************************************************************/
6033 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6034 /******************************************************************************/
6037 * deregister_app: iterator for removing each application registered by a client
6039 * @param cls closure
6040 * @param key the hash of the application id (used to access the hashmap)
6041 * @param value the value stored at the key (client)
6043 * @return GNUNET_OK on success
6046 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6048 struct MeshClient *c = cls;
6050 GNUNET_break (GNUNET_YES ==
6051 GNUNET_CONTAINER_multihashmap_remove (applications, key, c));
6057 * notify_client_connection_failure: notify a client that the connection to the
6058 * requested remote peer is not possible (for instance, no route found)
6059 * Function called when the socket is ready to queue more data. "buf" will be
6060 * NULL and "size" zero if the socket was closed for writing in the meantime.
6062 * @param cls closure
6063 * @param size number of bytes available in buf
6064 * @param buf where the callee should write the message
6065 * @return number of bytes written to buf
6068 notify_client_connection_failure (void *cls, size_t size, void *buf)
6071 struct MeshPeerInfo *peer_info;
6072 struct GNUNET_MESH_PeerControl *msg;
6073 struct GNUNET_PeerIdentity id;
6075 if (0 == size && NULL == buf)
6077 // TODO retry? cancel?
6081 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6082 peer_info = (struct MeshPeerInfo *) cls;
6083 msg = (struct GNUNET_MESH_PeerControl *) buf;
6084 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6085 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6086 // msg->tunnel_id = htonl(peer_info->t->tid);
6087 GNUNET_PEER_resolve (peer_info->id, &id);
6088 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6096 * Send keepalive packets for a peer
6098 * @param cls Closure (tunnel for which to send the keepalive).
6099 * @param tc Notification context.
6102 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6104 struct MeshTunnel *t = cls;
6105 struct GNUNET_MESH_TunnelKeepAlive *msg;
6106 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6109 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6110 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6116 "sending keepalive for tunnel %d\n", t->id.tid);
6118 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6119 msg->header.size = htons (size);
6120 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6121 msg->oid = my_full_id;
6122 msg->tid = htonl (t->id.tid);
6123 tunnel_send_multicast (t, &msg->header);
6125 t->path_refresh_task =
6126 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6127 tunnel_reset_timeout(t);
6132 * Function to process paths received for a new peer addition. The recorded
6133 * paths form the initial tunnel, which can be optimized later.
6134 * Called on each result obtained for the DHT search.
6136 * @param cls closure
6137 * @param exp when will this value expire
6138 * @param key key of the result
6139 * @param get_path path of the get request
6140 * @param get_path_length lenght of get_path
6141 * @param put_path path of the put request
6142 * @param put_path_length length of the put_path
6143 * @param type type of the result
6144 * @param size number of bytes in data
6145 * @param data pointer to the result data
6147 * TODO: re-issue the request after certain time? cancel after X results?
6150 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6151 const struct GNUNET_HashCode * key,
6152 const struct GNUNET_PeerIdentity *get_path,
6153 unsigned int get_path_length,
6154 const struct GNUNET_PeerIdentity *put_path,
6155 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6156 size_t size, const void *data)
6158 struct MeshPathInfo *path_info = cls;
6159 struct MeshPeerPath *p;
6160 struct GNUNET_PeerIdentity pi;
6163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6164 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6167 p = path_build_from_dht (get_path, get_path_length,
6168 put_path, put_path_length);
6169 path_add_to_peers (p, GNUNET_NO);
6171 for (i = 0; i < path_info->peer->ntunnels; i++)
6173 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6174 peer_info_connect (path_info->peer, path_info->t);
6182 * Function to process paths received for a new peer addition. The recorded
6183 * paths form the initial tunnel, which can be optimized later.
6184 * Called on each result obtained for the DHT search.
6186 * @param cls closure
6187 * @param exp when will this value expire
6188 * @param key key of the result
6189 * @param get_path path of the get request
6190 * @param get_path_length lenght of get_path
6191 * @param put_path path of the put request
6192 * @param put_path_length length of the put_path
6193 * @param type type of the result
6194 * @param size number of bytes in data
6195 * @param data pointer to the result data
6198 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6199 const struct GNUNET_HashCode * key,
6200 const struct GNUNET_PeerIdentity *get_path,
6201 unsigned int get_path_length,
6202 const struct GNUNET_PeerIdentity *put_path,
6203 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6204 size_t size, const void *data)
6206 const struct PBlock *pb = data;
6207 const struct GNUNET_PeerIdentity *pi = &pb->id;
6208 struct MeshTunnel *t = cls;
6209 struct MeshPeerInfo *peer_info;
6210 struct MeshPeerPath *p;
6212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6213 if (size != sizeof (struct PBlock))
6215 GNUNET_break_op (0);
6218 if (ntohl(pb->type) != t->type)
6220 GNUNET_break_op (0);
6223 GNUNET_assert (NULL != t->owner);
6224 peer_info = peer_info_get (pi);
6225 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6227 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6229 p = path_build_from_dht (get_path, get_path_length, put_path,
6231 path_add_to_peers (p, GNUNET_NO);
6233 tunnel_add_peer (t, peer_info);
6234 peer_info_connect (peer_info, t);
6238 /******************************************************************************/
6239 /********************* MESH LOCAL HANDLES **************************/
6240 /******************************************************************************/
6244 * Handler for client disconnection
6246 * @param cls closure
6247 * @param client identification of the client; NULL
6248 * for the last call when the server is destroyed
6251 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6253 struct MeshClient *c;
6254 struct MeshClient *next;
6257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6267 if (c->handle != client)
6269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6275 GNUNET_SERVER_client_drop (c->handle);
6276 c->shutting_down = GNUNET_YES;
6277 GNUNET_assert (NULL != c->own_tunnels);
6278 GNUNET_assert (NULL != c->incoming_tunnels);
6279 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6280 &tunnel_destroy_iterator, c);
6281 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6282 &tunnel_destroy_iterator, c);
6283 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6284 &tunnel_destroy_iterator, c);
6285 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6286 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6287 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6289 /* deregister clients applications */
6290 if (NULL != c->apps)
6292 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c);
6293 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6295 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6296 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6298 GNUNET_SCHEDULER_cancel (announce_applications_task);
6299 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6301 if (NULL != c->types)
6302 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6303 for (i = 0; i < c->n_regex; i++)
6305 GNUNET_free (c->regexes[i].regex);
6306 if (NULL != c->regexes[i].h)
6307 GNUNET_REGEX_announce_cancel (c->regexes[i].h);
6309 GNUNET_free_non_null (c->regexes);
6310 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6311 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6313 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6316 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6325 * Handler for new clients
6327 * @param cls closure
6328 * @param client identification of the client
6329 * @param message the actual message, which includes messages the client wants
6332 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6333 const struct GNUNET_MessageHeader *message)
6335 struct GNUNET_MESH_ClientConnect *cc_msg;
6336 struct MeshClient *c;
6337 GNUNET_MESH_ApplicationType *a;
6344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6345 /* Check data sanity */
6346 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6347 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6348 ntypes = ntohs (cc_msg->types);
6349 napps = ntohs (cc_msg->applications);
6351 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6358 /* Create new client structure */
6359 c = GNUNET_malloc (sizeof (struct MeshClient));
6360 c->id = next_client_id++;
6361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6363 GNUNET_SERVER_client_keep (client);
6364 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6367 GNUNET_MESH_ApplicationType at;
6368 struct GNUNET_HashCode hc;
6370 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6371 for (i = 0; i < napps; i++)
6374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6375 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6376 /* store in clients hashmap */
6377 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6378 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6379 /* store in global hashmap, for announcements */
6380 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6383 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6384 announce_applications_task =
6385 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6391 struct GNUNET_HashCode hc;
6393 t = (uint16_t *) & a[napps];
6394 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6395 for (i = 0; i < ntypes; i++)
6398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6399 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6401 /* store in clients hashmap */
6402 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6403 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6404 /* store in global hashmap */
6405 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6406 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6410 " client has %u+%u subscriptions\n", napps, ntypes);
6412 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6413 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6414 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6415 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6416 GNUNET_SERVER_notification_context_add (nc, client);
6417 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6419 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6425 * Handler for clients announcing available services by a regular expression.
6427 * @param cls closure
6428 * @param client identification of the client
6429 * @param message the actual message, which includes messages the client wants
6432 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6433 const struct GNUNET_MessageHeader *message)
6435 const struct GNUNET_MESH_RegexAnnounce *msg;
6436 struct MeshRegexDescriptor rd;
6437 struct MeshClient *c;
6442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6444 /* Sanity check for client registration */
6445 if (NULL == (c = client_get (client)))
6448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6453 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6455 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6456 if (NULL != c->partial_regex)
6458 regex = c->partial_regex;
6459 offset = strlen (c->partial_regex);
6460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6461 " continuation, already have %u bytes\n",
6470 regex = GNUNET_realloc (regex, offset + len + 1);
6471 memcpy (®ex[offset], &msg[1], len);
6472 regex[offset + len] = '\0';
6473 if (0 == ntohs (msg->last))
6475 c->partial_regex = regex;
6476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6477 " not ended, stored %u bytes for later\n",
6479 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6483 rd.compression = ntohs (msg->compression_characters);
6485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " compr %u\n", ntohs (rd.compression));
6488 GNUNET_array_append (c->regexes, c->n_regex, rd);
6489 c->partial_regex = NULL;
6490 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6492 c->regex_announce_task = GNUNET_SCHEDULER_add_now (®ex_announce, c);
6498 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6504 * Handler for requests of new tunnels
6506 * @param cls closure
6507 * @param client identification of the client
6508 * @param message the actual message
6511 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6512 const struct GNUNET_MessageHeader *message)
6514 struct GNUNET_MESH_TunnelMessage *t_msg;
6515 struct MeshTunnel *t;
6516 struct MeshClient *c;
6517 MESH_TunnelNumber tid;
6519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6521 /* Sanity check for client registration */
6522 if (NULL == (c = client_get (client)))
6525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6530 /* Message sanity check */
6531 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6534 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6538 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6539 /* Sanity check for tunnel numbering */
6540 tid = ntohl (t_msg->tunnel_id);
6541 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6544 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6547 /* Sanity check for duplicate tunnel IDs */
6548 if (NULL != tunnel_get_by_local_id (c, tid))
6551 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6555 while (NULL != tunnel_get_by_pi (myid, next_tid))
6556 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6557 t = tunnel_new (myid, next_tid++, c, tid);
6560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6562 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6565 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6567 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6568 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6571 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6577 * Handler for requests of deleting tunnels
6579 * @param cls closure
6580 * @param client identification of the client
6581 * @param message the actual message
6584 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6585 const struct GNUNET_MessageHeader *message)
6587 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6588 struct MeshClient *c;
6589 struct MeshTunnel *t;
6590 MESH_TunnelNumber tid;
6592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6593 "Got a DESTROY TUNNEL from client!\n");
6595 /* Sanity check for client registration */
6596 if (NULL == (c = client_get (client)))
6599 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6604 /* Message sanity check */
6605 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6612 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6614 /* Retrieve tunnel */
6615 tid = ntohl (tunnel_msg->tunnel_id);
6616 t = tunnel_get_by_local_id(c, tid);
6619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6624 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6626 client_ignore_tunnel (c, t);
6627 tunnel_destroy_empty (t);
6628 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6631 send_client_tunnel_disconnect (t, c);
6632 client_delete_tunnel (c, t);
6634 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6636 tunnel_send_destroy (t, 0);
6637 t->destroy = GNUNET_YES;
6638 /* The tunnel will be destroyed when the last message is transmitted. */
6639 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6645 * Handler for requests of seeting tunnel's speed.
6647 * @param cls Closure (unused).
6648 * @param client Identification of the client.
6649 * @param message The actual message.
6652 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6653 const struct GNUNET_MessageHeader *message)
6655 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6656 struct MeshClient *c;
6657 struct MeshTunnel *t;
6658 MESH_TunnelNumber tid;
6660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6661 "Got a SPEED request from client!\n");
6663 /* Sanity check for client registration */
6664 if (NULL == (c = client_get (client)))
6667 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6673 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6675 /* Retrieve tunnel */
6676 tid = ntohl (tunnel_msg->tunnel_id);
6677 t = tunnel_get_by_local_id(c, tid);
6680 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6682 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6686 switch (ntohs(message->type))
6688 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6689 t->speed_min = GNUNET_YES;
6691 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6692 t->speed_min = GNUNET_NO;
6697 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6702 * Handler for requests of seeting tunnel's buffering policy.
6704 * @param cls Closure (unused).
6705 * @param client Identification of the client.
6706 * @param message The actual message.
6709 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6710 const struct GNUNET_MessageHeader *message)
6712 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6713 struct MeshClient *c;
6714 struct MeshTunnel *t;
6715 MESH_TunnelNumber tid;
6717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6718 "Got a BUFFER request from client!\n");
6720 /* Sanity check for client registration */
6721 if (NULL == (c = client_get (client)))
6724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6729 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6731 /* Retrieve tunnel */
6732 tid = ntohl (tunnel_msg->tunnel_id);
6733 t = tunnel_get_by_local_id(c, tid);
6736 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6738 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6742 switch (ntohs(message->type))
6744 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6745 t->nobuffer = GNUNET_NO;
6747 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6748 t->nobuffer = GNUNET_YES;
6754 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6759 * Handler for connection requests to new peers
6761 * @param cls closure
6762 * @param client identification of the client
6763 * @param message the actual message (PeerControl)
6766 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6767 const struct GNUNET_MessageHeader *message)
6769 struct GNUNET_MESH_PeerControl *peer_msg;
6770 struct MeshPeerInfo *peer_info;
6771 struct MeshClient *c;
6772 struct MeshTunnel *t;
6773 MESH_TunnelNumber tid;
6775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6776 /* Sanity check for client registration */
6777 if (NULL == (c = client_get (client)))
6780 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6785 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6787 /* Sanity check for message size */
6788 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6795 /* Tunnel exists? */
6796 tid = ntohl (peer_msg->tunnel_id);
6797 t = tunnel_get_by_local_id (c, tid);
6801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6805 /* Does client own tunnel? */
6806 if (t->owner->handle != client)
6809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6813 GNUNET_i2s (&peer_msg->peer));
6814 peer_info = peer_info_get (&peer_msg->peer);
6816 tunnel_add_peer (t, peer_info);
6817 peer_info_connect (peer_info, t);
6819 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6825 * Handler for disconnection requests of peers in a tunnel
6827 * @param cls closure
6828 * @param client identification of the client
6829 * @param message the actual message (PeerControl)
6832 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6833 const struct GNUNET_MessageHeader *message)
6835 struct GNUNET_MESH_PeerControl *peer_msg;
6836 struct MeshPeerInfo *peer_info;
6837 struct MeshClient *c;
6838 struct MeshTunnel *t;
6839 MESH_TunnelNumber tid;
6841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6842 /* Sanity check for client registration */
6843 if (NULL == (c = client_get (client)))
6846 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6851 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6853 /* Sanity check for message size */
6854 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6861 /* Tunnel exists? */
6862 tid = ntohl (peer_msg->tunnel_id);
6863 t = tunnel_get_by_local_id (c, tid);
6867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6872 /* Does client own tunnel? */
6873 if (t->owner->handle != client)
6876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6881 GNUNET_i2s (&peer_msg->peer));
6882 /* Is the peer in the tunnel? */
6884 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6885 if (NULL == peer_info)
6888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6892 /* Ok, delete peer from tunnel */
6893 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6894 &peer_msg->peer.hashPubKey);
6896 send_destroy_path (t, peer_info->id);
6897 tunnel_delete_peer (t, peer_info->id);
6898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6903 * Handler for blacklist requests of peers in a tunnel
6905 * @param cls closure
6906 * @param client identification of the client
6907 * @param message the actual message (PeerControl)
6909 * FIXME implement DHT block bloomfilter
6912 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6913 const struct GNUNET_MessageHeader *message)
6915 struct GNUNET_MESH_PeerControl *peer_msg;
6916 struct MeshClient *c;
6917 struct MeshTunnel *t;
6918 MESH_TunnelNumber tid;
6920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6921 /* Sanity check for client registration */
6922 if (NULL == (c = client_get (client)))
6925 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6930 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6932 /* Sanity check for message size */
6933 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6936 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6940 /* Tunnel exists? */
6941 tid = ntohl (peer_msg->tunnel_id);
6942 t = tunnel_get_by_local_id (c, tid);
6946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6951 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6952 GNUNET_PEER_intern(&peer_msg->peer));
6957 * Handler for unblacklist requests of peers in a tunnel
6959 * @param cls closure
6960 * @param client identification of the client
6961 * @param message the actual message (PeerControl)
6964 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6965 const struct GNUNET_MessageHeader *message)
6967 struct GNUNET_MESH_PeerControl *peer_msg;
6968 struct MeshClient *c;
6969 struct MeshTunnel *t;
6970 MESH_TunnelNumber tid;
6974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6975 /* Sanity check for client registration */
6976 if (NULL == (c = client_get (client)))
6979 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6984 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6986 /* Sanity check for message size */
6987 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6990 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6994 /* Tunnel exists? */
6995 tid = ntohl (peer_msg->tunnel_id);
6996 t = tunnel_get_by_local_id (c, tid);
7000 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7005 /* if peer is not known, complain */
7006 pid = GNUNET_PEER_search (&peer_msg->peer);
7013 /* search and remove from list */
7014 for (i = 0; i < t->nblacklisted; i++)
7016 if (t->blacklisted[i] == pid)
7018 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7019 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7024 /* if peer hasn't been blacklisted, complain */
7030 * Handler for connection requests to new peers by type
7032 * @param cls closure
7033 * @param client identification of the client
7034 * @param message the actual message (ConnectPeerByType)
7037 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7038 const struct GNUNET_MessageHeader *message)
7040 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7041 struct MeshClient *c;
7042 struct MeshTunnel *t;
7043 struct GNUNET_HashCode hash;
7044 MESH_TunnelNumber tid;
7046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7047 /* Sanity check for client registration */
7048 if (NULL == (c = client_get (client)))
7051 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7056 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7058 /* Sanity check for message size */
7059 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7060 ntohs (connect_msg->header.size))
7063 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7067 /* Tunnel exists? */
7068 tid = ntohl (connect_msg->tunnel_id);
7069 t = tunnel_get_by_local_id (c, tid);
7073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7077 /* Does client own tunnel? */
7078 if (t->owner->handle != client)
7081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7085 /* Do WE have the service? */
7086 t->type = ntohl (connect_msg->type);
7087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7088 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7089 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7092 /* Yes! Fast forward, add ourselves to the tunnel and send the
7093 * good news to the client, and alert the destination client of
7094 * an incoming tunnel.
7096 * FIXME send a path create to self, avoid code duplication
7098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7099 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7100 peer_info_get (&my_full_id),
7101 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7104 send_client_peer_connected (t, myid);
7105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7106 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7108 t->local_tid_dest = next_local_tid++;
7109 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7110 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7111 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7115 /* Ok, lets find a peer offering the service */
7116 if (NULL != t->dht_get_type)
7118 GNUNET_DHT_get_stop (t->dht_get_type);
7120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7121 GNUNET_h2s (&hash));
7123 GNUNET_DHT_get_start (dht_handle,
7124 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7126 dht_replication_level,
7127 GNUNET_DHT_RO_RECORD_ROUTE |
7128 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7130 &dht_get_type_handler, t);
7132 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7138 * Handler for connection requests to new peers by a string service description.
7140 * @param cls closure
7141 * @param client identification of the client
7142 * @param message the actual message, which includes messages the client wants
7145 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7146 const struct GNUNET_MessageHeader *message)
7148 struct GNUNET_MESH_ConnectPeerByString *msg;
7149 struct MeshRegexSearchInfo *info;
7150 struct MeshTunnel *t;
7151 struct MeshClient *c;
7152 MESH_TunnelNumber tid;
7157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7158 "Connect by string started\n");
7159 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7160 size = htons (message->size);
7162 /* Sanity check for client registration */
7163 if (NULL == (c = client_get (client)))
7166 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7171 /* Message size sanity check */
7172 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7179 /* Tunnel exists? */
7180 tid = ntohl (msg->tunnel_id);
7181 t = tunnel_get_by_local_id (c, tid);
7185 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7189 /* Does client own tunnel? */
7190 if (t->owner->handle != client)
7193 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7198 " on tunnel %s [%u]\n",
7199 GNUNET_i2s(&my_full_id),
7202 /* Only one connect_by_string allowed at the same time! */
7203 /* FIXME: allow more, return handle at api level to cancel, document */
7204 if (NULL != t->regex_search)
7207 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7211 /* Find string itself */
7212 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7213 string = (const char *) &msg[1];
7215 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7217 info->description = GNUNET_strndup (string, len);
7218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7220 t->regex_search = info;
7222 info->search_handle = GNUNET_REGEX_search (dht_handle,
7224 ®ex_found_handler, info,
7227 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7233 * Handler for client traffic directed to one peer
7235 * @param cls closure
7236 * @param client identification of the client
7237 * @param message the actual message
7240 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7241 const struct GNUNET_MessageHeader *message)
7243 struct MeshClient *c;
7244 struct MeshTunnel *t;
7245 struct MeshPeerInfo *pi;
7246 struct GNUNET_MESH_Unicast *data_msg;
7247 MESH_TunnelNumber tid;
7250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7251 "Got a unicast request from a client!\n");
7253 /* Sanity check for client registration */
7254 if (NULL == (c = client_get (client)))
7257 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7262 data_msg = (struct GNUNET_MESH_Unicast *) message;
7264 /* Sanity check for message size */
7265 size = ntohs (message->size);
7266 if (sizeof (struct GNUNET_MESH_Unicast) +
7267 sizeof (struct GNUNET_MessageHeader) > size)
7270 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7274 /* Tunnel exists? */
7275 tid = ntohl (data_msg->tid);
7276 t = tunnel_get_by_local_id (c, tid);
7280 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7284 /* Is it a local tunnel? Then, does client own the tunnel? */
7285 if (t->owner->handle != client)
7288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7292 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7293 &data_msg->destination.hashPubKey);
7294 /* Is the selected peer in the tunnel? */
7298 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7302 /* PID should be as expected */
7303 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7307 "Unicast PID, expected %u, got %u\n",
7308 t->fwd_pid + 1, ntohl (data_msg->pid));
7309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7313 /* Ok, everything is correct, send the message
7314 * (pretend we got it from a mesh peer)
7317 /* Work around const limitation */
7318 char buf[ntohs (message->size)] GNUNET_ALIGN;
7319 struct GNUNET_MESH_Unicast *copy;
7321 copy = (struct GNUNET_MESH_Unicast *) buf;
7322 memcpy (buf, data_msg, size);
7323 copy->oid = my_full_id;
7324 copy->tid = htonl (t->id.tid);
7325 copy->ttl = htonl (default_ttl);
7326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7327 " calling generic handler...\n");
7328 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
7330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7331 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7338 * Handler for client traffic directed to the origin
7340 * @param cls closure
7341 * @param client identification of the client
7342 * @param message the actual message
7345 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7346 const struct GNUNET_MessageHeader *message)
7348 struct GNUNET_MESH_ToOrigin *data_msg;
7349 struct MeshTunnelClientInfo *clinfo;
7350 struct MeshClient *c;
7351 struct MeshTunnel *t;
7352 MESH_TunnelNumber tid;
7355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7356 "Got a ToOrigin request from a client!\n");
7357 /* Sanity check for client registration */
7358 if (NULL == (c = client_get (client)))
7361 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7366 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7368 /* Sanity check for message size */
7369 size = ntohs (message->size);
7370 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7371 sizeof (struct GNUNET_MessageHeader) > size)
7374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7378 /* Tunnel exists? */
7379 tid = ntohl (data_msg->tid);
7380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7381 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7384 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7387 t = tunnel_get_by_local_id (c, tid);
7390 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7397 /* It should be sent by someone who has this as incoming tunnel. */
7398 if (GNUNET_NO == client_knows_tunnel (c, t))
7401 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7405 /* PID should be as expected */
7406 clinfo = tunnel_get_client_fc (t, c);
7407 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7411 "To Origin PID, expected %u, got %u\n",
7412 clinfo->bck_pid + 1,
7413 ntohl (data_msg->pid));
7414 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7419 /* Ok, everything is correct, send the message
7420 * (pretend we got it from a mesh peer)
7423 char buf[ntohs (message->size)] GNUNET_ALIGN;
7424 struct GNUNET_MESH_ToOrigin *copy;
7426 /* Work around const limitation */
7427 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7428 memcpy (buf, data_msg, size);
7429 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7430 copy->tid = htonl (t->id.tid);
7431 copy->ttl = htonl (default_ttl);
7432 copy->pid = htonl (t->bck_pid + 1);
7434 copy->sender = my_full_id;
7435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7436 " calling generic handler...\n");
7437 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
7439 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7446 * Handler for client traffic directed to all peers in a tunnel
7448 * @param cls closure
7449 * @param client identification of the client
7450 * @param message the actual message
7453 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7454 const struct GNUNET_MessageHeader *message)
7456 struct MeshClient *c;
7457 struct MeshTunnel *t;
7458 struct GNUNET_MESH_Multicast *data_msg;
7459 MESH_TunnelNumber tid;
7461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7462 "Got a multicast request from a client!\n");
7464 /* Sanity check for client registration */
7465 if (NULL == (c = client_get (client)))
7468 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7473 data_msg = (struct GNUNET_MESH_Multicast *) message;
7475 /* Sanity check for message size */
7476 if (sizeof (struct GNUNET_MESH_Multicast) +
7477 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7484 /* Tunnel exists? */
7485 tid = ntohl (data_msg->tid);
7486 t = tunnel_get_by_local_id (c, tid);
7490 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7491 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7493 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7497 /* Does client own tunnel? */
7498 if (t->owner->handle != client)
7501 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7505 /* PID should be as expected */
7506 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7510 "Multicast PID, expected %u, got %u\n",
7511 t->fwd_pid + 1, ntohl (data_msg->pid));
7512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7517 char buf[ntohs (message->size)] GNUNET_ALIGN;
7518 struct GNUNET_MESH_Multicast *copy;
7520 copy = (struct GNUNET_MESH_Multicast *) buf;
7521 memcpy (buf, message, ntohs (message->size));
7522 copy->oid = my_full_id;
7523 copy->tid = htonl (t->id.tid);
7524 copy->ttl = htonl (default_ttl);
7525 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7527 " calling generic handler...\n");
7528 handle_mesh_data_multicast (client, &my_full_id, ©->header);
7531 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7537 * Handler for client's ACKs for payload traffic.
7539 * @param cls Closure (unused).
7540 * @param client Identification of the client.
7541 * @param message The actual message.
7544 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7545 const struct GNUNET_MessageHeader *message)
7547 struct GNUNET_MESH_LocalAck *msg;
7548 struct MeshTunnel *t;
7549 struct MeshClient *c;
7550 MESH_TunnelNumber tid;
7553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7554 /* Sanity check for client registration */
7555 if (NULL == (c = client_get (client)))
7558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7563 msg = (struct GNUNET_MESH_LocalAck *) message;
7565 /* Tunnel exists? */
7566 tid = ntohl (msg->tunnel_id);
7567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7568 t = tunnel_get_by_local_id (c, tid);
7572 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7573 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7578 ack = ntohl (msg->max_pid);
7579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7581 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
7582 if (NULL != t->owner && t->owner->handle == client)
7584 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7586 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7590 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7591 tunnel_set_client_fwd_ack (t, c, ack);
7592 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7595 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7602 * Iterator over all peers to send a monitoring client info about a tunnel.
7604 * @param cls Closure (message being built).
7605 * @param key Key (hashed tunnel ID, unused).
7606 * @param value Peer info.
7608 * @return GNUNET_YES, to keep iterating.
7611 monitor_peers_iterator (void *cls,
7612 const struct GNUNET_HashCode * key,
7615 struct GNUNET_MESH_LocalMonitor *msg = cls;
7616 struct GNUNET_PeerIdentity *id;
7617 struct MeshPeerInfo *info = value;
7619 id = (struct GNUNET_PeerIdentity *) &msg[1];
7620 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
7623 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7624 "* sending info about peer %s [%u]\n",
7625 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
7633 * Iterator over all tunnels to send a monitoring client info about each tunnel.
7635 * @param cls Closure (client handle).
7636 * @param key Key (hashed tunnel ID, unused).
7637 * @param value Tunnel info.
7639 * @return GNUNET_YES, to keep iterating.
7642 monitor_all_tunnels_iterator (void *cls,
7643 const struct GNUNET_HashCode * key,
7646 struct GNUNET_SERVER_Client *client = cls;
7647 struct MeshTunnel *t = value;
7648 struct GNUNET_MESH_LocalMonitor *msg;
7651 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
7652 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
7653 npeers * sizeof (struct GNUNET_PeerIdentity));
7654 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
7655 msg->tunnel_id = htonl (t->id.tid);
7656 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
7657 npeers * sizeof (struct GNUNET_PeerIdentity));
7658 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
7660 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
7661 monitor_peers_iterator,
7664 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7665 "* sending info about tunnel %s [%u] (%u peers)\n",
7666 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
7668 if (msg->npeers != npeers)
7671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7672 "Get tunnels fail: size %u - iter %u\n",
7673 npeers, msg->npeers);
7676 msg->npeers = htonl (npeers);
7677 GNUNET_SERVER_notification_context_unicast (nc, client,
7678 &msg->header, GNUNET_NO);
7684 * Handler for client's MONITOR request.
7686 * @param cls Closure (unused).
7687 * @param client Identification of the client.
7688 * @param message The actual message.
7691 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
7692 const struct GNUNET_MessageHeader *message)
7694 struct MeshClient *c;
7696 /* Sanity check for client registration */
7697 if (NULL == (c = client_get (client)))
7700 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7704 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7705 "Received get tunnels request from client %u\n",
7707 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
7708 monitor_all_tunnels_iterator,
7710 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7711 "Get tunnels request from client %u completed\n",
7713 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7718 * Data needed to build a Monitor_Tunnel message.
7720 struct MeshMonitorTunnelContext
7723 * Partial message, including peer count.
7725 struct GNUNET_MESH_LocalMonitor *msg;
7728 * Hashmap with positions: peer->position.
7730 struct GNUNET_CONTAINER_MultiHashMap *lookup;
7733 * Index of the parent of each peer in the message, realtive to the absolute
7734 * order in the array (can be in a previous message).
7736 uint32_t parents[1024];
7739 * Peers visited so far in the tree, aka position of the current peer.
7741 unsigned int npeers;
7744 * Client requesting the info.
7746 struct MeshClient *c;
7751 * Send a client a message about the structure of a tunnel.
7753 * @param ctx Context of the tunnel iteration, with info regarding the state
7754 * of the execution and the number of peers visited for this message.
7757 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
7759 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
7760 struct GNUNET_PeerIdentity *pid;
7761 unsigned int *parent;
7764 size = sizeof (struct GNUNET_MESH_LocalMonitor);
7765 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
7766 resp->header.size = htons (size);
7767 pid = (struct GNUNET_PeerIdentity *) &resp[1];
7768 parent = (unsigned int *) &pid[resp->npeers];
7769 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
7770 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
7771 &resp->header, GNUNET_NO);
7775 * Iterator over a tunnel tree to build a message containing all peers
7776 * the in the tunnel, including relay nodes.
7778 * @param cls Closure (pointer to pointer of message being built).
7779 * @param peer Short ID of a peer.
7780 * @param parent Short ID of the @c peer 's parent.
7783 tunnel_tree_iterator (void *cls,
7784 GNUNET_PEER_Id peer,
7785 GNUNET_PEER_Id parent)
7787 struct MeshMonitorTunnelContext *ctx = cls;
7788 struct GNUNET_MESH_LocalMonitor *msg;
7789 struct GNUNET_PeerIdentity *pid;
7790 struct GNUNET_PeerIdentity ppid;
7793 pid = (struct GNUNET_PeerIdentity *) &msg[1];
7794 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
7795 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
7796 &pid[msg->npeers].hashPubKey,
7797 (void *) (long) ctx->npeers,
7798 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7799 GNUNET_PEER_resolve (parent, &ppid);
7800 ctx->parents[msg->npeers] =
7801 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
7807 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
7809 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
7812 send_client_tunnel_info (ctx);
7819 * Handler for client's MONITOR_TUNNEL request.
7821 * @param cls Closure (unused).
7822 * @param client Identification of the client.
7823 * @param message The actual message.
7826 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
7827 const struct GNUNET_MessageHeader *message)
7829 const struct GNUNET_MESH_LocalMonitor *msg;
7830 struct GNUNET_MESH_LocalMonitor *resp;
7831 struct MeshMonitorTunnelContext ctx;
7832 struct MeshClient *c;
7833 struct MeshTunnel *t;
7835 /* Sanity check for client registration */
7836 if (NULL == (c = client_get (client)))
7839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7843 msg = (struct GNUNET_MESH_LocalMonitor *) message;
7844 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7845 "Received tunnel info request from client %u for tunnel %s[%X]\n",
7848 ntohl (msg->tunnel_id));
7849 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
7852 /* We don't know the tunnel */
7853 struct GNUNET_MESH_LocalMonitor warn;
7856 warn.npeers = htonl (UINT_MAX);
7857 GNUNET_SERVER_notification_context_unicast (nc, client,
7860 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7864 /* Initialize context */
7865 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
7869 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7872 /* Collect and send information */
7873 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
7874 send_client_tunnel_info (&ctx);
7877 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
7880 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7881 "Monitor tunnel request from client %u completed\n",
7883 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7888 * Functions to handle messages from clients
7890 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7891 {&handle_local_new_client, NULL,
7892 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7893 {&handle_local_announce_regex, NULL,
7894 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7895 {&handle_local_tunnel_create, NULL,
7896 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7897 sizeof (struct GNUNET_MESH_TunnelMessage)},
7898 {&handle_local_tunnel_destroy, NULL,
7899 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7900 sizeof (struct GNUNET_MESH_TunnelMessage)},
7901 {&handle_local_tunnel_speed, NULL,
7902 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7903 sizeof (struct GNUNET_MESH_TunnelMessage)},
7904 {&handle_local_tunnel_speed, NULL,
7905 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7906 sizeof (struct GNUNET_MESH_TunnelMessage)},
7907 {&handle_local_tunnel_buffer, NULL,
7908 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7909 sizeof (struct GNUNET_MESH_TunnelMessage)},
7910 {&handle_local_tunnel_buffer, NULL,
7911 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7912 sizeof (struct GNUNET_MESH_TunnelMessage)},
7913 {&handle_local_connect_add, NULL,
7914 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7915 sizeof (struct GNUNET_MESH_PeerControl)},
7916 {&handle_local_connect_del, NULL,
7917 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7918 sizeof (struct GNUNET_MESH_PeerControl)},
7919 {&handle_local_blacklist, NULL,
7920 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7921 sizeof (struct GNUNET_MESH_PeerControl)},
7922 {&handle_local_unblacklist, NULL,
7923 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7924 sizeof (struct GNUNET_MESH_PeerControl)},
7925 {&handle_local_connect_by_type, NULL,
7926 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7927 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7928 {&handle_local_connect_by_string, NULL,
7929 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7930 {&handle_local_unicast, NULL,
7931 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7932 {&handle_local_to_origin, NULL,
7933 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7934 {&handle_local_multicast, NULL,
7935 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7936 {&handle_local_ack, NULL,
7937 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7938 sizeof (struct GNUNET_MESH_LocalAck)},
7939 {&handle_local_get_tunnels, NULL,
7940 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
7941 sizeof (struct GNUNET_MessageHeader)},
7942 {&handle_local_show_tunnel, NULL,
7943 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
7944 sizeof (struct GNUNET_MESH_LocalMonitor)},
7950 * To be called on core init/fail.
7952 * @param cls service closure
7953 * @param server handle to the server for this service
7954 * @param identity the public identity of this peer
7957 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7958 const struct GNUNET_PeerIdentity *identity)
7961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7962 core_handle = server;
7963 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7967 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7969 GNUNET_i2s (identity));
7970 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7972 GNUNET_i2s (&my_full_id));
7973 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7975 GNUNET_abort(); // Try harder
7982 * Method called whenever a given peer connects.
7984 * @param cls closure
7985 * @param peer peer identity this notification is about
7988 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
7990 struct MeshPeerInfo *peer_info;
7991 struct MeshPeerPath *path;
7993 DEBUG_CONN ("Peer connected\n");
7994 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7995 peer_info = peer_info_get (peer);
7996 if (myid == peer_info->id)
7998 DEBUG_CONN (" (self)\n");
8003 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8005 path = path_new (2);
8006 path->peers[0] = myid;
8007 path->peers[1] = peer_info->id;
8008 GNUNET_PEER_change_rc (myid, 1);
8009 GNUNET_PEER_change_rc (peer_info->id, 1);
8010 peer_info_add_path (peer_info, path, GNUNET_YES);
8011 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8017 * Method called whenever a peer disconnects.
8019 * @param cls closure
8020 * @param peer peer identity this notification is about
8023 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8025 struct MeshPeerInfo *pi;
8026 struct MeshPeerQueue *q;
8027 struct MeshPeerQueue *n;
8029 DEBUG_CONN ("Peer disconnected\n");
8030 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8040 /* TODO try to reroute this traffic instead */
8041 queue_destroy(q, GNUNET_YES);
8044 if (NULL != pi->core_transmit)
8046 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8047 pi->core_transmit = NULL;
8049 peer_info_remove_path (pi, pi->id, myid);
8052 DEBUG_CONN (" (self)\n");
8054 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8059 /******************************************************************************/
8060 /************************ MAIN FUNCTIONS ****************************/
8061 /******************************************************************************/
8064 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8066 * @param cls closure
8067 * @param key current key code
8068 * @param value value in the hash map
8069 * @return GNUNET_YES if we should continue to iterate,
8073 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8075 struct MeshTunnel *t = value;
8082 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8084 * @param cls closure
8085 * @param key current key code
8086 * @param value value in the hash map
8087 * @return GNUNET_YES if we should continue to iterate,
8091 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8093 struct MeshPeerInfo *p = value;
8094 struct MeshPeerQueue *q;
8095 struct MeshPeerQueue *n;
8103 queue_destroy(q, GNUNET_YES);
8107 peer_info_destroy (p);
8113 * Task run during shutdown.
8119 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8123 if (core_handle != NULL)
8125 GNUNET_CORE_disconnect (core_handle);
8130 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
8133 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8134 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8135 if (dht_handle != NULL)
8137 GNUNET_DHT_disconnect (dht_handle);
8142 GNUNET_SERVER_notification_context_destroy (nc);
8145 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8147 GNUNET_SCHEDULER_cancel (announce_id_task);
8148 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8150 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8152 GNUNET_SCHEDULER_cancel (announce_applications_task);
8153 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8160 * Callback for hostkey read/generation.
8162 * @param cls Closure (Configuration handle).
8163 * @param pk The ECC private key.
8164 * @param emsg Error message, if any.
8167 key_generation_cb (void *cls,
8168 struct GNUNET_CRYPTO_EccPrivateKey *pk,
8171 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8172 struct MeshPeerInfo *peer;
8173 struct MeshPeerPath *p;
8178 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8179 _("Could not access hostkey: %s. Exiting.\n"),
8181 GNUNET_SCHEDULER_shutdown ();
8184 my_private_key = pk;
8185 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
8186 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8187 &my_full_id.hashPubKey);
8188 myid = GNUNET_PEER_intern (&my_full_id);
8189 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8190 "Mesh for peer [%s] starting\n",
8191 GNUNET_i2s(&my_full_id));
8193 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8194 NULL, /* Closure passed to MESH functions */
8195 &core_init, /* Call core_init once connected */
8196 &core_connect, /* Handle connects */
8197 &core_disconnect, /* remove peers on disconnects */
8198 NULL, /* Don't notify about all incoming messages */
8199 GNUNET_NO, /* For header only in notification */
8200 NULL, /* Don't notify about all outbound messages */
8201 GNUNET_NO, /* For header-only out notification */
8202 core_handlers); /* Register these handlers */
8204 if (core_handle == NULL)
8207 GNUNET_SCHEDULER_shutdown ();
8212 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8215 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8216 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8217 GNUNET_SERVER_disconnect_notify (server_handle,
8218 &handle_local_client_disconnect, NULL);
8222 clients_tail = NULL;
8225 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8226 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8228 /* Create a peer_info for the local peer */
8229 peer = peer_info_get (&my_full_id);
8232 GNUNET_PEER_change_rc (myid, 1);
8233 peer_info_add_path (peer, p, GNUNET_YES);
8234 GNUNET_SERVER_resume (server_handle);
8235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8240 * Process mesh requests.
8242 * @param cls closure
8243 * @param server the initialized server
8244 * @param c configuration to use
8247 run (void *cls, struct GNUNET_SERVER_Handle *server,
8248 const struct GNUNET_CONFIGURATION_Handle *c)
8252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8253 server_handle = server;
8256 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
8259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8261 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8262 "mesh", "peer/privatekey");
8263 GNUNET_SCHEDULER_shutdown ();
8268 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8269 &refresh_path_time))
8271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8273 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8274 "mesh", "refresh path time");
8275 GNUNET_SCHEDULER_shutdown ();
8280 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8281 &app_announce_time))
8283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8285 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8286 "mesh", "app announce time");
8287 GNUNET_SCHEDULER_shutdown ();
8290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8291 "APP_ANNOUNCE_TIME %llu ms\n",
8292 app_announce_time.rel_value);
8294 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8299 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8300 "mesh", "id announce time");
8301 GNUNET_SCHEDULER_shutdown ();
8306 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8311 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8312 "mesh", "connect timeout");
8313 GNUNET_SCHEDULER_shutdown ();
8318 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8323 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8324 "mesh", "max msgs queue");
8325 GNUNET_SCHEDULER_shutdown ();
8330 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8335 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8336 "mesh", "max tunnels");
8337 GNUNET_SCHEDULER_shutdown ();
8342 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8347 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8348 "mesh", "default ttl", 64);
8353 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8357 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8358 "mesh", "max peers", 1000);
8363 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8364 &dht_replication_level))
8366 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8368 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8369 "mesh", "dht replication level", 3);
8370 dht_replication_level = 3;
8373 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8374 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8375 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8376 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8377 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8379 dht_handle = GNUNET_DHT_connect (c, 64);
8380 if (NULL == dht_handle)
8384 stats = GNUNET_STATISTICS_create ("mesh", c);
8386 GNUNET_SERVER_suspend (server_handle);
8387 /* Scheduled the task to clean up when shutdown is called */
8388 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8390 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
8393 GNUNET_free (keyfile);
8398 * The main function for the mesh service.
8400 * @param argc number of arguments from the command line
8401 * @param argv command line arguments
8402 * @return 0 ok, 1 on error
8405 main (int argc, char *const *argv)
8410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8411 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8413 ret = (GNUNET_OK == r) ? 0 : 1;
8414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8418 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8419 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8420 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);