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 "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
64 #if MESH_DEBUG_CONNECTION
65 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
67 #define DEBUG_CONN(...)
71 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
73 #define DEBUG_DHT(...)
80 struct timespec __mesh_start;
81 struct timespec __mesh_end;
82 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
83 #define INTERVAL_END \
85 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
86 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
87 if (__diff < 0) __diff += 1000000000;\
91 #define INTERVAL_SHOW \
93 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
95 #define INTERVAL_START
100 /******************************************************************************/
101 /************************ DATA STRUCTURES ****************************/
102 /******************************************************************************/
104 /** FWD declaration */
110 * Struct representing a piece of data being sent to other peers
114 /** Tunnel it belongs to. */
115 struct MeshTunnel *t;
117 /** How many remaining neighbors still hav't got it. */
118 unsigned int reference_counter;
120 /** How many remaining neighbors we need to send this to. */
121 unsigned int total_out;
123 /** Size of the data. */
132 * Struct containing info about a queued transmission to this peer
139 struct MeshPeerQueue *next;
144 struct MeshPeerQueue *prev;
147 * Peer this transmission is directed to.
149 struct MeshPeerInfo *peer;
152 * Tunnel this message belongs to.
154 struct MeshTunnel *tunnel;
157 * Pointer to info stucture used as cls.
167 * Size of the message
174 * Struct to store regex information announced by clients.
176 struct MeshRegexDescriptor
179 * Regular expression itself.
184 * How many characters per edge can we squeeze?
186 uint16_t compression;
190 * Struct containing all info possibly needed to build a package when called
193 struct MeshTransmissionDescriptor
195 /** ID of the tunnel this packet travels in */
196 struct MESH_TunnelID *origin;
198 /** Who was this message being sent to */
199 struct MeshPeerInfo *peer;
201 /** Ultimate destination of the packet */
202 GNUNET_PEER_Id destination;
204 /** Data descriptor */
205 struct MeshData* mesh_data;
210 * Struct containing all information regarding a given peer
220 * Last time we heard from this peer
222 struct GNUNET_TIME_Absolute last_contact;
225 * Task handler for delayed connect task;
227 GNUNET_SCHEDULER_TaskIdentifier connect_task;
230 * Number of attempts to reconnect so far
232 int n_reconnect_attempts;
235 * Paths to reach the peer, ordered by ascending hop count
237 struct MeshPeerPath *path_head;
240 * Paths to reach the peer, ordered by ascending hop count
242 struct MeshPeerPath *path_tail;
245 * Handle to stop the DHT search for a path to this peer
247 struct GNUNET_DHT_GetHandle *dhtget;
250 * Closure given to the DHT GET
252 struct MeshPathInfo *dhtgetcls;
255 * Array of tunnels this peer participates in
256 * (most probably a small amount, therefore not a hashmap)
257 * When the path to the peer changes, notify these tunnels to let them
258 * re-adjust their path trees.
260 struct MeshTunnel **tunnels;
263 * Number of tunnels this peers participates in
265 unsigned int ntunnels;
268 * Transmission queue to core DLL head
270 struct MeshPeerQueue *queue_head;
273 * Transmission queue to core DLL tail
275 struct MeshPeerQueue *queue_tail;
278 * How many messages are in the queue to this peer.
280 unsigned int queue_n;
283 * Handle to for queued transmissions
285 struct GNUNET_CORE_TransmitHandle *core_transmit;
290 * Globally unique tunnel identification (owner + number)
291 * DO NOT USE OVER THE NETWORK
296 * Node that owns the tunnel
301 * Tunnel number to differentiate all the tunnels owned by the node oid
302 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
304 MESH_TunnelNumber tid;
309 * Struct containing all information regarding a tunnel
310 * For an intermediate node the improtant info used will be:
311 * - id Tunnel unique identification
312 * - paths[0] To know where to send it next
313 * - metainfo: ready, speeds, accounting
320 struct MESH_TunnelID id;
323 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
325 MESH_TunnelNumber local_tid;
328 * Local tunnel number for local destination clients (incoming number)
329 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
332 MESH_TunnelNumber local_tid_dest;
335 * Is the speed on the tunnel limited to the slowest peer?
340 * Is the tunnel bufferless (minimum latency)?
345 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
350 * Packet ID of the last bck packet sent (unique counter per hop).
355 * SKIP value for this tunnel.
360 * MeshTunnelFlowControlInfo of all children, indexed by GNUNET_PEER_Id.
361 * Contains the Flow Control info: FWD ACK value received,
362 * last BCK ACK sent, PID and SKIP values.
364 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
367 * Last ACK sent towards the origin (for traffic towards leaf node).
369 uint32_t last_fwd_ack;
372 * BCK ACK value received from the hop towards the owner of the tunnel,
373 * (previous node / owner): up to what message PID can we sent back to him.
378 * How many messages are in the forward queue (towards leaves).
380 unsigned int fwd_queue_n;
383 * How many messages do we accept in the forward queue.
385 unsigned int fwd_queue_max;
388 * How many messages are in the backward queue (towards origin).
390 unsigned int bck_queue_n;
393 * How many messages do we accept in the backward queue.
395 unsigned int bck_queue_max;
398 * Task to poll peer in case of a stall.
400 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
403 * Last time the tunnel was used
405 struct GNUNET_TIME_Absolute timestamp;
408 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
409 * containing peers added by id or by type, not intermediate peers.
411 struct GNUNET_CONTAINER_MultiHashMap *peers;
414 * Number of peers that are connected and potentially ready to receive data
416 unsigned int peers_ready;
419 * Number of peers that have been added to the tunnel
421 unsigned int peers_total;
424 * Client owner of the tunnel, if any
426 struct MeshClient *owner;
429 * Clients that have been informed about and want to stay in the tunnel.
431 struct MeshClient **clients;
434 * Flow control info for each client.
436 struct MeshTunnelFlowControlInfo *clients_fc;
439 * Number of elements in clients/clients_fc
441 unsigned int nclients;
444 * Clients that have been informed but requested to leave the tunnel.
446 struct MeshClient **ignore;
449 * Number of elements in clients
451 unsigned int nignore;
456 GNUNET_PEER_Id *blacklisted;
459 * Number of elements in blacklisted
461 unsigned int nblacklisted;
464 * Bloomfilter (for peer identities) to stop circular routes
466 char bloomfilter[MESH_BLOOM_SIZE];
471 struct MeshTunnelTree *tree;
474 * Application type we are looking for in this tunnel
476 GNUNET_MESH_ApplicationType type;
479 * Used to search peers offering a service
481 struct GNUNET_DHT_GetHandle *dht_get_type;
484 * Initial context of the regex search for a connect_by_string
486 struct MeshRegexSearchContext *regex_ctx;
489 * Task to keep the used paths alive
491 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
494 * Task to destroy the tunnel after timeout
496 * FIXME: merge the two? a tunnel will have either
497 * a path refresh OR a timeout, never both!
499 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
502 * Flag to signal the destruction of the tunnel.
503 * If this is set GNUNET_YES the tunnel will be destroyed
504 * when the queue is empty.
511 * Flow control info about a node in a tunnel. The node can be a local client
514 struct MeshTunnelFlowControlInfo
517 * Peer info of the node, NULL if local client.
519 struct MeshPeerInfo *peer;
522 * Client info of the node, NULL if remote peer.
524 struct MeshClient *client;
542 * Maximum PID they allowed us to send (FWD ACK received).
547 * Maximum PID we allowed them to send (BCK ACK sent).
552 * Circular buffer pointing to MeshPeerQueue elements for all
553 * payload traffic going to this node.
554 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
556 struct MeshPeerQueue **send_buffer;
559 * Index of the oldest element in the send_buffer.
561 unsigned int send_buffer_start;
564 * How many elements are already in the buffer.
566 unsigned int send_buffer_n;
569 * Tunnel this info is about
571 struct MeshTunnel *t;
574 * Task to poll peer in case of a stall.
576 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
581 * Info collected during iteration of child nodes in order to get the ACK value
584 struct MeshTunnelChildIteratorContext
587 * Tunnel whose info is being collected.
589 struct MeshTunnel *t;
592 * Is this context initialized? Is the value in max_child_ack valid?
597 * Maximum child ACK so far.
599 uint32_t max_child_ack;
602 * Number of children nodes
604 unsigned int nchildren;
609 * Info needed to work with tunnel paths and peers
616 struct MeshTunnel *t;
619 * Neighbouring peer to whom we send the packet to
621 struct MeshPeerInfo *peer;
626 struct MeshPeerPath *path;
631 * Struct containing information about a client of the service
638 struct MeshClient *next;
643 struct MeshClient *prev;
646 * Tunnels that belong to this client, indexed by local id
648 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
651 * Tunnels this client has accepted, indexed by incoming local id
653 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
656 * Tunnels this client has rejected, indexed by incoming local id
658 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
660 * Handle to communicate with the client
662 struct GNUNET_SERVER_Client *handle;
665 * Applications that this client has claimed to provide
667 struct GNUNET_CONTAINER_MultiHashMap *apps;
670 * Messages that this client has declared interest in
672 struct GNUNET_CONTAINER_MultiHashMap *types;
675 * Whether the client is active or shutting down (don't send confirmations
676 * to a client that is shutting down.
681 * ID of the client, mainly for debug messages
686 * Regular expressions describing the services offered by this client.
688 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
691 * Number of regular expressions in regexes.
693 unsigned int n_regex;
696 * Task to refresh all regular expresions in the DHT.
698 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
704 * Struct to keep information of searches of services described by a regex
705 * using a user-provided string service description.
707 struct MeshRegexSearchInfo
710 * Which tunnel is this for
712 struct MeshTunnel *t;
715 * User provided description of the searched service.
720 * Part of the description already consumed by the search.
727 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
730 * Results from running DHT GETs.
732 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
735 * Contexts, for each running DHT GET. Free all on end of search.
737 struct MeshRegexSearchContext **contexts;
740 * Number of contexts (branches/steps in search).
742 unsigned int n_contexts;
745 * Peer that is connecting via connect_by_string. When connected, free ctx.
750 * Other peers that are found but not yet being connected to.
752 GNUNET_PEER_Id *peers;
755 * Number of elements in peers.
757 unsigned int n_peers;
760 * Next peer to try to connect to.
765 * Timeout for a connect attempt.
766 * When reached, try to connect to a different peer, if any. If not,
767 * try the same peer again.
769 GNUNET_SCHEDULER_TaskIdentifier timeout;
774 * Struct to keep state of running searches that have consumed a part of
777 struct MeshRegexSearchContext
780 * Part of the description already consumed by
781 * this particular search branch.
786 * Information about the search.
788 struct MeshRegexSearchInfo *info;
791 * We just want to look for one edge, the longer the better.
794 unsigned int longest_match;
797 * Destination hash of the longest match.
799 struct GNUNET_HashCode hash;
802 /******************************************************************************/
803 /************************ DEBUG FUNCTIONS ****************************/
804 /******************************************************************************/
808 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
809 * @param cls string to print
810 * @param success GNUNET_OK if the PUT was transmitted,
811 * GNUNET_NO on timeout,
812 * GNUNET_SYSERR on disconnect from service
813 * after the PUT message was transmitted
814 * (so we don't know if it was received or not)
819 mesh_debug (void *cls, int success)
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
827 unsigned int debug_fwd_ack;
828 unsigned int debug_bck_ack;
832 /******************************************************************************/
833 /*********************** GLOBAL VARIABLES ****************************/
834 /******************************************************************************/
837 * Configuration parameters
839 static struct GNUNET_TIME_Relative refresh_path_time;
840 static struct GNUNET_TIME_Relative app_announce_time;
841 static struct GNUNET_TIME_Relative id_announce_time;
842 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
843 static struct GNUNET_TIME_Relative connect_timeout;
844 static unsigned long long default_ttl;
845 static unsigned long long dht_replication_level;
846 static unsigned long long max_tunnels;
847 static unsigned long long max_msgs_queue;
851 * Hostkey generation context
853 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
856 * DLL with all the clients, head.
858 static struct MeshClient *clients;
861 * DLL with all the clients, tail.
863 static struct MeshClient *clients_tail;
866 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
868 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
871 * Number of tunnels known.
873 static unsigned long long n_tunnels;
876 * Tunnels incoming, indexed by MESH_TunnelNumber
877 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
879 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
882 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
884 static struct GNUNET_CONTAINER_MultiHashMap *peers;
887 * Handle to communicate with transport
889 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
892 * Handle to communicate with core.
894 static struct GNUNET_CORE_Handle *core_handle;
899 static struct GNUNET_DHT_Handle *dht_handle;
904 static struct GNUNET_SERVER_Handle *server_handle;
907 * Handle to the statistics service.
909 static struct GNUNET_STATISTICS_Handle *stats;
912 * Notification context, to send messages to local clients.
914 static struct GNUNET_SERVER_NotificationContext *nc;
917 * Local peer own ID (memory efficient handle).
919 static GNUNET_PEER_Id myid;
922 * Local peer own ID (full value).
924 static struct GNUNET_PeerIdentity my_full_id;
929 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
934 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
937 * Tunnel ID for the next created tunnel (global tunnel number).
939 static MESH_TunnelNumber next_tid;
942 * Tunnel ID for the next incoming tunnel (local tunnel number).
944 static MESH_TunnelNumber next_local_tid;
947 * All application types provided by this peer.
949 static struct GNUNET_CONTAINER_MultiHashMap *applications;
952 * All message types clients of this peer are interested in.
954 static struct GNUNET_CONTAINER_MultiHashMap *types;
957 * Task to periodically announce provided applications.
959 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
962 * Task to periodically announce itself in the network.
964 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
967 * Next ID to assign to a client.
969 unsigned int next_client_id;
972 /******************************************************************************/
973 /*********************** DECLARATIONS **************************/
974 /******************************************************************************/
977 * Function to process paths received for a new peer addition. The recorded
978 * paths form the initial tunnel, which can be optimized later.
979 * Called on each result obtained for the DHT search.
982 * @param exp when will this value expire
983 * @param key key of the result
984 * @param type type of the result
985 * @param size number of bytes in data
986 * @param data pointer to the result data
989 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
990 const struct GNUNET_HashCode * key,
991 const struct GNUNET_PeerIdentity *get_path,
992 unsigned int get_path_length,
993 const struct GNUNET_PeerIdentity *put_path,
994 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
995 size_t size, const void *data);
999 * Function to process DHT string to regex matching.
1000 * Called on each result obtained for the DHT search.
1002 * @param cls closure (search context)
1003 * @param exp when will this value expire
1004 * @param key key of the result
1005 * @param get_path path of the get request (not used)
1006 * @param get_path_length lenght of get_path (not used)
1007 * @param put_path path of the put request (not used)
1008 * @param put_path_length length of the put_path (not used)
1009 * @param type type of the result
1010 * @param size number of bytes in data
1011 * @param data pointer to the result data
1013 * TODO: re-issue the request after certain time? cancel after X results?
1016 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1017 const struct GNUNET_HashCode * key,
1018 const struct GNUNET_PeerIdentity *get_path,
1019 unsigned int get_path_length,
1020 const struct GNUNET_PeerIdentity *put_path,
1021 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1022 size_t size, const void *data);
1026 * Function to process DHT string to regex matching.
1027 * Called on each result obtained for the DHT search.
1029 * @param cls closure (search context)
1030 * @param exp when will this value expire
1031 * @param key key of the result
1032 * @param get_path path of the get request (not used)
1033 * @param get_path_length lenght of get_path (not used)
1034 * @param put_path path of the put request (not used)
1035 * @param put_path_length length of the put_path (not used)
1036 * @param type type of the result
1037 * @param size number of bytes in data
1038 * @param data pointer to the result data
1041 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1042 const struct GNUNET_HashCode * key,
1043 const struct GNUNET_PeerIdentity *get_path,
1044 unsigned int get_path_length,
1045 const struct GNUNET_PeerIdentity *put_path,
1046 unsigned int put_path_length,
1047 enum GNUNET_BLOCK_Type type,
1048 size_t size, const void *data);
1052 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1053 * and insert it in the appropiate structures if the peer is not known yet.
1055 * @param peer Short identity of the peer.
1057 * @return Existing or newly created peer info.
1059 static struct MeshPeerInfo *
1060 peer_info_get_short (const GNUNET_PEER_Id peer);
1064 * Try to establish a new connection to this peer.
1065 * Use the best path for the given tunnel.
1066 * If the peer doesn't have any path to it yet, try to get one.
1067 * If the peer already has some path, send a CREATE PATH towards it.
1069 * @param peer PeerInfo of the peer.
1070 * @param t Tunnel for which to create the path, if possible.
1073 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1077 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1078 * needed rescources.
1079 * If peer already exists, reevaluate shortest path and change if different.
1081 * @param t Tunnel we want to add a new peer to
1082 * @param peer PeerInfo of the peer being added
1086 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1090 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1091 * that are no longer needed, as well as nodes of no longer reachable peers.
1092 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1094 * @param t Tunnel from which to remove the path.
1095 * @param peer Short id of the peer which should be removed.
1098 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1102 * Search for a tunnel by global ID using full PeerIdentities.
1104 * @param oid owner of the tunnel.
1105 * @param tid global tunnel number.
1107 * @return tunnel handler, NULL if doesn't exist.
1109 static struct MeshTunnel *
1110 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1114 * Delete an active client from the tunnel.
1120 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1123 * Notify a tunnel that a connection has broken that affects at least
1124 * some of its peers.
1126 * @param t Tunnel affected.
1127 * @param p1 Peer that got disconnected from p2.
1128 * @param p2 Peer that got disconnected from p1.
1130 * @return Short ID of the peer disconnected (either p1 or p2).
1131 * 0 if the tunnel remained unaffected.
1133 static GNUNET_PEER_Id
1134 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1139 * Get the current ack value for a tunnel, for data going from root to leaves,
1140 * taking in account the tunnel mode and the status of all children and clients.
1144 * @return Maximum PID allowed.
1147 tunnel_get_fwd_ack (struct MeshTunnel *t);
1151 * Add a client to a tunnel, initializing all needed data structures.
1153 * @param t Tunnel to which add the client.
1154 * @param c Client which to add to the tunnel.
1157 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1161 * Jump to the next edge, with the longest matching token.
1163 * @param block Block found in the DHT.
1164 * @param size Size of the block.
1165 * @param ctx Context of the search.
1167 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1170 regex_next_edge (const struct MeshRegexBlock *block,
1172 struct MeshRegexSearchContext *ctx);
1176 * Find a path to a peer that offers a regex servcie compatible
1177 * with a given string.
1179 * @param key The key of the accepting state.
1180 * @param ctx Context containing info about the string, tunnel, etc.
1183 regex_find_path (const struct GNUNET_HashCode *key,
1184 struct MeshRegexSearchContext *ctx);
1188 * @brief Queue and pass message to core when possible.
1190 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1191 * and accounts for it. In case the queue is full, the message is dropped and
1194 * Otherwise, message is treated as internal and allowed to go regardless of
1197 * @param cls Closure (@c type dependant). It will be used by queue_send to
1198 * build the message to be sent if not already prebuilt.
1199 * @param type Type of the message, 0 for a raw message.
1200 * @param size Size of the message.
1201 * @param dst Neighbor to send message to.
1202 * @param t Tunnel this message belongs to.
1205 queue_add (void *cls, uint16_t type, size_t size,
1206 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1210 * Free a transmission that was already queued with all resources
1211 * associated to the request.
1213 * @param queue Queue handler to cancel.
1214 * @param clear_cls Is it necessary to free associated cls?
1217 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1221 * @brief Get the next transmittable message from the queue.
1223 * This will be the head, except in the case of being a data packet
1224 * not allowed by the destination peer.
1226 * @param peer Destination peer.
1228 * @return The next viable MeshPeerQueue element to send to that peer.
1229 * NULL when there are no transmittable messages.
1231 struct MeshPeerQueue *
1232 queue_get_next (const struct MeshPeerInfo *peer);
1236 * Core callback to write a queued packet to core buffer
1238 * @param cls Closure (peer info).
1239 * @param size Number of bytes available in buf.
1240 * @param buf Where the to write the message.
1242 * @return number of bytes written to buf
1245 queue_send (void *cls, size_t size, void *buf);
1247 /******************************************************************************/
1248 /************************ ITERATORS ****************************/
1249 /******************************************************************************/
1252 * Iterator over found existing mesh regex blocks that match an ongoing search.
1254 * @param cls closure
1255 * @param key current key code
1256 * @param value value in the hash map
1257 * @return GNUNET_YES if we should continue to iterate,
1261 regex_result_iterator (void *cls,
1262 const struct GNUNET_HashCode * key,
1265 struct MeshRegexBlock *block = value;
1266 struct MeshRegexSearchContext *ctx = cls;
1268 if (GNUNET_YES == ntohl(block->accepting) &&
1269 ctx->position == strlen (ctx->info->description))
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1272 regex_find_path (key, ctx);
1273 return GNUNET_YES; // We found an accept state!
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1278 ctx->position, strlen(ctx->info->description),
1279 ntohl(block->accepting));
1282 regex_next_edge(block, SIZE_MAX, ctx);
1284 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1291 * Iterator over edges in a regex block retrieved from the DHT.
1293 * @param cls Closure (context of the search).
1294 * @param token Token that follows to next state.
1295 * @param len Lenght of token.
1296 * @param key Hash of next state.
1298 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1301 regex_edge_iterator (void *cls,
1304 const struct GNUNET_HashCode *key)
1306 struct MeshRegexSearchContext *ctx = cls;
1307 struct MeshRegexSearchInfo *info = ctx->info;
1311 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1316 current = &info->description[ctx->position];
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1318 current_len = strlen (info->description) - ctx->position;
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1323 if (len > current_len)
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1326 return GNUNET_YES; // Token too long, wont match
1328 if (0 != strncmp (current, token, len))
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1331 return GNUNET_YES; // Token doesn't match
1334 if (len > ctx->longest_match)
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1337 ctx->longest_match = len;
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1351 * Jump to the next edge, with the longest matching token.
1353 * @param block Block found in the DHT.
1354 * @param size Size of the block.
1355 * @param ctx Context of the search.
1357 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1360 regex_next_edge (const struct MeshRegexBlock *block,
1362 struct MeshRegexSearchContext *ctx)
1364 struct MeshRegexSearchContext *new_ctx;
1365 struct MeshRegexSearchInfo *info = ctx->info;
1366 struct GNUNET_DHT_GetHandle *get_h;
1370 /* Find the longest match for the current string position,
1371 * among tokens in the given block */
1372 ctx->longest_match = 0;
1373 result = GNUNET_MESH_regex_block_iterate (block, size,
1374 ®ex_edge_iterator, ctx);
1375 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1377 /* Did anything match? */
1378 if (0 == ctx->longest_match)
1381 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1382 new_ctx->info = info;
1383 new_ctx->position = ctx->position + ctx->longest_match;
1384 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1386 /* Check whether we already have a DHT GET running for it */
1388 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1391 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1393 ®ex_result_iterator,
1395 return; // We are already looking for it
1398 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1400 /* Start search in DHT */
1402 GNUNET_DHT_get_start (dht_handle, /* handle */
1403 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1404 &ctx->hash, /* key to search */
1405 dht_replication_level, /* replication level */
1406 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1407 NULL, /* xquery */ // FIXME BLOOMFILTER
1408 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1409 &dht_get_string_handler, new_ctx);
1411 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1414 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1423 * Iterator over hash map entries to cancel DHT GET requests after a
1424 * successful connect_by_string.
1426 * @param cls Closure (unused).
1427 * @param key Current key code (unused).
1428 * @param value Value in the hash map (get handle).
1429 * @return GNUNET_YES if we should continue to iterate,
1433 regex_cancel_dht_get (void *cls,
1434 const struct GNUNET_HashCode * key,
1437 struct GNUNET_DHT_GetHandle *h = value;
1439 GNUNET_DHT_get_stop (h);
1445 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1446 * search for connect_by_string.
1448 * @param cls Closure (unused).
1449 * @param key Current key code (unused).
1450 * @param value MeshRegexBlock in the hash map.
1451 * @return GNUNET_YES if we should continue to iterate,
1455 regex_free_result (void *cls,
1456 const struct GNUNET_HashCode * key,
1460 GNUNET_free (value);
1466 * Regex callback iterator to store own service description in the DHT.
1468 * @param cls closure.
1469 * @param key hash for current state.
1470 * @param proof proof for current state.
1471 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1472 * @param num_edges number of edges leaving current state.
1473 * @param edges edges leaving current state.
1476 regex_iterator (void *cls,
1477 const struct GNUNET_HashCode *key,
1480 unsigned int num_edges,
1481 const struct GNUNET_REGEX_Edge *edges)
1483 struct MeshRegexBlock *block;
1484 struct MeshRegexEdge *block_edge;
1485 enum GNUNET_DHT_RouteOption opt;
1489 unsigned int offset;
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 " regex dht put for state %s\n",
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1502 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1503 if (GNUNET_YES == accepting)
1505 struct MeshRegexAccept block;
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 " state %s is accepting, putting own id\n",
1510 size = sizeof (block);
1512 block.id = my_full_id;
1514 GNUNET_DHT_put(dht_handle, key,
1515 dht_replication_level,
1516 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1517 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1520 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1525 len = strlen(proof);
1526 size = sizeof (struct MeshRegexBlock) + len;
1527 block = GNUNET_malloc (size);
1530 block->n_proof = htonl (len);
1531 block->n_edges = htonl (num_edges);
1532 block->accepting = htonl (accepting);
1534 /* Store the proof at the end of the block. */
1535 aux = (char *) &block[1];
1536 memcpy (aux, proof, len);
1539 /* Store each edge in a variable length MeshEdge struct at the
1540 * very end of the MeshRegexBlock structure.
1542 for (i = 0; i < num_edges; i++)
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 " edge %s towards %s\n",
1547 GNUNET_h2s(&edges[i].destination));
1549 /* aux points at the end of the last block */
1550 len = strlen (edges[i].label);
1551 size += sizeof (struct MeshRegexEdge) + len;
1552 // Calculate offset FIXME is this ok? use size instead?
1553 offset = aux - (char *) block;
1554 block = GNUNET_realloc (block, size);
1555 aux = &((char *) block)[offset];
1556 block_edge = (struct MeshRegexEdge *) aux;
1557 block_edge->key = edges[i].destination;
1558 block_edge->n_token = htonl (len);
1559 aux = (char *) &block_edge[1];
1560 memcpy (aux, edges[i].label, len);
1564 GNUNET_DHT_put(dht_handle, key,
1565 dht_replication_level,
1567 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1569 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1573 GNUNET_free (block);
1578 * Store the regular expression describing a local service into the DHT.
1580 * @param regex The regular expresion.
1583 regex_put (const struct MeshRegexDescriptor *regex)
1585 struct GNUNET_REGEX_Automaton *dfa;
1587 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1588 dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1589 strlen(regex->regex),
1590 regex->compression);
1591 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1592 GNUNET_REGEX_automaton_destroy (dfa);
1593 DEBUG_DHT (" regex_put (%s) end\n", regex);
1598 * Find a path to a peer that offers a regex servcie compatible
1599 * with a given string.
1601 * @param key The key of the accepting state.
1602 * @param ctx Context containing info about the string, tunnel, etc.
1605 regex_find_path (const struct GNUNET_HashCode *key,
1606 struct MeshRegexSearchContext *ctx)
1608 struct GNUNET_DHT_GetHandle *get_h;
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1611 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1612 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1613 key, /* key to search */
1614 dht_replication_level, /* replication level */
1615 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1616 GNUNET_DHT_RO_RECORD_ROUTE,
1617 NULL, /* xquery */ // FIXME BLOOMFILTER
1618 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1619 &dht_get_string_accept_handler, ctx);
1620 GNUNET_break (GNUNET_OK ==
1621 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1624 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1629 * Function called if the connect attempt to a peer found via
1630 * connect_by_string times out. Try to connect to another peer, if any.
1631 * Otherwise try to reconnect to the same peer.
1633 * @param cls Closure (info about regex search).
1634 * @param tc TaskContext.
1637 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1639 struct MeshRegexSearchInfo *info = cls;
1640 struct MeshPeerInfo *peer_info;
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1645 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1646 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1654 if (0 < info->n_peers)
1656 // Select next peer, put current in that spot.
1657 id = info->peers[info->i_peer];
1658 info->peers[info->i_peer] = info->peer;
1659 info->i_peer = (info->i_peer + 1) % info->n_peers;
1663 // Try to connect to same peer again.
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1668 peer_info = peer_info_get_short(id);
1669 tunnel_add_peer (info->t, peer_info);
1671 tunnel_delete_peer (info->t, old);
1672 peer_info_connect (peer_info, info->t);
1673 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1674 ®ex_connect_timeout,
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1681 * Cancel an ongoing regex search in the DHT and free all resources.
1683 * @param ctx The search context.
1686 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1688 struct MeshRegexSearchInfo *info = ctx->info;
1691 GNUNET_free (info->description);
1692 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1693 ®ex_cancel_dht_get, NULL);
1694 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1695 ®ex_free_result, NULL);
1696 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1697 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1698 info->t->regex_ctx = NULL;
1699 for (i = 0; i < info->n_contexts; i++)
1701 GNUNET_free (info->contexts[i]);
1703 if (0 < info->n_contexts)
1704 GNUNET_free (info->contexts);
1705 if (0 < info->n_peers)
1706 GNUNET_free (info->peers);
1707 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1709 GNUNET_SCHEDULER_cancel(info->timeout);
1715 /******************************************************************************/
1716 /************************ PERIODIC FUNCTIONS ****************************/
1717 /******************************************************************************/
1720 * Announce iterator over for each application provided by the peer
1722 * @param cls closure
1723 * @param key current key code
1724 * @param value value in the hash map
1725 * @return GNUNET_YES if we should continue to
1730 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1732 struct PBlock block;
1733 struct MeshClient *c;
1735 block.id = my_full_id;
1736 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1737 GNUNET_assert(NULL != c);
1738 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1739 if (0 == block.type)
1744 block.type = htonl (block.type);
1746 GNUNET_break (NULL !=
1747 GNUNET_DHT_put (dht_handle, key,
1748 dht_replication_level,
1749 GNUNET_DHT_RO_RECORD_ROUTE |
1750 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1751 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1753 (const char *) &block,
1754 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1756 app_announce_time, NULL, NULL));
1762 * Periodically announce what applications are provided by local clients
1765 * @param cls closure
1766 * @param tc task context
1769 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1771 struct MeshClient *c = cls;
1774 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1775 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1780 DEBUG_DHT ("Starting PUT for regex\n");
1782 for (i = 0; i < c->n_regex; i++)
1784 regex_put (&c->regexes[i]);
1786 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1789 DEBUG_DHT ("Finished PUT for regex\n");
1796 * Periodically announce what applications are provided by local clients
1799 * @param cls closure
1800 * @param tc task context
1803 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1805 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1807 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1811 DEBUG_DHT ("Starting PUT for apps\n");
1813 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1815 announce_applications_task =
1816 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1818 DEBUG_DHT ("Finished PUT for apps\n");
1825 * Periodically announce self id in the DHT
1827 * @param cls closure
1828 * @param tc task context
1831 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1833 struct PBlock block;
1835 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1837 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1841 * - Set data expiration in function of X
1842 * - Adapt X to churn
1844 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1846 block.id = my_full_id;
1847 block.type = htonl (0);
1848 GNUNET_DHT_put (dht_handle, /* DHT handle */
1849 &my_full_id.hashPubKey, /* Key to use */
1850 dht_replication_level, /* Replication level */
1851 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1852 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1853 sizeof (block), /* Size of the data */
1854 (const char *) &block, /* Data itself */
1855 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1856 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1857 NULL, /* Continuation */
1858 NULL); /* Continuation closure */
1860 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1864 /******************************************************************************/
1865 /****************** GENERAL HELPER FUNCTIONS ************************/
1866 /******************************************************************************/
1869 * Decrements the reference counter and frees all resources if needed
1871 * @param mesh_data Data Descriptor used in a multicast message.
1872 * Freed no longer needed (last message).
1875 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1877 if (0 == --(mesh_data->reference_counter))
1879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1880 GNUNET_free (mesh_data->data);
1881 GNUNET_free (mesh_data);
1887 * Check if client has registered with the service and has not disconnected
1889 * @param client the client to check
1891 * @return non-NULL if client exists in the global DLL
1893 static struct MeshClient *
1894 client_get (struct GNUNET_SERVER_Client *client)
1896 struct MeshClient *c;
1901 if (c->handle == client)
1910 * Checks if a given client has subscribed to certain message type
1912 * @param message_type Type of message to check
1913 * @param c Client to check
1915 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1917 * FIXME: use of crypto_hash slows it down
1918 * The hash function alone takes 8-10us out of the ~55us for the whole
1919 * process of retransmitting the message from one local client to another.
1920 * Find faster implementation!
1923 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1925 struct GNUNET_HashCode hc;
1927 if (NULL == c->types)
1930 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1931 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1936 * Check whether client wants traffic from a tunnel.
1938 * @param c Client to check.
1939 * @param t Tunnel to be found.
1941 * @return GNUNET_YES if client knows tunnel.
1943 * TODO look in client hashmap
1946 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1950 for (i = 0; i < t->nclients; i++)
1951 if (t->clients[i] == c)
1958 * Check whether client has been informed about a tunnel.
1960 * @param c Client to check.
1961 * @param t Tunnel to be found.
1963 * @return GNUNET_YES if client knows tunnel.
1965 * TODO look in client hashmap
1968 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1972 for (i = 0; i < t->nignore; i++)
1973 if (t->ignore[i] == c)
1975 return client_wants_tunnel(c, t);
1980 * Marks a client as uninterested in traffic from the tunnel, updating both
1981 * client and tunnel to reflect this.
1983 * @param c Client that doesn't want traffic anymore.
1984 * @param t Tunnel which should be ignored.
1986 * FIXME when to delete an incoming tunnel?
1989 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1991 struct GNUNET_HashCode hash;
1993 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1994 GNUNET_break (GNUNET_YES ==
1995 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1997 GNUNET_break (GNUNET_YES ==
1998 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1999 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2000 tunnel_delete_active_client (t, c);
2001 GNUNET_array_append (t->ignore, t->nignore, c);
2006 * Deletes a tunnel from a client (either owner or destination). To be used on
2007 * tunnel destroy, otherwise, use client_ignore_tunnel.
2009 * @param c Client whose tunnel to delete.
2010 * @param t Tunnel which should be deleted.
2013 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2015 struct GNUNET_HashCode hash;
2019 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2020 GNUNET_assert (GNUNET_YES ==
2021 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2027 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2029 GNUNET_assert (GNUNET_YES ==
2030 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2034 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2042 * Send the message to all clients that have subscribed to its type
2044 * @param msg Pointer to the message itself
2045 * @param payload Pointer to the payload of the message.
2046 * @param t The tunnel to whose clients this message goes.
2048 * @return number of clients this message was sent to
2051 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2052 const struct GNUNET_MessageHeader *payload,
2053 struct MeshTunnel *t)
2055 struct MeshClient *c;
2056 MESH_TunnelNumber *tid;
2059 char cbuf[htons (msg->size)];
2061 type = ntohs (payload->type);
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2064 GNUNET_MESH_DEBUG_M2S (type));
2066 memcpy (cbuf, msg, sizeof (cbuf));
2067 switch (htons (msg->type))
2069 struct GNUNET_MESH_Unicast *uc;
2070 struct GNUNET_MESH_Multicast *mc;
2071 struct GNUNET_MESH_ToOrigin *to;
2073 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2074 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2077 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2078 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2081 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2082 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2090 for (count = 0, c = clients; c != NULL; c = c->next)
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2093 if (client_is_subscribed (type, c))
2095 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2099 *tid = htonl (t->local_tid);
2103 if (GNUNET_NO == client_knows_tunnel (c, t))
2105 /* This client doesn't know the tunnel */
2106 struct GNUNET_MESH_TunnelNotification tmsg;
2107 struct GNUNET_HashCode hash;
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2110 tmsg.header.size = htons (sizeof (tmsg));
2111 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2112 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2113 tmsg.tunnel_id = htonl (t->local_tid_dest);
2115 if (GNUNET_YES == t->speed_min)
2116 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2117 if (GNUNET_YES == t->nobuffer)
2118 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2119 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2120 &tmsg.header, GNUNET_NO);
2121 tunnel_add_client (t, c);
2122 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2124 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2125 c->incoming_tunnels, &hash, t,
2126 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2128 *tid = htonl (t->local_tid_dest);
2131 /* Check if the client wants to get traffic from the tunnel */
2132 if (GNUNET_NO == client_wants_tunnel(c, t))
2135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2136 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2137 (struct GNUNET_MessageHeader
2138 *) cbuf, GNUNET_NO);
2147 * Notify the client that owns the tunnel that a peer has connected to it
2148 * (the requested path to it has been confirmed).
2150 * @param t Tunnel whose owner to notify
2151 * @param id Short id of the peer that has connected
2154 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2156 struct GNUNET_MESH_PeerControl pc;
2158 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2159 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2160 pc.tunnel_id = htonl (t->local_tid);
2161 GNUNET_PEER_resolve (id, &pc.peer);
2162 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2168 * Notify all clients (not depending on registration status) that the incoming
2169 * tunnel is no longer valid.
2171 * @param t Tunnel that was destroyed.
2174 send_clients_tunnel_destroy (struct MeshTunnel *t)
2176 struct GNUNET_MESH_TunnelMessage msg;
2178 msg.header.size = htons (sizeof (msg));
2179 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2180 msg.tunnel_id = htonl (t->local_tid_dest);
2181 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2186 * Notify clients of tunnel disconnections, if needed.
2187 * In case the origin disconnects, the destination clients get a tunnel destroy
2188 * notification. If the last destination disconnects (only one remaining client
2189 * in tunnel), the origin gets a (local ID) peer disconnected.
2190 * Note that the function must be called BEFORE removing the client from
2193 * @param t Tunnel that was destroyed.
2194 * @param c Client that disconnected.
2197 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2203 struct GNUNET_MESH_TunnelMessage msg;
2205 msg.header.size = htons (sizeof (msg));
2206 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2207 msg.tunnel_id = htonl (t->local_tid_dest);
2208 for (i = 0; i < t->nclients; i++)
2209 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2210 &msg.header, GNUNET_NO);
2212 // FIXME when to disconnect an incoming tunnel?
2213 else if (1 == t->nclients && NULL != t->owner)
2215 struct GNUNET_MESH_PeerControl msg;
2217 msg.header.size = htons (sizeof (msg));
2218 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2219 msg.tunnel_id = htonl (t->local_tid);
2220 msg.peer = my_full_id;
2221 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2222 &msg.header, GNUNET_NO);
2228 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2229 * and insert it in the appropiate structures if the peer is not known yet.
2231 * @param peer Full identity of the peer.
2233 * @return Existing or newly created peer info.
2235 static struct MeshPeerInfo *
2236 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2238 struct MeshPeerInfo *peer_info;
2240 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2241 if (NULL == peer_info)
2244 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2245 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2246 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2247 peer_info->id = GNUNET_PEER_intern (peer);
2255 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2256 * and insert it in the appropiate structures if the peer is not known yet.
2258 * @param peer Short identity of the peer.
2260 * @return Existing or newly created peer info.
2262 static struct MeshPeerInfo *
2263 peer_info_get_short (const GNUNET_PEER_Id peer)
2265 struct GNUNET_PeerIdentity id;
2267 GNUNET_PEER_resolve (peer, &id);
2268 return peer_info_get (&id);
2273 * Iterator to remove the tunnel from the list of tunnels a peer participates
2276 * @param cls Closure (tunnel info)
2277 * @param key GNUNET_PeerIdentity of the peer (unused)
2278 * @param value PeerInfo of the peer
2280 * @return always GNUNET_YES, to keep iterating
2283 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2285 struct MeshTunnel *t = cls;
2286 struct MeshPeerInfo *peer = value;
2289 for (i = 0; i < peer->ntunnels; i++)
2292 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2295 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2296 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2305 * Core callback to write a pre-constructed data packet to core buffer
2307 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2308 * @param size Number of bytes available in buf.
2309 * @param buf Where the to write the message.
2311 * @return number of bytes written to buf
2314 send_core_data_raw (void *cls, size_t size, void *buf)
2316 struct MeshTransmissionDescriptor *info = cls;
2317 struct GNUNET_MessageHeader *msg;
2320 GNUNET_assert (NULL != info);
2321 GNUNET_assert (NULL != info->mesh_data);
2322 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2323 total_size = ntohs (msg->size);
2325 if (total_size > size)
2330 memcpy (buf, msg, total_size);
2331 data_descriptor_decrement_rc (info->mesh_data);
2338 * Sends an already built non-multicast message to a peer,
2339 * properly registrating all used resources.
2341 * @param message Message to send. Function makes a copy of it.
2342 * @param peer Short ID of the neighbor whom to send the message.
2343 * @param t Tunnel on which this message is transmitted.
2346 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2347 const struct GNUNET_PeerIdentity *peer,
2348 struct MeshTunnel *t)
2350 struct MeshTransmissionDescriptor *info;
2351 struct MeshPeerInfo *neighbor;
2352 struct MeshPeerPath *p;
2356 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2358 size = ntohs (message->size);
2359 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2360 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2361 info->mesh_data->data = GNUNET_malloc (size);
2362 memcpy (info->mesh_data->data, message, size);
2363 type = ntohs(message->type);
2366 struct GNUNET_MESH_Unicast *m;
2367 struct GNUNET_MESH_ToOrigin *to;
2369 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2370 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2371 m->ttl = htonl (ntohl (m->ttl) - 1);
2373 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2374 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2376 to->pid = htonl(t->bck_pid);
2378 info->mesh_data->data_len = size;
2379 info->mesh_data->reference_counter = 1;
2380 info->mesh_data->total_out = 1;
2381 neighbor = peer_info_get (peer);
2382 for (p = neighbor->path_head; NULL != p; p = p->next)
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 " %s IS NOT DIRECTLY CONNECTED\n",
2395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2398 for (p = neighbor->path_head; NULL != p; p = p->next)
2400 struct GNUNET_PeerIdentity debug_id;
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 " path with %u hops through:\n",
2406 for (i = 0; i < p->length; i++)
2408 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 i, GNUNET_i2s(&debug_id));
2415 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2416 GNUNET_free (info->mesh_data->data);
2417 GNUNET_free (info->mesh_data);
2421 info->peer = neighbor;
2422 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2433 * Sends a CREATE PATH message for a path to a peer, properly registrating
2434 * all used resources.
2436 * @param peer PeerInfo of the final peer for whom this path is being created.
2437 * @param p Path itself.
2438 * @param t Tunnel for which the path is created.
2441 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2442 struct MeshTunnel *t)
2444 struct GNUNET_PeerIdentity id;
2445 struct MeshPathInfo *path_info;
2446 struct MeshPeerInfo *neighbor;
2452 p = tree_get_path_to_peer (t->tree, peer->id);
2459 for (i = 0; i < p->length; i++)
2461 if (p->peers[i] == myid)
2464 if (i >= p->length - 1)
2470 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2472 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2473 path_info->path = p;
2475 neighbor = peer_info_get (&id);
2476 path_info->peer = neighbor;
2477 queue_add (path_info,
2478 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2479 sizeof (struct GNUNET_MESH_ManipulatePath) +
2480 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2487 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2489 * @param t Tunnel whose path to destroy.
2490 * @param destination Short ID of the peer to whom the path to destroy.
2493 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2495 struct MeshPeerPath *p;
2498 p = tree_get_path_to_peer (t->tree, destination);
2504 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2505 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2507 struct GNUNET_MESH_ManipulatePath *msg;
2508 struct GNUNET_PeerIdentity *pi;
2512 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2513 msg->header.size = htons (size);
2514 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2515 msg->tid = htonl (t->id.tid);
2516 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2517 for (i = 0; i < p->length; i++)
2519 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2521 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2528 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2530 * @param t Tunnel which to confirm.
2533 send_path_ack (struct MeshTunnel *t)
2535 struct MeshTransmissionDescriptor *info;
2536 struct GNUNET_PeerIdentity id;
2537 GNUNET_PEER_Id peer;
2539 peer = tree_get_predecessor (t->tree);
2540 GNUNET_PEER_resolve (peer, &id);
2541 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2542 info->origin = &t->id;
2543 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2544 GNUNET_assert (NULL != info->peer);
2547 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2548 sizeof (struct GNUNET_MESH_PathACK),
2555 * Try to establish a new connection to this peer.
2556 * Use the best path for the given tunnel.
2557 * If the peer doesn't have any path to it yet, try to get one.
2558 * If the peer already has some path, send a CREATE PATH towards it.
2560 * @param peer PeerInfo of the peer.
2561 * @param t Tunnel for which to create the path, if possible.
2564 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2566 struct MeshPeerPath *p;
2567 struct MeshPathInfo *path_info;
2569 if (NULL != peer->path_head)
2571 p = tree_get_path_to_peer (t->tree, peer->id);
2578 // FIXME always send create path to self
2581 send_create_path (peer, p, t);
2585 struct GNUNET_HashCode hash;
2588 send_client_peer_connected (t, myid);
2589 t->local_tid_dest = next_local_tid++;
2590 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2593 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2594 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2601 else if (NULL == peer->dhtget)
2603 struct GNUNET_PeerIdentity id;
2605 GNUNET_PEER_resolve (peer->id, &id);
2606 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2607 path_info->peer = peer;
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2610 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2611 peer->dhtgetcls = path_info;
2612 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2613 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2614 &id.hashPubKey, /* key to search */
2615 dht_replication_level, /* replication level */
2616 GNUNET_DHT_RO_RECORD_ROUTE |
2617 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2618 NULL, /* xquery */ // FIXME BLOOMFILTER
2619 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2620 &dht_get_id_handler, path_info);
2622 /* Otherwise, there is no path but the DHT get is already started. */
2627 * Task to delay the connection of a peer
2629 * @param cls Closure (path info with tunnel and peer to connect).
2630 * Will be free'd on exection.
2631 * @param tc TaskContext
2634 peer_info_connect_task (void *cls,
2635 const struct GNUNET_SCHEDULER_TaskContext *tc)
2637 struct MeshPathInfo *path_info = cls;
2639 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2641 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2646 peer_info_connect (path_info->peer, path_info->t);
2652 * Destroy the peer_info and free any allocated resources linked to it
2654 * @param pi The peer_info to destroy.
2656 * @return GNUNET_OK on success
2659 peer_info_destroy (struct MeshPeerInfo *pi)
2661 struct GNUNET_PeerIdentity id;
2662 struct MeshPeerPath *p;
2663 struct MeshPeerPath *nextp;
2665 GNUNET_PEER_resolve (pi->id, &id);
2666 GNUNET_PEER_change_rc (pi->id, -1);
2669 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2672 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2673 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2675 if (NULL != pi->dhtget)
2677 GNUNET_DHT_get_stop (pi->dhtget);
2678 GNUNET_free (pi->dhtgetcls);
2684 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2688 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2690 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2698 * Remove all paths that rely on a direct connection between p1 and p2
2699 * from the peer itself and notify all tunnels about it.
2701 * @param peer PeerInfo of affected peer.
2702 * @param p1 GNUNET_PEER_Id of one peer.
2703 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2706 * TODO: optimize (see below)
2709 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2712 struct MeshPeerPath *p;
2713 struct MeshPeerPath *aux;
2714 struct MeshPeerInfo *peer_d;
2716 unsigned int destroyed;
2721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2723 p = peer->path_head;
2727 for (i = 0; i < (p->length - 1); i++)
2729 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2730 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2732 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2743 for (i = 0; i < peer->ntunnels; i++)
2745 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2749 * Problem: one or more peers have been deleted from the tunnel tree.
2750 * We don't know who they are to try to add them again.
2751 * We need to try to find a new path for each of the disconnected peers.
2752 * Some of them might already have a path to reach them that does not
2753 * involve p1 and p2. Adding all anew might render in a better tree than
2754 * the trivial immediate fix.
2756 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2757 * its children will be reachable trough him.
2759 peer_d = peer_info_get_short (d);
2762 for (p = peer_d->path_head; NULL != p; p = p->next)
2764 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2772 /* No callback, as peer will be already disconnected and a connection
2773 * scheduled by tunnel_notify_connection_broken.
2775 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2779 peer_info_connect (peer_d, peer->tunnels[i]);
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2787 * Add the path to the peer and update the path used to reach it in case this
2790 * @param peer_info Destination peer to add the path to.
2791 * @param path New path to add. Last peer must be the peer in arg 1.
2792 * Path will be either used of freed if already known.
2793 * @param trusted Do we trust that this path is real?
2796 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2799 struct MeshPeerPath *aux;
2803 if ((NULL == peer_info) || (NULL == path))
2806 path_destroy (path);
2809 if (path->peers[path->length - 1] != peer_info->id)
2812 path_destroy (path);
2815 if (path->length <= 2 && GNUNET_NO == trusted)
2817 /* Only allow CORE to tell us about direct paths */
2818 path_destroy (path);
2821 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2822 for (l = 1; l < path->length; l++)
2824 if (path->peers[l] == myid)
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2827 for (l2 = 0; l2 < path->length - l; l2++)
2829 path->peers[l2] = path->peers[l + l2];
2834 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2839 struct GNUNET_PeerIdentity id;
2841 GNUNET_PEER_resolve (peer_info->id, &id);
2842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2843 path->length, GNUNET_i2s (&id));
2846 l = path_get_length (path);
2853 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2854 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2856 l2 = path_get_length (aux);
2859 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2860 peer_info->path_tail, aux, path);
2865 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2867 path_destroy (path);
2872 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2879 * Add the path to the origin peer and update the path used to reach it in case
2880 * this is the shortest.
2881 * The path is given in peer_info -> destination, therefore we turn the path
2882 * upside down first.
2884 * @param peer_info Peer to add the path to, being the origin of the path.
2885 * @param path New path to add after being inversed.
2886 * @param trusted Do we trust that this path is real?
2889 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2890 struct MeshPeerPath *path, int trusted)
2893 peer_info_add_path (peer_info, path, trusted);
2898 * Function called if the connection to the peer has been stalled for a while,
2899 * possibly due to a missed ACK. Poll the peer about its ACK status.
2901 * @param cls Closure (MeshPeerInfo of stalled peer).
2902 * @param tc TaskContext.
2905 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2907 struct MeshTunnelFlowControlInfo *fcinfo = cls;
2908 struct GNUNET_MESH_Poll msg;
2909 struct GNUNET_PeerIdentity id;
2910 struct MeshTunnel *t;
2912 // FIXME fc include code to poll clients
2913 if (NULL == fcinfo->peer)
2915 fcinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2916 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2922 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2923 msg.header.size = htons (sizeof (msg));
2924 msg.tid = htonl (t->id.tid);
2925 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2926 msg.last_ack = htonl (fcinfo->fwd_ack);
2928 GNUNET_PEER_resolve (fcinfo->peer->id, &id);
2929 send_prebuilt_message (&msg.header, &id, fcinfo->t);
2930 fcinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
2931 &tunnel_poll, fcinfo);
2937 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2938 * to obtain a local peer -> destination path and interning the peer ids.
2940 * @return Newly allocated and created path
2942 static struct MeshPeerPath *
2943 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2944 unsigned int get_path_length,
2945 const struct GNUNET_PeerIdentity *put_path,
2946 unsigned int put_path_length)
2948 struct MeshPeerPath *p;
2954 GNUNET_PEER_change_rc (myid, 1);
2955 i = get_path_length;
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2957 for (i--; i >= 0; i--)
2959 id = GNUNET_PEER_intern (&get_path[i]);
2960 if (p->length > 0 && id == p->peers[p->length - 1])
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2963 GNUNET_PEER_change_rc (id, -1);
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2968 GNUNET_i2s (&get_path[i]));
2970 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2971 p->peers[p->length - 1] = id;
2974 i = put_path_length;
2975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2976 for (i--; i >= 0; i--)
2978 id = GNUNET_PEER_intern (&put_path[i]);
2981 /* PUT path went through us, so discard the path up until now and start
2982 * from here to get a much shorter (and loop-free) path.
2987 if (p->length > 0 && id == p->peers[p->length - 1])
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2990 GNUNET_PEER_change_rc (id, -1);
2994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2995 GNUNET_i2s (&put_path[i]));
2997 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2998 p->peers[p->length - 1] = id;
3002 if (get_path_length > 0)
3003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3004 GNUNET_i2s (&get_path[0]));
3005 if (put_path_length > 0)
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3007 GNUNET_i2s (&put_path[0]));
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3010 for (i = 0; i < p->length; i++)
3012 struct GNUNET_PeerIdentity peer_id;
3014 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3016 GNUNET_i2s (&peer_id));
3024 * Adds a path to the peer_infos of all the peers in the path
3026 * @param p Path to process.
3027 * @param confirmed Whether we know if the path works or not.
3030 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3034 /* TODO: invert and add */
3035 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3036 for (i++; i < p->length; i++)
3038 struct MeshPeerInfo *aux;
3039 struct MeshPeerPath *copy;
3041 aux = peer_info_get_short (p->peers[i]);
3042 copy = path_duplicate (p);
3043 copy->length = i + 1;
3044 peer_info_add_path (aux, copy, GNUNET_NO);
3050 * Send keepalive packets for a peer
3052 * @param cls Closure (tunnel for which to send the keepalive).
3053 * @param tc Notification context.
3055 * TODO: implement explicit multicast keepalive?
3058 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3062 * Search for a tunnel among the incoming tunnels
3064 * @param tid the local id of the tunnel
3066 * @return tunnel handler, NULL if doesn't exist
3068 static struct MeshTunnel *
3069 tunnel_get_incoming (MESH_TunnelNumber tid)
3071 struct GNUNET_HashCode hash;
3073 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3074 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3075 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3080 * Search for a tunnel among the tunnels for a client
3082 * @param c the client whose tunnels to search in
3083 * @param tid the local id of the tunnel
3085 * @return tunnel handler, NULL if doesn't exist
3087 static struct MeshTunnel *
3088 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3090 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3092 return tunnel_get_incoming (tid);
3096 struct GNUNET_HashCode hash;
3098 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3099 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3105 * Search for a tunnel by global ID using PEER_ID
3107 * @param pi owner of the tunnel
3108 * @param tid global tunnel number
3110 * @return tunnel handler, NULL if doesn't exist
3112 static struct MeshTunnel *
3113 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3115 struct MESH_TunnelID id;
3116 struct GNUNET_HashCode hash;
3121 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3122 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3127 * Search for a tunnel by global ID using full PeerIdentities
3129 * @param oid owner of the tunnel
3130 * @param tid global tunnel number
3132 * @return tunnel handler, NULL if doesn't exist
3134 static struct MeshTunnel *
3135 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3137 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3142 * Delete an active client from the tunnel.
3148 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3152 for (i = 0; i < t->nclients; i++)
3154 if (t->clients[i] == c)
3156 t->clients[i] = t->clients[t->nclients - 1];
3157 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3158 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3160 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3168 * Delete an ignored client from the tunnel.
3174 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3178 for (i = 0; i < t->nignore; i++)
3180 if (t->ignore[i] == c)
3182 t->ignore[i] = t->ignore[t->nignore - 1];
3183 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3191 * Delete a client from the tunnel. It should be only done on
3192 * client disconnection, otherwise use client_ignore_tunnel.
3198 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3200 tunnel_delete_ignored_client (t, c);
3201 tunnel_delete_active_client (t, c);
3206 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3208 * Destroys queue elements of all waiting transmissions and frees all memory
3209 * used by the struct and its elements.
3211 * @param cls Closure (tunnel info).
3212 * @param key Hash of GNUNET_PEER_Id (unused).
3213 * @param value MeshTunnelChildInfo of the child.
3215 * @return always GNUNET_YES, to keep iterating
3218 tunnel_destroy_child (void *cls,
3219 const struct GNUNET_HashCode * key,
3222 struct MeshTunnelFlowControlInfo *cinfo = value;
3223 struct MeshTunnel *t = cls;
3224 struct MeshPeerQueue *q;
3228 for (c = 0; c < cinfo->send_buffer_n; c++)
3230 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3231 q = cinfo->send_buffer[i];
3232 cinfo->send_buffer[i] = NULL;
3234 queue_destroy (q, GNUNET_YES);
3237 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3239 GNUNET_free_non_null (cinfo->send_buffer);
3240 GNUNET_free (cinfo);
3246 * Callback used to notify a client owner of a tunnel that a peer has
3247 * disconnected, most likely because of a path change.
3249 * @param cls Closure (tunnel this notification is about).
3250 * @param peer_id Short ID of disconnected peer.
3253 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3255 struct MeshTunnel *t = cls;
3256 struct MeshPeerInfo *peer;
3257 struct MeshPathInfo *path_info;
3259 if (NULL != t->owner && NULL != nc)
3261 struct GNUNET_MESH_PeerControl msg;
3263 msg.header.size = htons (sizeof (msg));
3264 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3265 msg.tunnel_id = htonl (t->local_tid);
3266 GNUNET_PEER_resolve (peer_id, &msg.peer);
3267 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3268 &msg.header, GNUNET_NO);
3270 peer = peer_info_get_short (peer_id);
3271 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3272 path_info->peer = peer;
3274 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3280 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3281 * needed rescources.
3282 * If peer already exists, reevaluate shortest path and change if different.
3284 * @param t Tunnel we want to add a new peer to
3285 * @param peer PeerInfo of the peer being added
3289 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3291 struct GNUNET_PeerIdentity id;
3292 struct MeshPeerPath *best_p;
3293 struct MeshPeerPath *p;
3294 unsigned int best_cost;
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3298 GNUNET_PEER_resolve (peer->id, &id);
3300 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3303 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3304 GNUNET_assert (GNUNET_OK ==
3305 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3307 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3310 if (NULL != (p = peer->path_head))
3313 best_cost = tree_get_path_cost (t->tree, p);
3316 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3323 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3324 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3325 t->path_refresh_task =
3326 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3330 /* Start a DHT get */
3331 peer_info_connect (peer, t);
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3337 * Add a path to a tunnel which we don't own, just to remember the next hop.
3338 * If destination node was already in the tunnel, the first hop information
3339 * will be replaced with the new path.
3341 * @param t Tunnel we want to add a new peer to
3342 * @param p Path to add
3343 * @param own_pos Position of local node in path.
3347 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3348 unsigned int own_pos)
3350 struct GNUNET_PeerIdentity id;
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3353 GNUNET_assert (0 != own_pos);
3354 tree_add_path (t->tree, p, NULL, NULL);
3355 if (own_pos < p->length - 1)
3357 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3358 tree_update_first_hops (t->tree, myid, &id);
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3364 * Add a client to a tunnel, initializing all needed data structures.
3366 * @param t Tunnel to which add the client.
3367 * @param c Client which to add to the tunnel.
3370 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3372 struct MeshTunnelFlowControlInfo fcinfo;
3374 GNUNET_array_append (t->clients, t->nclients, c);
3376 fcinfo.fwd_ack = t->fwd_pid + 1;
3377 fcinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3378 fcinfo.fwd_pid = t->fwd_pid;
3379 fcinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3380 fcinfo.fc_poll = GNUNET_SCHEDULER_NO_TASK;
3381 // FIXME fc buffering is done by context_notify. Confirm this is OK.
3384 GNUNET_array_append (t->clients_fc, t->nclients, fcinfo);
3389 * Notifies a tunnel that a connection has broken that affects at least
3390 * some of its peers. Sends a notification towards the root of the tree.
3391 * In case the peer is the owner of the tree, notifies the client that owns
3392 * the tunnel and tries to reconnect.
3394 * @param t Tunnel affected.
3395 * @param p1 Peer that got disconnected from p2.
3396 * @param p2 Peer that got disconnected from p1.
3398 * @return Short ID of the peer disconnected (either p1 or p2).
3399 * 0 if the tunnel remained unaffected.
3401 static GNUNET_PEER_Id
3402 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3408 tree_notify_connection_broken (t->tree, p1, p2,
3409 &tunnel_notify_client_peer_disconnected,
3411 if (myid != p1 && myid != p2)
3417 if (tree_get_predecessor (t->tree) != 0)
3419 /* We are the peer still connected, notify owner of the disconnection. */
3420 struct GNUNET_MESH_PathBroken msg;
3421 struct GNUNET_PeerIdentity neighbor;
3423 msg.header.size = htons (sizeof (msg));
3424 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3425 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3426 msg.tid = htonl (t->id.tid);
3427 msg.peer1 = my_full_id;
3428 GNUNET_PEER_resolve (pid, &msg.peer2);
3429 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3430 send_prebuilt_message (&msg.header, &neighbor, t);
3438 * Send a multicast packet to a neighbor.
3440 * @param cls Closure (Info about the multicast packet)
3441 * @param neighbor_id Short ID of the neighbor to send the packet to.
3444 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3446 struct MeshData *mdata = cls;
3447 struct MeshTransmissionDescriptor *info;
3448 struct GNUNET_PeerIdentity neighbor;
3449 struct GNUNET_MessageHeader *msg;
3451 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3453 info->mesh_data = mdata;
3454 (mdata->reference_counter) ++;
3455 info->destination = neighbor_id;
3456 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3458 GNUNET_i2s (&neighbor));
3459 info->peer = peer_info_get (&neighbor);
3460 GNUNET_assert (NULL != info->peer);
3461 msg = (struct GNUNET_MessageHeader *) mdata->data;
3464 info->mesh_data->data_len,
3471 * Queue a message in a tunnel in multicast, sending a copy to each child node
3472 * down the local one in the tunnel tree.
3474 * @param t Tunnel in which to send the data.
3475 * @param msg Message to be sent.
3478 tunnel_send_multicast (struct MeshTunnel *t,
3479 const struct GNUNET_MessageHeader *msg)
3481 struct MeshData *mdata;
3483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3484 " sending a multicast packet...\n");
3486 mdata = GNUNET_malloc (sizeof (struct MeshData));
3487 mdata->data_len = ntohs (msg->size);
3489 mdata->data = GNUNET_malloc (mdata->data_len);
3490 memcpy (mdata->data, msg, mdata->data_len);
3491 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3493 struct GNUNET_MESH_Multicast *mcast;
3495 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3496 if (t->fwd_queue_n >= t->fwd_queue_max)
3499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3501 " message from %s!\n",
3502 GNUNET_i2s(&mcast->oid));
3503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3504 " message at %s!\n",
3505 GNUNET_i2s(&my_full_id));
3506 GNUNET_free (mdata->data);
3507 GNUNET_free (mdata);
3511 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3513 ntohl (mcast->ttl));
3517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3520 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3521 if (mdata->reference_counter == 0)
3523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3524 " no one to send data to\n");
3525 GNUNET_free (mdata->data);
3526 GNUNET_free (mdata);
3531 mdata->total_out = mdata->reference_counter;
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3534 " sending a multicast packet done\n");
3540 * Increase the SKIP value of all peers that
3541 * have not received a unicast message.
3543 * @param cls Closure (ID of the peer that HAS received the message).
3544 * @param key ID of the neighbor.
3545 * @param value FLow control information about the neighbor.
3547 * @return GNUNET_YES to keep iterating.
3550 tunnel_add_skip (void *cls,
3551 const struct GNUNET_HashCode * key,
3554 struct GNUNET_PeerIdentity *neighbor = cls;
3555 struct MeshTunnelFlowControlInfo *fcinfo = value;
3557 /* TODO compare only pointers? key == neighbor? */
3558 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3568 * @brief Get neighbor's Flow Control information.
3570 * Retrieves the MeshTunnelFlowControlInfo containing Flow Control data about
3571 * a direct descendant of the local node in a certain tunnel.
3572 * If the info is not yet there (recently created path), creates the data struct
3573 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3576 * @param t Tunnel related.
3577 * @param peer Neighbor whose Flow Control info is needed.
3579 * @return Neighbor's Flow Control info.
3581 static struct MeshTunnelFlowControlInfo *
3582 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3583 const struct GNUNET_PeerIdentity *peer)
3585 struct MeshTunnelFlowControlInfo *fcinfo;
3587 if (NULL == t->children_fc)
3590 fcinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3596 fcinfo = GNUNET_malloc (sizeof (struct MeshTunnelFlowControlInfo));
3597 fcinfo->peer = peer_info_get(peer);
3598 fcinfo->skip = t->fwd_pid;
3601 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3602 fcinfo->fwd_ack = t->fwd_pid + delta;
3603 fcinfo->bck_ack = delta;
3604 fcinfo->bck_pid = -1;
3606 fcinfo->send_buffer =
3607 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3609 GNUNET_assert (GNUNET_OK ==
3610 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3613 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3620 * Get the Flow Control info of a client.
3622 * @param t Tunnel on which to look.
3623 * @param c Client whose ACK to get.
3625 * @return ACK value.
3627 static struct MeshTunnelFlowControlInfo *
3628 tunnel_get_client_fc (struct MeshTunnel *t,
3629 struct MeshClient *c)
3633 for (i = 0; i < t->nclients; i++)
3635 if (t->clients[i] != c)
3637 return &t->clients_fc[i];
3640 return NULL; // won't get here. Just to avoid compiler / coverity complaints.
3645 * Iterator to get the appropiate ACK value from all children nodes.
3647 * @param cls Closue (tunnel).
3648 * @param id Id of the child node.
3651 tunnel_get_child_fwd_ack (void *cls,
3654 struct GNUNET_PeerIdentity peer_id;
3655 struct MeshTunnelFlowControlInfo *fcinfo;
3656 struct MeshTunnelChildIteratorContext *ctx = cls;
3657 struct MeshTunnel *t = ctx->t;
3660 GNUNET_PEER_resolve (id, &peer_id);
3661 fcinfo = tunnel_get_neighbor_fc (t, &peer_id);
3662 ack = fcinfo->fwd_ack;
3665 if (GNUNET_NO == ctx->init)
3667 ctx->max_child_ack = ack;
3668 ctx->init = GNUNET_YES;
3671 if (GNUNET_YES == t->speed_min)
3673 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3677 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3683 * Get the maximum PID allowed to transmit to any
3684 * tunnel child of the local peer, depending on the tunnel
3685 * buffering/speed settings.
3689 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3692 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3694 struct MeshTunnelChildIteratorContext ctx;
3696 ctx.max_child_ack = 0;
3698 ctx.init = GNUNET_NO;
3699 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3701 if (0 == ctx.nchildren)
3703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3704 " tunnel has no children, no FWD ACK\n");
3708 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3709 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3711 return (int64_t) ctx.max_child_ack;
3716 * Set the FWD ACK value of a client in a particular tunnel.
3718 * @param t Tunnel affected.
3719 * @param c Client whose ACK to set.
3720 * @param ack ACK value.
3723 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3724 struct MeshClient *c,
3729 for (i = 0; i < t->nclients; i++)
3731 if (t->clients[i] != c)
3733 t->clients_fc[i].fwd_ack = ack;
3741 * Get the highest ACK value of all clients in a particular tunnel,
3742 * according to the buffering/speed settings.
3744 * @param t Tunnel on which to look.
3746 * @return Corresponding ACK value (max uint32_t).
3747 * If no clients are suscribed, -1LL.
3750 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3755 if (0 == t->nclients)
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3758 " tunnel has no clients, no FWD ACK\n");
3762 for (ack = -1LL, i = 0; i < t->nclients; i++)
3765 (GNUNET_YES == t->speed_min &&
3766 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3767 (GNUNET_NO == t->speed_min &&
3768 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3770 ack = t->clients_fc[i].fwd_ack;
3774 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3775 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3777 return (uint32_t) ack;
3782 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3783 * mode and the status of all children nodes.
3787 * @return Maximum PID allowed.
3790 tunnel_get_fwd_ack (struct MeshTunnel *t)
3794 uint32_t buffer_free;
3798 count = t->fwd_pid - t->skip;
3799 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3800 child_ack = tunnel_get_children_fwd_ack (t);
3801 client_ack = tunnel_get_clients_fwd_ack (t);
3802 if (GNUNET_YES == t->nobuffer)
3805 if (-1LL == child_ack)
3806 child_ack = client_ack;
3807 if (-1LL == child_ack)
3810 client_ack = child_ack = ack;
3815 ack = count + buffer_free; // Overflow? OK!
3817 if (-1LL == child_ack)
3819 // Node has no children, child_ack AND core buffer are irrelevant.
3820 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3821 return (uint32_t) client_ack;
3823 if (-1LL == client_ack)
3827 if (GNUNET_YES == t->speed_min)
3829 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3830 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3834 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3835 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3838 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3839 count, buffer_free, child_ack, client_ack, ack);
3845 * Build a local ACK message and send it to a local client.
3847 * @param t Tunnel on which to send the ACK.
3848 * @param c Client to whom send the ACK.
3849 * @param ack Value of the ACK.
3852 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3854 struct GNUNET_MESH_LocalAck msg;
3856 msg.header.size = htons (sizeof (msg));
3857 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3858 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3859 msg.max_pid = htonl (ack);
3860 GNUNET_SERVER_notification_context_unicast(nc,
3867 * Build an ACK message and queue it to send to the given peer.
3869 * @param t Tunnel on which to send the ACK.
3870 * @param peer Peer to whom send the ACK.
3871 * @param ack Value of the ACK.
3874 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3876 struct GNUNET_MESH_ACK msg;
3878 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3879 msg.header.size = htons (sizeof (msg));
3880 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3881 msg.pid = htonl (ack);
3882 msg.tid = htonl (t->id.tid);
3884 send_prebuilt_message (&msg.header, peer, t);
3889 * Notify a the owner of a tunnel about how many more
3890 * payload packages will we accept on a given tunnel.
3892 * @param t Tunnel on which to send the ACK.
3895 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3900 "Sending client FWD ACK on tunnel %X\n",
3903 ack = tunnel_get_fwd_ack (t);
3905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3906 if (t->last_fwd_ack == ack)
3908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3913 t->last_fwd_ack = ack;
3914 send_local_ack (t, t->owner, ack);
3919 * Send an ACK informing the predecessor about the available buffer space.
3920 * In case there is no predecessor, inform the owning client.
3921 * If buffering is off, send only on behalf of children or self if endpoint.
3922 * If buffering is on, send when sent to children and buffer space is free.
3923 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3924 * the ACK itself goes "back" (towards root).
3926 * @param t Tunnel on which to send the ACK.
3927 * @param type Type of message that triggered the ACK transmission.
3930 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3932 struct GNUNET_PeerIdentity id;
3935 if (NULL != t->owner)
3937 tunnel_send_client_fwd_ack (t);
3940 /* Is it after unicast / multicast retransmission? */
3943 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3944 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3946 "ACK due to FWD DATA retransmission\n");
3947 if (GNUNET_YES == t->nobuffer)
3949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3953 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3954 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3960 /* Check if we need no retransmit the ACK */
3961 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3962 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3966 " t->qmax: %u, t->qn: %u\n",
3967 t->fwd_queue_max, t->fwd_queue_n);
3968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3969 " t->pid: %u, t->ack: %u\n",
3970 t->fwd_pid, t->last_fwd_ack);
3974 /* Ok, ACK might be necessary, what PID to ACK? */
3975 ack = tunnel_get_fwd_ack (t);
3977 /* If speed_min and not all children have ack'd, dont send yet */
3978 if (ack == t->last_fwd_ack)
3980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3984 t->last_fwd_ack = ack;
3985 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3986 send_ack (t, &id, ack);
3992 * Iterator to send a child node a BCK ACK to allow him to send more
3995 * @param cls Closure (tunnel).
3996 * @param id Id of the child node.
3999 tunnel_send_child_bck_ack (void *cls,
4002 struct MeshTunnel *t = cls;
4003 struct MeshTunnelFlowControlInfo *fcinfo;
4004 struct GNUNET_PeerIdentity peer;
4006 GNUNET_PEER_resolve (id, &peer);
4007 fcinfo = tunnel_get_neighbor_fc (t, &peer);
4009 if (fcinfo->bck_ack != fcinfo->bck_pid &&
4010 GNUNET_NO == GMC_is_pid_bigger (fcinfo->bck_ack, fcinfo->bck_pid))
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4013 " Not sending ACK, not needed\n");
4017 fcinfo->bck_ack = t->bck_queue_max - t->bck_queue_n + fcinfo->bck_pid;
4018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4019 " Sending BCK ACK %u\n",
4021 send_ack (t, &peer, fcinfo->bck_ack);
4026 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4028 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4030 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4031 * - implement a size 1 buffer in each client_fc AND children_fc
4032 * to hold at least 1 message per "child".
4033 * problem: violates no buffer policy
4034 * - ack 0 and make "children" poll for transmission slots
4035 * problem: big overhead, extra latency even in low traffic
4038 * @param t Tunnel on which to send the BCK ACKs.
4041 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4044 unsigned int tunnel_delta;
4046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4048 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4051 /* Find client whom to allow to send to origin (with lowest buffer space) */
4052 for (i = 0; i < t->nclients; i++)
4054 struct MeshTunnelFlowControlInfo *fcinfo;
4057 fcinfo = &t->clients_fc[i];
4058 delta = fcinfo->bck_ack - fcinfo->bck_pid;
4059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4060 t->clients[i]->id, delta);
4062 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4063 (GNUNET_YES == t->nobuffer && 0 == delta))
4067 ack = fcinfo->bck_pid;
4068 ack += t->nobuffer ? 1 : tunnel_delta;
4069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4070 " sending ack to client %u: %u\n",
4071 t->clients[i]->id, ack);
4072 send_local_ack (t, t->clients[i], ack);
4073 fcinfo->bck_ack = ack;
4077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4078 " not sending ack to client %u (td %u, d %u)\n",
4079 t->clients[i]->id, tunnel_delta, delta);
4086 * Send an ACK informing the children nodes and destination clients about
4087 * the available buffer space.
4088 * If buffering is off, send only on behalf of root (can be self).
4089 * If buffering is on, send when sent to predecessor and buffer space is free.
4090 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4091 * the ACK itself goes "forward" (towards children/clients).
4093 * @param t Tunnel on which to send the ACK.
4094 * @param type Type of message that triggered the ACK transmission.
4097 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4100 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4101 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4102 /* Is it after data to_origin retransmission? */
4105 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4106 if (GNUNET_YES == t->nobuffer)
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4109 " Not sending ACK, nobuffer\n");
4113 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4114 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4115 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4121 tunnel_send_clients_bck_ack (t);
4122 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4127 * @brief Re-initiate traffic to this peer if necessary.
4129 * Check if there is traffic queued towards this peer
4130 * and the core transmit handle is NULL (traffic was stalled).
4131 * If so, call core tmt rdy.
4133 * @param cls Closure (unused)
4134 * @param peer_id Short ID of peer to which initiate traffic.
4137 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4139 struct MeshPeerInfo *peer;
4140 struct GNUNET_PeerIdentity id;
4141 struct MeshPeerQueue *q;
4144 peer = peer_info_get_short(peer_id);
4145 if (NULL != peer->core_transmit)
4148 q = queue_get_next(peer);
4151 /* Might br multicast traffic already sent to this particular peer but
4152 * not to other children in this tunnel.
4153 * This way t->queue_n would be > 0 but the queue of this particular peer
4159 GNUNET_PEER_resolve (peer->id, &id);
4160 peer->core_transmit =
4161 GNUNET_CORE_notify_transmit_ready(core_handle,
4164 GNUNET_TIME_UNIT_FOREVER_REL,
4174 * @brief Allow transmission of FWD traffic on this tunnel
4176 * Check if there is traffic queued towards any children
4177 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4179 * @param t Tunnel on which to unlock FWD traffic.
4182 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4184 if (0 == t->fwd_queue_n)
4187 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4192 * @brief Allow transmission of BCK traffic on this tunnel
4194 * Check if there is traffic queued towards the root of the tree
4195 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4197 * @param t Tunnel on which to unlock BCK traffic.
4200 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4202 if (0 == t->bck_queue_n)
4205 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4210 * Send a message to all peers in this tunnel that the tunnel is no longer
4213 * @param t The tunnel whose peers to notify.
4216 tunnel_send_destroy (struct MeshTunnel *t)
4218 struct GNUNET_MESH_TunnelDestroy msg;
4220 msg.header.size = htons (sizeof (msg));
4221 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4222 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4223 msg.tid = htonl (t->id.tid);
4224 tunnel_send_multicast (t, &msg.header);
4229 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4231 * @param cls Closure (Tunnel which to cancel).
4232 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4235 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4237 struct MeshTunnel *t = cls;
4238 struct MeshPeerInfo *peer_info;
4239 struct MeshPeerQueue *pq;
4240 struct MeshPeerQueue *next;
4242 peer_info = peer_info_get_short (neighbor_id);
4243 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4246 if (pq->tunnel == t)
4248 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4249 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4250 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4252 // Should have been removed on destroy children
4255 queue_destroy (pq, GNUNET_YES);
4258 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4260 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4261 peer_info->core_transmit = NULL;
4266 * Destroy the tunnel and free any allocated resources linked to it.
4268 * @param t the tunnel to destroy
4270 * @return GNUNET_OK on success
4273 tunnel_destroy (struct MeshTunnel *t)
4275 struct MeshClient *c;
4276 struct GNUNET_HashCode hash;
4287 struct GNUNET_PeerIdentity id;
4289 GNUNET_PEER_resolve (t->id.oid, &id);
4290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4291 GNUNET_i2s (&id), t->id.tid);
4293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4297 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4298 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4306 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4308 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4315 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4316 for (i = 0; i < t->nclients; i++)
4320 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4326 for (i = 0; i < t->nignore; i++)
4330 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4337 if (t->nclients > 0)
4340 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4345 GNUNET_free (t->clients);
4346 GNUNET_free (t->clients_fc);
4349 if (NULL != t->peers)
4351 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4353 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4356 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4357 &tunnel_destroy_child,
4359 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4360 t->children_fc = NULL;
4362 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4363 tree_destroy (t->tree);
4365 if (NULL != t->regex_ctx)
4366 regex_cancel_search (t->regex_ctx);
4367 if (NULL != t->dht_get_type)
4368 GNUNET_DHT_get_stop (t->dht_get_type);
4369 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4370 GNUNET_SCHEDULER_cancel (t->timeout_task);
4371 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4372 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4375 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4382 * Create a new tunnel
4384 * @param owner Who is the owner of the tunnel (short ID).
4385 * @param tid Tunnel Number of the tunnel.
4386 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4387 * @param local Tunnel Number for the tunnel, for the client point of view.
4389 * @return A new initialized tunnel. NULL on error.
4391 static struct MeshTunnel *
4392 tunnel_new (GNUNET_PEER_Id owner,
4393 MESH_TunnelNumber tid,
4394 struct MeshClient *client,
4395 MESH_TunnelNumber local)
4397 struct MeshTunnel *t;
4398 struct GNUNET_HashCode hash;
4400 if (n_tunnels >= max_tunnels && NULL == client)
4403 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4406 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4407 t->bck_queue_max = t->fwd_queue_max;
4408 t->tree = tree_new (owner);
4410 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4411 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4412 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4413 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4414 t->local_tid = local;
4415 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4417 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4419 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4421 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4422 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4429 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4436 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4438 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4439 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4443 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4453 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4454 * that are no longer needed, as well as nodes of no longer reachable peers.
4455 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4457 * @param t Tunnel from which to remove the path.
4458 * @param peer Short id of the peer which should be removed.
4461 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4463 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4469 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4470 * client when the client disconnects. If the client is not the owner, the
4471 * owner will get notified if no more clients are in the tunnel and the client
4472 * get removed from the tunnel's list.
4474 * @param cls closure (client that is disconnecting)
4475 * @param key the hash of the local tunnel id (used to access the hashmap)
4476 * @param value the value stored at the key (tunnel to destroy)
4478 * @return GNUNET_OK, keep iterating.
4481 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4483 struct MeshTunnel *t = value;
4484 struct MeshClient *c = cls;
4486 send_client_tunnel_disconnect(t, c);
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4490 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4491 tunnel_delete_client(t, c);
4492 client_delete_tunnel(c, t);
4495 tunnel_send_destroy(t);
4497 t->destroy = GNUNET_YES;
4504 * Timeout function, destroys tunnel if called
4506 * @param cls Closure (tunnel to destroy).
4507 * @param tc TaskContext
4510 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4512 struct MeshTunnel *t = cls;
4513 struct GNUNET_PeerIdentity id;
4515 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4516 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4518 GNUNET_PEER_resolve(t->id.oid, &id);
4519 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4520 "Tunnel %s [%X] timed out. Destroying.\n",
4521 GNUNET_i2s(&id), t->id.tid);
4522 send_clients_tunnel_destroy (t);
4527 * Resets the tunnel timeout. Starts it if no timeout was running.
4529 * @param t Tunnel whose timeout to reset.
4531 * TODO use heap to improve efficiency of scheduler.
4534 tunnel_reset_timeout (struct MeshTunnel *t)
4536 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4537 GNUNET_SCHEDULER_cancel (t->timeout_task);
4539 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4540 (refresh_path_time, 4), &tunnel_timeout, t);
4544 /******************************************************************************/
4545 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4546 /******************************************************************************/
4549 * Function to send a create path packet to a peer.
4551 * @param cls closure
4552 * @param size number of bytes available in buf
4553 * @param buf where the callee should write the message
4554 * @return number of bytes written to buf
4557 send_core_path_create (void *cls, size_t size, void *buf)
4559 struct MeshPathInfo *info = cls;
4560 struct GNUNET_MESH_ManipulatePath *msg;
4561 struct GNUNET_PeerIdentity *peer_ptr;
4562 struct MeshTunnel *t = info->t;
4563 struct MeshPeerPath *p = info->path;
4568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4570 sizeof (struct GNUNET_MESH_ManipulatePath) +
4571 p->length * sizeof (struct GNUNET_PeerIdentity);
4573 if (size < size_needed || NULL == buf)
4578 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4579 msg->header.size = htons (size_needed);
4580 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4581 msg->tid = ntohl (t->id.tid);
4584 if (GNUNET_YES == t->speed_min)
4585 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4586 if (GNUNET_YES == t->nobuffer)
4587 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4588 msg->opt = htonl(opt);
4591 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4592 for (i = 0; i < p->length; i++)
4594 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4601 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4607 * Fill the core buffer
4609 * @param cls closure (data itself)
4610 * @param size number of bytes available in buf
4611 * @param buf where the callee should write the message
4613 * @return number of bytes written to buf
4616 send_core_data_multicast (void *cls, size_t size, void *buf)
4618 struct MeshTransmissionDescriptor *info = cls;
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4622 GNUNET_assert (NULL != info);
4623 GNUNET_assert (NULL != info->peer);
4624 total_size = info->mesh_data->data_len;
4625 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4627 if (total_size > size)
4632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4633 memcpy (buf, info->mesh_data->data, total_size);
4636 struct GNUNET_MESH_Multicast *mc;
4637 struct GNUNET_MessageHeader *mh;
4640 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4642 mc = (struct GNUNET_MESH_Multicast *) mh;
4643 mh = (struct GNUNET_MessageHeader *) &mc[1];
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4645 " multicast, payload type %s\n",
4646 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4648 " multicast, payload size %u\n", ntohs (mh->size));
4652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4653 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4657 data_descriptor_decrement_rc (info->mesh_data);
4658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4666 * Creates a path ack message in buf and frees all unused resources.
4668 * @param cls closure (MeshTransmissionDescriptor)
4669 * @param size number of bytes available in buf
4670 * @param buf where the callee should write the message
4671 * @return number of bytes written to buf
4674 send_core_path_ack (void *cls, size_t size, void *buf)
4676 struct MeshTransmissionDescriptor *info = cls;
4677 struct GNUNET_MESH_PathACK *msg = buf;
4679 GNUNET_assert (NULL != info);
4680 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4685 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4686 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4687 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4688 msg->tid = htonl (info->origin->tid);
4689 msg->peer_id = my_full_id;
4692 /* TODO add signature */
4694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4695 return sizeof (struct GNUNET_MESH_PathACK);
4700 * Free a transmission that was already queued with all resources
4701 * associated to the request.
4703 * @param queue Queue handler to cancel.
4704 * @param clear_cls Is it necessary to free associated cls?
4707 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4709 struct MeshTransmissionDescriptor *dd;
4710 struct MeshPathInfo *path_info;
4711 struct MeshTunnelFlowControlInfo *fcinfo;
4712 struct GNUNET_PeerIdentity id;
4716 if (GNUNET_YES == clear_cls)
4718 switch (queue->type)
4720 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4721 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4722 GNUNET_assert (GNUNET_YES == queue->tunnel->destroy);
4723 /* FIXME: don't cancel, send and destroy tunnel in queue_send */
4725 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4726 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4727 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4728 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4729 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4731 " prebuilt message\n");
4732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4734 GNUNET_MESH_DEBUG_M2S(queue->type));
4736 data_descriptor_decrement_rc (dd->mesh_data);
4738 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4740 path_info = queue->cls;
4741 path_destroy (path_info->path);
4745 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4746 " type %s unknown!\n",
4747 GNUNET_MESH_DEBUG_M2S(queue->type));
4749 GNUNET_free_non_null (queue->cls);
4751 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4752 queue->peer->queue_tail,
4755 /* Delete from child_fc in the appropiate tunnel */
4756 max = queue->tunnel->fwd_queue_max;
4757 GNUNET_PEER_resolve (queue->peer->id, &id);
4758 fcinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4761 GNUNET_assert (NULL != fcinfo->peer);
4762 for (i = 0; i < fcinfo->send_buffer_n; i++)
4765 i2 = (fcinfo->send_buffer_start + i) % max;
4766 if (fcinfo->send_buffer[i2] == queue)
4768 /* Found corresponding entry in the send_buffer. Move all others back. */
4773 for (j = i, j2 = 0, j3 = 0; j < fcinfo->send_buffer_n - 1; j++)
4775 j2 = (fcinfo->send_buffer_start + j) % max;
4776 j3 = (fcinfo->send_buffer_start + j + 1) % max;
4777 fcinfo->send_buffer[j2] = fcinfo->send_buffer[j3];
4780 fcinfo->send_buffer[j3] = NULL;
4781 fcinfo->send_buffer_n--;
4786 GNUNET_free (queue);
4791 * @brief Get the next transmittable message from the queue.
4793 * This will be the head, except in the case of being a data packet
4794 * not allowed by the destination peer.
4796 * @param peer Destination peer.
4798 * @return The next viable MeshPeerQueue element to send to that peer.
4799 * NULL when there are no transmittable messages.
4801 struct MeshPeerQueue *
4802 queue_get_next (const struct MeshPeerInfo *peer)
4804 struct MeshPeerQueue *q;
4805 struct MeshTunnel *t;
4806 struct MeshTransmissionDescriptor *info;
4807 struct MeshTunnelFlowControlInfo *fcinfo;
4808 struct GNUNET_MESH_Unicast *ucast;
4809 struct GNUNET_MESH_ToOrigin *to_orig;
4810 struct GNUNET_MESH_Multicast *mcast;
4811 struct GNUNET_PeerIdentity id;
4815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4816 for (q = peer->queue_head; NULL != q; q = q->next)
4820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4822 GNUNET_MESH_DEBUG_M2S(q->type));
4825 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4826 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4827 pid = ntohl (ucast->pid);
4828 GNUNET_PEER_resolve (info->peer->id, &id);
4829 fcinfo = tunnel_get_neighbor_fc (t, &id);
4830 ack = fcinfo->fwd_ack;
4832 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4833 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4834 pid = ntohl (to_orig->pid);
4837 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4838 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4839 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4841 // Not a multicast payload: multicast control traffic (destroy, etc)
4844 pid = ntohl (mcast->pid);
4845 GNUNET_PEER_resolve (info->peer->id, &id);
4846 fcinfo = tunnel_get_neighbor_fc (t, &id);
4847 ack = fcinfo->fwd_ack;
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4855 "********* ACK: %u, PID: %u\n",
4857 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4866 "********* NEXT!\n");
4869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4870 "********* nothing found\n");
4876 * Core callback to write a queued packet to core buffer
4878 * @param cls Closure (peer info).
4879 * @param size Number of bytes available in buf.
4880 * @param buf Where the to write the message.
4882 * @return number of bytes written to buf
4885 queue_send (void *cls, size_t size, void *buf)
4887 struct MeshPeerInfo *peer = cls;
4888 struct GNUNET_MessageHeader *msg;
4889 struct MeshPeerQueue *queue;
4890 struct MeshTunnel *t;
4891 struct MeshTunnelFlowControlInfo *fcinfo;
4892 struct GNUNET_PeerIdentity dst_id;
4895 peer->core_transmit = NULL;
4898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4899 queue = queue_get_next (peer);
4901 /* Queue has no internal mesh traffic nor sendable payload */
4904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4905 if (NULL == peer->queue_head)
4906 GNUNET_break (0); // Should've been canceled
4909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4911 GNUNET_PEER_resolve (peer->id, &dst_id);
4912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4913 "********* towards %s\n",
4914 GNUNET_i2s(&dst_id));
4915 /* Check if buffer size is enough for the message */
4916 if (queue->size > size)
4918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4919 "********* not enough room, reissue\n");
4920 peer->core_transmit =
4921 GNUNET_CORE_notify_transmit_ready (core_handle,
4924 GNUNET_TIME_UNIT_FOREVER_REL,
4931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4934 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4938 "********* unicast: t->q (%u/%u)\n",
4939 t->fwd_queue_n, t->fwd_queue_max);
4941 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4948 switch (queue->type)
4951 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4952 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4953 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4954 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4955 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4957 "********* raw: %s\n",
4958 GNUNET_MESH_DEBUG_M2S (queue->type));
4960 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4961 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4962 data_size = send_core_data_raw (queue->cls, size, buf);
4963 msg = (struct GNUNET_MessageHeader *) buf;
4964 switch (ntohs (msg->type)) // Type of preconstructed message
4966 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4967 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4969 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4970 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4976 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4979 struct MeshTransmissionDescriptor *info = queue->cls;
4981 if ((1 == info->mesh_data->reference_counter
4982 && GNUNET_YES == t->speed_min)
4984 (info->mesh_data->total_out == info->mesh_data->reference_counter
4985 && GNUNET_NO == t->speed_min))
4987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4988 "********* considered sent\n");
4992 data_size = send_core_data_multicast(queue->cls, size, buf);
4993 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4995 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4997 data_size = send_core_path_create (queue->cls, size, buf);
4999 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5001 data_size = send_core_path_ack (queue->cls, size, buf);
5003 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5005 data_size = send_core_data_multicast (queue->cls, size, buf);
5009 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5010 "********* type unknown: %u\n",
5014 switch (queue->type)
5016 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5017 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5018 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5019 fcinfo = tunnel_get_neighbor_fc (t, &dst_id);
5020 if (fcinfo->send_buffer[fcinfo->send_buffer_start] != queue)
5023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5024 "at pos %u (%p) != %p\n",
5025 fcinfo->send_buffer_start,
5026 fcinfo->send_buffer[fcinfo->send_buffer_start],
5029 if (fcinfo->send_buffer_n > 0)
5031 fcinfo->send_buffer[fcinfo->send_buffer_start] = NULL;
5032 fcinfo->send_buffer_n--;
5033 fcinfo->send_buffer_start++;
5034 fcinfo->send_buffer_start %= t->fwd_queue_max;
5045 /* Free queue, but cls was freed by send_core_* */
5046 queue_destroy (queue, GNUNET_NO);
5048 if (GNUNET_YES == t->destroy)
5050 // FIXME fc tunnel destroy all pending traffic? wait for it?
5051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5055 /* If more data in queue, send next */
5056 queue = queue_get_next(peer);
5059 struct GNUNET_PeerIdentity id;
5061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5062 GNUNET_PEER_resolve (peer->id, &id);
5063 peer->core_transmit =
5064 GNUNET_CORE_notify_transmit_ready(core_handle,
5067 GNUNET_TIME_UNIT_FOREVER_REL,
5075 if (NULL != peer->queue_head)
5077 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5078 "********* %s stalled\n",
5079 GNUNET_i2s(&my_full_id));
5081 fcinfo = tunnel_get_neighbor_fc (t, &dst_id);
5082 fcinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
5083 &tunnel_poll, fcinfo);
5086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5092 * @brief Queue and pass message to core when possible.
5094 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5095 * and accounts for it. In case the queue is full, the message is dropped and
5098 * Otherwise, message is treated as internal and allowed to go regardless of
5101 * @param cls Closure (@c type dependant). It will be used by queue_send to
5102 * build the message to be sent if not already prebuilt.
5103 * @param type Type of the message, 0 for a raw message.
5104 * @param size Size of the message.
5105 * @param dst Neighbor to send message to.
5106 * @param t Tunnel this message belongs to.
5109 queue_add (void *cls, uint16_t type, size_t size,
5110 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5112 struct MeshPeerQueue *queue;
5113 struct MeshTunnelFlowControlInfo *fcinfo;
5114 struct GNUNET_PeerIdentity id;
5120 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5121 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5123 n = &t->fwd_queue_n;
5124 max = &t->fwd_queue_max;
5126 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5128 n = &t->bck_queue_n;
5129 max = &t->bck_queue_max;
5135 struct MeshTransmissionDescriptor *td = cls;
5136 struct GNUNET_MESH_ToOrigin *to;
5138 to = td->mesh_data->data;
5140 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5141 "bck pid %u, bck ack %u, msg pid %u\n",
5142 t->bck_pid, t->bck_ack, ntohl(to->pid));
5143 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
5145 return; // Drop message
5149 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5155 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5156 GNUNET_PEER_resolve (dst->id, &id);
5157 if (NULL == dst->core_transmit)
5159 dst->core_transmit =
5160 GNUNET_CORE_notify_transmit_ready (core_handle,
5163 GNUNET_TIME_UNIT_FOREVER_REL,
5169 if (NULL == n) // Is this internal mesh traffic?
5172 // It's payload, keep track of buffer per peer.
5173 fcinfo = tunnel_get_neighbor_fc (t, &id);
5174 i = (fcinfo->send_buffer_start + fcinfo->send_buffer_n) % t->fwd_queue_max;
5175 if (NULL != fcinfo->send_buffer[i])
5177 GNUNET_break (fcinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5178 queue_destroy (fcinfo->send_buffer[fcinfo->send_buffer_start], GNUNET_YES);
5179 fcinfo->send_buffer_start++;
5180 fcinfo->send_buffer_start %= t->fwd_queue_max;
5184 fcinfo->send_buffer_n++;
5186 fcinfo->send_buffer[i] = queue;
5187 if (fcinfo->send_buffer_n > t->fwd_queue_max)
5190 fcinfo->send_buffer_n = t->fwd_queue_max;
5195 /******************************************************************************/
5196 /******************** MESH NETWORK HANDLERS **************************/
5197 /******************************************************************************/
5201 * Core handler for path creation
5203 * @param cls closure
5204 * @param message message
5205 * @param peer peer identity this notification is about
5206 * @param atsi performance data
5207 * @param atsi_count number of records in 'atsi'
5209 * @return GNUNET_OK to keep the connection open,
5210 * GNUNET_SYSERR to close it (signal serious error)
5213 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5214 const struct GNUNET_MessageHeader *message,
5215 const struct GNUNET_ATS_Information *atsi,
5216 unsigned int atsi_count)
5218 unsigned int own_pos;
5221 MESH_TunnelNumber tid;
5222 struct GNUNET_MESH_ManipulatePath *msg;
5223 struct GNUNET_PeerIdentity *pi;
5224 struct GNUNET_HashCode hash;
5225 struct MeshPeerPath *path;
5226 struct MeshPeerInfo *dest_peer_info;
5227 struct MeshPeerInfo *orig_peer_info;
5228 struct MeshTunnel *t;
5230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5231 "Received a path create msg [%s]\n",
5232 GNUNET_i2s (&my_full_id));
5233 size = ntohs (message->size);
5234 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5236 GNUNET_break_op (0);
5240 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5241 if (size % sizeof (struct GNUNET_PeerIdentity))
5243 GNUNET_break_op (0);
5246 size /= sizeof (struct GNUNET_PeerIdentity);
5249 GNUNET_break_op (0);
5252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5253 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5255 tid = ntohl (msg->tid);
5256 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5258 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5259 t = tunnel_get (pi, tid);
5260 if (NULL == t) // FIXME only for INCOMING tunnels?
5264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5265 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5268 // FIXME notify failure
5271 opt = ntohl (msg->opt);
5272 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5273 GNUNET_YES : GNUNET_NO;
5274 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5276 t->nobuffer = GNUNET_YES;
5277 t->last_fwd_ack = t->fwd_pid + 1;
5279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5280 " speed_min: %d, nobuffer:%d\n",
5281 t->speed_min, t->nobuffer);
5283 if (GNUNET_YES == t->nobuffer)
5285 t->bck_queue_max = 1;
5286 t->fwd_queue_max = 1;
5289 // FIXME only assign a local tid if a local client is interested (on demand)
5290 while (NULL != tunnel_get_incoming (next_local_tid))
5291 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5292 t->local_tid_dest = next_local_tid++;
5293 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5296 tunnel_reset_timeout (t);
5297 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5299 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5308 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5309 if (NULL == dest_peer_info)
5311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5312 " Creating PeerInfo for destination.\n");
5313 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5314 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5315 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5317 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5319 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5320 if (NULL == orig_peer_info)
5322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5323 " Creating PeerInfo for origin.\n");
5324 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5325 orig_peer_info->id = GNUNET_PEER_intern (pi);
5326 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5327 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5330 path = path_new (size);
5332 for (i = 0; i < size; i++)
5334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5335 GNUNET_i2s (&pi[i]));
5336 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5337 if (path->peers[i] == myid)
5340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5343 /* cannot be self, must be 'not found' */
5344 /* create path: self not found in path through self */
5345 GNUNET_break_op (0);
5346 path_destroy (path);
5350 path_add_to_peers (path, GNUNET_NO);
5351 tunnel_add_path (t, path, own_pos);
5352 if (own_pos == size - 1)
5354 /* It is for us! Send ack. */
5355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5356 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5357 if (NULL == t->peers)
5359 /* New tunnel! Notify clients on first payload message. */
5360 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5362 GNUNET_break (GNUNET_SYSERR !=
5363 GNUNET_CONTAINER_multihashmap_put (t->peers,
5364 &my_full_id.hashPubKey,
5367 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5372 struct MeshPeerPath *path2;
5374 /* It's for somebody else! Retransmit. */
5375 path2 = path_duplicate (path);
5376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5377 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5378 path2 = path_duplicate (path);
5379 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5380 send_create_path (dest_peer_info, path, t);
5387 * Core handler for path destruction
5389 * @param cls closure
5390 * @param message message
5391 * @param peer peer identity this notification is about
5392 * @param atsi performance data
5393 * @param atsi_count number of records in 'atsi'
5395 * @return GNUNET_OK to keep the connection open,
5396 * GNUNET_SYSERR to close it (signal serious error)
5399 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5400 const struct GNUNET_MessageHeader *message,
5401 const struct GNUNET_ATS_Information *atsi,
5402 unsigned int atsi_count)
5404 struct GNUNET_MESH_ManipulatePath *msg;
5405 struct GNUNET_PeerIdentity *pi;
5406 struct MeshPeerPath *path;
5407 struct MeshTunnel *t;
5408 unsigned int own_pos;
5412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5413 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5414 size = ntohs (message->size);
5415 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5417 GNUNET_break_op (0);
5421 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5422 if (size % sizeof (struct GNUNET_PeerIdentity))
5424 GNUNET_break_op (0);
5427 size /= sizeof (struct GNUNET_PeerIdentity);
5430 GNUNET_break_op (0);
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5435 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5436 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5438 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5440 t = tunnel_get (pi, ntohl (msg->tid));
5443 /* TODO notify back: we don't know this tunnel */
5444 GNUNET_break_op (0);
5447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5448 path = path_new (size);
5450 for (i = 0; i < size; i++)
5452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5453 GNUNET_i2s (&pi[i]));
5454 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5455 if (path->peers[i] == myid)
5458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5459 if (own_pos < path->length - 1)
5460 send_prebuilt_message (message, &pi[own_pos + 1], t);
5462 send_client_tunnel_disconnect(t, NULL);
5464 tunnel_delete_peer (t, path->peers[path->length - 1]);
5465 path_destroy (path);
5471 * Core handler for notifications of broken paths
5473 * @param cls closure
5474 * @param message message
5475 * @param peer peer identity this notification is about
5476 * @param atsi performance data
5477 * @param atsi_count number of records in 'atsi'
5479 * @return GNUNET_OK to keep the connection open,
5480 * GNUNET_SYSERR to close it (signal serious error)
5483 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5484 const struct GNUNET_MessageHeader *message,
5485 const struct GNUNET_ATS_Information *atsi,
5486 unsigned int atsi_count)
5488 struct GNUNET_MESH_PathBroken *msg;
5489 struct MeshTunnel *t;
5491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5492 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5493 msg = (struct GNUNET_MESH_PathBroken *) message;
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5495 GNUNET_i2s (&msg->peer1));
5496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5497 GNUNET_i2s (&msg->peer2));
5498 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5501 GNUNET_break_op (0);
5504 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5505 GNUNET_PEER_search (&msg->peer2));
5512 * Core handler for tunnel destruction
5514 * @param cls closure
5515 * @param message message
5516 * @param peer peer identity this notification is about
5517 * @param atsi performance data
5518 * @param atsi_count number of records in 'atsi'
5520 * @return GNUNET_OK to keep the connection open,
5521 * GNUNET_SYSERR to close it (signal serious error)
5524 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5525 const struct GNUNET_MessageHeader *message,
5526 const struct GNUNET_ATS_Information *atsi,
5527 unsigned int atsi_count)
5529 struct GNUNET_MESH_TunnelDestroy *msg;
5530 struct MeshTunnel *t;
5532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5533 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5534 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5536 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5537 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5540 /* Probably already got the message from another path,
5541 * destroyed the tunnel and retransmitted to children.
5544 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5547 if (t->id.oid == myid)
5549 GNUNET_break_op (0);
5552 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5554 /* Tunnel was incoming, notify clients */
5555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5556 t->local_tid, t->local_tid_dest);
5557 send_clients_tunnel_destroy (t);
5559 tunnel_send_destroy (t);
5560 t->destroy = GNUNET_YES;
5561 // TODO: add timeout to destroy the tunnel anyway
5567 * Core handler for mesh network traffic going from the origin to a peer
5569 * @param cls closure
5570 * @param peer peer identity this notification is about
5571 * @param message message
5572 * @param atsi performance data
5573 * @param atsi_count number of records in 'atsi'
5574 * @return GNUNET_OK to keep the connection open,
5575 * GNUNET_SYSERR to close it (signal serious error)
5578 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5579 const struct GNUNET_MessageHeader *message,
5580 const struct GNUNET_ATS_Information *atsi,
5581 unsigned int atsi_count)
5583 struct GNUNET_MESH_Unicast *msg;
5584 struct GNUNET_PeerIdentity *neighbor;
5585 struct MeshTunnelFlowControlInfo *fcinfo;
5586 struct MeshTunnel *t;
5587 GNUNET_PEER_Id dest_id;
5592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5595 size = ntohs (message->size);
5597 sizeof (struct GNUNET_MESH_Unicast) +
5598 sizeof (struct GNUNET_MessageHeader))
5603 msg = (struct GNUNET_MESH_Unicast *) message;
5604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5605 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5607 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5610 /* TODO notify back: we don't know this tunnel */
5611 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5612 GNUNET_break_op (0);
5615 pid = ntohl (msg->pid);
5616 if (t->fwd_pid == pid)
5618 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5620 " Already seen pid %u, DROPPING!\n", pid);
5625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5626 " pid %u not seen yet, forwarding\n", pid);
5629 t->skip += (pid - t->fwd_pid) - 1;
5632 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5634 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5635 GNUNET_break_op (0);
5636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5637 "Received PID %u, ACK %u\n",
5638 pid, t->last_fwd_ack);
5642 tunnel_reset_timeout (t);
5643 dest_id = GNUNET_PEER_search (&msg->destination);
5644 if (dest_id == myid)
5646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5647 " it's for us! sending to clients...\n");
5648 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5649 send_subscribed_clients (message, &msg[1].header, t);
5650 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5653 ttl = ntohl (msg->ttl);
5654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5657 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5659 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5663 " not for us, retransmitting...\n");
5665 neighbor = tree_get_first_hop (t->tree, dest_id);
5666 fcinfo = tunnel_get_neighbor_fc (t, neighbor);
5667 fcinfo->fwd_pid = pid;
5668 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5671 if (GNUNET_YES == t->nobuffer &&
5672 GNUNET_YES == GMC_is_pid_bigger (pid, fcinfo->fwd_ack))
5674 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5675 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, fcinfo->fwd_ack);
5676 GNUNET_break_op (0);
5679 send_prebuilt_message (message, neighbor, t);
5680 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5686 * Core handler for mesh network traffic going from the origin to all peers
5688 * @param cls closure
5689 * @param message message
5690 * @param peer peer identity this notification is about
5691 * @param atsi performance data
5692 * @param atsi_count number of records in 'atsi'
5693 * @return GNUNET_OK to keep the connection open,
5694 * GNUNET_SYSERR to close it (signal serious error)
5696 * TODO: Check who we got this from, to validate route.
5699 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5700 const struct GNUNET_MessageHeader *message,
5701 const struct GNUNET_ATS_Information *atsi,
5702 unsigned int atsi_count)
5704 struct GNUNET_MESH_Multicast *msg;
5705 struct MeshTunnel *t;
5709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5711 size = ntohs (message->size);
5712 if (sizeof (struct GNUNET_MESH_Multicast) +
5713 sizeof (struct GNUNET_MessageHeader) > size)
5715 GNUNET_break_op (0);
5718 msg = (struct GNUNET_MESH_Multicast *) message;
5719 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5723 /* TODO notify that we dont know that tunnel */
5724 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5725 GNUNET_break_op (0);
5728 pid = ntohl (msg->pid);
5729 if (t->fwd_pid == pid)
5731 /* already seen this packet, drop */
5732 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5734 " Already seen pid %u, DROPPING!\n", pid);
5735 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5741 " pid %u not seen yet, forwarding\n", pid);
5743 t->skip += (pid - t->fwd_pid) - 1;
5745 tunnel_reset_timeout (t);
5747 /* Transmit to locally interested clients */
5748 if (NULL != t->peers &&
5749 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5751 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5752 send_subscribed_clients (message, &msg[1].header, t);
5753 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5756 if (ntohl (msg->ttl) == 0)
5758 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5760 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5763 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5764 tunnel_send_multicast (t, message);
5770 * Core handler for mesh network traffic toward the owner of a tunnel
5772 * @param cls closure
5773 * @param message message
5774 * @param peer peer identity this notification is about
5775 * @param atsi performance data
5776 * @param atsi_count number of records in 'atsi'
5778 * @return GNUNET_OK to keep the connection open,
5779 * GNUNET_SYSERR to close it (signal serious error)
5782 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5783 const struct GNUNET_MessageHeader *message,
5784 const struct GNUNET_ATS_Information *atsi,
5785 unsigned int atsi_count)
5787 struct GNUNET_MESH_ToOrigin *msg;
5788 struct GNUNET_PeerIdentity id;
5789 struct MeshPeerInfo *peer_info;
5790 struct MeshTunnel *t;
5791 struct MeshTunnelFlowControlInfo *fcinfo;
5795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5797 size = ntohs (message->size);
5798 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5799 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5801 GNUNET_break_op (0);
5804 msg = (struct GNUNET_MESH_ToOrigin *) message;
5805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5806 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5807 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5808 pid = ntohl (msg->pid);
5812 /* TODO notify that we dont know this tunnel (whom)? */
5813 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5814 GNUNET_break_op (0);
5815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5816 "Received to_origin with PID %u on unknown tunnel\n",
5821 fcinfo = tunnel_get_neighbor_fc (t, peer);
5828 if (fcinfo->bck_pid == pid)
5830 /* already seen this packet, drop */
5831 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5833 " Already seen pid %u, DROPPING!\n", pid);
5834 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5839 " pid %u not seen yet, forwarding\n", pid);
5840 fcinfo->bck_pid = pid;
5842 if (NULL != t->owner)
5845 struct GNUNET_MESH_ToOrigin *copy;
5847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5848 " it's for us! sending to clients...\n");
5849 /* TODO signature verification */
5850 memcpy (cbuf, message, size);
5851 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5852 copy->tid = htonl (t->local_tid);
5854 copy->pid = htonl (t->bck_pid);
5855 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5856 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5857 ©->header, GNUNET_NO);
5858 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5862 " not for us, retransmitting...\n");
5864 peer_info = peer_info_get (&msg->oid);
5865 if (NULL == peer_info)
5867 /* unknown origin of tunnel */
5871 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5872 send_prebuilt_message (message, &id, t);
5873 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5880 * Core handler for mesh network traffic point-to-point acks.
5882 * @param cls closure
5883 * @param message message
5884 * @param peer peer identity this notification is about
5885 * @param atsi performance data
5886 * @param atsi_count number of records in 'atsi'
5888 * @return GNUNET_OK to keep the connection open,
5889 * GNUNET_SYSERR to close it (signal serious error)
5892 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5893 const struct GNUNET_MessageHeader *message,
5894 const struct GNUNET_ATS_Information *atsi,
5895 unsigned int atsi_count)
5897 struct GNUNET_MESH_ACK *msg;
5898 struct MeshTunnel *t;
5901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5903 msg = (struct GNUNET_MESH_ACK *) message;
5905 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5909 /* TODO notify that we dont know this tunnel (whom)? */
5910 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5911 GNUNET_break_op (0);
5914 ack = ntohl (msg->pid);
5915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5917 /* Is this a forward or backward ACK? */
5918 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5920 struct MeshTunnelFlowControlInfo *fcinfo;
5923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5924 fcinfo = tunnel_get_neighbor_fc (t, peer);
5925 fcinfo->fwd_ack = ack;
5926 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5927 tunnel_unlock_fwd_queues (t);
5931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5933 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5934 tunnel_unlock_bck_queue (t);
5941 * Core handler for mesh network traffic point-to-point ack polls.
5943 * @param cls closure
5944 * @param message message
5945 * @param peer peer identity this notification is about
5946 * @param atsi performance data
5947 * @param atsi_count number of records in 'atsi'
5949 * @return GNUNET_OK to keep the connection open,
5950 * GNUNET_SYSERR to close it (signal serious error)
5953 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5954 const struct GNUNET_MessageHeader *message,
5955 const struct GNUNET_ATS_Information *atsi,
5956 unsigned int atsi_count)
5958 struct GNUNET_MESH_Poll *msg;
5959 struct MeshTunnel *t;
5961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5964 msg = (struct GNUNET_MESH_Poll *) message;
5966 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5970 /* TODO notify that we dont know this tunnel (whom)? */
5971 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5972 GNUNET_break_op (0);
5976 /* Is this a forward or backward ACK? */
5977 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5979 struct MeshTunnelFlowControlInfo *fcinfo;
5981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5982 fcinfo = tunnel_get_neighbor_fc (t, peer);
5983 fcinfo->bck_ack = fcinfo->fwd_pid; // mark as ready to send
5984 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5989 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5997 * Core handler for path ACKs
5999 * @param cls closure
6000 * @param message message
6001 * @param peer peer identity this notification is about
6002 * @param atsi performance data
6003 * @param atsi_count number of records in 'atsi'
6005 * @return GNUNET_OK to keep the connection open,
6006 * GNUNET_SYSERR to close it (signal serious error)
6009 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6010 const struct GNUNET_MessageHeader *message,
6011 const struct GNUNET_ATS_Information *atsi,
6012 unsigned int atsi_count)
6014 struct GNUNET_MESH_PathACK *msg;
6015 struct GNUNET_PeerIdentity id;
6016 struct MeshPeerInfo *peer_info;
6017 struct MeshPeerPath *p;
6018 struct MeshTunnel *t;
6020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6021 GNUNET_i2s (&my_full_id));
6022 msg = (struct GNUNET_MESH_PathACK *) message;
6023 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6026 /* TODO notify that we don't know the tunnel */
6027 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6029 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6033 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6035 peer_info = peer_info_get (&msg->peer_id);
6036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6037 GNUNET_i2s (&msg->peer_id));
6038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6041 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6044 "connect_by_string completed, stopping search\n");
6045 regex_cancel_search (t->regex_ctx);
6046 t->regex_ctx = NULL;
6049 /* Add paths to peers? */
6050 p = tree_get_path_to_peer (t->tree, peer_info->id);
6053 path_add_to_peers (p, GNUNET_YES);
6061 /* Message for us? */
6062 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6065 if (NULL == t->owner)
6067 GNUNET_break_op (0);
6070 if (NULL != t->dht_get_type)
6072 GNUNET_DHT_get_stop (t->dht_get_type);
6073 t->dht_get_type = NULL;
6075 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6077 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6078 send_client_peer_connected (t, peer_info->id);
6083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6084 " not for us, retransmitting...\n");
6085 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6086 peer_info = peer_info_get (&msg->oid);
6087 if (NULL == peer_info)
6089 /* If we know the tunnel, we should DEFINITELY know the peer */
6093 send_prebuilt_message (message, &id, t);
6099 * Core handler for mesh keepalives.
6101 * @param cls closure
6102 * @param message message
6103 * @param peer peer identity this notification is about
6104 * @param atsi performance data
6105 * @param atsi_count number of records in 'atsi'
6106 * @return GNUNET_OK to keep the connection open,
6107 * GNUNET_SYSERR to close it (signal serious error)
6109 * TODO: Check who we got this from, to validate route.
6112 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6113 const struct GNUNET_MessageHeader *message,
6114 const struct GNUNET_ATS_Information *atsi,
6115 unsigned int atsi_count)
6117 struct GNUNET_MESH_TunnelKeepAlive *msg;
6118 struct MeshTunnel *t;
6120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6123 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6124 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6128 /* TODO notify that we dont know that tunnel */
6129 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6130 GNUNET_break_op (0);
6134 tunnel_reset_timeout (t);
6136 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6137 tunnel_send_multicast (t, message);
6144 * Functions to handle messages from core
6146 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6147 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6148 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6149 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6150 sizeof (struct GNUNET_MESH_PathBroken)},
6151 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6152 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6153 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6154 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6155 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6156 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6157 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6158 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6159 sizeof (struct GNUNET_MESH_ACK)},
6160 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6161 sizeof (struct GNUNET_MESH_Poll)},
6162 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6163 sizeof (struct GNUNET_MESH_PathACK)},
6169 /******************************************************************************/
6170 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6171 /******************************************************************************/
6174 * deregister_app: iterator for removing each application registered by a client
6176 * @param cls closure
6177 * @param key the hash of the application id (used to access the hashmap)
6178 * @param value the value stored at the key (client)
6180 * @return GNUNET_OK on success
6183 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6185 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6186 GNUNET_break (GNUNET_YES ==
6187 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6193 * notify_client_connection_failure: notify a client that the connection to the
6194 * requested remote peer is not possible (for instance, no route found)
6195 * Function called when the socket is ready to queue more data. "buf" will be
6196 * NULL and "size" zero if the socket was closed for writing in the meantime.
6198 * @param cls closure
6199 * @param size number of bytes available in buf
6200 * @param buf where the callee should write the message
6201 * @return number of bytes written to buf
6204 notify_client_connection_failure (void *cls, size_t size, void *buf)
6207 struct MeshPeerInfo *peer_info;
6208 struct GNUNET_MESH_PeerControl *msg;
6209 struct GNUNET_PeerIdentity id;
6211 if (0 == size && NULL == buf)
6213 // TODO retry? cancel?
6217 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6218 peer_info = (struct MeshPeerInfo *) cls;
6219 msg = (struct GNUNET_MESH_PeerControl *) buf;
6220 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6221 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6222 // msg->tunnel_id = htonl(peer_info->t->tid);
6223 GNUNET_PEER_resolve (peer_info->id, &id);
6224 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6232 * Send keepalive packets for a peer
6234 * @param cls Closure (tunnel for which to send the keepalive).
6235 * @param tc Notification context.
6238 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6240 struct MeshTunnel *t = cls;
6241 struct GNUNET_MESH_TunnelKeepAlive *msg;
6242 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6245 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6246 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6252 "sending keepalive for tunnel %d\n", t->id.tid);
6254 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6255 msg->header.size = htons (size);
6256 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6257 msg->oid = my_full_id;
6258 msg->tid = htonl (t->id.tid);
6259 tunnel_send_multicast (t, &msg->header);
6261 t->path_refresh_task =
6262 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6263 tunnel_reset_timeout(t);
6268 * Function to process paths received for a new peer addition. The recorded
6269 * paths form the initial tunnel, which can be optimized later.
6270 * Called on each result obtained for the DHT search.
6272 * @param cls closure
6273 * @param exp when will this value expire
6274 * @param key key of the result
6275 * @param get_path path of the get request
6276 * @param get_path_length lenght of get_path
6277 * @param put_path path of the put request
6278 * @param put_path_length length of the put_path
6279 * @param type type of the result
6280 * @param size number of bytes in data
6281 * @param data pointer to the result data
6283 * TODO: re-issue the request after certain time? cancel after X results?
6286 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6287 const struct GNUNET_HashCode * key,
6288 const struct GNUNET_PeerIdentity *get_path,
6289 unsigned int get_path_length,
6290 const struct GNUNET_PeerIdentity *put_path,
6291 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6292 size_t size, const void *data)
6294 struct MeshPathInfo *path_info = cls;
6295 struct MeshPeerPath *p;
6296 struct GNUNET_PeerIdentity pi;
6299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6300 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6303 p = path_build_from_dht (get_path, get_path_length, put_path,
6305 path_add_to_peers (p, GNUNET_NO);
6307 for (i = 0; i < path_info->peer->ntunnels; i++)
6309 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6310 peer_info_connect (path_info->peer, path_info->t);
6318 * Function to process paths received for a new peer addition. The recorded
6319 * paths form the initial tunnel, which can be optimized later.
6320 * Called on each result obtained for the DHT search.
6322 * @param cls closure
6323 * @param exp when will this value expire
6324 * @param key key of the result
6325 * @param get_path path of the get request
6326 * @param get_path_length lenght of get_path
6327 * @param put_path path of the put request
6328 * @param put_path_length length of the put_path
6329 * @param type type of the result
6330 * @param size number of bytes in data
6331 * @param data pointer to the result data
6334 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6335 const struct GNUNET_HashCode * key,
6336 const struct GNUNET_PeerIdentity *get_path,
6337 unsigned int get_path_length,
6338 const struct GNUNET_PeerIdentity *put_path,
6339 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6340 size_t size, const void *data)
6342 const struct PBlock *pb = data;
6343 const struct GNUNET_PeerIdentity *pi = &pb->id;
6344 struct MeshTunnel *t = cls;
6345 struct MeshPeerInfo *peer_info;
6346 struct MeshPeerPath *p;
6348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6349 if (size != sizeof (struct PBlock))
6351 GNUNET_break_op (0);
6354 if (ntohl(pb->type) != t->type)
6356 GNUNET_break_op (0);
6359 GNUNET_assert (NULL != t->owner);
6360 peer_info = peer_info_get (pi);
6361 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6365 p = path_build_from_dht (get_path, get_path_length, put_path,
6367 path_add_to_peers (p, GNUNET_NO);
6369 tunnel_add_peer (t, peer_info);
6370 peer_info_connect (peer_info, t);
6375 * Function to process DHT string to regex matching.
6376 * Called on each result obtained for the DHT search.
6378 * @param cls closure (search context)
6379 * @param exp when will this value expire
6380 * @param key key of the result
6381 * @param get_path path of the get request (not used)
6382 * @param get_path_length lenght of get_path (not used)
6383 * @param put_path path of the put request (not used)
6384 * @param put_path_length length of the put_path (not used)
6385 * @param type type of the result
6386 * @param size number of bytes in data
6387 * @param data pointer to the result data
6390 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6391 const struct GNUNET_HashCode * key,
6392 const struct GNUNET_PeerIdentity *get_path,
6393 unsigned int get_path_length,
6394 const struct GNUNET_PeerIdentity *put_path,
6395 unsigned int put_path_length,
6396 enum GNUNET_BLOCK_Type type,
6397 size_t size, const void *data)
6399 const struct MeshRegexAccept *block = data;
6400 struct MeshRegexSearchContext *ctx = cls;
6401 struct MeshRegexSearchInfo *info = ctx->info;
6402 struct MeshPeerPath *p;
6403 struct MeshPeerInfo *peer_info;
6405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6408 peer_info = peer_info_get(&block->id);
6409 p = path_build_from_dht (get_path, get_path_length, put_path,
6411 path_add_to_peers (p, GNUNET_NO);
6414 tunnel_add_peer (info->t, peer_info);
6415 peer_info_connect (peer_info, info->t);
6416 if (0 == info->peer)
6418 info->peer = peer_info->id;
6422 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6425 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6426 ®ex_connect_timeout,
6434 * Function to process DHT string to regex matching.
6435 * Called on each result obtained for the DHT search.
6437 * @param cls closure (search context)
6438 * @param exp when will this value expire
6439 * @param key key of the result
6440 * @param get_path path of the get request (not used)
6441 * @param get_path_length lenght of get_path (not used)
6442 * @param put_path path of the put request (not used)
6443 * @param put_path_length length of the put_path (not used)
6444 * @param type type of the result
6445 * @param size number of bytes in data
6446 * @param data pointer to the result data
6448 * TODO: re-issue the request after certain time? cancel after X results?
6451 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6452 const struct GNUNET_HashCode * key,
6453 const struct GNUNET_PeerIdentity *get_path,
6454 unsigned int get_path_length,
6455 const struct GNUNET_PeerIdentity *put_path,
6456 unsigned int put_path_length,
6457 enum GNUNET_BLOCK_Type type,
6458 size_t size, const void *data)
6460 const struct MeshRegexBlock *block = data;
6461 struct MeshRegexSearchContext *ctx = cls;
6462 struct MeshRegexSearchInfo *info = ctx->info;
6466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6467 "DHT GET STRING RETURNED RESULTS\n");
6468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6469 " key: %s\n", GNUNET_h2s (key));
6471 copy = GNUNET_malloc (size);
6472 memcpy (copy, data, size);
6473 GNUNET_break (GNUNET_OK ==
6474 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6475 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6476 len = ntohl (block->n_proof);
6478 char proof[len + 1];
6480 memcpy (proof, &block[1], len);
6482 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6484 GNUNET_break_op (0);
6488 len = strlen (info->description);
6489 if (len == ctx->position) // String processed
6491 if (GNUNET_YES == ntohl (block->accepting))
6493 regex_find_path(key, ctx);
6497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6498 // FIXME REGEX this block not successful, wait for more? start timeout?
6503 regex_next_edge (block, size, ctx);
6508 /******************************************************************************/
6509 /********************* MESH LOCAL HANDLES **************************/
6510 /******************************************************************************/
6514 * Handler for client disconnection
6516 * @param cls closure
6517 * @param client identification of the client; NULL
6518 * for the last call when the server is destroyed
6521 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6523 struct MeshClient *c;
6524 struct MeshClient *next;
6527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6536 if (c->handle != client)
6538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6544 GNUNET_SERVER_client_drop (c->handle);
6545 c->shutting_down = GNUNET_YES;
6546 GNUNET_assert (NULL != c->own_tunnels);
6547 GNUNET_assert (NULL != c->incoming_tunnels);
6548 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6549 &tunnel_destroy_iterator, c);
6550 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6551 &tunnel_destroy_iterator, c);
6552 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6553 &tunnel_destroy_iterator, c);
6554 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6555 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6556 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6558 /* deregister clients applications */
6559 if (NULL != c->apps)
6561 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6562 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6564 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6565 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6567 GNUNET_SCHEDULER_cancel (announce_applications_task);
6568 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6570 if (NULL != c->types)
6571 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6572 for (i = 0; i < c->n_regex; i++)
6574 GNUNET_free (c->regexes[i].regex);
6576 GNUNET_free_non_null (c->regexes);
6577 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6578 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6580 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6583 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6592 * Handler for new clients
6594 * @param cls closure
6595 * @param client identification of the client
6596 * @param message the actual message, which includes messages the client wants
6599 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6600 const struct GNUNET_MessageHeader *message)
6602 struct GNUNET_MESH_ClientConnect *cc_msg;
6603 struct MeshClient *c;
6604 GNUNET_MESH_ApplicationType *a;
6611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6612 /* Check data sanity */
6613 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6614 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6615 ntypes = ntohs (cc_msg->types);
6616 napps = ntohs (cc_msg->applications);
6618 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6625 /* Create new client structure */
6626 c = GNUNET_malloc (sizeof (struct MeshClient));
6627 c->id = next_client_id++;
6628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6630 GNUNET_SERVER_client_keep (client);
6631 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6634 GNUNET_MESH_ApplicationType at;
6635 struct GNUNET_HashCode hc;
6637 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6638 for (i = 0; i < napps; i++)
6641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6642 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6643 /* store in clients hashmap */
6644 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6645 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6646 /* store in global hashmap, for announcements */
6647 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6648 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6650 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6651 announce_applications_task =
6652 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6658 struct GNUNET_HashCode hc;
6660 t = (uint16_t *) & a[napps];
6661 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6662 for (i = 0; i < ntypes; i++)
6665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6666 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6668 /* store in clients hashmap */
6669 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6670 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6671 /* store in global hashmap */
6672 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6673 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6677 " client has %u+%u subscriptions\n", napps, ntypes);
6679 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6680 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6681 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6682 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6683 GNUNET_SERVER_notification_context_add (nc, client);
6684 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6686 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6692 * Handler for clients announcing available services by a regular expression.
6694 * @param cls closure
6695 * @param client identification of the client
6696 * @param message the actual message, which includes messages the client wants
6699 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6700 const struct GNUNET_MessageHeader *message)
6702 struct GNUNET_MESH_RegexAnnounce *msg;
6703 struct MeshRegexDescriptor rd;
6704 struct MeshClient *c;
6708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6710 /* Sanity check for client registration */
6711 if (NULL == (c = client_get (client)))
6714 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6719 msg = (struct GNUNET_MESH_RegexAnnounce *) message;
6720 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6721 regex = GNUNET_malloc (len + 1);
6722 memcpy (regex, &msg[1], len);
6725 rd.compression = ntohs (msg->compression_characters);
6726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6729 GNUNET_array_append (c->regexes, c->n_regex, rd);
6730 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6732 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6738 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6744 * Handler for requests of new tunnels
6746 * @param cls closure
6747 * @param client identification of the client
6748 * @param message the actual message
6751 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6752 const struct GNUNET_MessageHeader *message)
6754 struct GNUNET_MESH_TunnelMessage *t_msg;
6755 struct MeshTunnel *t;
6756 struct MeshClient *c;
6757 MESH_TunnelNumber tid;
6759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6761 /* Sanity check for client registration */
6762 if (NULL == (c = client_get (client)))
6765 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6770 /* Message sanity check */
6771 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6774 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6778 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6779 /* Sanity check for tunnel numbering */
6780 tid = ntohl (t_msg->tunnel_id);
6781 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6784 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6787 /* Sanity check for duplicate tunnel IDs */
6788 if (NULL != tunnel_get_by_local_id (c, tid))
6791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6795 while (NULL != tunnel_get_by_pi (myid, next_tid))
6796 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6797 t = tunnel_new (myid, next_tid++, c, tid);
6800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6805 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6807 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6808 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6811 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6817 * Handler for requests of deleting tunnels
6819 * @param cls closure
6820 * @param client identification of the client
6821 * @param message the actual message
6824 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6825 const struct GNUNET_MessageHeader *message)
6827 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6828 struct MeshClient *c;
6829 struct MeshTunnel *t;
6830 MESH_TunnelNumber tid;
6832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6833 "Got a DESTROY TUNNEL from client!\n");
6835 /* Sanity check for client registration */
6836 if (NULL == (c = client_get (client)))
6839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6844 /* Message sanity check */
6845 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6852 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6854 /* Retrieve tunnel */
6855 tid = ntohl (tunnel_msg->tunnel_id);
6856 t = tunnel_get_by_local_id(c, tid);
6859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6861 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6864 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6866 client_ignore_tunnel (c, t);
6868 // TODO: when to destroy incoming tunnel?
6869 if (t->nclients == 0)
6871 GNUNET_assert (GNUNET_YES ==
6872 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6874 GNUNET_assert (GNUNET_YES ==
6875 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6876 &my_full_id.hashPubKey,
6880 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6883 send_client_tunnel_disconnect(t, c);
6884 client_delete_tunnel(c, t);
6886 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6888 tunnel_send_destroy (t);
6889 t->destroy = GNUNET_YES;
6890 // The tunnel will be destroyed when the last message is transmitted.
6891 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6897 * Handler for requests of seeting tunnel's speed.
6899 * @param cls Closure (unused).
6900 * @param client Identification of the client.
6901 * @param message The actual message.
6904 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6905 const struct GNUNET_MessageHeader *message)
6907 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6908 struct MeshClient *c;
6909 struct MeshTunnel *t;
6910 MESH_TunnelNumber tid;
6912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6913 "Got a SPEED request from client!\n");
6915 /* Sanity check for client registration */
6916 if (NULL == (c = client_get (client)))
6919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6925 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6927 /* Retrieve tunnel */
6928 tid = ntohl (tunnel_msg->tunnel_id);
6929 t = tunnel_get_by_local_id(c, tid);
6932 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6934 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6938 switch (ntohs(message->type))
6940 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6941 t->speed_min = GNUNET_YES;
6943 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6944 t->speed_min = GNUNET_NO;
6949 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6954 * Handler for requests of seeting tunnel's buffering policy.
6956 * @param cls Closure (unused).
6957 * @param client Identification of the client.
6958 * @param message The actual message.
6961 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6962 const struct GNUNET_MessageHeader *message)
6964 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6965 struct MeshClient *c;
6966 struct MeshTunnel *t;
6967 MESH_TunnelNumber tid;
6969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6970 "Got a BUFFER request from client!\n");
6972 /* Sanity check for client registration */
6973 if (NULL == (c = client_get (client)))
6976 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6981 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6983 /* Retrieve tunnel */
6984 tid = ntohl (tunnel_msg->tunnel_id);
6985 t = tunnel_get_by_local_id(c, tid);
6988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6990 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6994 switch (ntohs(message->type))
6996 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6997 t->nobuffer = GNUNET_NO;
6999 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7000 t->nobuffer = GNUNET_YES;
7006 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7011 * Handler for connection requests to new peers
7013 * @param cls closure
7014 * @param client identification of the client
7015 * @param message the actual message (PeerControl)
7018 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7019 const struct GNUNET_MessageHeader *message)
7021 struct GNUNET_MESH_PeerControl *peer_msg;
7022 struct MeshPeerInfo *peer_info;
7023 struct MeshClient *c;
7024 struct MeshTunnel *t;
7025 MESH_TunnelNumber tid;
7027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7028 /* Sanity check for client registration */
7029 if (NULL == (c = client_get (client)))
7032 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7037 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7039 /* Sanity check for message size */
7040 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7043 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7047 /* Tunnel exists? */
7048 tid = ntohl (peer_msg->tunnel_id);
7049 t = tunnel_get_by_local_id (c, tid);
7053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7057 /* Does client own tunnel? */
7058 if (t->owner->handle != client)
7061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7065 GNUNET_i2s (&peer_msg->peer));
7066 peer_info = peer_info_get (&peer_msg->peer);
7068 tunnel_add_peer (t, peer_info);
7069 peer_info_connect (peer_info, t);
7071 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7077 * Handler for disconnection requests of peers in a tunnel
7079 * @param cls closure
7080 * @param client identification of the client
7081 * @param message the actual message (PeerControl)
7084 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7085 const struct GNUNET_MessageHeader *message)
7087 struct GNUNET_MESH_PeerControl *peer_msg;
7088 struct MeshPeerInfo *peer_info;
7089 struct MeshClient *c;
7090 struct MeshTunnel *t;
7091 MESH_TunnelNumber tid;
7093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7094 /* Sanity check for client registration */
7095 if (NULL == (c = client_get (client)))
7098 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7103 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7105 /* Sanity check for message size */
7106 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7113 /* Tunnel exists? */
7114 tid = ntohl (peer_msg->tunnel_id);
7115 t = tunnel_get_by_local_id (c, tid);
7119 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7124 /* Does client own tunnel? */
7125 if (t->owner->handle != client)
7128 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7133 GNUNET_i2s (&peer_msg->peer));
7134 /* Is the peer in the tunnel? */
7136 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7137 if (NULL == peer_info)
7140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7144 /* Ok, delete peer from tunnel */
7145 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7146 &peer_msg->peer.hashPubKey);
7148 send_destroy_path (t, peer_info->id);
7149 tunnel_delete_peer (t, peer_info->id);
7150 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7155 * Handler for blacklist requests of peers in a tunnel
7157 * @param cls closure
7158 * @param client identification of the client
7159 * @param message the actual message (PeerControl)
7161 * FIXME implement DHT block bloomfilter
7164 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7165 const struct GNUNET_MessageHeader *message)
7167 struct GNUNET_MESH_PeerControl *peer_msg;
7168 struct MeshClient *c;
7169 struct MeshTunnel *t;
7170 MESH_TunnelNumber tid;
7172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7173 /* Sanity check for client registration */
7174 if (NULL == (c = client_get (client)))
7177 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7182 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7184 /* Sanity check for message size */
7185 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7192 /* Tunnel exists? */
7193 tid = ntohl (peer_msg->tunnel_id);
7194 t = tunnel_get_by_local_id (c, tid);
7198 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7203 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7204 GNUNET_PEER_intern(&peer_msg->peer));
7209 * Handler for unblacklist requests of peers in a tunnel
7211 * @param cls closure
7212 * @param client identification of the client
7213 * @param message the actual message (PeerControl)
7216 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7217 const struct GNUNET_MessageHeader *message)
7219 struct GNUNET_MESH_PeerControl *peer_msg;
7220 struct MeshClient *c;
7221 struct MeshTunnel *t;
7222 MESH_TunnelNumber tid;
7226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7227 /* Sanity check for client registration */
7228 if (NULL == (c = client_get (client)))
7231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7236 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7238 /* Sanity check for message size */
7239 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7246 /* Tunnel exists? */
7247 tid = ntohl (peer_msg->tunnel_id);
7248 t = tunnel_get_by_local_id (c, tid);
7252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7257 /* if peer is not known, complain */
7258 pid = GNUNET_PEER_search (&peer_msg->peer);
7265 /* search and remove from list */
7266 for (i = 0; i < t->nblacklisted; i++)
7268 if (t->blacklisted[i] == pid)
7270 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7271 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7276 /* if peer hasn't been blacklisted, complain */
7282 * Handler for connection requests to new peers by type
7284 * @param cls closure
7285 * @param client identification of the client
7286 * @param message the actual message (ConnectPeerByType)
7289 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7290 const struct GNUNET_MessageHeader *message)
7292 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7293 struct MeshClient *c;
7294 struct MeshTunnel *t;
7295 struct GNUNET_HashCode hash;
7296 MESH_TunnelNumber tid;
7298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7299 /* Sanity check for client registration */
7300 if (NULL == (c = client_get (client)))
7303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7308 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7310 /* Sanity check for message size */
7311 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7312 ntohs (connect_msg->header.size))
7315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7319 /* Tunnel exists? */
7320 tid = ntohl (connect_msg->tunnel_id);
7321 t = tunnel_get_by_local_id (c, tid);
7325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7329 /* Does client own tunnel? */
7330 if (t->owner->handle != client)
7333 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7337 /* Do WE have the service? */
7338 t->type = ntohl (connect_msg->type);
7339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7340 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7341 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7344 /* Yes! Fast forward, add ourselves to the tunnel and send the
7345 * good news to the client, and alert the destination client of
7346 * an incoming tunnel.
7348 * FIXME send a path create to self, avoid code duplication
7350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7351 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7352 peer_info_get (&my_full_id),
7353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7356 send_client_peer_connected (t, myid);
7357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7358 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7360 t->local_tid_dest = next_local_tid++;
7361 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7362 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7367 /* Ok, lets find a peer offering the service */
7368 if (NULL != t->dht_get_type)
7370 GNUNET_DHT_get_stop (t->dht_get_type);
7372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7373 GNUNET_h2s (&hash));
7375 GNUNET_DHT_get_start (dht_handle,
7376 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7378 dht_replication_level,
7379 GNUNET_DHT_RO_RECORD_ROUTE |
7380 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7382 &dht_get_type_handler, t);
7384 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7390 * Handler for connection requests to new peers by a string service description.
7392 * @param cls closure
7393 * @param client identification of the client
7394 * @param message the actual message, which includes messages the client wants
7397 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7398 const struct GNUNET_MessageHeader *message)
7400 struct GNUNET_MESH_ConnectPeerByString *msg;
7401 struct MeshRegexSearchContext *ctx;
7402 struct MeshRegexSearchInfo *info;
7403 struct GNUNET_DHT_GetHandle *get_h;
7404 struct GNUNET_HashCode key;
7405 struct MeshTunnel *t;
7406 struct MeshClient *c;
7407 MESH_TunnelNumber tid;
7411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7412 "Connect by string started\n");
7413 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7414 size = htons (message->size);
7416 /* Sanity check for client registration */
7417 if (NULL == (c = client_get (client)))
7420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7425 /* Message size sanity check */
7426 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7433 /* Tunnel exists? */
7434 tid = ntohl (msg->tunnel_id);
7435 t = tunnel_get_by_local_id (c, tid);
7439 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7443 /* Does client own tunnel? */
7444 if (t->owner->handle != client)
7447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7452 " on tunnel %s [%u]\n",
7453 GNUNET_i2s(&my_full_id),
7456 /* Only one connect_by_string allowed at the same time! */
7457 /* FIXME: allow more, return handle at api level to cancel, document */
7458 if (NULL != t->regex_ctx)
7461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7465 /* Find string itself */
7466 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7467 string = (const char *) &msg[1];
7469 /* Initialize context */
7470 size = GNUNET_REGEX_get_first_key(string, len, &key);
7471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7472 " consumed %u bits out of %u\n", size, len);
7473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7474 " looking for %s\n", GNUNET_h2s (&key));
7476 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7478 info->description = GNUNET_malloc (len + 1);
7479 memcpy (info->description, string, len);
7480 info->description[len] = '\0';
7481 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7482 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7485 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7486 ctx->position = size;
7490 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7492 /* Start search in DHT */
7493 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7494 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7495 &key, /* key to search */
7496 dht_replication_level, /* replication level */
7497 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7498 NULL, /* xquery */ // FIXME BLOOMFILTER
7499 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7500 &dht_get_string_handler, ctx);
7502 GNUNET_break (GNUNET_OK ==
7503 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7506 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7508 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7514 * Handler for client traffic directed to one peer
7516 * @param cls closure
7517 * @param client identification of the client
7518 * @param message the actual message
7521 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7522 const struct GNUNET_MessageHeader *message)
7524 struct MeshClient *c;
7525 struct MeshTunnel *t;
7526 struct MeshPeerInfo *pi;
7527 struct GNUNET_MESH_Unicast *data_msg;
7528 MESH_TunnelNumber tid;
7531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7532 "Got a unicast request from a client!\n");
7534 /* Sanity check for client registration */
7535 if (NULL == (c = client_get (client)))
7538 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7543 data_msg = (struct GNUNET_MESH_Unicast *) message;
7545 /* Sanity check for message size */
7546 size = ntohs (message->size);
7547 if (sizeof (struct GNUNET_MESH_Unicast) +
7548 sizeof (struct GNUNET_MessageHeader) > size)
7551 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7555 /* Tunnel exists? */
7556 tid = ntohl (data_msg->tid);
7557 t = tunnel_get_by_local_id (c, tid);
7561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7565 /* Is it a local tunnel? Then, does client own the tunnel? */
7566 if (t->owner->handle != client)
7569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7573 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7574 &data_msg->destination.hashPubKey);
7575 /* Is the selected peer in the tunnel? */
7579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7583 /* PID should be as expected */
7584 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7587 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7588 "Unicast PID, expected %u, got %u\n",
7589 t->fwd_pid + 1, ntohl (data_msg->pid));
7590 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7594 /* Ok, everything is correct, send the message
7595 * (pretend we got it from a mesh peer)
7598 /* Work around const limitation */
7599 char buf[ntohs (message->size)] GNUNET_ALIGN;
7600 struct GNUNET_MESH_Unicast *copy;
7602 copy = (struct GNUNET_MESH_Unicast *) buf;
7603 memcpy (buf, data_msg, size);
7604 copy->oid = my_full_id;
7605 copy->tid = htonl (t->id.tid);
7606 copy->ttl = htonl (default_ttl);
7607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7608 " calling generic handler...\n");
7609 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7612 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7619 * Handler for client traffic directed to the origin
7621 * @param cls closure
7622 * @param client identification of the client
7623 * @param message the actual message
7626 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7627 const struct GNUNET_MessageHeader *message)
7629 struct GNUNET_MESH_ToOrigin *data_msg;
7630 struct MeshTunnelFlowControlInfo *fcinfo;
7631 struct MeshClient *c;
7632 struct MeshTunnel *t;
7633 MESH_TunnelNumber tid;
7636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7637 "Got a ToOrigin request from a client!\n");
7638 /* Sanity check for client registration */
7639 if (NULL == (c = client_get (client)))
7642 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7647 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7649 /* Sanity check for message size */
7650 size = ntohs (message->size);
7651 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7652 sizeof (struct GNUNET_MessageHeader) > size)
7655 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7659 /* Tunnel exists? */
7660 tid = ntohl (data_msg->tid);
7661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7662 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7668 t = tunnel_get_by_local_id (c, tid);
7671 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7672 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7674 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7678 /* It should be sent by someone who has this as incoming tunnel. */
7679 if (GNUNET_NO == client_knows_tunnel (c, t))
7682 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7686 /* PID should be as expected */
7687 fcinfo = tunnel_get_client_fc (t, c);
7688 if (ntohl (data_msg->pid) != fcinfo->bck_pid + 1)
7691 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7692 "To Origin PID, expected %u, got %u\n",
7693 fcinfo->bck_pid + 1,
7694 ntohl (data_msg->pid));
7695 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7700 /* Ok, everything is correct, send the message
7701 * (pretend we got it from a mesh peer)
7704 char buf[ntohs (message->size)] GNUNET_ALIGN;
7705 struct GNUNET_MESH_ToOrigin *copy;
7707 /* Work around const limitation */
7708 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7709 memcpy (buf, data_msg, size);
7710 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7711 copy->tid = htonl (t->id.tid);
7712 copy->ttl = htonl (default_ttl);
7713 copy->pid = htonl (t->bck_pid + 1);
7715 copy->sender = my_full_id;
7716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7717 " calling generic handler...\n");
7718 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7720 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7727 * Handler for client traffic directed to all peers in a tunnel
7729 * @param cls closure
7730 * @param client identification of the client
7731 * @param message the actual message
7734 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7735 const struct GNUNET_MessageHeader *message)
7737 struct MeshClient *c;
7738 struct MeshTunnel *t;
7739 struct GNUNET_MESH_Multicast *data_msg;
7740 MESH_TunnelNumber tid;
7742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7743 "Got a multicast request from a client!\n");
7745 /* Sanity check for client registration */
7746 if (NULL == (c = client_get (client)))
7749 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7754 data_msg = (struct GNUNET_MESH_Multicast *) message;
7756 /* Sanity check for message size */
7757 if (sizeof (struct GNUNET_MESH_Multicast) +
7758 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7765 /* Tunnel exists? */
7766 tid = ntohl (data_msg->tid);
7767 t = tunnel_get_by_local_id (c, tid);
7771 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7772 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7774 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7778 /* Does client own tunnel? */
7779 if (t->owner->handle != client)
7782 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7786 /* PID should be as expected */
7787 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7791 "Multicast PID, expected %u, got %u\n",
7792 t->fwd_pid + 1, ntohl (data_msg->pid));
7793 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7798 char buf[ntohs (message->size)] GNUNET_ALIGN;
7799 struct GNUNET_MESH_Multicast *copy;
7801 copy = (struct GNUNET_MESH_Multicast *) buf;
7802 memcpy (buf, message, ntohs (message->size));
7803 copy->oid = my_full_id;
7804 copy->tid = htonl (t->id.tid);
7805 copy->ttl = htonl (default_ttl);
7806 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7808 " calling generic handler...\n");
7809 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7812 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7818 * Handler for client's ACKs for payload traffic.
7820 * @param cls Closure (unused).
7821 * @param client Identification of the client.
7822 * @param message The actual message.
7825 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7826 const struct GNUNET_MessageHeader *message)
7828 struct GNUNET_MESH_LocalAck *msg;
7829 struct MeshTunnel *t;
7830 struct MeshClient *c;
7831 MESH_TunnelNumber tid;
7834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7835 /* Sanity check for client registration */
7836 if (NULL == (c = client_get (client)))
7839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7844 msg = (struct GNUNET_MESH_LocalAck *) message;
7846 /* Tunnel exists? */
7847 tid = ntohl (msg->tunnel_id);
7848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7849 t = tunnel_get_by_local_id (c, tid);
7853 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7855 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7859 ack = ntohl (msg->max_pid);
7860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7862 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7863 if (NULL != t->owner && t->owner->handle == client)
7865 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7867 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7871 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7872 tunnel_set_client_fwd_ack (t, c, ack);
7873 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7876 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7883 * Functions to handle messages from clients
7885 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7886 {&handle_local_new_client, NULL,
7887 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7888 {&handle_local_announce_regex, NULL,
7889 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7890 {&handle_local_tunnel_create, NULL,
7891 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7892 sizeof (struct GNUNET_MESH_TunnelMessage)},
7893 {&handle_local_tunnel_destroy, NULL,
7894 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7895 sizeof (struct GNUNET_MESH_TunnelMessage)},
7896 {&handle_local_tunnel_speed, NULL,
7897 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7898 sizeof (struct GNUNET_MESH_TunnelMessage)},
7899 {&handle_local_tunnel_speed, NULL,
7900 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7901 sizeof (struct GNUNET_MESH_TunnelMessage)},
7902 {&handle_local_tunnel_buffer, NULL,
7903 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7904 sizeof (struct GNUNET_MESH_TunnelMessage)},
7905 {&handle_local_tunnel_buffer, NULL,
7906 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7907 sizeof (struct GNUNET_MESH_TunnelMessage)},
7908 {&handle_local_connect_add, NULL,
7909 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7910 sizeof (struct GNUNET_MESH_PeerControl)},
7911 {&handle_local_connect_del, NULL,
7912 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7913 sizeof (struct GNUNET_MESH_PeerControl)},
7914 {&handle_local_blacklist, NULL,
7915 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7916 sizeof (struct GNUNET_MESH_PeerControl)},
7917 {&handle_local_unblacklist, NULL,
7918 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7919 sizeof (struct GNUNET_MESH_PeerControl)},
7920 {&handle_local_connect_by_type, NULL,
7921 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7922 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7923 {&handle_local_connect_by_string, NULL,
7924 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7925 {&handle_local_unicast, NULL,
7926 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7927 {&handle_local_to_origin, NULL,
7928 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7929 {&handle_local_multicast, NULL,
7930 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7931 {&handle_local_ack, NULL,
7932 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7933 sizeof (struct GNUNET_MESH_LocalAck)},
7939 * To be called on core init/fail.
7941 * @param cls service closure
7942 * @param server handle to the server for this service
7943 * @param identity the public identity of this peer
7946 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7947 const struct GNUNET_PeerIdentity *identity)
7950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7951 core_handle = server;
7952 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7955 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7956 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7958 GNUNET_abort(); // Try harder
7965 * Method called whenever a given peer connects.
7967 * @param cls closure
7968 * @param peer peer identity this notification is about
7969 * @param atsi performance data for the connection
7970 * @param atsi_count number of records in 'atsi'
7973 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7974 const struct GNUNET_ATS_Information *atsi,
7975 unsigned int atsi_count)
7977 struct MeshPeerInfo *peer_info;
7978 struct MeshPeerPath *path;
7980 DEBUG_CONN ("Peer connected\n");
7981 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7982 peer_info = peer_info_get (peer);
7983 if (myid == peer_info->id)
7985 DEBUG_CONN (" (self)\n");
7990 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7992 path = path_new (2);
7993 path->peers[0] = myid;
7994 path->peers[1] = peer_info->id;
7995 GNUNET_PEER_change_rc (myid, 1);
7996 GNUNET_PEER_change_rc (peer_info->id, 1);
7997 peer_info_add_path (peer_info, path, GNUNET_YES);
7998 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8004 * Method called whenever a peer disconnects.
8006 * @param cls closure
8007 * @param peer peer identity this notification is about
8010 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8012 struct MeshPeerInfo *pi;
8013 struct MeshPeerQueue *q;
8014 struct MeshPeerQueue *n;
8016 DEBUG_CONN ("Peer disconnected\n");
8017 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8027 /* TODO try to reroute this traffic instead */
8028 queue_destroy(q, GNUNET_YES);
8031 if (NULL != pi->core_transmit)
8033 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8034 pi->core_transmit = NULL;
8036 peer_info_remove_path (pi, pi->id, myid);
8039 DEBUG_CONN (" (self)\n");
8041 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8046 /******************************************************************************/
8047 /************************ MAIN FUNCTIONS ****************************/
8048 /******************************************************************************/
8051 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8053 * @param cls closure
8054 * @param key current key code
8055 * @param value value in the hash map
8056 * @return GNUNET_YES if we should continue to iterate,
8060 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8062 struct MeshTunnel *t = value;
8069 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8071 * @param cls closure
8072 * @param key current key code
8073 * @param value value in the hash map
8074 * @return GNUNET_YES if we should continue to iterate,
8078 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8080 struct MeshPeerInfo *p = value;
8081 struct MeshPeerQueue *q;
8082 struct MeshPeerQueue *n;
8090 queue_destroy(q, GNUNET_YES);
8094 peer_info_destroy (p);
8100 * Task run during shutdown.
8106 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8110 if (core_handle != NULL)
8112 GNUNET_CORE_disconnect (core_handle);
8117 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8120 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8121 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8122 if (dht_handle != NULL)
8124 GNUNET_DHT_disconnect (dht_handle);
8129 GNUNET_SERVER_notification_context_destroy (nc);
8132 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8134 GNUNET_SCHEDULER_cancel (announce_id_task);
8135 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8142 * Callback for hostkey read/generation
8145 * @param pk the private key
8146 * @param emsg error message
8149 key_generation_cb (void *cls,
8150 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8153 struct MeshPeerInfo *peer;
8154 struct MeshPeerPath *p;
8159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8160 _("Mesh service could not access hostkey. Exiting.\n"));
8161 GNUNET_SCHEDULER_shutdown ();
8164 my_private_key = pk;
8165 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8166 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8167 &my_full_id.hashPubKey);
8168 myid = GNUNET_PEER_intern (&my_full_id);
8169 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8170 "Mesh for peer [%s] starting\n",
8171 GNUNET_i2s(&my_full_id));
8173 // transport_handle = GNUNET_TRANSPORT_connect(c,
8183 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8186 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8187 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8188 GNUNET_SERVER_disconnect_notify (server_handle,
8189 &handle_local_client_disconnect, NULL);
8193 clients_tail = NULL;
8196 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8197 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8199 /* Create a peer_info for the local peer */
8200 peer = peer_info_get (&my_full_id);
8203 GNUNET_PEER_change_rc (myid, 1);
8204 peer_info_add_path (peer, p, GNUNET_YES);
8205 GNUNET_SERVER_resume (server_handle);
8206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8211 * Process mesh requests.
8213 * @param cls closure
8214 * @param server the initialized server
8215 * @param c configuration to use
8218 run (void *cls, struct GNUNET_SERVER_Handle *server,
8219 const struct GNUNET_CONFIGURATION_Handle *c)
8223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8224 server_handle = server;
8225 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8226 NULL, /* Closure passed to MESH functions */
8227 &core_init, /* Call core_init once connected */
8228 &core_connect, /* Handle connects */
8229 &core_disconnect, /* remove peers on disconnects */
8230 NULL, /* Don't notify about all incoming messages */
8231 GNUNET_NO, /* For header only in notification */
8232 NULL, /* Don't notify about all outbound messages */
8233 GNUNET_NO, /* For header-only out notification */
8234 core_handlers); /* Register these handlers */
8236 if (core_handle == NULL)
8239 GNUNET_SCHEDULER_shutdown ();
8244 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8249 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8251 GNUNET_SCHEDULER_shutdown ();
8256 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8257 &refresh_path_time))
8259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8261 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8262 "mesh", "refresh path time");
8263 GNUNET_SCHEDULER_shutdown ();
8268 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8269 &app_announce_time))
8271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8273 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8274 "mesh", "app announce time");
8275 GNUNET_SCHEDULER_shutdown ();
8278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8281 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8286 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8287 "mesh", "id announce time");
8288 GNUNET_SCHEDULER_shutdown ();
8296 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
8297 &unacknowledged_wait_time))
8299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8301 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8302 "mesh", "unacknowledged wait time");
8303 GNUNET_SCHEDULER_shutdown ();
8308 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8313 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8314 "mesh", "connect timeout");
8315 GNUNET_SCHEDULER_shutdown ();
8320 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8325 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8326 "mesh", "max msgs queue");
8327 GNUNET_SCHEDULER_shutdown ();
8332 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8337 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8338 "mesh", "max tunnels");
8339 GNUNET_SCHEDULER_shutdown ();
8344 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8349 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8350 "mesh", "default ttl", 64);
8355 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8356 &dht_replication_level))
8358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8360 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8361 "mesh", "dht replication level", 10);
8362 dht_replication_level = 10;
8365 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8366 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8367 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8368 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8369 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8371 dht_handle = GNUNET_DHT_connect (c, 64);
8372 if (NULL == dht_handle)
8376 stats = GNUNET_STATISTICS_create ("mesh", c);
8378 GNUNET_SERVER_suspend (server_handle);
8379 /* Scheduled the task to clean up when shutdown is called */
8380 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8382 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8383 GNUNET_free (keyfile);
8388 * The main function for the mesh service.
8390 * @param argc number of arguments from the command line
8391 * @param argv command line arguments
8392 * @return 0 ok, 1 on error
8395 main (int argc, char *const *argv)
8400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8401 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8403 ret = (GNUNET_OK == r) ? 0 : 1;
8404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8408 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8409 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8410 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);