2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
42 * - relay corking down to core
43 * - set ttl relative to tree depth
44 * - Add data ACK count in path ACK
45 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
51 #include "mesh_protocol.h"
52 #include "mesh_tunnel_tree.h"
53 #include "block_mesh.h"
54 #include "mesh_block_lib.h"
55 #include "gnunet_dht_service.h"
56 #include "gnunet_statistics_service.h"
57 #include "gnunet_regex_lib.h"
59 #define MESH_BLOOM_SIZE 128
61 #define MESH_DEBUG_DHT GNUNET_NO
62 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_YES
65 #if MESH_DEBUG_CONNECTION
66 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
68 #define DEBUG_CONN(...)
72 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
74 #define DEBUG_DHT(...)
81 struct timespec __mesh_start;
82 struct timespec __mesh_end;
83 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
84 #define INTERVAL_END \
86 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
87 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
88 if (__diff < 0) __diff += 1000000000;\
92 #define INTERVAL_SHOW \
94 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
96 #define INTERVAL_START
101 /******************************************************************************/
102 /************************ DATA STRUCTURES ****************************/
103 /******************************************************************************/
105 /** FWD declaration */
111 * Struct representing a piece of data being sent to other peers
115 /** Tunnel it belongs to. */
116 struct MeshTunnel *t;
118 /** In case of a multicast, task to allow a client to send more data if
119 * some neighbor is too slow. */
120 GNUNET_SCHEDULER_TaskIdentifier *task;
122 /** How many remaining neighbors we need to send this to. */
123 unsigned int reference_counter;
125 /** Size of the data. */
134 * Struct containing info about a queued transmission to this peer
141 struct MeshPeerQueue *next;
146 struct MeshPeerQueue *prev;
149 * Peer this transmission is directed to.
151 struct MeshPeerInfo *peer;
154 * Tunnel this message belongs to.
156 struct MeshTunnel *tunnel;
159 * Pointer to info stucture used as cls.
169 * Size of the message
176 * Struct containing all info possibly needed to build a package when called
179 struct MeshTransmissionDescriptor
181 /** ID of the tunnel this packet travels in */
182 struct MESH_TunnelID *origin;
184 /** Who was this message being sent to */
185 struct MeshPeerInfo *peer;
187 /** Ultimate destination of the packet */
188 GNUNET_PEER_Id destination;
190 /** Data descriptor */
191 struct MeshData* mesh_data;
196 * Struct containing all information regarding a given peer
206 * Last time we heard from this peer
208 struct GNUNET_TIME_Absolute last_contact;
211 * Number of attempts to reconnect so far
213 int n_reconnect_attempts;
216 * Paths to reach the peer, ordered by ascending hop count
218 struct MeshPeerPath *path_head;
221 * Paths to reach the peer, ordered by ascending hop count
223 struct MeshPeerPath *path_tail;
226 * Handle to stop the DHT search for a path to this peer
228 struct GNUNET_DHT_GetHandle *dhtget;
231 * Closure given to the DHT GET
233 struct MeshPathInfo *dhtgetcls;
236 * Array of tunnels this peer participates in
237 * (most probably a small amount, therefore not a hashmap)
238 * When the path to the peer changes, notify these tunnels to let them
239 * re-adjust their path trees.
241 struct MeshTunnel **tunnels;
244 * Number of tunnels this peers participates in
246 unsigned int ntunnels;
249 * Transmission queue to core DLL head
251 struct MeshPeerQueue *queue_head;
254 * Transmission queue to core DLL tail
256 struct MeshPeerQueue *queue_tail;
259 * How many messages are in the queue to this peer.
261 unsigned int queue_n;
264 * Handle to for queued transmissions
266 struct GNUNET_CORE_TransmitHandle *core_transmit;
271 * Globally unique tunnel identification (owner + number)
272 * DO NOT USE OVER THE NETWORK
277 * Node that owns the tunnel
282 * Tunnel number to differentiate all the tunnels owned by the node oid
283 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
285 MESH_TunnelNumber tid;
290 * Struct containing all information regarding a tunnel
291 * For an intermediate node the improtant info used will be:
292 * - id Tunnel unique identification
293 * - paths[0] To know where to send it next
294 * - metainfo: ready, speeds, accounting
301 struct MESH_TunnelID id;
304 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
306 MESH_TunnelNumber local_tid;
309 * Local tunnel number for local destination clients (incoming number)
310 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
313 MESH_TunnelNumber local_tid_dest;
316 * Is the speed on the tunnel limited to the slowest peer?
321 * Is the tunnel bufferless (minimum latency)?
326 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
331 * Packet ID of the last bck packet sent (unique counter per hop).
336 * SKIP value for this tunnel.
341 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
342 * Contains the Flow Control info: FWD ACK value received,
343 * last BCK ACK sent, PID and SKIP values.
345 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
348 * Last ACK sent towards the origin (for traffic towards leaf node).
350 uint32_t last_fwd_ack;
353 * BCK ACK value received from the hop towards the owner of the tunnel,
354 * (previous node / owner): up to what message PID can we sent back to him.
359 * How many messages are in the forward queue (towards leaves).
361 unsigned int fwd_queue_n;
364 * How many messages do we accept in the forward queue.
366 unsigned int fwd_queue_max;
369 * How many messages are in the backward queue (towards origin).
371 unsigned int bck_queue_n;
374 * How many messages do we accept in the backward queue.
376 unsigned int bck_queue_max;
379 * Last time the tunnel was used
381 struct GNUNET_TIME_Absolute timestamp;
384 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
385 * containing peers added by id or by type, not intermediate peers.
387 struct GNUNET_CONTAINER_MultiHashMap *peers;
390 * Number of peers that are connected and potentially ready to receive data
392 unsigned int peers_ready;
395 * Number of peers that have been added to the tunnel
397 unsigned int peers_total;
400 * Client owner of the tunnel, if any
402 struct MeshClient *owner;
405 * Clients that have been informed about and want to stay in the tunnel.
407 struct MeshClient **clients;
410 * Flow control info for each client.
412 struct MeshTunnelClientInfo *clients_fc;
415 * Number of elements in clients/clients_fc
417 unsigned int nclients;
420 * Clients that have been informed but requested to leave the tunnel.
422 struct MeshClient **ignore;
425 * Number of elements in clients
427 unsigned int nignore;
432 GNUNET_PEER_Id *blacklisted;
435 * Number of elements in blacklisted
437 unsigned int nblacklisted;
440 * Bloomfilter (for peer identities) to stop circular routes
442 char bloomfilter[MESH_BLOOM_SIZE];
447 struct MeshTunnelTree *tree;
450 * Application type we are looking for in this tunnel
452 GNUNET_MESH_ApplicationType type;
455 * Used to search peers offering a service
457 struct GNUNET_DHT_GetHandle *dht_get_type;
460 * Initial context of the regex search for a connect_by_string
462 struct MeshRegexSearchContext *regex_ctx;
465 * Task to keep the used paths alive
467 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
470 * Task to destroy the tunnel after timeout
472 * FIXME: merge the two? a tunnel will have either
473 * a path refresh OR a timeout, never both!
475 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
478 * Flag to signal the destruction of the tunnel.
479 * If this is set GNUNET_YES the tunnel will be destroyed
480 * when the queue is empty.
487 * Info about a child node in a tunnel, needed to perform flow control.
489 struct MeshTunnelChildInfo
492 * ID of the child node.
507 * Maximum PID allowed (FWD ACK received).
512 * Last ACK sent to that child (BCK ACK).
517 * Circular buffer pointing to MeshPeerQueue elements.
518 * Size determined by the tunnel queue size.
520 struct MeshPeerQueue **send_buffer;
523 * Index of the oldest element in the send_buffer.
525 unsigned int send_buffer_start;
528 * How many elements are already in the buffer.
530 unsigned int send_buffer_n;
535 * Info about a leaf client of a tunnel, needed to perform flow control.
537 struct MeshTunnelClientInfo
540 * PID of the last packet sent to the client (FWD).
545 * PID of the last packet received from the client (BCK).
550 * Maximum PID allowed (FWD ACK received).
555 * Last ACK sent to that child (BCK ACK).
563 * Info collected during iteration of child nodes in order to get the ACK value
566 struct MeshTunnelChildIteratorContext
569 * Tunnel whose info is being collected.
571 struct MeshTunnel *t;
574 * Is this context initialized? Is the value in max_child_ack valid?
579 * Maximum child ACK so far.
581 uint32_t max_child_ack;
584 * Number of children nodes
586 unsigned int nchildren;
591 * Info needed to work with tunnel paths and peers
598 struct MeshTunnel *t;
601 * Neighbouring peer to whom we send the packet to
603 struct MeshPeerInfo *peer;
608 struct MeshPeerPath *path;
613 * Struct containing information about a client of the service
620 struct MeshClient *next;
625 struct MeshClient *prev;
628 * Tunnels that belong to this client, indexed by local id
630 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
633 * Tunnels this client has accepted, indexed by incoming local id
635 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
638 * Tunnels this client has rejected, indexed by incoming local id
640 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
642 * Handle to communicate with the client
644 struct GNUNET_SERVER_Client *handle;
647 * Applications that this client has claimed to provide
649 struct GNUNET_CONTAINER_MultiHashMap *apps;
652 * Messages that this client has declared interest in
654 struct GNUNET_CONTAINER_MultiHashMap *types;
657 * Whether the client is active or shutting down (don't send confirmations
658 * to a client that is shutting down.
663 * ID of the client, mainly for debug messages
668 * Regular expressions describing the services offered by this client.
670 char **regexes; // FIXME add timeout? API to remove a regex?
673 * Number of regular expressions in regexes.
675 unsigned int n_regex;
678 * Task to refresh all regular expresions in the DHT.
680 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
686 * Struct to keep information of searches of services described by a regex
687 * using a user-provided string service description.
689 struct MeshRegexSearchInfo
692 * Which tunnel is this for
694 struct MeshTunnel *t;
697 * User provided description of the searched service.
702 * Part of the description already consumed by the search.
709 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
712 * Results from running DHT GETs.
714 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
717 * Contexts, for each running DHT GET. Free all on end of search.
719 struct MeshRegexSearchContext **contexts;
722 * Number of contexts (branches/steps in search).
724 unsigned int n_contexts;
727 * Peer that is connecting via connect_by_string. When connected, free ctx.
732 * Other peers that are found but not yet being connected to.
734 GNUNET_PEER_Id *peers;
737 * Number of elements in peers.
739 unsigned int n_peers;
742 * Next peer to try to connect to.
747 * Timeout for a connect attempt.
748 * When reached, try to connect to a different peer, if any. If not,
749 * try the same peer again.
751 GNUNET_SCHEDULER_TaskIdentifier timeout;
756 * Struct to keep state of running searches that have consumed a part of
759 struct MeshRegexSearchContext
762 * Part of the description already consumed by
763 * this particular search branch.
768 * Information about the search.
770 struct MeshRegexSearchInfo *info;
774 /******************************************************************************/
775 /************************ DEBUG FUNCTIONS ****************************/
776 /******************************************************************************/
780 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
781 * @param cls string to print
782 * @param success GNUNET_OK if the PUT was transmitted,
783 * GNUNET_NO on timeout,
784 * GNUNET_SYSERR on disconnect from service
785 * after the PUT message was transmitted
786 * (so we don't know if it was received or not)
791 mesh_debug (void *cls, int success)
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
799 unsigned int debug_fwd_ack;
800 unsigned int debug_bck_ack;
804 /******************************************************************************/
805 /*********************** GLOBAL VARIABLES ****************************/
806 /******************************************************************************/
809 * Configuration parameters
811 static struct GNUNET_TIME_Relative refresh_path_time;
812 static struct GNUNET_TIME_Relative app_announce_time;
813 static struct GNUNET_TIME_Relative id_announce_time;
814 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
815 static struct GNUNET_TIME_Relative connect_timeout;
816 static long long unsigned int default_ttl;
817 static long long unsigned int dht_replication_level;
818 static long long unsigned int max_tunnels;
819 static long long unsigned int max_msgs_queue;
822 * DLL with all the clients, head.
824 static struct MeshClient *clients;
827 * DLL with all the clients, tail.
829 static struct MeshClient *clients_tail;
832 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
834 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
837 * Number of tunnels known.
839 static unsigned long long n_tunnels;
842 * Tunnels incoming, indexed by MESH_TunnelNumber
843 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
845 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
848 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
850 static struct GNUNET_CONTAINER_MultiHashMap *peers;
853 * Handle to communicate with transport
855 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
858 * Handle to communicate with core.
860 static struct GNUNET_CORE_Handle *core_handle;
865 static struct GNUNET_DHT_Handle *dht_handle;
870 static struct GNUNET_SERVER_Handle *server_handle;
873 * Handle to the statistics service.
875 static struct GNUNET_STATISTICS_Handle *stats;
878 * Notification context, to send messages to local clients.
880 static struct GNUNET_SERVER_NotificationContext *nc;
883 * Local peer own ID (memory efficient handle).
885 static GNUNET_PEER_Id myid;
888 * Local peer own ID (full value).
890 static struct GNUNET_PeerIdentity my_full_id;
895 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
900 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
903 * Tunnel ID for the next created tunnel (global tunnel number).
905 static MESH_TunnelNumber next_tid;
908 * Tunnel ID for the next incoming tunnel (local tunnel number).
910 static MESH_TunnelNumber next_local_tid;
913 * All application types provided by this peer.
915 static struct GNUNET_CONTAINER_MultiHashMap *applications;
918 * All message types clients of this peer are interested in.
920 static struct GNUNET_CONTAINER_MultiHashMap *types;
923 * Task to periodically announce provided applications.
925 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
928 * Task to periodically announce itself in the network.
930 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
933 * Next ID to assign to a client.
935 unsigned int next_client_id;
938 /******************************************************************************/
939 /*********************** DECLARATIONS **************************/
940 /******************************************************************************/
942 /* FIXME move declarations here */
945 * Function to process paths received for a new peer addition. The recorded
946 * paths form the initial tunnel, which can be optimized later.
947 * Called on each result obtained for the DHT search.
950 * @param exp when will this value expire
951 * @param key key of the result
952 * @param type type of the result
953 * @param size number of bytes in data
954 * @param data pointer to the result data
957 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
958 const struct GNUNET_HashCode * key,
959 const struct GNUNET_PeerIdentity *get_path,
960 unsigned int get_path_length,
961 const struct GNUNET_PeerIdentity *put_path,
962 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
963 size_t size, const void *data);
967 * Function to process DHT string to regex matching.
968 * Called on each result obtained for the DHT search.
970 * @param cls closure (search context)
971 * @param exp when will this value expire
972 * @param key key of the result
973 * @param get_path path of the get request (not used)
974 * @param get_path_length lenght of get_path (not used)
975 * @param put_path path of the put request (not used)
976 * @param put_path_length length of the put_path (not used)
977 * @param type type of the result
978 * @param size number of bytes in data
979 * @param data pointer to the result data
981 * TODO: re-issue the request after certain time? cancel after X results?
984 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
985 const struct GNUNET_HashCode * key,
986 const struct GNUNET_PeerIdentity *get_path,
987 unsigned int get_path_length,
988 const struct GNUNET_PeerIdentity *put_path,
989 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
990 size_t size, const void *data);
994 * Function to process DHT string to regex matching.
995 * Called on each result obtained for the DHT search.
997 * @param cls closure (search context)
998 * @param exp when will this value expire
999 * @param key key of the result
1000 * @param get_path path of the get request (not used)
1001 * @param get_path_length lenght of get_path (not used)
1002 * @param put_path path of the put request (not used)
1003 * @param put_path_length length of the put_path (not used)
1004 * @param type type of the result
1005 * @param size number of bytes in data
1006 * @param data pointer to the result data
1009 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1010 const struct GNUNET_HashCode * key,
1011 const struct GNUNET_PeerIdentity *get_path,
1012 unsigned int get_path_length,
1013 const struct GNUNET_PeerIdentity *put_path,
1014 unsigned int put_path_length,
1015 enum GNUNET_BLOCK_Type type,
1016 size_t size, const void *data);
1020 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1021 * and insert it in the appropiate structures if the peer is not known yet.
1023 * @param peer Short identity of the peer.
1025 * @return Existing or newly created peer info.
1027 static struct MeshPeerInfo *
1028 peer_info_get_short (const GNUNET_PEER_Id peer);
1032 * Try to establish a new connection to this peer.
1033 * Use the best path for the given tunnel.
1034 * If the peer doesn't have any path to it yet, try to get one.
1035 * If the peer already has some path, send a CREATE PATH towards it.
1037 * @param peer PeerInfo of the peer.
1038 * @param t Tunnel for which to create the path, if possible.
1041 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1045 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1046 * needed rescources.
1047 * If peer already exists, reevaluate shortest path and change if different.
1049 * @param t Tunnel we want to add a new peer to
1050 * @param peer PeerInfo of the peer being added
1054 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1058 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1059 * that are no longer needed, as well as nodes of no longer reachable peers.
1060 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1062 * @param t Tunnel from which to remove the path.
1063 * @param peer Short id of the peer which should be removed.
1066 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1070 * Search for a tunnel by global ID using full PeerIdentities.
1072 * @param oid owner of the tunnel.
1073 * @param tid global tunnel number.
1075 * @return tunnel handler, NULL if doesn't exist.
1077 static struct MeshTunnel *
1078 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1082 * Delete an active client from the tunnel.
1088 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1091 * Notify a tunnel that a connection has broken that affects at least
1092 * some of its peers.
1094 * @param t Tunnel affected.
1095 * @param p1 Peer that got disconnected from p2.
1096 * @param p2 Peer that got disconnected from p1.
1098 * @return Short ID of the peer disconnected (either p1 or p2).
1099 * 0 if the tunnel remained unaffected.
1101 static GNUNET_PEER_Id
1102 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1107 * Get the current ack value for a tunnel, for data going from root to leaves,
1108 * taking in account the tunnel mode and the status of all children and clients.
1112 * @return Maximum PID allowed.
1115 tunnel_get_fwd_ack (struct MeshTunnel *t);
1119 * Add a client to a tunnel, initializing all needed data structures.
1121 * @param t Tunnel to which add the client.
1122 * @param c Client which to add to the tunnel.
1125 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1129 * Iterator over edges in a regex block retrieved from the DHT.
1131 * @param cls Closure.
1132 * @param token Token that follows to next state.
1133 * @param len Lenght of token.
1134 * @param key Hash of next state.
1136 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1139 regex_edge_iterator (void *cls,
1142 const struct GNUNET_HashCode *key);
1146 * Find a path to a peer that offers a regex servcie compatible
1147 * with a given string.
1149 * @param key The key of the accepting state.
1150 * @param ctx Context containing info about the string, tunnel, etc.
1153 regex_find_path (const struct GNUNET_HashCode *key,
1154 struct MeshRegexSearchContext *ctx);
1158 * Queue and pass message to core when possible.
1160 * @param cls Closure (type dependant).
1161 * @param type Type of the message, 0 for a raw message.
1162 * @param size Size of the message.
1163 * @param dst Neighbor to send message to.
1164 * @param t Tunnel this message belongs to.
1167 queue_add (void *cls, uint16_t type, size_t size,
1168 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1172 * Free a transmission that was already queued with all resources
1173 * associated to the request.
1175 * @param queue Queue handler to cancel.
1176 * @param clear_cls Is it necessary to free associated cls?
1179 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1183 * @brief Get the next transmittable message from the queue.
1185 * This will be the head, except in the case of being a data packet
1186 * not allowed by the destination peer.
1188 * @param peer Destination peer.
1190 * @return The next viable MeshPeerQueue element to send to that peer.
1191 * NULL when there are no transmittable messages.
1193 struct MeshPeerQueue *
1194 queue_get_next (const struct MeshPeerInfo *peer);
1198 * Core callback to write a queued packet to core buffer
1200 * @param cls Closure (peer info).
1201 * @param size Number of bytes available in buf.
1202 * @param buf Where the to write the message.
1204 * @return number of bytes written to buf
1207 queue_send (void *cls, size_t size, void *buf);
1209 /******************************************************************************/
1210 /************************ ITERATORS ****************************/
1211 /******************************************************************************/
1214 * Iterator over found existing mesh regex blocks that match an ongoing search.
1216 * @param cls closure
1217 * @param key current key code
1218 * @param value value in the hash map
1219 * @return GNUNET_YES if we should continue to iterate,
1223 regex_result_iterator (void *cls,
1224 const struct GNUNET_HashCode * key,
1227 struct MeshRegexBlock *block = value;
1228 struct MeshRegexSearchContext *ctx = cls;
1230 if (GNUNET_YES == ntohl(block->accepting) &&
1231 ctx->position == strlen (ctx->info->description))
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1234 regex_find_path (key, ctx);
1235 return GNUNET_YES; // We found an accept state!
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1240 ctx->position, strlen(ctx->info->description),
1241 ntohl(block->accepting));
1244 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1245 ®ex_edge_iterator, ctx);
1252 * Iterator over edges in a regex block retrieved from the DHT.
1254 * @param cls Closure (context of the search).
1255 * @param token Token that follows to next state.
1256 * @param len Lenght of token.
1257 * @param key Hash of next state.
1259 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1262 regex_edge_iterator (void *cls,
1265 const struct GNUNET_HashCode *key)
1267 struct MeshRegexSearchContext *ctx = cls;
1268 struct MeshRegexSearchContext *new_ctx;
1269 struct MeshRegexSearchInfo *info = ctx->info;
1270 struct GNUNET_DHT_GetHandle *get_h;
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1277 current = &info->description[ctx->position];
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1279 current_len = strlen (info->description) - ctx->position;
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1284 if (len > current_len)
1286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1287 return GNUNET_YES; // Token too long, wont match
1289 if (0 != strncmp (current, token, len))
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1292 return GNUNET_YES; // Token doesn't match
1294 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1295 new_ctx->info = info;
1296 new_ctx->position = ctx->position + len;
1297 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1299 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1302 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1303 ®ex_result_iterator,
1305 return GNUNET_YES; // We are already looking for it
1307 /* Start search in DHT */
1309 GNUNET_DHT_get_start (dht_handle, /* handle */
1310 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1311 key, /* key to search */
1312 dht_replication_level, /* replication level */
1313 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1314 NULL, /* xquery */ // FIXME BLOOMFILTER
1315 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1316 &dht_get_string_handler, new_ctx);
1318 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1319 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1329 * Iterator over hash map entries to cancel DHT GET requests after a
1330 * successful connect_by_string.
1332 * @param cls Closure (unused).
1333 * @param key Current key code (unused).
1334 * @param value Value in the hash map (get handle).
1335 * @return GNUNET_YES if we should continue to iterate,
1339 regex_cancel_dht_get (void *cls,
1340 const struct GNUNET_HashCode * key,
1343 struct GNUNET_DHT_GetHandle *h = value;
1345 GNUNET_DHT_get_stop (h);
1351 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1352 * search for connect_by_string.
1354 * @param cls Closure (unused).
1355 * @param key Current key code (unused).
1356 * @param value MeshRegexBlock in the hash map.
1357 * @return GNUNET_YES if we should continue to iterate,
1361 regex_free_result (void *cls,
1362 const struct GNUNET_HashCode * key,
1366 GNUNET_free (value);
1372 * Regex callback iterator to store own service description in the DHT.
1374 * @param cls closure.
1375 * @param key hash for current state.
1376 * @param proof proof for current state.
1377 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1378 * @param num_edges number of edges leaving current state.
1379 * @param edges edges leaving current state.
1382 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1383 int accepting, unsigned int num_edges,
1384 const struct GNUNET_REGEX_Edge *edges)
1386 struct MeshRegexBlock *block;
1387 struct MeshRegexEdge *block_edge;
1388 enum GNUNET_DHT_RouteOption opt;
1392 unsigned int offset;
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 " regex dht put for state %s\n",
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1406 if (GNUNET_YES == accepting)
1408 struct MeshRegexAccept block;
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1411 " state %s is accepting, putting own id\n",
1413 size = sizeof (block);
1415 block.id = my_full_id;
1417 GNUNET_DHT_put(dht_handle, key,
1418 dht_replication_level,
1419 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1420 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1423 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1428 len = strlen(proof);
1429 size = sizeof (struct MeshRegexBlock) + len;
1430 block = GNUNET_malloc (size);
1433 block->n_proof = htonl (len);
1434 block->n_edges = htonl (num_edges);
1435 block->accepting = htonl (accepting);
1437 /* Store the proof at the end of the block. */
1438 aux = (char *) &block[1];
1439 memcpy (aux, proof, len);
1442 /* Store each edge in a variable length MeshEdge struct at the
1443 * very end of the MeshRegexBlock structure.
1445 for (i = 0; i < num_edges; i++)
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1448 " edge %s towards %s\n",
1450 GNUNET_h2s(&edges[i].destination));
1452 /* aux points at the end of the last block */
1453 len = strlen (edges[i].label);
1454 size += sizeof (struct MeshRegexEdge) + len;
1455 // Calculate offset FIXME is this ok? use size instead?
1456 offset = aux - (char *) block;
1457 block = GNUNET_realloc (block, size);
1458 aux = &((char *) block)[offset];
1459 block_edge = (struct MeshRegexEdge *) aux;
1460 block_edge->key = edges[i].destination;
1461 block_edge->n_token = htonl (len);
1462 aux = (char *) &block_edge[1];
1463 memcpy (aux, edges[i].label, len);
1467 GNUNET_DHT_put(dht_handle, key,
1468 dht_replication_level,
1470 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1472 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1476 GNUNET_free (block);
1481 * Store the regular expression describing a local service into the DHT.
1483 * @param regex The regular expresion.
1486 regex_put (const char *regex)
1488 struct GNUNET_REGEX_Automaton *dfa;
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1491 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1492 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1493 GNUNET_REGEX_automaton_destroy (dfa);
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1499 * Find a path to a peer that offers a regex servcie compatible
1500 * with a given string.
1502 * @param key The key of the accepting state.
1503 * @param ctx Context containing info about the string, tunnel, etc.
1506 regex_find_path (const struct GNUNET_HashCode *key,
1507 struct MeshRegexSearchContext *ctx)
1509 struct GNUNET_DHT_GetHandle *get_h;
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1512 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1513 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1514 key, /* key to search */
1515 dht_replication_level, /* replication level */
1516 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1517 GNUNET_DHT_RO_RECORD_ROUTE,
1518 NULL, /* xquery */ // FIXME BLOOMFILTER
1519 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1520 &dht_get_string_accept_handler, ctx);
1521 GNUNET_break (GNUNET_OK ==
1522 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1525 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1530 * Function called if the connect attempt to a peer found via
1531 * connect_by_string times out. Try to connect to another peer, if any.
1532 * Otherwise try to reconnect to the same peer.
1534 * @param cls Closure (info about regex search).
1535 * @param tc TaskContext.
1538 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1540 struct MeshRegexSearchInfo *info = cls;
1541 struct MeshPeerInfo *peer_info;
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1546 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1547 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1555 if (0 < info->n_peers)
1557 // Select next peer, put current in that spot.
1558 id = info->peers[info->i_peer];
1559 info->peers[info->i_peer] = info->peer;
1560 info->i_peer = (info->i_peer + 1) % info->n_peers;
1564 // Try to connect to same peer again.
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1569 peer_info = peer_info_get_short(id);
1570 tunnel_add_peer (info->t, peer_info);
1572 tunnel_delete_peer (info->t, old);
1573 peer_info_connect (peer_info, info->t);
1574 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1575 ®ex_connect_timeout,
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1582 * Cancel an ongoing regex search in the DHT and free all resources.
1584 * @param ctx The search context.
1587 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1589 struct MeshRegexSearchInfo *info = ctx->info;
1592 GNUNET_free (info->description);
1593 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1594 ®ex_cancel_dht_get, NULL);
1595 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1596 ®ex_free_result, NULL);
1597 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1598 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1599 info->t->regex_ctx = NULL;
1600 for (i = 0; i < info->n_contexts; i++)
1602 GNUNET_free (info->contexts[i]);
1604 if (0 < info->n_contexts)
1605 GNUNET_free (info->contexts);
1606 if (0 < info->n_peers)
1607 GNUNET_free (info->peers);
1608 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1610 GNUNET_SCHEDULER_cancel(info->timeout);
1616 /******************************************************************************/
1617 /************************ PERIODIC FUNCTIONS ****************************/
1618 /******************************************************************************/
1621 * Announce iterator over for each application provided by the peer
1623 * @param cls closure
1624 * @param key current key code
1625 * @param value value in the hash map
1626 * @return GNUNET_YES if we should continue to
1631 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1633 struct PBlock block;
1634 struct MeshClient *c;
1636 block.id = my_full_id;
1637 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1638 GNUNET_assert(NULL != c);
1639 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1640 if (0 == block.type)
1645 block.type = htonl (block.type);
1647 GNUNET_break (NULL !=
1648 GNUNET_DHT_put (dht_handle, key,
1649 dht_replication_level,
1650 GNUNET_DHT_RO_RECORD_ROUTE |
1651 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1652 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1654 (const char *) &block,
1655 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1657 app_announce_time, NULL, NULL));
1663 * Periodically announce what applications are provided by local clients
1666 * @param cls closure
1667 * @param tc task context
1670 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1672 struct MeshClient *c = cls;
1675 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1677 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1681 DEBUG_DHT ("Starting PUT for regex\n");
1683 for (i = 0; i < c->n_regex; i++)
1685 regex_put (c->regexes[i]);
1687 c->regex_announce_task =
1688 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1689 DEBUG_DHT ("Finished PUT for regex\n");
1696 * Periodically announce what applications are provided by local clients
1699 * @param cls closure
1700 * @param tc task context
1703 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1705 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1707 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1711 DEBUG_DHT ("Starting PUT for apps\n");
1713 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1715 announce_applications_task =
1716 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1718 DEBUG_DHT ("Finished PUT for apps\n");
1725 * Periodically announce self id in the DHT
1727 * @param cls closure
1728 * @param tc task context
1731 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1733 struct PBlock block;
1735 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1737 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1741 * - Set data expiration in function of X
1742 * - Adapt X to churn
1744 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1746 block.id = my_full_id;
1747 block.type = htonl (0);
1748 GNUNET_DHT_put (dht_handle, /* DHT handle */
1749 &my_full_id.hashPubKey, /* Key to use */
1750 dht_replication_level, /* Replication level */
1751 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1752 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1753 sizeof (block), /* Size of the data */
1754 (const char *) &block, /* Data itself */
1755 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1756 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1757 NULL, /* Continuation */
1758 NULL); /* Continuation closure */
1760 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1764 /******************************************************************************/
1765 /****************** GENERAL HELPER FUNCTIONS ************************/
1766 /******************************************************************************/
1769 * Decrements the reference counter and frees all resources if needed
1771 * @param mesh_data Data Descriptor used in a multicast message.
1772 * Freed no longer needed (last message).
1775 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1777 if (0 == --(mesh_data->reference_counter))
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1780 if (NULL != mesh_data->task)
1782 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1784 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1786 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1788 GNUNET_free (mesh_data->task);
1790 GNUNET_free (mesh_data->data);
1791 GNUNET_free (mesh_data);
1797 * Check if client has registered with the service and has not disconnected
1799 * @param client the client to check
1801 * @return non-NULL if client exists in the global DLL
1803 static struct MeshClient *
1804 client_get (struct GNUNET_SERVER_Client *client)
1806 struct MeshClient *c;
1811 if (c->handle == client)
1820 * Checks if a given client has subscribed to certain message type
1822 * @param message_type Type of message to check
1823 * @param c Client to check
1825 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1827 * FIXME: use of crypto_hash slows it down
1828 * The hash function alone takes 8-10us out of the ~55us for the whole
1829 * process of retransmitting the message from one local client to another.
1830 * Find faster implementation!
1833 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1835 struct GNUNET_HashCode hc;
1837 if (NULL == c->types)
1840 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1841 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1846 * Allow a client to send more data after transmitting a multicast message
1847 * which some neighbor has not yet accepted altough a reasonable time has
1850 * @param cls Closure (DataDescriptor containing the task identifier)
1851 * @param tc Task Context
1853 * FIXME fc replace with proper ack
1856 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1858 struct MeshData *mdata = cls;
1860 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1863 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1864 mdata->reference_counter);
1865 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1866 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1871 * Check whether client wants traffic from a tunnel.
1873 * @param c Client to check.
1874 * @param t Tunnel to be found.
1876 * @return GNUNET_YES if client knows tunnel.
1878 * TODO look in client hashmap
1881 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1885 for (i = 0; i < t->nclients; i++)
1886 if (t->clients[i] == c)
1893 * Check whether client has been informed about a tunnel.
1895 * @param c Client to check.
1896 * @param t Tunnel to be found.
1898 * @return GNUNET_YES if client knows tunnel.
1900 * TODO look in client hashmap
1903 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1907 for (i = 0; i < t->nignore; i++)
1908 if (t->ignore[i] == c)
1910 return client_wants_tunnel(c, t);
1915 * Marks a client as uninterested in traffic from the tunnel, updating both
1916 * client and tunnel to reflect this.
1918 * @param c Client that doesn't want traffic anymore.
1919 * @param t Tunnel which should be ignored.
1921 * FIXME when to delete an incoming tunnel?
1924 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1926 struct GNUNET_HashCode hash;
1928 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1929 GNUNET_break (GNUNET_YES ==
1930 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1932 GNUNET_break (GNUNET_YES ==
1933 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1934 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1935 tunnel_delete_active_client (t, c);
1936 GNUNET_array_append (t->ignore, t->nignore, c);
1941 * Deletes a tunnel from a client (either owner or destination). To be used on
1942 * tunnel destroy, otherwise, use client_ignore_tunnel.
1944 * @param c Client whose tunnel to delete.
1945 * @param t Tunnel which should be deleted.
1948 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1950 struct GNUNET_HashCode hash;
1954 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1955 GNUNET_assert (GNUNET_YES ==
1956 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1962 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1964 GNUNET_assert (GNUNET_YES ==
1965 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1969 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1977 * Send the message to all clients that have subscribed to its type
1979 * @param msg Pointer to the message itself
1980 * @param payload Pointer to the payload of the message.
1981 * @param t The tunnel to whose clients this message goes.
1983 * @return number of clients this message was sent to
1986 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1987 const struct GNUNET_MessageHeader *payload,
1988 struct MeshTunnel *t)
1990 struct MeshClient *c;
1991 MESH_TunnelNumber *tid;
1994 char cbuf[htons (msg->size)];
1996 type = ntohs (payload->type);
1997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1999 GNUNET_MESH_DEBUG_M2S (type));
2001 memcpy (cbuf, msg, sizeof (cbuf));
2002 switch (htons (msg->type))
2004 struct GNUNET_MESH_Unicast *uc;
2005 struct GNUNET_MESH_Multicast *mc;
2006 struct GNUNET_MESH_ToOrigin *to;
2008 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2009 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2012 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2013 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2016 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2017 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2025 for (count = 0, c = clients; c != NULL; c = c->next)
2027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2028 if (client_is_subscribed (type, c))
2030 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2034 *tid = htonl (t->local_tid);
2038 if (GNUNET_NO == client_knows_tunnel (c, t))
2040 /* This client doesn't know the tunnel */
2041 struct GNUNET_MESH_TunnelNotification tmsg;
2042 struct GNUNET_HashCode hash;
2044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2045 tmsg.header.size = htons (sizeof (tmsg));
2046 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2047 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2048 tmsg.tunnel_id = htonl (t->local_tid_dest);
2049 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2050 &tmsg.header, GNUNET_NO);
2051 tunnel_add_client (t, c);
2052 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2054 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2055 c->incoming_tunnels, &hash, t,
2056 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2058 *tid = htonl (t->local_tid_dest);
2061 /* Check if the client wants to get traffic from the tunnel */
2062 if (GNUNET_NO == client_wants_tunnel(c, t))
2065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2066 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2067 (struct GNUNET_MessageHeader
2068 *) cbuf, GNUNET_NO);
2077 * Notify the client that owns the tunnel that a peer has connected to it
2078 * (the requested path to it has been confirmed).
2080 * @param t Tunnel whose owner to notify
2081 * @param id Short id of the peer that has connected
2084 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2086 struct GNUNET_MESH_PeerControl pc;
2088 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2089 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2090 pc.tunnel_id = htonl (t->local_tid);
2091 GNUNET_PEER_resolve (id, &pc.peer);
2092 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2098 * Notify all clients (not depending on registration status) that the incoming
2099 * tunnel is no longer valid.
2101 * @param t Tunnel that was destroyed.
2104 send_clients_tunnel_destroy (struct MeshTunnel *t)
2106 struct GNUNET_MESH_TunnelMessage msg;
2108 msg.header.size = htons (sizeof (msg));
2109 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2110 msg.tunnel_id = htonl (t->local_tid_dest);
2111 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2116 * Notify clients of tunnel disconnections, if needed.
2117 * In case the origin disconnects, the destination clients get a tunnel destroy
2118 * notification. If the last destination disconnects (only one remaining client
2119 * in tunnel), the origin gets a (local ID) peer disconnected.
2120 * Note that the function must be called BEFORE removing the client from
2123 * @param t Tunnel that was destroyed.
2124 * @param c Client that disconnected.
2127 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2133 struct GNUNET_MESH_TunnelMessage msg;
2135 msg.header.size = htons (sizeof (msg));
2136 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2137 msg.tunnel_id = htonl (t->local_tid_dest);
2138 for (i = 0; i < t->nclients; i++)
2139 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2140 &msg.header, GNUNET_NO);
2142 // FIXME when to disconnect an incoming tunnel?
2143 else if (1 == t->nclients && NULL != t->owner)
2145 struct GNUNET_MESH_PeerControl msg;
2147 msg.header.size = htons (sizeof (msg));
2148 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2149 msg.tunnel_id = htonl (t->local_tid);
2150 msg.peer = my_full_id;
2151 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2152 &msg.header, GNUNET_NO);
2158 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2159 * and insert it in the appropiate structures if the peer is not known yet.
2161 * @param peer Full identity of the peer.
2163 * @return Existing or newly created peer info.
2165 static struct MeshPeerInfo *
2166 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2168 struct MeshPeerInfo *peer_info;
2170 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2171 if (NULL == peer_info)
2174 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2175 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2176 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2177 peer_info->id = GNUNET_PEER_intern (peer);
2185 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2186 * and insert it in the appropiate structures if the peer is not known yet.
2188 * @param peer Short identity of the peer.
2190 * @return Existing or newly created peer info.
2192 static struct MeshPeerInfo *
2193 peer_info_get_short (const GNUNET_PEER_Id peer)
2195 struct GNUNET_PeerIdentity id;
2197 GNUNET_PEER_resolve (peer, &id);
2198 return peer_info_get (&id);
2203 * Iterator to remove the tunnel from the list of tunnels a peer participates
2206 * @param cls Closure (tunnel info)
2207 * @param key GNUNET_PeerIdentity of the peer (unused)
2208 * @param value PeerInfo of the peer
2210 * @return always GNUNET_YES, to keep iterating
2213 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2215 struct MeshTunnel *t = cls;
2216 struct MeshPeerInfo *peer = value;
2219 for (i = 0; i < peer->ntunnels; i++)
2222 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2225 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2226 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2235 * Core callback to write a pre-constructed data packet to core buffer
2237 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2238 * @param size Number of bytes available in buf.
2239 * @param buf Where the to write the message.
2241 * @return number of bytes written to buf
2244 send_core_data_raw (void *cls, size_t size, void *buf)
2246 struct MeshTransmissionDescriptor *info = cls;
2247 struct GNUNET_MessageHeader *msg;
2250 GNUNET_assert (NULL != info);
2251 GNUNET_assert (NULL != info->mesh_data);
2252 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2253 total_size = ntohs (msg->size);
2255 if (total_size > size)
2260 memcpy (buf, msg, total_size);
2261 data_descriptor_decrement_rc (info->mesh_data);
2268 * Sends an already built non-multicast message to a peer,
2269 * properly registrating all used resources.
2271 * @param message Message to send. Function makes a copy of it.
2272 * @param peer Short ID of the neighbor whom to send the message.
2273 * @param t Tunnel on which this message is transmitted.
2276 send_message (const struct GNUNET_MessageHeader *message,
2277 const struct GNUNET_PeerIdentity *peer,
2278 struct MeshTunnel *t)
2280 struct MeshTransmissionDescriptor *info;
2281 struct MeshPeerInfo *neighbor;
2282 struct MeshPeerPath *p;
2286 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2288 size = ntohs (message->size);
2289 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2290 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2291 info->mesh_data->data = GNUNET_malloc (size);
2292 memcpy (info->mesh_data->data, message, size);
2293 type = ntohs(message->type);
2294 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2296 struct GNUNET_MESH_Unicast *m;
2298 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2299 m->ttl = htonl (ntohl (m->ttl) - 1);
2301 info->mesh_data->data_len = size;
2302 info->mesh_data->reference_counter = 1;
2303 neighbor = peer_info_get (peer);
2304 for (p = neighbor->path_head; NULL != p; p = p->next)
2314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2315 " %s IS NOT DIRECTLY CONNECTED\n",
2317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2320 for (p = neighbor->path_head; NULL != p; p = p->next)
2322 struct GNUNET_PeerIdentity debug_id;
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326 " path with %u hops through:\n",
2328 for (i = 0; i < p->length; i++)
2330 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2333 i, GNUNET_i2s(&debug_id));
2337 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2338 GNUNET_free (info->mesh_data->data);
2339 GNUNET_free (info->mesh_data);
2343 info->peer = neighbor;
2344 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2347 * TODO: in this case we only need the service to retransmit
2348 * the message down the path. If we pass the real type to queue_add,
2349 * queue_send will try to build the message from scratch. This can
2350 * probably be done by some other way instead of deleteing the type
2364 * Sends a CREATE PATH message for a path to a peer, properly registrating
2365 * all used resources.
2367 * @param peer PeerInfo of the final peer for whom this path is being created.
2368 * @param p Path itself.
2369 * @param t Tunnel for which the path is created.
2372 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2373 struct MeshTunnel *t)
2375 struct GNUNET_PeerIdentity id;
2376 struct MeshPathInfo *path_info;
2377 struct MeshPeerInfo *neighbor;
2383 p = tree_get_path_to_peer (t->tree, peer->id);
2390 for (i = 0; i < p->length; i++)
2392 if (p->peers[i] == myid)
2395 if (i >= p->length - 1)
2401 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2403 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2404 path_info->path = p;
2406 neighbor = peer_info_get (&id);
2407 path_info->peer = neighbor;
2408 queue_add (path_info,
2409 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2410 sizeof (struct GNUNET_MESH_ManipulatePath) +
2411 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2418 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2420 * @param t Tunnel whose path to destroy.
2421 * @param destination Short ID of the peer to whom the path to destroy.
2424 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2426 struct MeshPeerPath *p;
2429 p = tree_get_path_to_peer (t->tree, destination);
2435 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2436 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2438 struct GNUNET_MESH_ManipulatePath *msg;
2439 struct GNUNET_PeerIdentity *pi;
2443 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2444 msg->header.size = htons (size);
2445 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2446 msg->tid = htonl (t->id.tid);
2447 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2448 for (i = 0; i < p->length; i++)
2450 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2452 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2459 * Try to establish a new connection to this peer.
2460 * Use the best path for the given tunnel.
2461 * If the peer doesn't have any path to it yet, try to get one.
2462 * If the peer already has some path, send a CREATE PATH towards it.
2464 * @param peer PeerInfo of the peer.
2465 * @param t Tunnel for which to create the path, if possible.
2468 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2470 struct MeshPeerPath *p;
2471 struct MeshPathInfo *path_info;
2473 if (NULL != peer->path_head)
2475 p = tree_get_path_to_peer (t->tree, peer->id);
2482 // FIXME always send create path to self
2485 send_create_path (peer, p, t);
2489 struct GNUNET_HashCode hash;
2492 send_client_peer_connected (t, myid);
2493 t->local_tid_dest = next_local_tid++;
2494 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2497 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2505 else if (NULL == peer->dhtget)
2507 struct GNUNET_PeerIdentity id;
2509 GNUNET_PEER_resolve (peer->id, &id);
2510 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2511 path_info->peer = peer;
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2514 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2515 peer->dhtgetcls = path_info;
2516 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2517 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2518 &id.hashPubKey, /* key to search */
2519 dht_replication_level, /* replication level */
2520 GNUNET_DHT_RO_RECORD_ROUTE |
2521 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2522 NULL, /* xquery */ // FIXME BLOOMFILTER
2523 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2524 &dht_get_id_handler, path_info);
2526 /* Otherwise, there is no path but the DHT get is already started. */
2531 * Task to delay the connection of a peer
2533 * @param cls Closure (path info with tunnel and peer to connect).
2534 * Will be free'd on exection.
2535 * @param tc TaskContext
2538 peer_info_connect_task (void *cls,
2539 const struct GNUNET_SCHEDULER_TaskContext *tc)
2541 struct MeshPathInfo *path_info = cls;
2543 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2548 peer_info_connect (path_info->peer, path_info->t);
2554 * Destroy the peer_info and free any allocated resources linked to it
2556 * @param pi The peer_info to destroy.
2558 * @return GNUNET_OK on success
2561 peer_info_destroy (struct MeshPeerInfo *pi)
2563 struct GNUNET_PeerIdentity id;
2564 struct MeshPeerPath *p;
2565 struct MeshPeerPath *nextp;
2567 GNUNET_PEER_resolve (pi->id, &id);
2568 GNUNET_PEER_change_rc (pi->id, -1);
2571 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2575 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2577 if (NULL != pi->dhtget)
2579 GNUNET_DHT_get_stop (pi->dhtget);
2580 GNUNET_free (pi->dhtgetcls);
2586 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2596 * Remove all paths that rely on a direct connection between p1 and p2
2597 * from the peer itself and notify all tunnels about it.
2599 * @param peer PeerInfo of affected peer.
2600 * @param p1 GNUNET_PEER_Id of one peer.
2601 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2604 * TODO: optimize (see below)
2607 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2610 struct MeshPeerPath *p;
2611 struct MeshPeerPath *aux;
2612 struct MeshPeerInfo *peer_d;
2614 unsigned int destroyed;
2619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2621 p = peer->path_head;
2625 for (i = 0; i < (p->length - 1); i++)
2627 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2628 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2630 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2641 for (i = 0; i < peer->ntunnels; i++)
2643 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2647 * Problem: one or more peers have been deleted from the tunnel tree.
2648 * We don't know who they are to try to add them again.
2649 * We need to try to find a new path for each of the disconnected peers.
2650 * Some of them might already have a path to reach them that does not
2651 * involve p1 and p2. Adding all anew might render in a better tree than
2652 * the trivial immediate fix.
2654 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2655 * its children will be reachable trough him.
2657 peer_d = peer_info_get_short (d);
2660 for (p = peer_d->path_head; NULL != p; p = p->next)
2662 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2670 /* No callback, as peer will be already disconnected and a connection
2671 * scheduled by tunnel_notify_connection_broken.
2673 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2677 peer_info_connect (peer_d, peer->tunnels[i]);
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2685 * Add the path to the peer and update the path used to reach it in case this
2688 * @param peer_info Destination peer to add the path to.
2689 * @param path New path to add. Last peer must be the peer in arg 1.
2690 * Path will be either used of freed if already known.
2691 * @param trusted Do we trust that this path is real?
2694 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2697 struct MeshPeerPath *aux;
2701 if ((NULL == peer_info) || (NULL == path))
2704 path_destroy (path);
2707 if (path->peers[path->length - 1] != peer_info->id)
2710 path_destroy (path);
2713 if (path->length <= 2 && GNUNET_NO == trusted)
2715 /* Only allow CORE to tell us about direct paths */
2716 path_destroy (path);
2719 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2720 for (l = 1; l < path->length; l++)
2722 if (path->peers[l] == myid)
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2725 for (l2 = 0; l2 < path->length - l; l2++)
2727 path->peers[l2] = path->peers[l + l2];
2732 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2737 struct GNUNET_PeerIdentity id;
2739 GNUNET_PEER_resolve (peer_info->id, &id);
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2741 path->length, GNUNET_i2s (&id));
2744 l = path_get_length (path);
2751 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2752 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2754 l2 = path_get_length (aux);
2757 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2758 peer_info->path_tail, aux, path);
2763 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2765 path_destroy (path);
2770 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2777 * Add the path to the origin peer and update the path used to reach it in case
2778 * this is the shortest.
2779 * The path is given in peer_info -> destination, therefore we turn the path
2780 * upside down first.
2782 * @param peer_info Peer to add the path to, being the origin of the path.
2783 * @param path New path to add after being inversed.
2784 * @param trusted Do we trust that this path is real?
2787 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2788 struct MeshPeerPath *path, int trusted)
2791 peer_info_add_path (peer_info, path, trusted);
2796 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2797 * to obtain a local peer -> destination path and interning the peer ids.
2799 * @return Newly allocated and created path
2801 static struct MeshPeerPath *
2802 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2803 unsigned int get_path_length,
2804 const struct GNUNET_PeerIdentity *put_path,
2805 unsigned int put_path_length)
2807 struct MeshPeerPath *p;
2813 GNUNET_PEER_change_rc (myid, 1);
2814 i = get_path_length;
2815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2816 for (i--; i >= 0; i--)
2818 id = GNUNET_PEER_intern (&get_path[i]);
2819 if (p->length > 0 && id == p->peers[p->length - 1])
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2822 GNUNET_PEER_change_rc (id, -1);
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2827 GNUNET_i2s (&get_path[i]));
2829 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2830 p->peers[p->length - 1] = id;
2833 i = put_path_length;
2834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2835 for (i--; i >= 0; i--)
2837 id = GNUNET_PEER_intern (&put_path[i]);
2840 /* PUT path went through us, so discard the path up until now and start
2841 * from here to get a much shorter (and loop-free) path.
2846 if (p->length > 0 && id == p->peers[p->length - 1])
2848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2849 GNUNET_PEER_change_rc (id, -1);
2853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2854 GNUNET_i2s (&put_path[i]));
2856 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2857 p->peers[p->length - 1] = id;
2861 if (get_path_length > 0)
2862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2863 GNUNET_i2s (&get_path[0]));
2864 if (put_path_length > 0)
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2866 GNUNET_i2s (&put_path[0]));
2867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2869 for (i = 0; i < p->length; i++)
2871 struct GNUNET_PeerIdentity peer_id;
2873 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2875 GNUNET_i2s (&peer_id));
2883 * Adds a path to the peer_infos of all the peers in the path
2885 * @param p Path to process.
2886 * @param confirmed Whether we know if the path works or not.
2889 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2893 /* TODO: invert and add */
2894 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2895 for (i++; i < p->length; i++)
2897 struct MeshPeerInfo *aux;
2898 struct MeshPeerPath *copy;
2900 aux = peer_info_get_short (p->peers[i]);
2901 copy = path_duplicate (p);
2902 copy->length = i + 1;
2903 peer_info_add_path (aux, copy, GNUNET_NO);
2909 * Send keepalive packets for a peer
2911 * @param cls Closure (tunnel for which to send the keepalive).
2912 * @param tc Notification context.
2914 * TODO: implement explicit multicast keepalive?
2917 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2921 * Search for a tunnel among the incoming tunnels
2923 * @param tid the local id of the tunnel
2925 * @return tunnel handler, NULL if doesn't exist
2927 static struct MeshTunnel *
2928 tunnel_get_incoming (MESH_TunnelNumber tid)
2930 struct GNUNET_HashCode hash;
2932 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2933 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2934 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2939 * Search for a tunnel among the tunnels for a client
2941 * @param c the client whose tunnels to search in
2942 * @param tid the local id of the tunnel
2944 * @return tunnel handler, NULL if doesn't exist
2946 static struct MeshTunnel *
2947 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2949 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2951 return tunnel_get_incoming (tid);
2955 struct GNUNET_HashCode hash;
2957 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2958 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2964 * Search for a tunnel by global ID using PEER_ID
2966 * @param pi owner of the tunnel
2967 * @param tid global tunnel number
2969 * @return tunnel handler, NULL if doesn't exist
2971 static struct MeshTunnel *
2972 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2974 struct MESH_TunnelID id;
2975 struct GNUNET_HashCode hash;
2980 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2981 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2986 * Search for a tunnel by global ID using full PeerIdentities
2988 * @param oid owner of the tunnel
2989 * @param tid global tunnel number
2991 * @return tunnel handler, NULL if doesn't exist
2993 static struct MeshTunnel *
2994 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2996 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3001 * Delete an active client from the tunnel.
3007 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3011 for (i = 0; i < t->nclients; i++)
3013 if (t->clients[i] == c)
3015 t->clients[i] = t->clients[t->nclients - 1];
3016 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3017 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3019 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3027 * Delete an ignored client from the tunnel.
3033 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3037 for (i = 0; i < t->nignore; i++)
3039 if (t->ignore[i] == c)
3041 t->ignore[i] = t->ignore[t->nignore - 1];
3042 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3050 * Delete a client from the tunnel. It should be only done on
3051 * client disconnection, otherwise use client_ignore_tunnel.
3057 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3059 tunnel_delete_ignored_client (t, c);
3060 tunnel_delete_active_client (t, c);
3065 * Iterator to free MeshTunnelChildInfo of tunnel children.
3067 * @param cls Closure (tunnel info).
3068 * @param key Hash of GNUNET_PEER_Id (unused).
3069 * @param value MeshTunnelChildInfo of the child.
3071 * @return always GNUNET_YES, to keep iterating
3074 tunnel_destroy_child (void *cls,
3075 const struct GNUNET_HashCode * key,
3078 struct MeshTunnelChildInfo *cinfo = value;
3079 struct MeshTunnel *t = cls;
3083 for (c = 0; c < cinfo->send_buffer_n; c++)
3085 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3086 if (NULL != cinfo->send_buffer[i])
3087 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3090 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3092 GNUNET_free_non_null (cinfo->send_buffer);
3093 GNUNET_free (cinfo);
3099 * Callback used to notify a client owner of a tunnel that a peer has
3100 * disconnected, most likely because of a path change.
3102 * @param cls Closure (tunnel this notification is about).
3103 * @param peer_id Short ID of disconnected peer.
3106 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3108 struct MeshTunnel *t = cls;
3109 struct MeshPeerInfo *peer;
3110 struct MeshPathInfo *path_info;
3112 if (NULL != t->owner && NULL != nc)
3114 struct GNUNET_MESH_PeerControl msg;
3116 msg.header.size = htons (sizeof (msg));
3117 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3118 msg.tunnel_id = htonl (t->local_tid);
3119 GNUNET_PEER_resolve (peer_id, &msg.peer);
3120 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3121 &msg.header, GNUNET_NO);
3123 peer = peer_info_get_short (peer_id);
3124 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3125 path_info->peer = peer;
3127 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3132 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3133 * needed rescources.
3134 * If peer already exists, reevaluate shortest path and change if different.
3136 * @param t Tunnel we want to add a new peer to
3137 * @param peer PeerInfo of the peer being added
3141 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3143 struct GNUNET_PeerIdentity id;
3144 struct MeshPeerPath *best_p;
3145 struct MeshPeerPath *p;
3146 unsigned int best_cost;
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3150 GNUNET_PEER_resolve (peer->id, &id);
3152 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3155 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3156 GNUNET_assert (GNUNET_OK ==
3157 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3159 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3162 if (NULL != (p = peer->path_head))
3165 best_cost = tree_get_path_cost (t->tree, p);
3168 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3175 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3176 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3177 t->path_refresh_task =
3178 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3182 /* Start a DHT get */
3183 peer_info_connect (peer, t);
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3189 * Add a path to a tunnel which we don't own, just to remember the next hop.
3190 * If destination node was already in the tunnel, the first hop information
3191 * will be replaced with the new path.
3193 * @param t Tunnel we want to add a new peer to
3194 * @param p Path to add
3195 * @param own_pos Position of local node in path.
3199 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3200 unsigned int own_pos)
3202 struct GNUNET_PeerIdentity id;
3204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3205 GNUNET_assert (0 != own_pos);
3206 tree_add_path (t->tree, p, NULL, NULL);
3207 if (own_pos < p->length - 1)
3209 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3210 tree_update_first_hops (t->tree, myid, &id);
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3216 * Add a client to a tunnel, initializing all needed data structures.
3218 * @param t Tunnel to which add the client.
3219 * @param c Client which to add to the tunnel.
3222 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3224 struct MeshTunnelClientInfo clinfo;
3226 GNUNET_array_append (t->clients, t->nclients, c);
3227 clinfo.fwd_ack = t->fwd_pid + 1;
3228 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3229 clinfo.fwd_pid = t->fwd_pid;
3230 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3232 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3237 * Notifies a tunnel that a connection has broken that affects at least
3238 * some of its peers. Sends a notification towards the root of the tree.
3239 * In case the peer is the owner of the tree, notifies the client that owns
3240 * the tunnel and tries to reconnect.
3242 * @param t Tunnel affected.
3243 * @param p1 Peer that got disconnected from p2.
3244 * @param p2 Peer that got disconnected from p1.
3246 * @return Short ID of the peer disconnected (either p1 or p2).
3247 * 0 if the tunnel remained unaffected.
3249 static GNUNET_PEER_Id
3250 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3256 tree_notify_connection_broken (t->tree, p1, p2,
3257 &tunnel_notify_client_peer_disconnected,
3259 if (myid != p1 && myid != p2)
3265 if (tree_get_predecessor (t->tree) != 0)
3267 /* We are the peer still connected, notify owner of the disconnection. */
3268 struct GNUNET_MESH_PathBroken msg;
3269 struct GNUNET_PeerIdentity neighbor;
3271 msg.header.size = htons (sizeof (msg));
3272 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3273 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3274 msg.tid = htonl (t->id.tid);
3275 msg.peer1 = my_full_id;
3276 GNUNET_PEER_resolve (pid, &msg.peer2);
3277 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3278 send_message (&msg.header, &neighbor, t);
3286 * Send a multicast packet to a neighbor.
3288 * @param cls Closure (Info about the multicast packet)
3289 * @param neighbor_id Short ID of the neighbor to send the packet to.
3292 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3294 struct MeshData *mdata = cls;
3295 struct MeshTransmissionDescriptor *info;
3296 struct GNUNET_PeerIdentity neighbor;
3298 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3300 info->mesh_data = mdata;
3301 (mdata->reference_counter) ++;
3302 info->destination = neighbor_id;
3303 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3305 GNUNET_i2s (&neighbor));
3306 info->peer = peer_info_get (&neighbor);
3307 GNUNET_assert (NULL != info->peer);
3309 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3310 info->mesh_data->data_len,
3317 * Queue a message in a tunnel in multicast, sending a copy to each child node
3318 * down the local one in the tunnel tree.
3320 * @param t Tunnel in which to send the data.
3321 * @param msg Message to be sent.
3322 * @param internal Has the service generated this message?
3325 tunnel_send_multicast (struct MeshTunnel *t,
3326 const struct GNUNET_MessageHeader *msg,
3329 struct MeshData *mdata;
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3332 " sending a multicast packet...\n");
3334 mdata = GNUNET_malloc (sizeof (struct MeshData));
3335 mdata->data_len = ntohs (msg->size);
3337 mdata->data = GNUNET_malloc (mdata->data_len);
3338 memcpy (mdata->data, msg, mdata->data_len);
3339 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3341 struct GNUNET_MESH_Multicast *mcast;
3343 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3344 if (t->fwd_queue_n >= t->fwd_queue_max)
3347 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3349 " message from %s!\n",
3350 GNUNET_i2s(&mcast->oid));
3351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3352 " message at %s!\n",
3353 GNUNET_i2s(&my_full_id));
3354 GNUNET_free (mdata->data);
3355 GNUNET_free (mdata);
3359 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3361 ntohl (mcast->ttl));
3365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3367 if (NULL != t->owner &&
3368 GNUNET_YES != t->owner->shutting_down &&
3369 GNUNET_NO == internal)
3371 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3373 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3379 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3380 if (mdata->reference_counter == 0)
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3383 " no one to send data to\n");
3384 GNUNET_free (mdata->data);
3385 if (NULL != mdata->task)
3387 GNUNET_SCHEDULER_cancel(*(mdata->task));
3388 GNUNET_free (mdata->task);
3389 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3391 GNUNET_free (mdata);
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3395 " sending a multicast packet done\n");
3401 * Increase the SKIP value of all peers that
3402 * have not received a unicast message.
3404 * @param cls Closure (ID of the peer that HAS received the message).
3405 * @param key ID of the neighbor.
3406 * @param value Information about the neighbor.
3408 * @return GNUNET_YES to keep iterating.
3411 tunnel_add_skip (void *cls,
3412 const struct GNUNET_HashCode * key,
3415 struct GNUNET_PeerIdentity *neighbor = cls;
3416 struct MeshTunnelChildInfo *cinfo = value;
3418 /* TODO compare only pointers? key == neighbor? */
3419 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3429 * @brief Get neighbor's Flow Control information.
3431 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3432 * descendant of the local node in a certain tunnel.
3433 * If the info is not yet there (recently created path), creates the data struct
3434 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3437 * @param t Tunnel related.
3438 * @param peer Neighbor whose Flow Control info is needed.
3440 * @return Neighbor's Flow Control info.
3442 static struct MeshTunnelChildInfo *
3443 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3444 const struct GNUNET_PeerIdentity *peer)
3446 struct MeshTunnelChildInfo *cinfo;
3447 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3453 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3454 cinfo->id = GNUNET_PEER_intern (peer);
3455 cinfo->skip = t->fwd_pid;
3457 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3458 cinfo->fwd_ack = t->fwd_pid + delta;
3459 cinfo->bck_ack = delta;
3461 cinfo->send_buffer =
3462 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3464 GNUNET_assert (GNUNET_OK ==
3465 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3468 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3475 * Get the Flow Control info of a client.
3477 * @param t Tunnel on which to look.
3478 * @param c Client whose ACK to get.
3480 * @return ACK value.
3482 static struct MeshTunnelClientInfo *
3483 tunnel_get_client_fc (struct MeshTunnel *t,
3484 struct MeshClient *c)
3488 for (i = 0; i < t->nclients; i++)
3490 if (t->clients[i] != c)
3492 return &t->clients_fc[i];
3495 return NULL; // avoid compiler / coverity complaints
3500 * Iterator to get the appropiate ACK value from all children nodes.
3502 * @param cls Closue (tunnel).
3503 * @param id Id of the child node.
3506 tunnel_get_child_fwd_ack (void *cls,
3509 struct GNUNET_PeerIdentity peer_id;
3510 struct MeshTunnelChildInfo *cinfo;
3511 struct MeshTunnelChildIteratorContext *ctx = cls;
3512 struct MeshTunnel *t = ctx->t;
3515 GNUNET_PEER_resolve (id, &peer_id);
3516 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3517 ack = cinfo->fwd_ack;
3520 if (GNUNET_NO == ctx->init)
3522 ctx->max_child_ack = ack;
3523 ctx->init = GNUNET_YES;
3526 if (GNUNET_YES == t->speed_min)
3528 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3532 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3539 * Get the maximum PID allowed to transmit to any
3540 * tunnel child of the local peer, depending on the tunnel
3541 * buffering/speed settings.
3545 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3548 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3550 struct MeshTunnelChildIteratorContext ctx;
3552 ctx.max_child_ack = 0;
3554 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3556 if (0 == ctx.nchildren)
3558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3559 " tunnel has no children, no FWD ACK\n");
3563 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3564 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3566 return (int64_t) ctx.max_child_ack;
3571 * Set the FWD ACK value of a client in a particular tunnel.
3573 * @param t Tunnel affected.
3574 * @param c Client whose ACK to set.
3575 * @param ack ACK value.
3578 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3579 struct MeshClient *c,
3584 for (i = 0; i < t->nclients; i++)
3586 if (t->clients[i] != c)
3588 t->clients_fc[i].fwd_ack = ack;
3596 * Get the highest ACK value of all clients in a particular tunnel,
3597 * according to the buffering/speed settings.
3599 * @param t Tunnel on which to look.
3601 * @return Corresponding ACK value (max uint32_t).
3602 * If no clients are suscribed, -1LL.
3605 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3610 if (0 == t->nclients)
3612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3613 " tunnel has no clients, no FWD ACK\n");
3617 for (ack = -1LL, i = 0; i < t->nclients; i++)
3620 (GNUNET_YES == t->speed_min &&
3621 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3622 (GNUNET_NO == t->speed_min &&
3623 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3625 ack = t->clients_fc[i].fwd_ack;
3629 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3630 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3632 return (uint32_t) ack;
3637 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3638 * mode and the status of all children nodes.
3642 * @return Maximum PID allowed.
3645 tunnel_get_fwd_ack (struct MeshTunnel *t)
3649 uint32_t buffer_free;
3653 count = t->fwd_pid - t->skip;
3654 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3655 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3656 child_ack = tunnel_get_children_fwd_ack (t);
3657 client_ack = tunnel_get_clients_fwd_ack (t);
3658 if (-1LL == child_ack)
3660 // Node has no children, child_ack AND core buffer are irrelevant.
3661 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3662 return (uint32_t) client_ack;
3664 if (-1LL == client_ack)
3668 if (GNUNET_YES == t->speed_min)
3670 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3671 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3675 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3676 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3678 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3679 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3681 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3682 count, buffer_free, child_ack, client_ack, ack);
3688 * Build a local ACK message and send it to a local client.
3690 * @param t Tunnel on which to send the ACK.
3691 * @param c Client to whom send the ACK.
3692 * @param ack Value of the ACK.
3695 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3697 struct GNUNET_MESH_LocalAck msg;
3699 msg.header.size = htons (sizeof (msg));
3700 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3701 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3702 msg.max_pid = htonl (ack);
3703 GNUNET_SERVER_notification_context_unicast(nc,
3710 * Build an ACK message and queue it to send to the given peer.
3712 * @param t Tunnel on which to send the ACK.
3713 * @param peer Peer to whom send the ACK.
3714 * @param ack Value of the ACK.
3717 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3719 struct GNUNET_MESH_ACK msg;
3721 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3722 msg.header.size = htons (sizeof (msg));
3723 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3724 msg.pid = htonl (ack);
3725 msg.tid = htonl (t->id.tid);
3727 send_message (&msg.header, peer, t);
3732 * Notify a the owner of a tunnel about how many more
3733 * payload packages will we accept on a given tunnel.
3735 * @param t Tunnel on which to send the ACK.
3738 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3743 "Sending client FWD ACK on tunnel %X\n",
3746 ack = tunnel_get_fwd_ack (t);
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3749 if (t->last_fwd_ack == ack)
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3753 t->last_fwd_ack = ack;
3754 send_local_ack (t, t->owner, ack);
3759 * Send an ACK informing the predecessor about the available buffer space.
3760 * In case there is no predecessor, inform the owning client.
3761 * If buffering is off, send only on behalf of children or self if endpoint.
3762 * If buffering is on, send when sent to children and buffer space is free.
3763 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3764 * the ACK itself goes "back" (towards root).
3766 * @param t Tunnel on which to send the ACK.
3767 * @param type Type of message that triggered the ACK transmission.
3770 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3772 struct GNUNET_PeerIdentity id;
3775 if (NULL != t->owner)
3777 tunnel_send_client_fwd_ack (t);
3780 /* Is it after unicast / multicast retransmission? */
3783 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3784 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3786 "ACK due to FWD DATA retransmission\n");
3787 if (GNUNET_YES == t->nobuffer)
3789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3793 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3794 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3800 /* Check if we need no retransmit the ACK */
3801 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3802 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3806 " t->qmax: %u, t->qn: %u\n",
3807 t->fwd_queue_max, t->fwd_queue_n);
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3809 " t->pid: %u, t->ack: %u\n",
3810 t->fwd_pid, t->last_fwd_ack);
3814 /* Ok, ACK might be necessary, what PID to ACK? */
3815 ack = tunnel_get_fwd_ack (t);
3817 /* If speed_min and not all children have ack'd, dont send yet */
3818 if (ack == t->last_fwd_ack)
3820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3824 t->last_fwd_ack = ack;
3825 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3826 send_ack (t, &id, ack);
3832 * Iterator to send a child node a BCK ACK to allow him to send more
3835 * @param cls Closure (tunnel).
3836 * @param id Id of the child node.
3839 tunnel_send_child_bck_ack (void *cls,
3842 struct MeshTunnel *t = cls;
3843 struct MeshTunnelChildInfo *cinfo;
3844 struct GNUNET_PeerIdentity peer;
3846 GNUNET_PEER_resolve (id, &peer);
3847 cinfo = tunnel_get_neighbor_fc (t, &peer);
3849 if (cinfo->bck_ack != cinfo->pid &&
3850 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3854 send_ack (t, &peer, cinfo->bck_ack);
3859 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3861 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3863 * @param t Tunnel on which to send the BCK ACKs.
3866 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3869 unsigned int tunnel_delta;
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3873 tunnel_delta = t->bck_ack - t->bck_pid;
3874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3876 /* Find client whom to allow to send to origin (with lowest buffer space) */
3877 for (i = 0; i < t->nclients; i++)
3879 struct MeshTunnelClientInfo *clinfo;
3882 clinfo = &t->clients_fc[i];
3883 delta = clinfo->bck_ack - clinfo->bck_pid;
3884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3885 t->clients[i]->id, delta);
3887 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3888 (GNUNET_YES == t->nobuffer && 0 == delta))
3892 ack = clinfo->bck_pid;
3893 ack += t->nobuffer ? 1 : tunnel_delta;
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3895 " sending ack to client %u: %u\n",
3896 t->clients[i]->id, ack);
3897 send_local_ack (t, t->clients[i], ack);
3898 clinfo->bck_ack = ack;
3905 * Send an ACK informing the children nodes and destination clients about
3906 * the available buffer space.
3907 * If buffering is off, send only on behalf of root (can be self).
3908 * If buffering is on, send when sent to predecessor and buffer space is free.
3909 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3910 * the ACK itself goes "forward" (towards children/clients).
3912 * @param t Tunnel on which to send the ACK.
3913 * @param type Type of message that triggered the ACK transmission.
3916 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3919 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3920 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3921 /* Is it after data to_origin retransmission? */
3924 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3925 if (GNUNET_YES == t->nobuffer)
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3928 " Not sending ACK, nobuffer\n");
3932 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3933 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3939 tunnel_send_clients_bck_ack (t);
3940 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3945 * @brief Re-initiate traffic to this peer if necessary.
3947 * Check if there is traffic queued towards this peer
3948 * and the core transmit handle is NULL (traffic was stalled).
3949 * If so, call core tmt rdy.
3951 * @param cls Closure (unused)
3952 * @param peer_id Short ID of peer to which initiate traffic.
3955 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3957 struct MeshPeerInfo *peer;
3958 struct GNUNET_PeerIdentity id;
3959 struct MeshPeerQueue *q;
3962 peer = peer_info_get_short(peer_id);
3963 if (NULL != peer->core_transmit)
3966 q = queue_get_next(peer);
3969 /* Might br multicast traffic already sent to this particular peer but
3970 * not to other children in this tunnel.
3971 * This way t->queue_n would be > 0 but the queue of this particular peer
3977 GNUNET_PEER_resolve (peer->id, &id);
3978 peer->core_transmit =
3979 GNUNET_CORE_notify_transmit_ready(core_handle,
3982 GNUNET_TIME_UNIT_FOREVER_REL,
3992 * @brief Allow transmission of FWD traffic on this tunnel
3994 * Check if there is traffic queued towards any children
3995 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3997 * @param t Tunnel on which to unlock FWD traffic.
4000 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4002 if (0 == t->fwd_queue_n)
4005 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4010 * @brief Allow transmission of BCK traffic on this tunnel
4012 * Check if there is traffic queued towards the root of the tree
4013 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4015 * @param t Tunnel on which to unlock BCK traffic.
4018 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4020 if (0 == t->bck_queue_n)
4023 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4028 * Send a message to all peers in this tunnel that the tunnel is no longer
4031 * @param t The tunnel whose peers to notify.
4034 tunnel_send_destroy (struct MeshTunnel *t)
4036 struct GNUNET_MESH_TunnelDestroy msg;
4038 msg.header.size = htons (sizeof (msg));
4039 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4040 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4041 msg.tid = htonl (t->id.tid);
4042 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4047 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4049 * @param cls Closure (Tunnel which to cancel).
4050 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4053 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4055 struct MeshTunnel *t = cls;
4056 struct MeshPeerInfo *peer_info;
4057 struct MeshPeerQueue *pq;
4058 struct MeshPeerQueue *next;
4060 peer_info = peer_info_get_short (neighbor_id);
4061 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4064 if (pq->tunnel == t)
4066 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4067 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4068 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4070 // Should have been removed on destroy children
4073 queue_destroy (pq, GNUNET_YES);
4076 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4078 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4079 peer_info->core_transmit = NULL;
4084 * Destroy the tunnel and free any allocated resources linked to it.
4086 * @param t the tunnel to destroy
4088 * @return GNUNET_OK on success
4091 tunnel_destroy (struct MeshTunnel *t)
4093 struct MeshClient *c;
4094 struct GNUNET_HashCode hash;
4105 struct GNUNET_PeerIdentity id;
4107 GNUNET_PEER_resolve (t->id.oid, &id);
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4109 GNUNET_i2s (&id), t->id.tid);
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4115 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4116 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4121 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4124 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4128 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4129 for (i = 0; i < t->nclients; i++)
4133 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4138 for (i = 0; i < t->nignore; i++)
4142 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4147 if (t->nclients > 0)
4150 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4154 GNUNET_free (t->clients);
4156 if (NULL != t->peers)
4158 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4160 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4163 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4164 &tunnel_destroy_child,
4166 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4168 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4170 tree_destroy (t->tree);
4172 if (NULL != t->regex_ctx)
4173 regex_cancel_search (t->regex_ctx);
4174 if (NULL != t->dht_get_type)
4175 GNUNET_DHT_get_stop (t->dht_get_type);
4176 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4177 GNUNET_SCHEDULER_cancel (t->timeout_task);
4178 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4179 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4182 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4183 GNUNET_assert (0 <= n_tunnels);
4190 * Create a new tunnel
4192 * @param owner Who is the owner of the tunnel (short ID).
4193 * @param tid Tunnel Number of the tunnel.
4194 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4195 * @param local Tunnel Number for the tunnel, for the client point of view.
4197 * @return A new initialized tunnel. NULL on error.
4199 static struct MeshTunnel *
4200 tunnel_new (GNUNET_PEER_Id owner,
4201 MESH_TunnelNumber tid,
4202 struct MeshClient *client,
4203 MESH_TunnelNumber local)
4205 struct MeshTunnel *t;
4206 struct GNUNET_HashCode hash;
4208 if (n_tunnels >= max_tunnels && NULL == client)
4211 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4214 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4215 t->bck_queue_max = t->fwd_queue_max;
4216 t->tree = tree_new (owner);
4218 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4219 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4220 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4221 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4222 t->local_tid = local;
4223 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4225 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4227 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4229 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4230 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4237 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4244 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4246 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4247 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4251 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4261 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4262 * that are no longer needed, as well as nodes of no longer reachable peers.
4263 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4265 * @param t Tunnel from which to remove the path.
4266 * @param peer Short id of the peer which should be removed.
4269 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4271 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4277 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4278 * client when the client disconnects. If the client is not the owner, the
4279 * owner will get notified if no more clients are in the tunnel and the client
4280 * get removed from the tunnel's list.
4282 * @param cls closure (client that is disconnecting)
4283 * @param key the hash of the local tunnel id (used to access the hashmap)
4284 * @param value the value stored at the key (tunnel to destroy)
4286 * @return GNUNET_OK on success
4289 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4291 struct MeshTunnel *t = value;
4292 struct MeshClient *c = cls;
4295 send_client_tunnel_disconnect(t, c);
4298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4299 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4300 tunnel_delete_client(t, c);
4301 client_delete_tunnel(c, t);
4304 tunnel_send_destroy(t);
4305 r = tunnel_destroy (t);
4311 * Timeout function, destroys tunnel if called
4313 * @param cls Closure (tunnel to destroy).
4314 * @param tc TaskContext
4317 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4319 struct MeshTunnel *t = cls;
4321 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4323 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4328 * Resets the tunnel timeout. Starts it if no timeout was running.
4330 * @param t Tunnel whose timeout to reset.
4332 * TODO use heap to improve efficiency of scheduler.
4335 tunnel_reset_timeout (struct MeshTunnel *t)
4337 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4338 GNUNET_SCHEDULER_cancel (t->timeout_task);
4340 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4341 (refresh_path_time, 4), &tunnel_timeout, t);
4345 /******************************************************************************/
4346 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4347 /******************************************************************************/
4350 * Function to send a create path packet to a peer.
4352 * @param cls closure
4353 * @param size number of bytes available in buf
4354 * @param buf where the callee should write the message
4355 * @return number of bytes written to buf
4358 send_core_path_create (void *cls, size_t size, void *buf)
4360 struct MeshPathInfo *info = cls;
4361 struct GNUNET_MESH_ManipulatePath *msg;
4362 struct GNUNET_PeerIdentity *peer_ptr;
4363 struct MeshTunnel *t = info->t;
4364 struct MeshPeerPath *p = info->path;
4369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4371 sizeof (struct GNUNET_MESH_ManipulatePath) +
4372 p->length * sizeof (struct GNUNET_PeerIdentity);
4374 if (size < size_needed || NULL == buf)
4379 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4380 msg->header.size = htons (size_needed);
4381 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4382 msg->tid = ntohl (t->id.tid);
4385 if (GNUNET_YES == t->speed_min)
4386 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4387 if (GNUNET_YES == t->nobuffer)
4388 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4389 msg->opt = htonl(opt);
4391 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4392 for (i = 0; i < p->length; i++)
4394 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4401 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4407 * Fill the core buffer
4409 * @param cls closure (data itself)
4410 * @param size number of bytes available in buf
4411 * @param buf where the callee should write the message
4413 * @return number of bytes written to buf
4416 send_core_data_multicast (void *cls, size_t size, void *buf)
4418 struct MeshTransmissionDescriptor *info = cls;
4421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4422 GNUNET_assert (NULL != info);
4423 GNUNET_assert (NULL != info->peer);
4424 total_size = info->mesh_data->data_len;
4425 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4427 if (total_size > size)
4432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4433 memcpy (buf, info->mesh_data->data, total_size);
4436 struct GNUNET_MESH_Multicast *mc;
4437 struct GNUNET_MessageHeader *mh;
4440 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4442 mc = (struct GNUNET_MESH_Multicast *) mh;
4443 mh = (struct GNUNET_MessageHeader *) &mc[1];
4444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4445 " multicast, payload type %s\n",
4446 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4448 " multicast, payload size %u\n", ntohs (mh->size));
4452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4453 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4457 data_descriptor_decrement_rc (info->mesh_data);
4458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4466 * Creates a path ack message in buf and frees all unused resources.
4468 * @param cls closure (MeshTransmissionDescriptor)
4469 * @param size number of bytes available in buf
4470 * @param buf where the callee should write the message
4471 * @return number of bytes written to buf
4474 send_core_path_ack (void *cls, size_t size, void *buf)
4476 struct MeshTransmissionDescriptor *info = cls;
4477 struct GNUNET_MESH_PathACK *msg = buf;
4479 GNUNET_assert (NULL != info);
4480 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4485 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4486 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4487 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4488 msg->tid = htonl (info->origin->tid);
4489 msg->peer_id = my_full_id;
4492 /* TODO add signature */
4494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4495 return sizeof (struct GNUNET_MESH_PathACK);
4500 * Free a transmission that was already queued with all resources
4501 * associated to the request.
4503 * @param queue Queue handler to cancel.
4504 * @param clear_cls Is it necessary to free associated cls?
4507 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4509 struct MeshTransmissionDescriptor *dd;
4510 struct MeshPathInfo *path_info;
4512 if (GNUNET_YES == clear_cls)
4514 switch (queue->type)
4516 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4517 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4518 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4521 data_descriptor_decrement_rc (dd->mesh_data);
4523 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4525 path_info = queue->cls;
4526 path_destroy (path_info->path);
4530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4531 " type %u unknown!\n",
4534 GNUNET_free_non_null (queue->cls);
4536 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4537 queue->peer->queue_tail,
4539 GNUNET_free (queue);
4544 * @brief Get the next transmittable message from the queue.
4546 * This will be the head, except in the case of being a data packet
4547 * not allowed by the destination peer.
4549 * @param peer Destination peer.
4551 * @return The next viable MeshPeerQueue element to send to that peer.
4552 * NULL when there are no transmittable messages.
4554 struct MeshPeerQueue *
4555 queue_get_next (const struct MeshPeerInfo *peer)
4557 struct MeshPeerQueue *q;
4558 struct MeshTunnel *t;
4559 struct MeshTransmissionDescriptor *info;
4560 struct MeshTunnelChildInfo *cinfo;
4561 struct GNUNET_MESH_Unicast *ucast;
4562 struct GNUNET_MESH_ToOrigin *to_orig;
4563 struct GNUNET_MESH_Multicast *mcast;
4564 struct GNUNET_PeerIdentity id;
4568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4569 for (q = peer->queue_head; NULL != q; q = q->next)
4573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4575 GNUNET_MESH_DEBUG_M2S(q->type));
4578 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4579 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4580 pid = ntohl (ucast->pid);
4581 GNUNET_PEER_resolve (info->peer->id, &id);
4582 cinfo = tunnel_get_neighbor_fc(t, &id);
4583 ack = cinfo->fwd_ack;
4585 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4586 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4587 pid = ntohl (to_orig->pid);
4590 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4591 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4592 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4594 // Not a multicast payload: multicast control traffic (destroy, etc)
4597 pid = ntohl (mcast->pid);
4598 GNUNET_PEER_resolve (info->peer->id, &id);
4599 cinfo = tunnel_get_neighbor_fc(t, &id);
4600 ack = cinfo->fwd_ack;
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4608 "********* ACK: %u, PID: %u\n",
4610 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4619 "********* NEXT!\n");
4622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4623 "********* nothing found\n");
4629 * Core callback to write a queued packet to core buffer
4631 * @param cls Closure (peer info).
4632 * @param size Number of bytes available in buf.
4633 * @param buf Where the to write the message.
4635 * @return number of bytes written to buf
4638 queue_send (void *cls, size_t size, void *buf)
4640 struct MeshPeerInfo *peer = cls;
4641 struct GNUNET_MessageHeader *msg;
4642 struct MeshPeerQueue *queue;
4643 struct MeshTunnel *t;
4644 struct MeshTunnelChildInfo *cinfo;
4645 struct GNUNET_PeerIdentity dst_id;
4648 peer->core_transmit = NULL;
4650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4651 queue = queue_get_next(peer);
4653 /* Queue has no internal mesh traffic not sendable payload */
4656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4657 if (NULL == peer->queue_head)
4658 GNUNET_break(0); // Should've been canceled
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4663 GNUNET_PEER_resolve (peer->id, &dst_id);
4664 /* Check if buffer size is enough for the message */
4665 if (queue->size > size)
4669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4670 "********* not enough room, reissue\n");
4671 peer->core_transmit =
4672 GNUNET_CORE_notify_transmit_ready(core_handle,
4675 GNUNET_TIME_UNIT_FOREVER_REL,
4682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4685 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4689 "********* unicast: t->q (%u/%u)\n",
4690 t->fwd_queue_n, t->fwd_queue_max);
4692 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4699 switch (queue->type)
4702 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4703 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4704 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4706 "********* raw: %s\n",
4707 GNUNET_MESH_DEBUG_M2S (queue->type));
4709 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4710 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4711 data_size = send_core_data_raw (queue->cls, size, buf);
4712 msg = (struct GNUNET_MessageHeader *) buf;
4713 switch (ntohs (msg->type)) // Type of preconstructed message
4715 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4716 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4718 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4719 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4725 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4728 struct MeshTransmissionDescriptor *info = queue->cls;
4729 if (info->mesh_data->reference_counter == 1)
4731 // FIXME fc (t->fwd_queue_n--)
4733 data_size = send_core_data_multicast(queue->cls, size, buf);
4734 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4736 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4738 data_size = send_core_path_create(queue->cls, size, buf);
4740 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4742 data_size = send_core_path_ack(queue->cls, size, buf);
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4747 "********* type unknown: %u\n",
4751 switch (queue->type)
4753 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4754 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4755 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4756 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4757 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4760 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4761 "at pos %u (%p) != %p\n",
4762 cinfo->send_buffer_start,
4763 cinfo->send_buffer[cinfo->send_buffer_start],
4766 GNUNET_break(cinfo->send_buffer_n > 0);
4767 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4768 cinfo->send_buffer_n--;
4769 cinfo->send_buffer_start++;
4770 cinfo->send_buffer_start %= t->fwd_queue_max;
4776 /* Free queue, but cls was freed by send_core_* */
4777 queue_destroy (queue, GNUNET_NO);
4779 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4785 /* If more data in queue, send next */
4786 queue = queue_get_next(peer);
4789 struct GNUNET_PeerIdentity id;
4791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4792 GNUNET_PEER_resolve (peer->id, &id);
4793 peer->core_transmit =
4794 GNUNET_CORE_notify_transmit_ready(core_handle,
4797 GNUNET_TIME_UNIT_FOREVER_REL,
4805 if (NULL != peer->queue_head)
4806 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4807 "********* %s stalled\n",
4808 GNUNET_i2s(&my_full_id));
4810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4816 * Queue and pass message to core when possible.
4818 * @param cls Closure (type dependant).
4819 * @param type Type of the message, 0 for a raw message.
4820 * @param size Size of the message.
4821 * @param dst Neighbor to send message to.
4822 * @param t Tunnel this message belongs to.
4825 queue_add (void *cls, uint16_t type, size_t size,
4826 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4828 struct MeshPeerQueue *queue;
4829 struct MeshTunnelChildInfo *cinfo;
4830 struct GNUNET_PeerIdentity id;
4836 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4837 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4839 n = &t->fwd_queue_n;
4840 max = &t->fwd_queue_max;
4842 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4844 n = &t->bck_queue_n;
4845 max = &t->bck_queue_max;
4850 if (NULL == t->owner)
4851 GNUNET_break_op(0); // TODO: kill connection?
4854 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4856 return; // Drop message
4860 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4866 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4867 GNUNET_PEER_resolve (dst->id, &id);
4868 if (NULL == dst->core_transmit)
4870 dst->core_transmit =
4871 GNUNET_CORE_notify_transmit_ready(core_handle,
4874 GNUNET_TIME_UNIT_FOREVER_REL,
4880 if (NULL == n) // Is this internal mesh traffic?
4883 // It's payload, keep track of buffer per peer.
4884 cinfo = tunnel_get_neighbor_fc(t, &id);
4885 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4886 if (NULL != cinfo->send_buffer[i])
4888 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4889 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4890 cinfo->send_buffer_start++;
4891 cinfo->send_buffer_start %= t->fwd_queue_max;
4892 cinfo->send_buffer_n--;
4894 cinfo->send_buffer[i] = queue;
4895 cinfo->send_buffer_n++;
4896 if (cinfo->send_buffer_n > t->fwd_queue_max)
4899 cinfo->send_buffer_n = t->fwd_queue_max;
4904 /******************************************************************************/
4905 /******************** MESH NETWORK HANDLERS **************************/
4906 /******************************************************************************/
4910 * Core handler for path creation
4912 * @param cls closure
4913 * @param message message
4914 * @param peer peer identity this notification is about
4915 * @param atsi performance data
4916 * @param atsi_count number of records in 'atsi'
4918 * @return GNUNET_OK to keep the connection open,
4919 * GNUNET_SYSERR to close it (signal serious error)
4922 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4923 const struct GNUNET_MessageHeader *message,
4924 const struct GNUNET_ATS_Information *atsi,
4925 unsigned int atsi_count)
4927 unsigned int own_pos;
4930 MESH_TunnelNumber tid;
4931 struct GNUNET_MESH_ManipulatePath *msg;
4932 struct GNUNET_PeerIdentity *pi;
4933 struct GNUNET_HashCode hash;
4934 struct MeshPeerPath *path;
4935 struct MeshPeerInfo *dest_peer_info;
4936 struct MeshPeerInfo *orig_peer_info;
4937 struct MeshTunnel *t;
4939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4940 "Received a path create msg [%s]\n",
4941 GNUNET_i2s (&my_full_id));
4942 size = ntohs (message->size);
4943 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4945 GNUNET_break_op (0);
4949 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4950 if (size % sizeof (struct GNUNET_PeerIdentity))
4952 GNUNET_break_op (0);
4955 size /= sizeof (struct GNUNET_PeerIdentity);
4958 GNUNET_break_op (0);
4961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4962 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4964 tid = ntohl (msg->tid);
4965 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4967 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4968 t = tunnel_get (pi, tid);
4969 if (NULL == t) // FIXME only for INCOMING tunnels?
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4974 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4977 // FIXME notify failure
4980 opt = ntohl (msg->opt);
4981 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4982 GNUNET_YES : GNUNET_NO;
4983 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4984 GNUNET_YES : GNUNET_NO;
4985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4986 " speed_min: %d, nobuffer:%d\n",
4987 t->speed_min, t->nobuffer);
4989 if (GNUNET_YES == t->nobuffer)
4991 t->bck_queue_max = 1;
4992 t->fwd_queue_max = 1;
4995 // FIXME only assign a local tid if a local client is interested (on demand)
4996 while (NULL != tunnel_get_incoming (next_local_tid))
4997 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4998 t->local_tid_dest = next_local_tid++;
4999 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5002 tunnel_reset_timeout (t);
5003 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5005 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5006 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5014 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5015 if (NULL == dest_peer_info)
5017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5018 " Creating PeerInfo for destination.\n");
5019 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5020 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5021 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5023 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5025 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5026 if (NULL == orig_peer_info)
5028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5029 " Creating PeerInfo for origin.\n");
5030 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5031 orig_peer_info->id = GNUNET_PEER_intern (pi);
5032 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5033 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5036 path = path_new (size);
5038 for (i = 0; i < size; i++)
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5041 GNUNET_i2s (&pi[i]));
5042 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5043 if (path->peers[i] == myid)
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5049 /* cannot be self, must be 'not found' */
5050 /* create path: self not found in path through self */
5051 GNUNET_break_op (0);
5052 path_destroy (path);
5056 path_add_to_peers (path, GNUNET_NO);
5057 tunnel_add_path (t, path, own_pos);
5058 if (own_pos == size - 1)
5060 /* It is for us! Send ack. */
5061 struct MeshTransmissionDescriptor *info;
5063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5064 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5065 if (NULL == t->peers)
5067 /* New tunnel! Notify clients on data. */
5068 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5070 GNUNET_break (GNUNET_SYSERR !=
5071 GNUNET_CONTAINER_multihashmap_put (t->peers,
5072 &my_full_id.hashPubKey,
5075 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5076 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5077 info->origin = &t->id;
5078 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5079 GNUNET_assert (NULL != info->peer);
5081 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5082 sizeof (struct GNUNET_MESH_PathACK),
5088 struct MeshPeerPath *path2;
5090 /* It's for somebody else! Retransmit. */
5091 path2 = path_duplicate (path);
5092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5093 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5094 path2 = path_duplicate (path);
5095 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5096 send_create_path (dest_peer_info, path, t);
5103 * Core handler for path destruction
5105 * @param cls closure
5106 * @param message message
5107 * @param peer peer identity this notification is about
5108 * @param atsi performance data
5109 * @param atsi_count number of records in 'atsi'
5111 * @return GNUNET_OK to keep the connection open,
5112 * GNUNET_SYSERR to close it (signal serious error)
5115 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5116 const struct GNUNET_MessageHeader *message,
5117 const struct GNUNET_ATS_Information *atsi,
5118 unsigned int atsi_count)
5120 struct GNUNET_MESH_ManipulatePath *msg;
5121 struct GNUNET_PeerIdentity *pi;
5122 struct MeshPeerPath *path;
5123 struct MeshTunnel *t;
5124 unsigned int own_pos;
5128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5129 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5130 size = ntohs (message->size);
5131 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5133 GNUNET_break_op (0);
5137 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5138 if (size % sizeof (struct GNUNET_PeerIdentity))
5140 GNUNET_break_op (0);
5143 size /= sizeof (struct GNUNET_PeerIdentity);
5146 GNUNET_break_op (0);
5149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5151 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5152 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5154 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5156 t = tunnel_get (pi, ntohl (msg->tid));
5159 /* TODO notify back: we don't know this tunnel */
5160 GNUNET_break_op (0);
5163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5164 path = path_new (size);
5166 for (i = 0; i < size; i++)
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5169 GNUNET_i2s (&pi[i]));
5170 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5171 if (path->peers[i] == myid)
5174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5175 if (own_pos < path->length - 1)
5176 send_message (message, &pi[own_pos + 1], t);
5178 send_client_tunnel_disconnect(t, NULL);
5180 tunnel_delete_peer (t, path->peers[path->length - 1]);
5181 path_destroy (path);
5187 * Core handler for notifications of broken paths
5189 * @param cls closure
5190 * @param message message
5191 * @param peer peer identity this notification is about
5192 * @param atsi performance data
5193 * @param atsi_count number of records in 'atsi'
5195 * @return GNUNET_OK to keep the connection open,
5196 * GNUNET_SYSERR to close it (signal serious error)
5199 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5200 const struct GNUNET_MessageHeader *message,
5201 const struct GNUNET_ATS_Information *atsi,
5202 unsigned int atsi_count)
5204 struct GNUNET_MESH_PathBroken *msg;
5205 struct MeshTunnel *t;
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5208 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5209 msg = (struct GNUNET_MESH_PathBroken *) message;
5210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5211 GNUNET_i2s (&msg->peer1));
5212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5213 GNUNET_i2s (&msg->peer2));
5214 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5217 GNUNET_break_op (0);
5220 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5221 GNUNET_PEER_search (&msg->peer2));
5228 * Core handler for tunnel destruction
5230 * @param cls closure
5231 * @param message message
5232 * @param peer peer identity this notification is about
5233 * @param atsi performance data
5234 * @param atsi_count number of records in 'atsi'
5236 * @return GNUNET_OK to keep the connection open,
5237 * GNUNET_SYSERR to close it (signal serious error)
5240 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5241 const struct GNUNET_MessageHeader *message,
5242 const struct GNUNET_ATS_Information *atsi,
5243 unsigned int atsi_count)
5245 struct GNUNET_MESH_TunnelDestroy *msg;
5246 struct MeshTunnel *t;
5248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5249 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5250 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5252 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5253 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5256 /* Probably already got the message from another path,
5257 * destroyed the tunnel and retransmitted to children.
5260 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5263 if (t->id.oid == myid)
5265 GNUNET_break_op (0);
5268 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5270 /* Tunnel was incoming, notify clients */
5271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5272 t->local_tid, t->local_tid_dest);
5273 send_clients_tunnel_destroy (t);
5275 tunnel_send_destroy (t);
5276 t->destroy = GNUNET_YES;
5277 // TODO: add timeout to destroy the tunnel anyway
5283 * Core handler for mesh network traffic going from the origin to a peer
5285 * @param cls closure
5286 * @param peer peer identity this notification is about
5287 * @param message message
5288 * @param atsi performance data
5289 * @param atsi_count number of records in 'atsi'
5290 * @return GNUNET_OK to keep the connection open,
5291 * GNUNET_SYSERR to close it (signal serious error)
5294 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5295 const struct GNUNET_MessageHeader *message,
5296 const struct GNUNET_ATS_Information *atsi,
5297 unsigned int atsi_count)
5299 struct GNUNET_MESH_Unicast *msg;
5300 struct GNUNET_PeerIdentity *neighbor;
5301 struct MeshTunnelChildInfo *cinfo;
5302 struct MeshTunnel *t;
5303 GNUNET_PEER_Id dest_id;
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5311 size = ntohs (message->size);
5313 sizeof (struct GNUNET_MESH_Unicast) +
5314 sizeof (struct GNUNET_MessageHeader))
5319 msg = (struct GNUNET_MESH_Unicast *) message;
5320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5321 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5323 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5326 /* TODO notify back: we don't know this tunnel */
5327 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5328 GNUNET_break_op (0);
5331 pid = ntohl (msg->pid);
5332 if (t->fwd_pid == pid)
5334 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5336 " Already seen pid %u, DROPPING!\n", pid);
5341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5342 " pid %u not seen yet, forwarding\n", pid);
5345 t->skip += (pid - t->fwd_pid) - 1;
5348 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5350 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5351 GNUNET_break_op (0);
5352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5353 "Received PID %u, ACK %u\n",
5354 pid, t->last_fwd_ack);
5358 tunnel_reset_timeout (t);
5359 dest_id = GNUNET_PEER_search (&msg->destination);
5360 if (dest_id == myid)
5362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5363 " it's for us! sending to clients...\n");
5364 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5365 send_subscribed_clients (message, &msg[1].header, t);
5366 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5369 ttl = ntohl (msg->ttl);
5370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5373 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5374 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5375 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5379 " not for us, retransmitting...\n");
5381 neighbor = tree_get_first_hop (t->tree, dest_id);
5382 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5384 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5387 if (GNUNET_YES == t->nobuffer &&
5388 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5390 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5391 GNUNET_break_op (0);
5394 send_message (message, neighbor, t);
5395 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5401 * Core handler for mesh network traffic going from the origin to all peers
5403 * @param cls closure
5404 * @param message message
5405 * @param peer peer identity this notification is about
5406 * @param atsi performance data
5407 * @param atsi_count number of records in 'atsi'
5408 * @return GNUNET_OK to keep the connection open,
5409 * GNUNET_SYSERR to close it (signal serious error)
5411 * TODO: Check who we got this from, to validate route.
5414 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5415 const struct GNUNET_MessageHeader *message,
5416 const struct GNUNET_ATS_Information *atsi,
5417 unsigned int atsi_count)
5419 struct GNUNET_MESH_Multicast *msg;
5420 struct MeshTunnel *t;
5424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5426 size = ntohs (message->size);
5427 if (sizeof (struct GNUNET_MESH_Multicast) +
5428 sizeof (struct GNUNET_MessageHeader) > size)
5430 GNUNET_break_op (0);
5433 msg = (struct GNUNET_MESH_Multicast *) message;
5434 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5438 /* TODO notify that we dont know that tunnel */
5439 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5440 GNUNET_break_op (0);
5443 pid = ntohl (msg->pid);
5444 if (t->fwd_pid == pid)
5446 /* already seen this packet, drop */
5447 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5449 " Already seen pid %u, DROPPING!\n", pid);
5450 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5456 " pid %u not seen yet, forwarding\n", pid);
5458 t->skip += (pid - t->fwd_pid) - 1;
5460 tunnel_reset_timeout (t);
5462 /* Transmit to locally interested clients */
5463 if (NULL != t->peers &&
5464 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5466 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5467 send_subscribed_clients (message, &msg[1].header, t);
5468 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5471 if (ntohl (msg->ttl) == 0)
5473 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5474 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5475 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5478 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5479 tunnel_send_multicast (t, message, GNUNET_NO);
5485 * Core handler for mesh network traffic toward the owner of a tunnel
5487 * @param cls closure
5488 * @param message message
5489 * @param peer peer identity this notification is about
5490 * @param atsi performance data
5491 * @param atsi_count number of records in 'atsi'
5493 * @return GNUNET_OK to keep the connection open,
5494 * GNUNET_SYSERR to close it (signal serious error)
5497 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5498 const struct GNUNET_MessageHeader *message,
5499 const struct GNUNET_ATS_Information *atsi,
5500 unsigned int atsi_count)
5502 struct GNUNET_MESH_ToOrigin *msg;
5503 struct GNUNET_PeerIdentity id;
5504 struct MeshPeerInfo *peer_info;
5505 struct MeshTunnel *t;
5509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5511 size = ntohs (message->size);
5512 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5513 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5515 GNUNET_break_op (0);
5518 msg = (struct GNUNET_MESH_ToOrigin *) message;
5519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5520 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5521 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5525 /* TODO notify that we dont know this tunnel (whom)? */
5526 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5527 GNUNET_break_op (0);
5531 if (NULL != t->owner)
5534 struct GNUNET_MESH_ToOrigin *copy;
5536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5537 " it's for us! sending to clients...\n");
5538 /* TODO signature verification */
5539 memcpy (cbuf, message, size);
5540 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5541 copy->tid = htonl (t->local_tid);
5542 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5543 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5544 ©->header, GNUNET_NO);
5545 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5549 " not for us, retransmitting...\n");
5551 peer_info = peer_info_get (&msg->oid);
5552 if (NULL == peer_info)
5554 /* unknown origin of tunnel */
5558 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5559 send_message (message, &id, t);
5560 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5567 * Core handler for mesh network traffic point-to-point acks.
5569 * @param cls closure
5570 * @param message message
5571 * @param peer peer identity this notification is about
5572 * @param atsi performance data
5573 * @param atsi_count number of records in 'atsi'
5575 * @return GNUNET_OK to keep the connection open,
5576 * GNUNET_SYSERR to close it (signal serious error)
5579 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5580 const struct GNUNET_MessageHeader *message,
5581 const struct GNUNET_ATS_Information *atsi,
5582 unsigned int atsi_count)
5584 struct GNUNET_MESH_ACK *msg;
5585 struct MeshTunnel *t;
5588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5590 msg = (struct GNUNET_MESH_ACK *) message;
5592 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5596 /* TODO notify that we dont know this tunnel (whom)? */
5597 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5598 GNUNET_break_op (0);
5601 ack = ntohl (msg->pid);
5603 /* Is this a forward or backward ACK? */
5604 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5606 struct MeshTunnelChildInfo *cinfo;
5609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5610 cinfo = tunnel_get_neighbor_fc (t, peer);
5611 cinfo->fwd_ack = ack;
5612 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5613 tunnel_unlock_fwd_queues (t);
5617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5619 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5620 tunnel_unlock_bck_queue (t);
5627 * Core handler for path ACKs
5629 * @param cls closure
5630 * @param message message
5631 * @param peer peer identity this notification is about
5632 * @param atsi performance data
5633 * @param atsi_count number of records in 'atsi'
5635 * @return GNUNET_OK to keep the connection open,
5636 * GNUNET_SYSERR to close it (signal serious error)
5639 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5640 const struct GNUNET_MessageHeader *message,
5641 const struct GNUNET_ATS_Information *atsi,
5642 unsigned int atsi_count)
5644 struct GNUNET_MESH_PathACK *msg;
5645 struct GNUNET_PeerIdentity id;
5646 struct MeshPeerInfo *peer_info;
5647 struct MeshPeerPath *p;
5648 struct MeshTunnel *t;
5650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5651 GNUNET_i2s (&my_full_id));
5652 msg = (struct GNUNET_MESH_PathACK *) message;
5653 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5656 /* TODO notify that we don't know the tunnel */
5657 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5659 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5663 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5665 peer_info = peer_info_get (&msg->peer_id);
5666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5667 GNUNET_i2s (&msg->peer_id));
5668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5671 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5674 "connect_by_string completed, stopping search\n");
5675 regex_cancel_search (t->regex_ctx);
5676 t->regex_ctx = NULL;
5679 /* Add paths to peers? */
5680 p = tree_get_path_to_peer (t->tree, peer_info->id);
5683 path_add_to_peers (p, GNUNET_YES);
5691 /* Message for us? */
5692 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5695 if (NULL == t->owner)
5697 GNUNET_break_op (0);
5700 if (NULL != t->dht_get_type)
5702 GNUNET_DHT_get_stop (t->dht_get_type);
5703 t->dht_get_type = NULL;
5705 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5707 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5708 send_client_peer_connected (t, peer_info->id);
5713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5714 " not for us, retransmitting...\n");
5715 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5716 peer_info = peer_info_get (&msg->oid);
5717 if (NULL == peer_info)
5719 /* If we know the tunnel, we should DEFINITELY know the peer */
5723 send_message (message, &id, t);
5729 * Functions to handle messages from core
5731 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5732 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5733 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5734 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5735 sizeof (struct GNUNET_MESH_PathBroken)},
5736 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5737 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5738 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5739 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5740 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5741 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5742 sizeof (struct GNUNET_MESH_ACK)},
5743 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5744 sizeof (struct GNUNET_MESH_PathACK)},
5750 /******************************************************************************/
5751 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5752 /******************************************************************************/
5755 * deregister_app: iterator for removing each application registered by a client
5757 * @param cls closure
5758 * @param key the hash of the application id (used to access the hashmap)
5759 * @param value the value stored at the key (client)
5761 * @return GNUNET_OK on success
5764 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5766 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5767 GNUNET_break (GNUNET_YES ==
5768 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5774 * notify_client_connection_failure: notify a client that the connection to the
5775 * requested remote peer is not possible (for instance, no route found)
5776 * Function called when the socket is ready to queue more data. "buf" will be
5777 * NULL and "size" zero if the socket was closed for writing in the meantime.
5779 * @param cls closure
5780 * @param size number of bytes available in buf
5781 * @param buf where the callee should write the message
5782 * @return number of bytes written to buf
5785 notify_client_connection_failure (void *cls, size_t size, void *buf)
5788 struct MeshPeerInfo *peer_info;
5789 struct GNUNET_MESH_PeerControl *msg;
5790 struct GNUNET_PeerIdentity id;
5792 if (0 == size && NULL == buf)
5794 // TODO retry? cancel?
5798 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5799 peer_info = (struct MeshPeerInfo *) cls;
5800 msg = (struct GNUNET_MESH_PeerControl *) buf;
5801 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5802 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5803 // msg->tunnel_id = htonl(peer_info->t->tid);
5804 GNUNET_PEER_resolve (peer_info->id, &id);
5805 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5813 * Send keepalive packets for a peer
5815 * @param cls Closure (tunnel for which to send the keepalive).
5816 * @param tc Notification context.
5818 * TODO: implement explicit multicast keepalive?
5821 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5823 struct MeshTunnel *t = cls;
5824 struct GNUNET_MessageHeader *payload;
5825 struct GNUNET_MESH_Multicast *msg;
5827 sizeof (struct GNUNET_MESH_Multicast) +
5828 sizeof (struct GNUNET_MessageHeader);
5831 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5835 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5838 "sending keepalive for tunnel %d\n", t->id.tid);
5840 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5841 msg->header.size = htons (size);
5842 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5843 // FIXME: change type to != MULTICAST
5844 msg->oid = my_full_id;
5845 msg->tid = htonl (t->id.tid);
5846 msg->ttl = htonl (default_ttl);
5847 msg->pid = htonl (t->fwd_pid + 1);
5849 payload = (struct GNUNET_MessageHeader *) &msg[1];
5850 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5851 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5852 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5854 t->path_refresh_task =
5855 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5861 * Function to process paths received for a new peer addition. The recorded
5862 * paths form the initial tunnel, which can be optimized later.
5863 * Called on each result obtained for the DHT search.
5865 * @param cls closure
5866 * @param exp when will this value expire
5867 * @param key key of the result
5868 * @param get_path path of the get request
5869 * @param get_path_length lenght of get_path
5870 * @param put_path path of the put request
5871 * @param put_path_length length of the put_path
5872 * @param type type of the result
5873 * @param size number of bytes in data
5874 * @param data pointer to the result data
5876 * TODO: re-issue the request after certain time? cancel after X results?
5879 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5880 const struct GNUNET_HashCode * key,
5881 const struct GNUNET_PeerIdentity *get_path,
5882 unsigned int get_path_length,
5883 const struct GNUNET_PeerIdentity *put_path,
5884 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5885 size_t size, const void *data)
5887 struct MeshPathInfo *path_info = cls;
5888 struct MeshPeerPath *p;
5889 struct GNUNET_PeerIdentity pi;
5892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5893 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5896 p = path_build_from_dht (get_path, get_path_length, put_path,
5898 path_add_to_peers (p, GNUNET_NO);
5900 for (i = 0; i < path_info->peer->ntunnels; i++)
5902 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5903 peer_info_connect (path_info->peer, path_info->t);
5911 * Function to process paths received for a new peer addition. The recorded
5912 * paths form the initial tunnel, which can be optimized later.
5913 * Called on each result obtained for the DHT search.
5915 * @param cls closure
5916 * @param exp when will this value expire
5917 * @param key key of the result
5918 * @param get_path path of the get request
5919 * @param get_path_length lenght of get_path
5920 * @param put_path path of the put request
5921 * @param put_path_length length of the put_path
5922 * @param type type of the result
5923 * @param size number of bytes in data
5924 * @param data pointer to the result data
5927 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5928 const struct GNUNET_HashCode * key,
5929 const struct GNUNET_PeerIdentity *get_path,
5930 unsigned int get_path_length,
5931 const struct GNUNET_PeerIdentity *put_path,
5932 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5933 size_t size, const void *data)
5935 const struct PBlock *pb = data;
5936 const struct GNUNET_PeerIdentity *pi = &pb->id;
5937 struct MeshTunnel *t = cls;
5938 struct MeshPeerInfo *peer_info;
5939 struct MeshPeerPath *p;
5941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5942 if (size != sizeof (struct PBlock))
5944 GNUNET_break_op (0);
5947 if (ntohl(pb->type) != t->type)
5949 GNUNET_break_op (0);
5952 GNUNET_assert (NULL != t->owner);
5953 peer_info = peer_info_get (pi);
5954 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5956 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5958 p = path_build_from_dht (get_path, get_path_length, put_path,
5960 path_add_to_peers (p, GNUNET_NO);
5962 tunnel_add_peer (t, peer_info);
5963 peer_info_connect (peer_info, t);
5968 * Function to process DHT string to regex matching.
5969 * Called on each result obtained for the DHT search.
5971 * @param cls closure (search context)
5972 * @param exp when will this value expire
5973 * @param key key of the result
5974 * @param get_path path of the get request (not used)
5975 * @param get_path_length lenght of get_path (not used)
5976 * @param put_path path of the put request (not used)
5977 * @param put_path_length length of the put_path (not used)
5978 * @param type type of the result
5979 * @param size number of bytes in data
5980 * @param data pointer to the result data
5983 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5984 const struct GNUNET_HashCode * key,
5985 const struct GNUNET_PeerIdentity *get_path,
5986 unsigned int get_path_length,
5987 const struct GNUNET_PeerIdentity *put_path,
5988 unsigned int put_path_length,
5989 enum GNUNET_BLOCK_Type type,
5990 size_t size, const void *data)
5992 const struct MeshRegexAccept *block = data;
5993 struct MeshRegexSearchContext *ctx = cls;
5994 struct MeshRegexSearchInfo *info = ctx->info;
5995 struct MeshPeerPath *p;
5996 struct MeshPeerInfo *peer_info;
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6001 peer_info = peer_info_get(&block->id);
6002 p = path_build_from_dht (get_path, get_path_length, put_path,
6004 path_add_to_peers (p, GNUNET_NO);
6007 tunnel_add_peer (info->t, peer_info);
6008 peer_info_connect (peer_info, info->t);
6009 if (0 == info->peer)
6011 info->peer = peer_info->id;
6015 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6018 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6019 ®ex_connect_timeout,
6027 * Function to process DHT string to regex matching.
6028 * Called on each result obtained for the DHT search.
6030 * @param cls closure (search context)
6031 * @param exp when will this value expire
6032 * @param key key of the result
6033 * @param get_path path of the get request (not used)
6034 * @param get_path_length lenght of get_path (not used)
6035 * @param put_path path of the put request (not used)
6036 * @param put_path_length length of the put_path (not used)
6037 * @param type type of the result
6038 * @param size number of bytes in data
6039 * @param data pointer to the result data
6041 * TODO: re-issue the request after certain time? cancel after X results?
6044 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6045 const struct GNUNET_HashCode * key,
6046 const struct GNUNET_PeerIdentity *get_path,
6047 unsigned int get_path_length,
6048 const struct GNUNET_PeerIdentity *put_path,
6049 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6050 size_t size, const void *data)
6052 const struct MeshRegexBlock *block = data;
6053 struct MeshRegexSearchContext *ctx = cls;
6054 struct MeshRegexSearchInfo *info = ctx->info;
6058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6059 "DHT GET STRING RETURNED RESULTS\n");
6060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6061 " key: %s\n", GNUNET_h2s (key));
6063 copy = GNUNET_malloc (size);
6064 memcpy (copy, data, size);
6065 GNUNET_break (GNUNET_OK ==
6066 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6067 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6068 len = ntohl (block->n_proof);
6070 char proof[len + 1];
6072 memcpy (proof, &block[1], len);
6074 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6076 GNUNET_break_op (0);
6080 len = strlen (info->description);
6081 if (len == ctx->position) // String processed
6083 if (GNUNET_YES == ntohl (block->accepting))
6085 regex_find_path(key, ctx);
6089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6090 // FIXME REGEX this block not successful, wait for more? start timeout?
6094 GNUNET_break (GNUNET_OK ==
6095 GNUNET_MESH_regex_block_iterate (block, size,
6096 ®ex_edge_iterator, ctx));
6100 /******************************************************************************/
6101 /********************* MESH LOCAL HANDLES **************************/
6102 /******************************************************************************/
6106 * Handler for client disconnection
6108 * @param cls closure
6109 * @param client identification of the client; NULL
6110 * for the last call when the server is destroyed
6113 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6115 struct MeshClient *c;
6116 struct MeshClient *next;
6119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6128 if (c->handle != client)
6130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6136 GNUNET_SERVER_client_drop (c->handle);
6137 c->shutting_down = GNUNET_YES;
6138 GNUNET_assert (NULL != c->own_tunnels);
6139 GNUNET_assert (NULL != c->incoming_tunnels);
6140 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6141 &tunnel_destroy_iterator, c);
6142 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6143 &tunnel_destroy_iterator, c);
6144 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6145 &tunnel_destroy_iterator, c);
6146 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6147 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6148 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6150 /* deregister clients applications */
6151 if (NULL != c->apps)
6153 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6154 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6156 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6157 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6159 GNUNET_SCHEDULER_cancel (announce_applications_task);
6160 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6162 if (NULL != c->types)
6163 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6164 for (i = 0; i < c->n_regex; i++)
6166 GNUNET_free (c->regexes[i]);
6168 GNUNET_free_non_null (c->regexes);
6169 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6170 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6172 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6175 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6184 * Handler for new clients
6186 * @param cls closure
6187 * @param client identification of the client
6188 * @param message the actual message, which includes messages the client wants
6191 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6192 const struct GNUNET_MessageHeader *message)
6194 struct GNUNET_MESH_ClientConnect *cc_msg;
6195 struct MeshClient *c;
6196 GNUNET_MESH_ApplicationType *a;
6203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6204 /* Check data sanity */
6205 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6206 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6207 ntypes = ntohs (cc_msg->types);
6208 napps = ntohs (cc_msg->applications);
6210 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6213 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6217 /* Create new client structure */
6218 c = GNUNET_malloc (sizeof (struct MeshClient));
6219 c->id = next_client_id++;
6220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6222 GNUNET_SERVER_client_keep (client);
6223 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6226 GNUNET_MESH_ApplicationType at;
6227 struct GNUNET_HashCode hc;
6229 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6230 for (i = 0; i < napps; i++)
6233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6234 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6235 /* store in clients hashmap */
6236 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6237 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6238 /* store in global hashmap, for announcements */
6239 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6240 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6242 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6243 announce_applications_task =
6244 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6250 struct GNUNET_HashCode hc;
6252 t = (uint16_t *) & a[napps];
6253 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6254 for (i = 0; i < ntypes; i++)
6257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6258 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6260 /* store in clients hashmap */
6261 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6262 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6263 /* store in global hashmap */
6264 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6269 " client has %u+%u subscriptions\n", napps, ntypes);
6271 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6272 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6273 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6274 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6275 GNUNET_SERVER_notification_context_add (nc, client);
6276 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6278 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6284 * Handler for clients announcing available services by a regular expression.
6286 * @param cls closure
6287 * @param client identification of the client
6288 * @param message the actual message, which includes messages the client wants
6291 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6292 const struct GNUNET_MessageHeader *message)
6294 struct MeshClient *c;
6298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6300 /* Sanity check for client registration */
6301 if (NULL == (c = client_get (client)))
6304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6309 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6310 regex = GNUNET_malloc (len + 1);
6311 memcpy (regex, &message[1], len);
6313 GNUNET_array_append (c->regexes, c->n_regex, regex);
6314 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6316 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6322 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6328 * Handler for requests of new tunnels
6330 * @param cls closure
6331 * @param client identification of the client
6332 * @param message the actual message
6335 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6336 const struct GNUNET_MessageHeader *message)
6338 struct GNUNET_MESH_TunnelMessage *t_msg;
6339 struct MeshTunnel *t;
6340 struct MeshClient *c;
6341 MESH_TunnelNumber tid;
6343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6345 /* Sanity check for client registration */
6346 if (NULL == (c = client_get (client)))
6349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6354 /* Message sanity check */
6355 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6362 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6363 /* Sanity check for tunnel numbering */
6364 tid = ntohl (t_msg->tunnel_id);
6365 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6371 /* Sanity check for duplicate tunnel IDs */
6372 if (NULL != tunnel_get_by_local_id (c, tid))
6375 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6379 while (NULL != tunnel_get_by_pi (myid, next_tid))
6380 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6381 t = tunnel_new (myid, next_tid++, c, tid);
6384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6389 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6391 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6392 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6395 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6401 * Handler for requests of deleting tunnels
6403 * @param cls closure
6404 * @param client identification of the client
6405 * @param message the actual message
6408 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6409 const struct GNUNET_MessageHeader *message)
6411 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6412 struct MeshClient *c;
6413 struct MeshTunnel *t;
6414 MESH_TunnelNumber tid;
6416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6417 "Got a DESTROY TUNNEL from client!\n");
6419 /* Sanity check for client registration */
6420 if (NULL == (c = client_get (client)))
6423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6428 /* Message sanity check */
6429 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6436 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6438 /* Retrieve tunnel */
6439 tid = ntohl (tunnel_msg->tunnel_id);
6440 t = tunnel_get_by_local_id(c, tid);
6443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6445 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6448 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6450 client_ignore_tunnel (c, t);
6452 // TODO: when to destroy incoming tunnel?
6453 if (t->nclients == 0)
6455 GNUNET_assert (GNUNET_YES ==
6456 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6458 GNUNET_assert (GNUNET_YES ==
6459 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6460 &my_full_id.hashPubKey,
6464 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6467 send_client_tunnel_disconnect(t, c);
6468 client_delete_tunnel(c, t);
6470 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6472 tunnel_send_destroy (t);
6473 t->destroy = GNUNET_YES;
6474 // The tunnel will be destroyed when the last message is transmitted.
6475 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6481 * Handler for requests of seeting tunnel's speed.
6483 * @param cls Closure (unused).
6484 * @param client Identification of the client.
6485 * @param message The actual message.
6488 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6489 const struct GNUNET_MessageHeader *message)
6491 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6492 struct MeshClient *c;
6493 struct MeshTunnel *t;
6494 MESH_TunnelNumber tid;
6496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6497 "Got a SPEED request from client!\n");
6499 /* Sanity check for client registration */
6500 if (NULL == (c = client_get (client)))
6503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6509 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6511 /* Retrieve tunnel */
6512 tid = ntohl (tunnel_msg->tunnel_id);
6513 t = tunnel_get_by_local_id(c, tid);
6516 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6518 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6522 switch (ntohs(message->type))
6524 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6525 t->speed_min = GNUNET_YES;
6527 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6528 t->speed_min = GNUNET_NO;
6533 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6538 * Handler for requests of seeting tunnel's buffering policy.
6540 * @param cls Closure (unused).
6541 * @param client Identification of the client.
6542 * @param message The actual message.
6545 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6546 const struct GNUNET_MessageHeader *message)
6548 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6549 struct MeshClient *c;
6550 struct MeshTunnel *t;
6551 MESH_TunnelNumber tid;
6553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6554 "Got a BUFFER request from client!\n");
6556 /* Sanity check for client registration */
6557 if (NULL == (c = client_get (client)))
6560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6565 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6567 /* Retrieve tunnel */
6568 tid = ntohl (tunnel_msg->tunnel_id);
6569 t = tunnel_get_by_local_id(c, tid);
6572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6578 switch (ntohs(message->type))
6580 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6581 t->nobuffer = GNUNET_NO;
6583 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6584 t->nobuffer = GNUNET_YES;
6590 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6595 * Handler for connection requests to new peers
6597 * @param cls closure
6598 * @param client identification of the client
6599 * @param message the actual message (PeerControl)
6602 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6603 const struct GNUNET_MessageHeader *message)
6605 struct GNUNET_MESH_PeerControl *peer_msg;
6606 struct MeshPeerInfo *peer_info;
6607 struct MeshClient *c;
6608 struct MeshTunnel *t;
6609 MESH_TunnelNumber tid;
6611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6612 /* Sanity check for client registration */
6613 if (NULL == (c = client_get (client)))
6616 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6621 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6623 /* Sanity check for message size */
6624 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6627 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6631 /* Tunnel exists? */
6632 tid = ntohl (peer_msg->tunnel_id);
6633 t = tunnel_get_by_local_id (c, tid);
6637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6641 /* Does client own tunnel? */
6642 if (t->owner->handle != client)
6645 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6649 GNUNET_i2s (&peer_msg->peer));
6650 peer_info = peer_info_get (&peer_msg->peer);
6652 tunnel_add_peer (t, peer_info);
6653 peer_info_connect (peer_info, t);
6655 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6661 * Handler for disconnection requests of peers in a tunnel
6663 * @param cls closure
6664 * @param client identification of the client
6665 * @param message the actual message (PeerControl)
6668 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6669 const struct GNUNET_MessageHeader *message)
6671 struct GNUNET_MESH_PeerControl *peer_msg;
6672 struct MeshPeerInfo *peer_info;
6673 struct MeshClient *c;
6674 struct MeshTunnel *t;
6675 MESH_TunnelNumber tid;
6677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6678 /* Sanity check for client registration */
6679 if (NULL == (c = client_get (client)))
6682 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6687 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6689 /* Sanity check for message size */
6690 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6697 /* Tunnel exists? */
6698 tid = ntohl (peer_msg->tunnel_id);
6699 t = tunnel_get_by_local_id (c, tid);
6703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6708 /* Does client own tunnel? */
6709 if (t->owner->handle != client)
6712 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6717 GNUNET_i2s (&peer_msg->peer));
6718 /* Is the peer in the tunnel? */
6720 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6721 if (NULL == peer_info)
6724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6728 /* Ok, delete peer from tunnel */
6729 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6730 &peer_msg->peer.hashPubKey);
6732 send_destroy_path (t, peer_info->id);
6733 tunnel_delete_peer (t, peer_info->id);
6734 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6739 * Handler for blacklist requests of peers in a tunnel
6741 * @param cls closure
6742 * @param client identification of the client
6743 * @param message the actual message (PeerControl)
6745 * FIXME implement DHT block bloomfilter
6748 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6749 const struct GNUNET_MessageHeader *message)
6751 struct GNUNET_MESH_PeerControl *peer_msg;
6752 struct MeshClient *c;
6753 struct MeshTunnel *t;
6754 MESH_TunnelNumber tid;
6756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6757 /* Sanity check for client registration */
6758 if (NULL == (c = client_get (client)))
6761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6766 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6768 /* Sanity check for message size */
6769 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6772 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6776 /* Tunnel exists? */
6777 tid = ntohl (peer_msg->tunnel_id);
6778 t = tunnel_get_by_local_id (c, tid);
6782 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6787 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6788 GNUNET_PEER_intern(&peer_msg->peer));
6793 * Handler for unblacklist requests of peers in a tunnel
6795 * @param cls closure
6796 * @param client identification of the client
6797 * @param message the actual message (PeerControl)
6800 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6801 const struct GNUNET_MessageHeader *message)
6803 struct GNUNET_MESH_PeerControl *peer_msg;
6804 struct MeshClient *c;
6805 struct MeshTunnel *t;
6806 MESH_TunnelNumber tid;
6810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6811 /* Sanity check for client registration */
6812 if (NULL == (c = client_get (client)))
6815 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6820 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6822 /* Sanity check for message size */
6823 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6826 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6830 /* Tunnel exists? */
6831 tid = ntohl (peer_msg->tunnel_id);
6832 t = tunnel_get_by_local_id (c, tid);
6836 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6841 /* if peer is not known, complain */
6842 pid = GNUNET_PEER_search (&peer_msg->peer);
6849 /* search and remove from list */
6850 for (i = 0; i < t->nblacklisted; i++)
6852 if (t->blacklisted[i] == pid)
6854 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6855 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6860 /* if peer hasn't been blacklisted, complain */
6866 * Handler for connection requests to new peers by type
6868 * @param cls closure
6869 * @param client identification of the client
6870 * @param message the actual message (ConnectPeerByType)
6873 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6874 const struct GNUNET_MessageHeader *message)
6876 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6877 struct MeshClient *c;
6878 struct MeshTunnel *t;
6879 struct GNUNET_HashCode hash;
6880 MESH_TunnelNumber tid;
6882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6883 /* Sanity check for client registration */
6884 if (NULL == (c = client_get (client)))
6887 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6892 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6894 /* Sanity check for message size */
6895 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6896 ntohs (connect_msg->header.size))
6899 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6903 /* Tunnel exists? */
6904 tid = ntohl (connect_msg->tunnel_id);
6905 t = tunnel_get_by_local_id (c, tid);
6909 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6913 /* Does client own tunnel? */
6914 if (t->owner->handle != client)
6917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6921 /* Do WE have the service? */
6922 t->type = ntohl (connect_msg->type);
6923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6924 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6925 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6928 /* Yes! Fast forward, add ourselves to the tunnel and send the
6929 * good news to the client, and alert the destination client of
6930 * an incoming tunnel.
6932 * FIXME send a path create to self, avoid code duplication
6934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6935 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6936 peer_info_get (&my_full_id),
6937 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6940 send_client_peer_connected (t, myid);
6941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6942 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6944 t->local_tid_dest = next_local_tid++;
6945 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6946 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6947 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6951 /* Ok, lets find a peer offering the service */
6952 if (NULL != t->dht_get_type)
6954 GNUNET_DHT_get_stop (t->dht_get_type);
6956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6957 GNUNET_h2s (&hash));
6959 GNUNET_DHT_get_start (dht_handle,
6960 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6962 dht_replication_level,
6963 GNUNET_DHT_RO_RECORD_ROUTE |
6964 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6966 &dht_get_type_handler, t);
6968 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6974 * Handler for connection requests to new peers by a string service description.
6976 * @param cls closure
6977 * @param client identification of the client
6978 * @param message the actual message, which includes messages the client wants
6981 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6982 const struct GNUNET_MessageHeader *message)
6984 struct GNUNET_MESH_ConnectPeerByString *msg;
6985 struct MeshRegexSearchContext *ctx;
6986 struct MeshRegexSearchInfo *info;
6987 struct GNUNET_DHT_GetHandle *get_h;
6988 struct GNUNET_HashCode key;
6989 struct MeshTunnel *t;
6990 struct MeshClient *c;
6991 MESH_TunnelNumber tid;
6995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6996 "Connect by string started\n");
6997 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6998 size = htons (message->size);
7000 /* Sanity check for client registration */
7001 if (NULL == (c = client_get (client)))
7004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7009 /* Message size sanity check */
7010 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7013 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7017 /* Tunnel exists? */
7018 tid = ntohl (msg->tunnel_id);
7019 t = tunnel_get_by_local_id (c, tid);
7023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7027 /* Does client own tunnel? */
7028 if (t->owner->handle != client)
7031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7036 " on tunnel %s [%u]\n",
7037 GNUNET_i2s(&my_full_id),
7040 /* Only one connect_by_string allowed at the same time! */
7041 /* FIXME: allow more, return handle at api level to cancel, document */
7042 if (NULL != t->regex_ctx)
7045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7049 /* Find string itself */
7050 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7051 string = (const char *) &msg[1];
7053 /* Initialize context */
7054 size = GNUNET_REGEX_get_first_key(string, len, &key);
7055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7056 " consumed %u bits out of %u\n", size, len);
7057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7058 " looking for %s\n", GNUNET_h2s (&key));
7060 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7062 info->description = GNUNET_malloc (len + 1);
7063 memcpy (info->description, string, len);
7064 info->description[len] = '\0';
7065 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7066 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7069 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7070 ctx->position = size;
7074 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7076 /* Start search in DHT */
7077 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7078 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7079 &key, /* key to search */
7080 dht_replication_level, /* replication level */
7081 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7082 NULL, /* xquery */ // FIXME BLOOMFILTER
7083 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7084 &dht_get_string_handler, ctx);
7086 GNUNET_break (GNUNET_OK ==
7087 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7090 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7092 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7098 * Handler for client traffic directed to one peer
7100 * @param cls closure
7101 * @param client identification of the client
7102 * @param message the actual message
7105 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7106 const struct GNUNET_MessageHeader *message)
7108 struct MeshClient *c;
7109 struct MeshTunnel *t;
7110 struct MeshPeerInfo *pi;
7111 struct GNUNET_MESH_Unicast *data_msg;
7112 MESH_TunnelNumber tid;
7115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7116 "Got a unicast request from a client!\n");
7118 /* Sanity check for client registration */
7119 if (NULL == (c = client_get (client)))
7122 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7127 data_msg = (struct GNUNET_MESH_Unicast *) message;
7129 /* Sanity check for message size */
7130 size = ntohs (message->size);
7131 if (sizeof (struct GNUNET_MESH_Unicast) +
7132 sizeof (struct GNUNET_MessageHeader) > size)
7135 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7139 /* Tunnel exists? */
7140 tid = ntohl (data_msg->tid);
7141 t = tunnel_get_by_local_id (c, tid);
7145 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7149 /* Is it a local tunnel? Then, does client own the tunnel? */
7150 if (t->owner->handle != client)
7153 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7157 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7158 &data_msg->destination.hashPubKey);
7159 /* Is the selected peer in the tunnel? */
7163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7167 /* PID should be as expected */
7168 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7172 "Unicast PID, expected %u, got %u\n",
7173 t->fwd_pid + 1, ntohl (data_msg->pid));
7174 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7178 /* Ok, everything is correct, send the message
7179 * (pretend we got it from a mesh peer)
7182 /* Work around const limitation */
7183 char buf[ntohs (message->size)] GNUNET_ALIGN;
7184 struct GNUNET_MESH_Unicast *copy;
7186 copy = (struct GNUNET_MESH_Unicast *) buf;
7187 memcpy (buf, data_msg, size);
7188 copy->oid = my_full_id;
7189 copy->tid = htonl (t->id.tid);
7190 copy->ttl = htonl (default_ttl);
7191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7192 " calling generic handler...\n");
7193 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7196 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7203 * Handler for client traffic directed to the origin
7205 * @param cls closure
7206 * @param client identification of the client
7207 * @param message the actual message
7210 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7211 const struct GNUNET_MessageHeader *message)
7213 struct GNUNET_MESH_ToOrigin *data_msg;
7214 struct MeshTunnelClientInfo *clinfo;
7215 struct MeshClient *c;
7216 struct MeshTunnel *t;
7217 MESH_TunnelNumber tid;
7220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7221 "Got a ToOrigin request from a client!\n");
7222 /* Sanity check for client registration */
7223 if (NULL == (c = client_get (client)))
7226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7231 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7233 /* Sanity check for message size */
7234 size = ntohs (message->size);
7235 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7236 sizeof (struct GNUNET_MessageHeader) > size)
7239 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7243 /* Tunnel exists? */
7244 tid = ntohl (data_msg->tid);
7245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7246 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7249 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7252 t = tunnel_get_by_local_id (c, tid);
7255 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7258 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7262 /* It should be sent by someone who has this as incoming tunnel. */
7263 if (GNUNET_NO == client_knows_tunnel (c, t))
7266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7270 /* PID should be as expected */
7271 clinfo = tunnel_get_client_fc (t, c);
7272 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7276 "To Origin PID, expected %u, got %u\n",
7277 clinfo->bck_pid + 1,
7278 ntohl (data_msg->pid));
7279 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7283 /* Ok, everything is correct, send the message
7284 * (pretend we got it from a mesh peer)
7288 char buf[ntohs (message->size)] GNUNET_ALIGN;
7289 struct GNUNET_MESH_ToOrigin *copy;
7291 /* Work around const limitation */
7292 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7293 memcpy (buf, data_msg, size);
7294 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7295 copy->tid = htonl (t->id.tid);
7296 copy->ttl = htonl (default_ttl);
7297 if (ntohl (copy->pid) != (t->bck_pid + 1))
7300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7301 "To Origin PID, expected %u, got %u\n",
7307 copy->sender = my_full_id;
7308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7309 " calling generic handler...\n");
7310 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7312 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7319 * Handler for client traffic directed to all peers in a tunnel
7321 * @param cls closure
7322 * @param client identification of the client
7323 * @param message the actual message
7326 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7327 const struct GNUNET_MessageHeader *message)
7329 struct MeshClient *c;
7330 struct MeshTunnel *t;
7331 struct GNUNET_MESH_Multicast *data_msg;
7332 MESH_TunnelNumber tid;
7334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7335 "Got a multicast request from a client!\n");
7337 /* Sanity check for client registration */
7338 if (NULL == (c = client_get (client)))
7341 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7346 data_msg = (struct GNUNET_MESH_Multicast *) message;
7348 /* Sanity check for message size */
7349 if (sizeof (struct GNUNET_MESH_Multicast) +
7350 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7353 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7357 /* Tunnel exists? */
7358 tid = ntohl (data_msg->tid);
7359 t = tunnel_get_by_local_id (c, tid);
7363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7366 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7370 /* Does client own tunnel? */
7371 if (t->owner->handle != client)
7374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7378 /* PID should be as expected */
7379 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7383 "Multicast PID, expected %u, got %u\n",
7384 t->fwd_pid + 1, ntohl (data_msg->pid));
7385 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7390 char buf[ntohs (message->size)] GNUNET_ALIGN;
7391 struct GNUNET_MESH_Multicast *copy;
7393 copy = (struct GNUNET_MESH_Multicast *) buf;
7394 memcpy (buf, message, ntohs (message->size));
7395 copy->oid = my_full_id;
7396 copy->tid = htonl (t->id.tid);
7397 copy->ttl = htonl (default_ttl);
7398 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7400 " calling generic handler...\n");
7401 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7404 /* receive done gets called when last copy is sent to a neighbor */
7410 * Handler for client's ACKs for payload traffic.
7412 * @param cls Closure (unused).
7413 * @param client Identification of the client.
7414 * @param message The actual message.
7417 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7418 const struct GNUNET_MessageHeader *message)
7420 struct GNUNET_MESH_LocalAck *msg;
7421 struct MeshTunnel *t;
7422 struct MeshClient *c;
7423 MESH_TunnelNumber tid;
7426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7427 /* Sanity check for client registration */
7428 if (NULL == (c = client_get (client)))
7431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7436 msg = (struct GNUNET_MESH_LocalAck *) message;
7438 /* Tunnel exists? */
7439 tid = ntohl (msg->tunnel_id);
7440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7441 t = tunnel_get_by_local_id (c, tid);
7445 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7446 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7451 ack = ntohl (msg->max_pid);
7452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7454 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7455 if (NULL != t->owner && t->owner->handle == client)
7457 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7459 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7463 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7464 tunnel_set_client_fwd_ack (t, c, ack);
7465 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7468 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7475 * Functions to handle messages from clients
7477 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7478 {&handle_local_new_client, NULL,
7479 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7480 {&handle_local_announce_regex, NULL,
7481 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7482 {&handle_local_tunnel_create, NULL,
7483 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7484 sizeof (struct GNUNET_MESH_TunnelMessage)},
7485 {&handle_local_tunnel_destroy, NULL,
7486 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7487 sizeof (struct GNUNET_MESH_TunnelMessage)},
7488 {&handle_local_tunnel_speed, NULL,
7489 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7490 sizeof (struct GNUNET_MESH_TunnelMessage)},
7491 {&handle_local_tunnel_speed, NULL,
7492 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7493 sizeof (struct GNUNET_MESH_TunnelMessage)},
7494 {&handle_local_tunnel_buffer, NULL,
7495 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7496 sizeof (struct GNUNET_MESH_TunnelMessage)},
7497 {&handle_local_tunnel_buffer, NULL,
7498 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7499 sizeof (struct GNUNET_MESH_TunnelMessage)},
7500 {&handle_local_connect_add, NULL,
7501 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7502 sizeof (struct GNUNET_MESH_PeerControl)},
7503 {&handle_local_connect_del, NULL,
7504 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7505 sizeof (struct GNUNET_MESH_PeerControl)},
7506 {&handle_local_blacklist, NULL,
7507 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7508 sizeof (struct GNUNET_MESH_PeerControl)},
7509 {&handle_local_unblacklist, NULL,
7510 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7511 sizeof (struct GNUNET_MESH_PeerControl)},
7512 {&handle_local_connect_by_type, NULL,
7513 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7514 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7515 {&handle_local_connect_by_string, NULL,
7516 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7517 {&handle_local_unicast, NULL,
7518 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7519 {&handle_local_to_origin, NULL,
7520 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7521 {&handle_local_multicast, NULL,
7522 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7523 {&handle_local_ack, NULL,
7524 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7525 sizeof (struct GNUNET_MESH_LocalAck)},
7531 * To be called on core init/fail.
7533 * @param cls service closure
7534 * @param server handle to the server for this service
7535 * @param identity the public identity of this peer
7538 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7539 const struct GNUNET_PeerIdentity *identity)
7542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7543 core_handle = server;
7544 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7548 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7550 GNUNET_abort(); // Try harder
7557 * Method called whenever a given peer connects.
7559 * @param cls closure
7560 * @param peer peer identity this notification is about
7561 * @param atsi performance data for the connection
7562 * @param atsi_count number of records in 'atsi'
7565 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7566 const struct GNUNET_ATS_Information *atsi,
7567 unsigned int atsi_count)
7569 struct MeshPeerInfo *peer_info;
7570 struct MeshPeerPath *path;
7572 DEBUG_CONN ("Peer connected\n");
7573 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7574 peer_info = peer_info_get (peer);
7575 if (myid == peer_info->id)
7577 DEBUG_CONN (" (self)\n");
7582 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7584 path = path_new (2);
7585 path->peers[0] = myid;
7586 path->peers[1] = peer_info->id;
7587 GNUNET_PEER_change_rc (myid, 1);
7588 GNUNET_PEER_change_rc (peer_info->id, 1);
7589 peer_info_add_path (peer_info, path, GNUNET_YES);
7590 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7596 * Method called whenever a peer disconnects.
7598 * @param cls closure
7599 * @param peer peer identity this notification is about
7602 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7604 struct MeshPeerInfo *pi;
7605 struct MeshPeerQueue *q;
7606 struct MeshPeerQueue *n;
7608 DEBUG_CONN ("Peer disconnected\n");
7609 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7621 /* try to reroute this traffic instead */
7622 queue_destroy(q, GNUNET_YES);
7626 peer_info_remove_path (pi, pi->id, myid);
7629 DEBUG_CONN (" (self)\n");
7631 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7636 /******************************************************************************/
7637 /************************ MAIN FUNCTIONS ****************************/
7638 /******************************************************************************/
7641 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7643 * @param cls closure
7644 * @param key current key code
7645 * @param value value in the hash map
7646 * @return GNUNET_YES if we should continue to iterate,
7650 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7652 struct MeshTunnel *t = value;
7659 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7661 * @param cls closure
7662 * @param key current key code
7663 * @param value value in the hash map
7664 * @return GNUNET_YES if we should continue to iterate,
7668 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7670 struct MeshPeerInfo *p = value;
7671 struct MeshPeerQueue *q;
7672 struct MeshPeerQueue *n;
7680 queue_destroy(q, GNUNET_YES);
7684 peer_info_destroy (p);
7689 * Task run during shutdown.
7695 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7699 if (core_handle != NULL)
7701 GNUNET_CORE_disconnect (core_handle);
7704 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7705 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7706 if (dht_handle != NULL)
7708 GNUNET_DHT_disconnect (dht_handle);
7713 GNUNET_SERVER_notification_context_destroy (nc);
7716 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7718 GNUNET_SCHEDULER_cancel (announce_id_task);
7719 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7725 * Process mesh requests.
7727 * @param cls closure
7728 * @param server the initialized server
7729 * @param c configuration to use
7732 run (void *cls, struct GNUNET_SERVER_Handle *server,
7733 const struct GNUNET_CONFIGURATION_Handle *c)
7735 struct MeshPeerInfo *peer;
7736 struct MeshPeerPath *p;
7739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7740 server_handle = server;
7741 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7742 NULL, /* Closure passed to MESH functions */
7743 &core_init, /* Call core_init once connected */
7744 &core_connect, /* Handle connects */
7745 &core_disconnect, /* remove peers on disconnects */
7746 NULL, /* Don't notify about all incoming messages */
7747 GNUNET_NO, /* For header only in notification */
7748 NULL, /* Don't notify about all outbound messages */
7749 GNUNET_NO, /* For header-only out notification */
7750 core_handlers); /* Register these handlers */
7752 if (core_handle == NULL)
7755 GNUNET_SCHEDULER_shutdown ();
7760 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7763 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7765 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7767 GNUNET_SCHEDULER_shutdown ();
7772 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7773 &refresh_path_time))
7775 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7777 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7778 "refresh path time");
7779 GNUNET_SCHEDULER_shutdown ();
7784 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7785 &app_announce_time))
7787 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7789 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7790 "app announce time");
7791 GNUNET_SCHEDULER_shutdown ();
7796 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7801 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7802 "id announce time");
7803 GNUNET_SCHEDULER_shutdown ();
7808 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7809 &unacknowledged_wait_time))
7811 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7813 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7814 "unacknowledged wait time");
7815 GNUNET_SCHEDULER_shutdown ();
7820 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7823 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7825 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7827 GNUNET_SCHEDULER_shutdown ();
7832 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7835 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7837 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7839 GNUNET_SCHEDULER_shutdown ();
7844 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7849 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7851 GNUNET_SCHEDULER_shutdown ();
7856 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7859 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7861 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7867 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7868 &dht_replication_level))
7870 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7872 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7873 "dht replication level", 10);
7874 dht_replication_level = 10;
7878 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7879 GNUNET_free (keyfile);
7880 if (my_private_key == NULL)
7882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7883 _("Mesh service could not access hostkey. Exiting.\n"));
7884 GNUNET_SCHEDULER_shutdown ();
7887 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7888 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7889 &my_full_id.hashPubKey);
7890 myid = GNUNET_PEER_intern (&my_full_id);
7891 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7892 "Mesh for peer [%s] starting\n",
7893 GNUNET_i2s(&my_full_id));
7895 // transport_handle = GNUNET_TRANSPORT_connect(c,
7902 dht_handle = GNUNET_DHT_connect (c, 64);
7903 if (dht_handle == NULL)
7908 stats = GNUNET_STATISTICS_create ("mesh", c);
7912 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7914 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7915 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7916 peers = GNUNET_CONTAINER_multihashmap_create (32);
7917 applications = GNUNET_CONTAINER_multihashmap_create (32);
7918 types = GNUNET_CONTAINER_multihashmap_create (32);
7920 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7921 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7922 GNUNET_SERVER_disconnect_notify (server_handle,
7923 &handle_local_client_disconnect, NULL);
7927 clients_tail = NULL;
7930 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7931 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7933 /* Create a peer_info for the local peer */
7934 peer = peer_info_get (&my_full_id);
7937 GNUNET_PEER_change_rc (myid, 1);
7938 peer_info_add_path (peer, p, GNUNET_YES);
7940 /* Scheduled the task to clean up when shutdown is called */
7941 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7948 * The main function for the mesh service.
7950 * @param argc number of arguments from the command line
7951 * @param argv command line arguments
7952 * @return 0 ok, 1 on error
7955 main (int argc, char *const *argv)
7959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7962 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
7968 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7969 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
7970 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);