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...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
44 * - set ttl relative to tree depth
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_YES
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #if MESH_DEBUG_CONNECTION
64 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
66 #define DEBUG_CONN(...)
70 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
72 #define DEBUG_DHT(...)
75 /******************************************************************************/
76 /************************ DATA STRUCTURES ****************************/
77 /******************************************************************************/
79 /** FWD declaration */
85 * Struct representing a piece of data being sent to other peers
89 /** Tunnel it belongs to. */
92 /** In case of a multicast, task to allow a client to send more data if
93 * some neighbor is too slow. */
94 GNUNET_SCHEDULER_TaskIdentifier *task;
96 /** How many remaining neighbors we need to send this to. */
97 unsigned int *reference_counter;
99 /** Size of the data. */
108 * Struct containing info about a queued transmission to this peer
115 struct MeshPeerQueue *next;
120 struct MeshPeerQueue *prev;
123 * Peer this transmission is directed to.
125 struct MeshPeerInfo *peer;
128 * Tunnel this message belongs to.
130 struct MeshTunnel *tunnel;
133 * Pointer to info stucture used as cls.
143 * Size of the message
150 * Struct containing all info possibly needed to build a package when called
153 struct MeshTransmissionDescriptor
155 /** ID of the tunnel this packet travels in */
156 struct MESH_TunnelID *origin;
158 /** Who was this message being sent to */
159 struct MeshPeerInfo *peer;
161 /** Ultimate destination of the packet */
162 GNUNET_PEER_Id destination;
164 /** Data descriptor */
165 struct MeshData* mesh_data;
170 * Struct containing all information regarding a given peer
180 * Last time we heard from this peer
182 struct GNUNET_TIME_Absolute last_contact;
185 * Number of attempts to reconnect so far
187 int n_reconnect_attempts;
190 * Paths to reach the peer, ordered by ascending hop count
192 struct MeshPeerPath *path_head;
195 * Paths to reach the peer, ordered by ascending hop count
197 struct MeshPeerPath *path_tail;
200 * Handle to stop the DHT search for a path to this peer
202 struct GNUNET_DHT_GetHandle *dhtget;
205 * Closure given to the DHT GET
207 struct MeshPathInfo *dhtgetcls;
210 * Array of tunnels this peer participates in
211 * (most probably a small amount, therefore not a hashmap)
212 * When the path to the peer changes, notify these tunnels to let them
213 * re-adjust their path trees.
215 struct MeshTunnel **tunnels;
218 * Number of tunnels this peers participates in
220 unsigned int ntunnels;
223 * Transmission queue to core DLL head
225 struct MeshPeerQueue *queue_head;
228 * Transmission queue to core DLL tail
230 struct MeshPeerQueue *queue_tail;
233 * How many messages are in the queue to this peer.
235 unsigned int queue_n;
238 * Handle to for queued transmissions
240 struct GNUNET_CORE_TransmitHandle *core_transmit;
245 * Globally unique tunnel identification (owner + number)
246 * DO NOT USE OVER THE NETWORK
251 * Node that owns the tunnel
256 * Tunnel number to differentiate all the tunnels owned by the node oid
257 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
259 MESH_TunnelNumber tid;
264 * Struct containing all information regarding a tunnel
265 * For an intermediate node the improtant info used will be:
266 * - id Tunnel unique identification
267 * - paths[0] To know where to send it next
268 * - metainfo: ready, speeds, accounting
275 struct MESH_TunnelID id;
278 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
280 MESH_TunnelNumber local_tid;
283 * Local tunnel number for local destination clients (incoming number)
284 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
287 MESH_TunnelNumber local_tid_dest;
290 * Local count ID of the last packet seen/sent.
295 * SKIP value for this tunnel.
300 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
302 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
305 * Last ACK sent towards the origin.
310 * How many messages are in the queue.
312 unsigned int queue_n;
315 * How many messages do we accept in the queue.
317 unsigned int queue_max;
320 * Is the speed on the tunnel limited to the slowest peer?
325 * Is the tunnel bufferless (minimum latency)?
330 * Flag to signal the destruction of the tunnel.
331 * If this is set GNUNET_YES the tunnel will be destroyed
332 * when the queue is empty.
337 * Last time the tunnel was used
339 struct GNUNET_TIME_Absolute timestamp;
342 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
343 * containing peers added by id or by type, not intermediate peers.
345 struct GNUNET_CONTAINER_MultiHashMap *peers;
348 * Number of peers that are connected and potentially ready to receive data
350 unsigned int peers_ready;
353 * Number of peers that have been added to the tunnel
355 unsigned int peers_total;
358 * Client owner of the tunnel, if any
360 struct MeshClient *owner;
363 * Clients that have been informed about and want to stay in the tunnel.
365 struct MeshClient **clients;
368 * FWD ACK value of each active client: up to what message can we transmit
371 uint32_t *clients_acks;
374 * BCK ACK value of the root client, owner of the tunnel,
375 * up to what message PID can we sent him.
377 uint32_t root_client_ack;
380 * Number of elements in clients/clients_acks
382 unsigned int nclients;
385 * Clients that have been informed but requested to leave the tunnel.
387 struct MeshClient **ignore;
390 * Number of elements in clients
392 unsigned int nignore;
397 GNUNET_PEER_Id *blacklisted;
400 * Number of elements in blacklisted
402 unsigned int nblacklisted;
405 * Bloomfilter (for peer identities) to stop circular routes
407 char bloomfilter[MESH_BLOOM_SIZE];
412 struct MeshTunnelTree *tree;
415 * Application type we are looking for in this tunnel
417 GNUNET_MESH_ApplicationType type;
420 * Used to search peers offering a service
422 struct GNUNET_DHT_GetHandle *dht_get_type;
425 * Initial context of the regex search for a connect_by_string
427 struct MeshRegexSearchContext *regex_ctx;
430 * Task to keep the used paths alive
432 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
435 * Task to destroy the tunnel after timeout
437 * FIXME: merge the two? a tunnel will have either
438 * a path refresh OR a timeout, never both!
440 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
445 * Info about a child node in a tunnel, needed to perform flow control.
447 struct MeshTunnelChildInfo
450 * ID of the child node.
465 * Maximum PID allowed (FWD ACK received).
470 * Last ACK sent to that child (BCK ACK).
477 * Info collected during iteration of child nodes in order to get the ACK value
480 struct MeshTunnelChildIteratorContext
483 * Tunnel whose info is being collected.
485 struct MeshTunnel *t;
488 * Maximum child ACK so far.
490 uint32_t max_child_ack;
493 * Number of children nodes
495 unsigned int nchildren;
500 * Info needed to work with tunnel paths and peers
507 struct MeshTunnel *t;
510 * Neighbouring peer to whom we send the packet to
512 struct MeshPeerInfo *peer;
517 struct MeshPeerPath *path;
522 * Struct containing information about a client of the service
529 struct MeshClient *next;
534 struct MeshClient *prev;
537 * Tunnels that belong to this client, indexed by local id
539 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
542 * Tunnels this client has accepted, indexed by incoming local id
544 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
547 * Tunnels this client has rejected, indexed by incoming local id
549 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
551 * Handle to communicate with the client
553 struct GNUNET_SERVER_Client *handle;
556 * Applications that this client has claimed to provide
558 struct GNUNET_CONTAINER_MultiHashMap *apps;
561 * Messages that this client has declared interest in
563 struct GNUNET_CONTAINER_MultiHashMap *types;
566 * Whether the client is active or shutting down (don't send confirmations
567 * to a client that is shutting down.
572 * ID of the client, mainly for debug messages
577 * Regular expressions describing the services offered by this client.
579 char **regexes; // FIXME add timeout? API to remove a regex?
582 * Number of regular expressions in regexes.
584 unsigned int n_regex;
587 * Task to refresh all regular expresions in the DHT.
589 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
595 * Struct to keep information of searches of services described by a regex
596 * using a user-provided string service description.
598 struct MeshRegexSearchInfo
601 * Which tunnel is this for
603 struct MeshTunnel *t;
606 * User provided description of the searched service.
611 * Part of the description already consumed by the search.
618 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
621 * Results from running DHT GETs.
623 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
626 * Contexts, for each running DHT GET. Free all on end of search.
628 struct MeshRegexSearchContext **contexts;
631 * Number of contexts (branches/steps in search).
633 unsigned int n_contexts;
636 * Peer that is connecting via connect_by_string. When connected, free ctx.
641 * Other peers that are found but not yet being connected to.
643 GNUNET_PEER_Id *peers;
646 * Number of elements in peers.
648 unsigned int n_peers;
651 * Next peer to try to connect to.
656 * Timeout for a connect attempt.
657 * When reached, try to connect to a different peer, if any. If not,
658 * try the same peer again.
660 GNUNET_SCHEDULER_TaskIdentifier timeout;
665 * Struct to keep state of running searches that have consumed a part of
668 struct MeshRegexSearchContext
671 * Part of the description already consumed by
672 * this particular search branch.
677 * Information about the search.
679 struct MeshRegexSearchInfo *info;
683 /******************************************************************************/
684 /************************ DEBUG FUNCTIONS ****************************/
685 /******************************************************************************/
689 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
690 * @param cls string to print
691 * @param success GNUNET_OK if the PUT was transmitted,
692 * GNUNET_NO on timeout,
693 * GNUNET_SYSERR on disconnect from service
694 * after the PUT message was transmitted
695 * (so we don't know if it was received or not)
700 mesh_debug (void *cls, int success)
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
710 /******************************************************************************/
711 /*********************** GLOBAL VARIABLES ****************************/
712 /******************************************************************************/
716 * Configuration parameters
718 static struct GNUNET_TIME_Relative refresh_path_time;
719 static struct GNUNET_TIME_Relative app_announce_time;
720 static struct GNUNET_TIME_Relative id_announce_time;
721 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
722 static struct GNUNET_TIME_Relative connect_timeout;
723 static long long unsigned int default_ttl;
724 static long long unsigned int dht_replication_level;
725 static long long unsigned int max_tunnels;
726 static long long unsigned int max_msgs_queue;
729 * DLL with all the clients, head.
731 static struct MeshClient *clients;
734 * DLL with all the clients, tail.
736 static struct MeshClient *clients_tail;
739 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
741 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
744 * Number of tunnels known.
746 static unsigned long long n_tunnels;
749 * Tunnels incoming, indexed by MESH_TunnelNumber
750 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
752 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
755 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
757 static struct GNUNET_CONTAINER_MultiHashMap *peers;
760 * Handle to communicate with transport
762 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
765 * Handle to communicate with core.
767 static struct GNUNET_CORE_Handle *core_handle;
772 static struct GNUNET_DHT_Handle *dht_handle;
777 static struct GNUNET_SERVER_Handle *server_handle;
780 * Handle to the statistics service.
782 static struct GNUNET_STATISTICS_Handle *stats;
785 * Notification context, to send messages to local clients.
787 static struct GNUNET_SERVER_NotificationContext *nc;
790 * Local peer own ID (memory efficient handle).
792 static GNUNET_PEER_Id myid;
795 * Local peer own ID (full value).
797 static struct GNUNET_PeerIdentity my_full_id;
802 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
807 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
810 * Tunnel ID for the next created tunnel (global tunnel number).
812 static MESH_TunnelNumber next_tid;
815 * Tunnel ID for the next incoming tunnel (local tunnel number).
817 static MESH_TunnelNumber next_local_tid;
820 * All application types provided by this peer.
822 static struct GNUNET_CONTAINER_MultiHashMap *applications;
825 * All message types clients of this peer are interested in.
827 static struct GNUNET_CONTAINER_MultiHashMap *types;
830 * Task to periodically announce provided applications.
832 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
835 * Task to periodically announce itself in the network.
837 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
840 * Next ID to assign to a client.
842 unsigned int next_client_id;
845 /******************************************************************************/
846 /*********************** DECLARATIONS **************************/
847 /******************************************************************************/
849 /* FIXME move declarations here */
852 * Function to process paths received for a new peer addition. The recorded
853 * paths form the initial tunnel, which can be optimized later.
854 * Called on each result obtained for the DHT search.
857 * @param exp when will this value expire
858 * @param key key of the result
859 * @param type type of the result
860 * @param size number of bytes in data
861 * @param data pointer to the result data
864 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
865 const struct GNUNET_HashCode * key,
866 const struct GNUNET_PeerIdentity *get_path,
867 unsigned int get_path_length,
868 const struct GNUNET_PeerIdentity *put_path,
869 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
870 size_t size, const void *data);
874 * Function to process DHT string to regex matching.
875 * Called on each result obtained for the DHT search.
877 * @param cls closure (search context)
878 * @param exp when will this value expire
879 * @param key key of the result
880 * @param get_path path of the get request (not used)
881 * @param get_path_length lenght of get_path (not used)
882 * @param put_path path of the put request (not used)
883 * @param put_path_length length of the put_path (not used)
884 * @param type type of the result
885 * @param size number of bytes in data
886 * @param data pointer to the result data
888 * TODO: re-issue the request after certain time? cancel after X results?
891 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
892 const struct GNUNET_HashCode * key,
893 const struct GNUNET_PeerIdentity *get_path,
894 unsigned int get_path_length,
895 const struct GNUNET_PeerIdentity *put_path,
896 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
897 size_t size, const void *data);
901 * Function to process DHT string to regex matching.
902 * Called on each result obtained for the DHT search.
904 * @param cls closure (search context)
905 * @param exp when will this value expire
906 * @param key key of the result
907 * @param get_path path of the get request (not used)
908 * @param get_path_length lenght of get_path (not used)
909 * @param put_path path of the put request (not used)
910 * @param put_path_length length of the put_path (not used)
911 * @param type type of the result
912 * @param size number of bytes in data
913 * @param data pointer to the result data
916 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
917 const struct GNUNET_HashCode * key,
918 const struct GNUNET_PeerIdentity *get_path,
919 unsigned int get_path_length,
920 const struct GNUNET_PeerIdentity *put_path,
921 unsigned int put_path_length,
922 enum GNUNET_BLOCK_Type type,
923 size_t size, const void *data);
927 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
928 * and insert it in the appropiate structures if the peer is not known yet.
930 * @param peer Short identity of the peer.
932 * @return Existing or newly created peer info.
934 static struct MeshPeerInfo *
935 peer_info_get_short (const GNUNET_PEER_Id peer);
939 * Try to establish a new connection to this peer.
940 * Use the best path for the given tunnel.
941 * If the peer doesn't have any path to it yet, try to get one.
942 * If the peer already has some path, send a CREATE PATH towards it.
944 * @param peer PeerInfo of the peer.
945 * @param t Tunnel for which to create the path, if possible.
948 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
952 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
954 * If peer already exists, reevaluate shortest path and change if different.
956 * @param t Tunnel we want to add a new peer to
957 * @param peer PeerInfo of the peer being added
961 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
965 * Removes an explicit path from a tunnel, freeing all intermediate nodes
966 * that are no longer needed, as well as nodes of no longer reachable peers.
967 * The tunnel itself is also destoyed if results in a remote empty tunnel.
969 * @param t Tunnel from which to remove the path.
970 * @param peer Short id of the peer which should be removed.
973 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
977 * Search for a tunnel by global ID using full PeerIdentities.
979 * @param oid owner of the tunnel.
980 * @param tid global tunnel number.
982 * @return tunnel handler, NULL if doesn't exist.
984 static struct MeshTunnel *
985 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
989 * Delete an active client from the tunnel.
995 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
998 * Notify a tunnel that a connection has broken that affects at least
1001 * @param t Tunnel affected.
1002 * @param p1 Peer that got disconnected from p2.
1003 * @param p2 Peer that got disconnected from p1.
1005 * @return Short ID of the peer disconnected (either p1 or p2).
1006 * 0 if the tunnel remained unaffected.
1008 static GNUNET_PEER_Id
1009 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1014 * Get the current ack value for a tunnel, for data going from root to leaves,
1015 * taking in account the tunnel mode and the status of all children and clients.
1019 * @return Maximum PID allowed.
1022 tunnel_get_fwd_ack (struct MeshTunnel *t);
1025 * Get the current ack value for a tunnel, for data going from leaves to root,
1026 * taking in account the tunnel mode and the status of all children and clients.
1030 * @return Maximum PID allowed.
1033 tunnel_get_bck_ack (struct MeshTunnel *t);
1036 * Add a client to a tunnel, initializing all needed data structures.
1038 * @param t Tunnel to which add the client.
1039 * @param c Client which to add to the tunnel.
1042 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1046 * Iterator over edges in a regex block retrieved from the DHT.
1048 * @param cls Closure.
1049 * @param token Token that follows to next state.
1050 * @param len Lenght of token.
1051 * @param key Hash of next state.
1053 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1056 regex_edge_iterator (void *cls,
1059 const struct GNUNET_HashCode *key);
1063 * Find a path to a peer that offers a regex servcie compatible
1064 * with a given string.
1066 * @param key The key of the accepting state.
1067 * @param ctx Context containing info about the string, tunnel, etc.
1070 regex_find_path (const struct GNUNET_HashCode *key,
1071 struct MeshRegexSearchContext *ctx);
1075 * Queue and pass message to core when possible.
1077 * @param cls Closure (type dependant).
1078 * @param type Type of the message, 0 for a raw message.
1079 * @param size Size of the message.
1080 * @param dst Neighbor to send message to.
1081 * @param t Tunnel this message belongs to.
1084 queue_add (void *cls, uint16_t type, size_t size,
1085 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1088 * Free a transmission that was already queued with all resources
1089 * associated to the request.
1091 * @param queue Queue handler to cancel.
1092 * @param clear_cls Is it necessary to free associated cls?
1095 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1097 /******************************************************************************/
1098 /************************ ITERATORS ****************************/
1099 /******************************************************************************/
1102 * Iterator over found existing mesh regex blocks that match an ongoing search.
1104 * @param cls closure
1105 * @param key current key code
1106 * @param value value in the hash map
1107 * @return GNUNET_YES if we should continue to iterate,
1111 regex_result_iterator (void *cls,
1112 const struct GNUNET_HashCode * key,
1115 struct MeshRegexBlock *block = value;
1116 struct MeshRegexSearchContext *ctx = cls;
1118 if (GNUNET_YES == ntohl(block->accepting) &&
1119 ctx->position == strlen (ctx->info->description))
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1122 regex_find_path (key, ctx);
1123 return GNUNET_YES; // We found an accept state!
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1128 ctx->position, strlen(ctx->info->description),
1129 ntohl(block->accepting));
1132 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1133 ®ex_edge_iterator, ctx);
1140 * Iterator over edges in a regex block retrieved from the DHT.
1142 * @param cls Closure (context of the search).
1143 * @param token Token that follows to next state.
1144 * @param len Lenght of token.
1145 * @param key Hash of next state.
1147 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1150 regex_edge_iterator (void *cls,
1153 const struct GNUNET_HashCode *key)
1155 struct MeshRegexSearchContext *ctx = cls;
1156 struct MeshRegexSearchContext *new_ctx;
1157 struct MeshRegexSearchInfo *info = ctx->info;
1158 struct GNUNET_DHT_GetHandle *get_h;
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1165 current = &info->description[ctx->position];
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1167 current_len = strlen (info->description) - ctx->position;
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1172 if (len > current_len)
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1175 return GNUNET_YES; // Token too long, wont match
1177 if (0 != strncmp (current, token, len))
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1180 return GNUNET_YES; // Token doesn't match
1182 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1183 new_ctx->info = info;
1184 new_ctx->position = ctx->position + len;
1185 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1187 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1190 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1191 ®ex_result_iterator,
1193 return GNUNET_YES; // We are already looking for it
1195 /* Start search in DHT */
1197 GNUNET_DHT_get_start (dht_handle, /* handle */
1198 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1199 key, /* key to search */
1200 dht_replication_level, /* replication level */
1201 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1202 NULL, /* xquery */ // FIXME BLOOMFILTER
1203 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1204 &dht_get_string_handler, new_ctx);
1206 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1207 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1217 * Iterator over hash map entries to cancel DHT GET requests after a
1218 * successful connect_by_string.
1220 * @param cls Closure (unused).
1221 * @param key Current key code (unused).
1222 * @param value Value in the hash map (get handle).
1223 * @return GNUNET_YES if we should continue to iterate,
1227 regex_cancel_dht_get (void *cls,
1228 const struct GNUNET_HashCode * key,
1231 struct GNUNET_DHT_GetHandle *h = value;
1233 GNUNET_DHT_get_stop (h);
1239 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1240 * search for connect_by_string.
1242 * @param cls Closure (unused).
1243 * @param key Current key code (unused).
1244 * @param value MeshRegexBlock in the hash map.
1245 * @return GNUNET_YES if we should continue to iterate,
1249 regex_free_result (void *cls,
1250 const struct GNUNET_HashCode * key,
1254 GNUNET_free (value);
1260 * Regex callback iterator to store own service description in the DHT.
1262 * @param cls closure.
1263 * @param key hash for current state.
1264 * @param proof proof for current state.
1265 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1266 * @param num_edges number of edges leaving current state.
1267 * @param edges edges leaving current state.
1270 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1271 int accepting, unsigned int num_edges,
1272 const struct GNUNET_REGEX_Edge *edges)
1274 struct MeshRegexBlock *block;
1275 struct MeshRegexEdge *block_edge;
1276 enum GNUNET_DHT_RouteOption opt;
1280 unsigned int offset;
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284 " regex dht put for state %s\n",
1286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1294 if (GNUNET_YES == accepting)
1296 struct MeshRegexAccept block;
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1299 " state %s is accepting, putting own id\n",
1301 size = sizeof (block);
1303 block.id = my_full_id;
1305 GNUNET_DHT_put(dht_handle, key,
1306 dht_replication_level,
1307 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1308 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1311 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1316 len = strlen(proof);
1317 size = sizeof (struct MeshRegexBlock) + len;
1318 block = GNUNET_malloc (size);
1321 block->n_proof = htonl (len);
1322 block->n_edges = htonl (num_edges);
1323 block->accepting = htonl (accepting);
1325 /* Store the proof at the end of the block. */
1326 aux = (char *) &block[1];
1327 memcpy (aux, proof, len);
1330 /* Store each edge in a variable length MeshEdge struct at the
1331 * very end of the MeshRegexBlock structure.
1333 for (i = 0; i < num_edges; i++)
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 " edge %s towards %s\n",
1338 GNUNET_h2s(&edges[i].destination));
1340 /* aux points at the end of the last block */
1341 len = strlen (edges[i].label);
1342 size += sizeof (struct MeshRegexEdge) + len;
1343 // Calculate offset FIXME is this ok? use size instead?
1344 offset = aux - (char *) block;
1345 block = GNUNET_realloc (block, size);
1346 aux = &((char *) block)[offset];
1347 block_edge = (struct MeshRegexEdge *) aux;
1348 block_edge->key = edges[i].destination;
1349 block_edge->n_token = htonl (len);
1350 aux = (char *) &block_edge[1];
1351 memcpy (aux, edges[i].label, len);
1355 GNUNET_DHT_put(dht_handle, key,
1356 dht_replication_level,
1358 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1360 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1364 GNUNET_free (block);
1369 * Store the regular expression describing a local service into the DHT.
1371 * @param regex The regular expresion.
1374 regex_put (const char *regex)
1376 struct GNUNET_REGEX_Automaton *dfa;
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1379 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1380 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1381 GNUNET_REGEX_automaton_destroy (dfa);
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1387 * Find a path to a peer that offers a regex servcie compatible
1388 * with a given string.
1390 * @param key The key of the accepting state.
1391 * @param ctx Context containing info about the string, tunnel, etc.
1394 regex_find_path (const struct GNUNET_HashCode *key,
1395 struct MeshRegexSearchContext *ctx)
1397 struct GNUNET_DHT_GetHandle *get_h;
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1400 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1401 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1402 key, /* key to search */
1403 dht_replication_level, /* replication level */
1404 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1405 GNUNET_DHT_RO_RECORD_ROUTE,
1406 NULL, /* xquery */ // FIXME BLOOMFILTER
1407 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1408 &dht_get_string_accept_handler, ctx);
1409 GNUNET_break (GNUNET_OK ==
1410 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1413 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1418 * Function called if the connect attempt to a peer found via
1419 * connect_by_string times out. Try to connect to another peer, if any.
1420 * Otherwise try to reconnect to the same peer.
1422 * @param cls Closure (info about regex search).
1423 * @param tc TaskContext.
1426 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1428 struct MeshRegexSearchInfo *info = cls;
1429 struct MeshPeerInfo *peer_info;
1433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1434 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1435 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1443 if (0 < info->n_peers)
1445 // Select next peer, put current in that spot.
1446 id = info->peers[info->i_peer];
1447 info->peers[info->i_peer] = info->peer;
1448 info->i_peer = (info->i_peer + 1) % info->n_peers;
1452 // Try to connect to same peer again.
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1457 peer_info = peer_info_get_short(id);
1458 tunnel_add_peer (info->t, peer_info);
1460 tunnel_delete_peer (info->t, old);
1461 peer_info_connect (peer_info, info->t);
1462 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1463 ®ex_connect_timeout,
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1470 * Cancel an ongoing regex search in the DHT and free all resources.
1472 * @param ctx The search context.
1475 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1477 struct MeshRegexSearchInfo *info = ctx->info;
1480 GNUNET_free (info->description);
1481 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1482 ®ex_cancel_dht_get, NULL);
1483 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1484 ®ex_free_result, NULL);
1485 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1486 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1487 info->t->regex_ctx = NULL;
1488 for (i = 0; i < info->n_contexts; i++)
1490 GNUNET_free (info->contexts[i]);
1492 if (0 < info->n_contexts)
1493 GNUNET_free (info->contexts);
1494 if (0 < info->n_peers)
1495 GNUNET_free (info->peers);
1496 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1498 GNUNET_SCHEDULER_cancel(info->timeout);
1504 /******************************************************************************/
1505 /************************ PERIODIC FUNCTIONS ****************************/
1506 /******************************************************************************/
1509 * Announce iterator over for each application provided by the peer
1511 * @param cls closure
1512 * @param key current key code
1513 * @param value value in the hash map
1514 * @return GNUNET_YES if we should continue to
1519 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1521 struct PBlock block;
1522 struct MeshClient *c;
1524 block.id = my_full_id;
1525 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1526 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1527 if (0 == block.type)
1532 block.type = htonl (block.type);
1534 GNUNET_break (NULL !=
1535 GNUNET_DHT_put (dht_handle, key,
1536 dht_replication_level,
1537 GNUNET_DHT_RO_RECORD_ROUTE |
1538 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1539 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1541 (const char *) &block,
1542 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1544 app_announce_time, NULL, NULL));
1550 * Periodically announce what applications are provided by local clients
1553 * @param cls closure
1554 * @param tc task context
1557 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1559 struct MeshClient *c = cls;
1562 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1564 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1568 DEBUG_DHT ("Starting PUT for regex\n");
1570 for (i = 0; i < c->n_regex; i++)
1572 regex_put (c->regexes[i]);
1574 c->regex_announce_task =
1575 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1576 DEBUG_DHT ("Finished PUT for regex\n");
1583 * Periodically announce what applications are provided by local clients
1586 * @param cls closure
1587 * @param tc task context
1590 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1592 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1594 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1598 DEBUG_DHT ("Starting PUT for apps\n");
1600 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1602 announce_applications_task =
1603 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1605 DEBUG_DHT ("Finished PUT for apps\n");
1612 * Periodically announce self id in the DHT
1614 * @param cls closure
1615 * @param tc task context
1618 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1620 struct PBlock block;
1622 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1624 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1628 * - Set data expiration in function of X
1629 * - Adapt X to churn
1631 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1633 block.id = my_full_id;
1634 block.type = htonl (0);
1635 GNUNET_DHT_put (dht_handle, /* DHT handle */
1636 &my_full_id.hashPubKey, /* Key to use */
1637 dht_replication_level, /* Replication level */
1638 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1639 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1640 sizeof (block), /* Size of the data */
1641 (const char *) &block, /* Data itself */
1642 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1643 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1644 NULL, /* Continuation */
1645 NULL); /* Continuation closure */
1647 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1651 /******************************************************************************/
1652 /****************** GENERAL HELPER FUNCTIONS ************************/
1653 /******************************************************************************/
1656 * Check if one pid is bigger than other, accounting for overflow.
1658 * @param bigger Argument that should be bigger.
1659 * @param smaller Argument that should be smaller.
1661 * @return True if big is bigger than small
1664 is_pid_bigger (uint32_t bigger, uint32_t smaller)
1666 return (GNUNET_YES == PID_OVERFLOW(smaller, bigger) ||
1667 (bigger > smaller && GNUNET_NO == PID_OVERFLOW(bigger, smaller)));
1671 * Get the higher ACK value out of two values, taking in account overflow.
1673 * @param a First ACK value.
1674 * @param b Second ACK value.
1676 * @return Highest ACK value from the two.
1679 max_pid (uint32_t a, uint32_t b)
1681 if (is_pid_bigger(a, b))
1688 * Get the lower ACK value out of two values, taking in account overflow.
1690 * @param a First ACK value.
1691 * @param b Second ACK value.
1693 * @return Lowest ACK value from the two.
1696 min_pid (uint32_t a, uint32_t b)
1698 if (is_pid_bigger(a, b))
1705 * Decrements the reference counter and frees all resources if needed
1707 * @param mesh_data Data Descriptor used in a multicast message.
1708 * Freed no longer needed (last message).
1711 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1713 /* Make sure it's a multicast packet */
1714 GNUNET_assert (NULL != mesh_data->reference_counter);
1716 if (0 == --(*(mesh_data->reference_counter)))
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1719 if (NULL != mesh_data->task)
1721 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1723 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1725 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1727 GNUNET_free (mesh_data->task);
1729 GNUNET_free (mesh_data->reference_counter);
1730 GNUNET_free (mesh_data->data);
1731 GNUNET_free (mesh_data);
1737 * Check if client has registered with the service and has not disconnected
1739 * @param client the client to check
1741 * @return non-NULL if client exists in the global DLL
1743 static struct MeshClient *
1744 client_get (struct GNUNET_SERVER_Client *client)
1746 struct MeshClient *c;
1751 if (c->handle == client)
1760 * Checks if a given client has subscribed to certain message type
1762 * @param message_type Type of message to check
1763 * @param c Client to check
1765 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1768 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1770 struct GNUNET_HashCode hc;
1772 if (NULL == c->types)
1774 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1775 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1780 * Allow a client to send more data after transmitting a multicast message
1781 * which some neighbor has not yet accepted altough a reasonable time has
1784 * @param cls Closure (DataDescriptor containing the task identifier)
1785 * @param tc Task Context
1787 * FIXME reference counter cshould be just int
1790 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1792 struct MeshData *mdata = cls;
1794 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1796 GNUNET_assert (NULL != mdata->reference_counter);
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1798 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1799 *(mdata->reference_counter));
1800 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1801 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1806 * Check whether client wants traffic from a tunnel.
1808 * @param c Client to check.
1809 * @param t Tunnel to be found.
1811 * @return GNUNET_YES if client knows tunnel.
1813 * TODO look in client hashmap
1816 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1820 for (i = 0; i < t->nclients; i++)
1821 if (t->clients[i] == c)
1828 * Check whether client has been informed about a tunnel.
1830 * @param c Client to check.
1831 * @param t Tunnel to be found.
1833 * @return GNUNET_YES if client knows tunnel.
1835 * TODO look in client hashmap
1838 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1842 for (i = 0; i < t->nignore; i++)
1843 if (t->ignore[i] == c)
1845 return client_wants_tunnel(c, t);
1850 * Marks a client as uninterested in traffic from the tunnel, updating both
1851 * client and tunnel to reflect this.
1853 * @param c Client that doesn't want traffic anymore.
1854 * @param t Tunnel which should be ignored.
1856 * FIXME when to delete an incoming tunnel?
1859 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1861 struct GNUNET_HashCode hash;
1863 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1864 GNUNET_break (GNUNET_YES ==
1865 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1867 GNUNET_break (GNUNET_YES ==
1868 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1869 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1870 tunnel_delete_active_client (t, c);
1871 GNUNET_array_append (t->ignore, t->nignore, c);
1876 * Deletes a tunnel from a client (either owner or destination). To be used on
1877 * tunnel destroy, otherwise, use client_ignore_tunnel.
1879 * @param c Client whose tunnel to delete.
1880 * @param t Tunnel which should be deleted.
1883 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1885 struct GNUNET_HashCode hash;
1889 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1890 GNUNET_assert (GNUNET_YES ==
1891 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1897 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1899 GNUNET_assert (GNUNET_YES ==
1900 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1904 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1912 * Send the message to all clients that have subscribed to its type
1914 * @param msg Pointer to the message itself
1915 * @param payload Pointer to the payload of the message.
1916 * @return number of clients this message was sent to
1919 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1920 const struct GNUNET_MessageHeader *payload)
1922 struct GNUNET_PeerIdentity *oid;
1923 struct MeshClient *c;
1924 struct MeshTunnel *t;
1925 MESH_TunnelNumber *tid;
1928 char cbuf[htons (msg->size)];
1930 type = ntohs (payload->type);
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1934 memcpy (cbuf, msg, sizeof (cbuf));
1935 switch (htons (msg->type))
1937 struct GNUNET_MESH_Unicast *uc;
1938 struct GNUNET_MESH_Multicast *mc;
1939 struct GNUNET_MESH_ToOrigin *to;
1941 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1942 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1946 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1947 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1951 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1952 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1960 t = tunnel_get (oid, ntohl (*tid));
1967 for (count = 0, c = clients; c != NULL; c = c->next)
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1970 if (client_is_subscribed (type, c))
1972 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1976 *tid = htonl (t->local_tid);
1980 if (GNUNET_NO == client_knows_tunnel (c, t))
1982 /* This client doesn't know the tunnel */
1983 struct GNUNET_MESH_TunnelNotification tmsg;
1984 struct GNUNET_HashCode hash;
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1987 tmsg.header.size = htons (sizeof (tmsg));
1988 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1989 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1990 tmsg.tunnel_id = htonl (t->local_tid_dest);
1991 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1992 &tmsg.header, GNUNET_NO);
1993 tunnel_add_client (t, c);
1994 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1996 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1997 c->incoming_tunnels, &hash, t,
1998 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2000 *tid = htonl (t->local_tid_dest);
2003 /* Check if the client wants to get traffic from the tunnel */
2004 if (GNUNET_NO == client_wants_tunnel(c, t))
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2008 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2009 (struct GNUNET_MessageHeader
2010 *) cbuf, GNUNET_NO);
2018 * Notify the client that owns the tunnel that a peer has connected to it
2019 * (the requested path to it has been confirmed).
2021 * @param t Tunnel whose owner to notify
2022 * @param id Short id of the peer that has connected
2025 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2027 struct GNUNET_MESH_PeerControl pc;
2029 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2030 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2031 pc.tunnel_id = htonl (t->local_tid);
2032 GNUNET_PEER_resolve (id, &pc.peer);
2033 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2039 * Notify a the client of a tunnel about how many more
2040 * payload packages will we accept on a given tunnel,
2041 * distinguiching between root and leaf clients.
2043 * @param c Client whom to send the ACK.
2044 * @param t Tunnel on which to send the ACK.
2047 send_client_tunnel_ack (struct MeshClient *c, struct MeshTunnel *t)
2049 struct GNUNET_MESH_LocalAck msg;
2050 MESH_TunnelNumber tid;
2053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2054 "Sending client ACK on tunnel %X\n",
2059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to client %u\n", c->id);
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (owner, FWD ACK)\n");
2064 ack = tunnel_get_fwd_ack (t);
2069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (leaf, BCK ACK)\n");
2070 ack = tunnel_get_bck_ack (t);
2071 tid = t->local_tid_dest;
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2075 if (t->last_ack == ack)
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2080 msg.header.size = htons (sizeof (msg));
2081 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2082 msg.tunnel_id = htonl (tid);
2083 msg.max_pid = htonl (ack);
2085 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2086 &msg.header, GNUNET_NO);
2091 * Notify all clients (not depending on registration status) that the incoming
2092 * tunnel is no longer valid.
2094 * @param t Tunnel that was destroyed.
2097 send_clients_tunnel_destroy (struct MeshTunnel *t)
2099 struct GNUNET_MESH_TunnelMessage msg;
2101 msg.header.size = htons (sizeof (msg));
2102 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2103 msg.tunnel_id = htonl (t->local_tid_dest);
2104 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2109 * Notify clients of tunnel disconnections, if needed.
2110 * In case the origin disconnects, the destination clients get a tunnel destroy
2111 * notification. If the last destination disconnects (only one remaining client
2112 * in tunnel), the origin gets a (local ID) peer disconnected.
2113 * Note that the function must be called BEFORE removing the client from
2116 * @param t Tunnel that was destroyed.
2117 * @param c Client that disconnected.
2120 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2126 struct GNUNET_MESH_TunnelMessage msg;
2128 msg.header.size = htons (sizeof (msg));
2129 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2130 msg.tunnel_id = htonl (t->local_tid_dest);
2131 for (i = 0; i < t->nclients; i++)
2132 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2133 &msg.header, GNUNET_NO);
2135 // FIXME when to disconnect an incoming tunnel?
2136 else if (1 == t->nclients && NULL != t->owner)
2138 struct GNUNET_MESH_PeerControl msg;
2140 msg.header.size = htons (sizeof (msg));
2141 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2142 msg.tunnel_id = htonl (t->local_tid);
2143 msg.peer = my_full_id;
2144 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2145 &msg.header, GNUNET_NO);
2151 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2152 * and insert it in the appropiate structures if the peer is not known yet.
2154 * @param peer Full identity of the peer.
2156 * @return Existing or newly created peer info.
2158 static struct MeshPeerInfo *
2159 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2161 struct MeshPeerInfo *peer_info;
2163 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2164 if (NULL == peer_info)
2167 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2168 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2169 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2170 peer_info->id = GNUNET_PEER_intern (peer);
2178 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2179 * and insert it in the appropiate structures if the peer is not known yet.
2181 * @param peer Short identity of the peer.
2183 * @return Existing or newly created peer info.
2185 static struct MeshPeerInfo *
2186 peer_info_get_short (const GNUNET_PEER_Id peer)
2188 struct GNUNET_PeerIdentity id;
2190 GNUNET_PEER_resolve (peer, &id);
2191 return peer_info_get (&id);
2196 * Iterator to remove the tunnel from the list of tunnels a peer participates
2199 * @param cls Closure (tunnel info)
2200 * @param key GNUNET_PeerIdentity of the peer (unused)
2201 * @param value PeerInfo of the peer
2203 * @return always GNUNET_YES, to keep iterating
2206 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2208 struct MeshTunnel *t = cls;
2209 struct MeshPeerInfo *peer = value;
2212 for (i = 0; i < peer->ntunnels; i++)
2215 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2218 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2219 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2228 * Core callback to write a pre-constructed data packet to core buffer
2230 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2231 * @param size Number of bytes available in buf.
2232 * @param buf Where the to write the message.
2234 * @return number of bytes written to buf
2237 send_core_data_raw (void *cls, size_t size, void *buf)
2239 struct MeshTransmissionDescriptor *info = cls;
2240 struct GNUNET_MessageHeader *msg;
2243 GNUNET_assert (NULL != info);
2244 GNUNET_assert (NULL != info->mesh_data);
2245 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2246 total_size = ntohs (msg->size);
2248 if (total_size > size)
2253 memcpy (buf, msg, total_size);
2254 data_descriptor_decrement_rc (info->mesh_data);
2261 * Sends an already built non-multicast message to a peer,
2262 * properly registrating all used resources.
2264 * @param message Message to send. Function makes a copy of it.
2265 * @param peer Short ID of the neighbor whom to send the message.
2266 * @param t Tunnel on which this message is transmitted.
2269 send_message (const struct GNUNET_MessageHeader *message,
2270 const struct GNUNET_PeerIdentity *peer,
2271 struct MeshTunnel *t)
2273 struct MeshTransmissionDescriptor *info;
2274 struct MeshPeerInfo *neighbor;
2275 struct MeshPeerPath *p;
2278 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2280 size = ntohs (message->size);
2281 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2282 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2283 info->mesh_data->data = GNUNET_malloc (size);
2284 memcpy (info->mesh_data->data, message, size);
2285 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
2287 struct GNUNET_MESH_Unicast *m;
2289 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2290 m->ttl = htonl (ntohl (m->ttl) - 1);
2292 info->mesh_data->data_len = size;
2293 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2294 *info->mesh_data->reference_counter = 1;
2295 neighbor = peer_info_get (peer);
2296 for (p = neighbor->path_head; NULL != p; p = p->next)
2305 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2306 GNUNET_free (info->mesh_data->data);
2307 GNUNET_free (info->mesh_data);
2311 info->peer = neighbor;
2321 * Sends a CREATE PATH message for a path to a peer, properly registrating
2322 * all used resources.
2324 * @param peer PeerInfo of the final peer for whom this path is being created.
2325 * @param p Path itself.
2326 * @param t Tunnel for which the path is created.
2329 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2330 struct MeshTunnel *t)
2332 struct GNUNET_PeerIdentity id;
2333 struct MeshPathInfo *path_info;
2334 struct MeshPeerInfo *neighbor;
2340 p = tree_get_path_to_peer (t->tree, peer->id);
2347 for (i = 0; i < p->length; i++)
2349 if (p->peers[i] == myid)
2352 if (i >= p->length - 1)
2358 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2360 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2361 path_info->path = p;
2363 neighbor = peer_info_get (&id);
2364 path_info->peer = neighbor;
2365 queue_add (path_info,
2366 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2367 sizeof (struct GNUNET_MESH_ManipulatePath) +
2368 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2375 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2377 * @param t Tunnel whose path to destroy.
2378 * @param destination Short ID of the peer to whom the path to destroy.
2381 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2383 struct MeshPeerPath *p;
2386 p = tree_get_path_to_peer (t->tree, destination);
2392 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2393 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2395 struct GNUNET_MESH_ManipulatePath *msg;
2396 struct GNUNET_PeerIdentity *pi;
2400 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2401 msg->header.size = htons (size);
2402 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2403 msg->tid = htonl (t->id.tid);
2404 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2405 for (i = 0; i < p->length; i++)
2407 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2409 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2416 * Try to establish a new connection to this peer.
2417 * Use the best path for the given tunnel.
2418 * If the peer doesn't have any path to it yet, try to get one.
2419 * If the peer already has some path, send a CREATE PATH towards it.
2421 * @param peer PeerInfo of the peer.
2422 * @param t Tunnel for which to create the path, if possible.
2425 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2427 struct MeshPeerPath *p;
2428 struct MeshPathInfo *path_info;
2430 if (NULL != peer->path_head)
2432 p = tree_get_path_to_peer (t->tree, peer->id);
2439 // FIXME always send create path to self
2442 send_create_path (peer, p, t);
2446 struct GNUNET_HashCode hash;
2449 send_client_peer_connected (t, myid);
2450 t->local_tid_dest = next_local_tid++;
2451 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2454 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2462 else if (NULL == peer->dhtget)
2464 struct GNUNET_PeerIdentity id;
2466 GNUNET_PEER_resolve (peer->id, &id);
2467 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2468 path_info->peer = peer;
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2471 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2472 peer->dhtgetcls = path_info;
2473 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2474 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2475 &id.hashPubKey, /* key to search */
2476 dht_replication_level, /* replication level */
2477 GNUNET_DHT_RO_RECORD_ROUTE |
2478 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2479 NULL, /* xquery */ // FIXME BLOOMFILTER
2480 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2481 &dht_get_id_handler, path_info);
2483 /* Otherwise, there is no path but the DHT get is already started. */
2488 * Task to delay the connection of a peer
2490 * @param cls Closure (path info with tunnel and peer to connect).
2491 * Will be free'd on exection.
2492 * @param tc TaskContext
2495 peer_info_connect_task (void *cls,
2496 const struct GNUNET_SCHEDULER_TaskContext *tc)
2498 struct MeshPathInfo *path_info = cls;
2500 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2505 peer_info_connect (path_info->peer, path_info->t);
2511 * Destroy the peer_info and free any allocated resources linked to it
2513 * @param pi The peer_info to destroy.
2515 * @return GNUNET_OK on success
2518 peer_info_destroy (struct MeshPeerInfo *pi)
2520 struct GNUNET_PeerIdentity id;
2521 struct MeshPeerPath *p;
2522 struct MeshPeerPath *nextp;
2524 GNUNET_PEER_resolve (pi->id, &id);
2525 GNUNET_PEER_change_rc (pi->id, -1);
2528 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2531 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2532 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2534 if (NULL != pi->dhtget)
2536 GNUNET_DHT_get_stop (pi->dhtget);
2537 GNUNET_free (pi->dhtgetcls);
2543 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2553 * Remove all paths that rely on a direct connection between p1 and p2
2554 * from the peer itself and notify all tunnels about it.
2556 * @param peer PeerInfo of affected peer.
2557 * @param p1 GNUNET_PEER_Id of one peer.
2558 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2561 * TODO: optimize (see below)
2564 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2567 struct MeshPeerPath *p;
2568 struct MeshPeerPath *aux;
2569 struct MeshPeerInfo *peer_d;
2571 unsigned int destroyed;
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2578 p = peer->path_head;
2582 for (i = 0; i < (p->length - 1); i++)
2584 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2585 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2587 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2598 for (i = 0; i < peer->ntunnels; i++)
2600 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2604 * Problem: one or more peers have been deleted from the tunnel tree.
2605 * We don't know who they are to try to add them again.
2606 * We need to try to find a new path for each of the disconnected peers.
2607 * Some of them might already have a path to reach them that does not
2608 * involve p1 and p2. Adding all anew might render in a better tree than
2609 * the trivial immediate fix.
2611 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2612 * its children will be reachable trough him.
2614 peer_d = peer_info_get_short (d);
2617 for (p = peer_d->path_head; NULL != p; p = p->next)
2619 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2627 /* No callback, as peer will be already disconnected and a connection
2628 * scheduled by tunnel_notify_connection_broken.
2630 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2634 peer_info_connect (peer_d, peer->tunnels[i]);
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2642 * Add the path to the peer and update the path used to reach it in case this
2645 * @param peer_info Destination peer to add the path to.
2646 * @param path New path to add. Last peer must be the peer in arg 1.
2647 * Path will be either used of freed if already known.
2648 * @param trusted Do we trust that this path is real?
2651 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2654 struct MeshPeerPath *aux;
2658 if ((NULL == peer_info) || (NULL == path))
2661 path_destroy (path);
2664 if (path->peers[path->length - 1] != peer_info->id)
2667 path_destroy (path);
2670 if (path->length <= 2 && GNUNET_NO == trusted)
2672 /* Only allow CORE to tell us about direct paths */
2673 path_destroy (path);
2676 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2677 for (l = 1; l < path->length; l++)
2679 if (path->peers[l] == myid)
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2682 for (l2 = 0; l2 < path->length - l; l2++)
2684 path->peers[l2] = path->peers[l + l2];
2689 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2694 struct GNUNET_PeerIdentity id;
2696 GNUNET_PEER_resolve (peer_info->id, &id);
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2698 path->length, GNUNET_i2s (&id));
2701 l = path_get_length (path);
2708 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2709 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2711 l2 = path_get_length (aux);
2714 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2715 peer_info->path_tail, aux, path);
2720 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2722 path_destroy (path);
2727 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2734 * Add the path to the origin peer and update the path used to reach it in case
2735 * this is the shortest.
2736 * The path is given in peer_info -> destination, therefore we turn the path
2737 * upside down first.
2739 * @param peer_info Peer to add the path to, being the origin of the path.
2740 * @param path New path to add after being inversed.
2741 * @param trusted Do we trust that this path is real?
2744 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2745 struct MeshPeerPath *path, int trusted)
2748 peer_info_add_path (peer_info, path, trusted);
2753 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2754 * to obtain a local peer -> destination path and interning the peer ids.
2756 * @return Newly allocated and created path
2758 static struct MeshPeerPath *
2759 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2760 unsigned int get_path_length,
2761 const struct GNUNET_PeerIdentity *put_path,
2762 unsigned int put_path_length)
2764 struct MeshPeerPath *p;
2770 GNUNET_PEER_change_rc (myid, 1);
2771 i = get_path_length;
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2773 for (i--; i >= 0; i--)
2775 id = GNUNET_PEER_intern (&get_path[i]);
2776 if (p->length > 0 && id == p->peers[p->length - 1])
2778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2779 GNUNET_PEER_change_rc (id, -1);
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2784 GNUNET_i2s (&get_path[i]));
2786 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2787 p->peers[p->length - 1] = id;
2790 i = put_path_length;
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2792 for (i--; i >= 0; i--)
2794 id = GNUNET_PEER_intern (&put_path[i]);
2797 /* PUT path went through us, so discard the path up until now and start
2798 * from here to get a much shorter (and loop-free) path.
2803 if (p->length > 0 && id == p->peers[p->length - 1])
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2806 GNUNET_PEER_change_rc (id, -1);
2810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2811 GNUNET_i2s (&put_path[i]));
2813 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2814 p->peers[p->length - 1] = id;
2818 if (get_path_length > 0)
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2820 GNUNET_i2s (&get_path[0]));
2821 if (put_path_length > 0)
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2823 GNUNET_i2s (&put_path[0]));
2824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2826 for (i = 0; i < p->length; i++)
2828 struct GNUNET_PeerIdentity peer_id;
2830 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2832 GNUNET_i2s (&peer_id));
2840 * Adds a path to the peer_infos of all the peers in the path
2842 * @param p Path to process.
2843 * @param confirmed Whether we know if the path works or not.
2846 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2850 /* TODO: invert and add */
2851 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2852 for (i++; i < p->length; i++)
2854 struct MeshPeerInfo *aux;
2855 struct MeshPeerPath *copy;
2857 aux = peer_info_get_short (p->peers[i]);
2858 copy = path_duplicate (p);
2859 copy->length = i + 1;
2860 peer_info_add_path (aux, copy, GNUNET_NO);
2866 * Send keepalive packets for a peer
2868 * @param cls Closure (tunnel for which to send the keepalive).
2869 * @param tc Notification context.
2871 * TODO: implement explicit multicast keepalive?
2874 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2878 * Search for a tunnel among the incoming tunnels
2880 * @param tid the local id of the tunnel
2882 * @return tunnel handler, NULL if doesn't exist
2884 static struct MeshTunnel *
2885 tunnel_get_incoming (MESH_TunnelNumber tid)
2887 struct GNUNET_HashCode hash;
2889 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2890 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2891 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2896 * Search for a tunnel among the tunnels for a client
2898 * @param c the client whose tunnels to search in
2899 * @param tid the local id of the tunnel
2901 * @return tunnel handler, NULL if doesn't exist
2903 static struct MeshTunnel *
2904 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2906 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2908 return tunnel_get_incoming (tid);
2912 struct GNUNET_HashCode hash;
2914 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2915 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2921 * Search for a tunnel by global ID using PEER_ID
2923 * @param pi owner of the tunnel
2924 * @param tid global tunnel number
2926 * @return tunnel handler, NULL if doesn't exist
2928 static struct MeshTunnel *
2929 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2931 struct MESH_TunnelID id;
2932 struct GNUNET_HashCode hash;
2937 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2938 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2943 * Search for a tunnel by global ID using full PeerIdentities
2945 * @param oid owner of the tunnel
2946 * @param tid global tunnel number
2948 * @return tunnel handler, NULL if doesn't exist
2950 static struct MeshTunnel *
2951 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2953 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2958 * Delete an active client from the tunnel.
2964 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2968 for (i = 0; i < t->nclients; i++)
2970 if (t->clients[i] == c)
2972 t->clients[i] = t->clients[t->nclients - 1];
2973 t->clients_acks[i] = t->clients_acks[t->nclients - 1];
2974 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2976 GNUNET_array_grow (t->clients_acks, t->nclients, t->nclients - 1);
2984 * Delete an ignored client from the tunnel.
2990 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2994 for (i = 0; i < t->nignore; i++)
2996 if (t->ignore[i] == c)
2998 t->ignore[i] = t->ignore[t->nignore - 1];
2999 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3007 * Delete a client from the tunnel. It should be only done on
3008 * client disconnection, otherwise use client_ignore_tunnel.
3014 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3016 tunnel_delete_ignored_client (t, c);
3017 tunnel_delete_active_client (t, c);
3022 * Iterator to free MeshTunnelChildInfo of tunnel children.
3024 * @param cls Closure (tunnel info).
3025 * @param key Hash of GNUNET_PEER_Id (unused).
3026 * @param value MeshTunnelChildInfo of the child.
3028 * @return always GNUNET_YES, to keep iterating
3031 tunnel_destroy_child (void *cls,
3032 const struct GNUNET_HashCode * key,
3035 GNUNET_free (value);
3041 * Callback used to notify a client owner of a tunnel that a peer has
3042 * disconnected, most likely because of a path change.
3044 * @param cls Closure (tunnel this notification is about).
3045 * @param peer_id Short ID of disconnected peer.
3048 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3050 struct MeshTunnel *t = cls;
3051 struct MeshPeerInfo *peer;
3052 struct MeshPathInfo *path_info;
3054 if (NULL != t->owner && NULL != nc)
3056 struct GNUNET_MESH_PeerControl msg;
3058 msg.header.size = htons (sizeof (msg));
3059 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3060 msg.tunnel_id = htonl (t->local_tid);
3061 GNUNET_PEER_resolve (peer_id, &msg.peer);
3062 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3063 &msg.header, GNUNET_NO);
3065 peer = peer_info_get_short (peer_id);
3066 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3067 path_info->peer = peer;
3069 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3074 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3075 * needed rescources.
3076 * If peer already exists, reevaluate shortest path and change if different.
3078 * @param t Tunnel we want to add a new peer to
3079 * @param peer PeerInfo of the peer being added
3083 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3085 struct GNUNET_PeerIdentity id;
3086 struct MeshPeerPath *best_p;
3087 struct MeshPeerPath *p;
3088 unsigned int best_cost;
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3092 GNUNET_PEER_resolve (peer->id, &id);
3094 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3097 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3098 GNUNET_assert (GNUNET_OK ==
3099 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3101 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3104 if (NULL != (p = peer->path_head))
3107 best_cost = tree_get_path_cost (t->tree, p);
3110 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3117 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3118 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3119 t->path_refresh_task =
3120 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3124 /* Start a DHT get */
3125 peer_info_connect (peer, t);
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3131 * Add a path to a tunnel which we don't own, just to remember the next hop.
3132 * If destination node was already in the tunnel, the first hop information
3133 * will be replaced with the new path.
3135 * @param t Tunnel we want to add a new peer to
3136 * @param p Path to add
3137 * @param own_pos Position of local node in path.
3141 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3142 unsigned int own_pos)
3144 struct GNUNET_PeerIdentity id;
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3147 GNUNET_assert (0 != own_pos);
3148 tree_add_path (t->tree, p, NULL, NULL);
3149 if (own_pos < p->length - 1)
3151 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3152 tree_update_first_hops (t->tree, myid, &id);
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3159 * Notifies a tunnel that a connection has broken that affects at least
3160 * some of its peers. Sends a notification towards the root of the tree.
3161 * In case the peer is the owner of the tree, notifies the client that owns
3162 * the tunnel and tries to reconnect.
3164 * @param t Tunnel affected.
3165 * @param p1 Peer that got disconnected from p2.
3166 * @param p2 Peer that got disconnected from p1.
3168 * @return Short ID of the peer disconnected (either p1 or p2).
3169 * 0 if the tunnel remained unaffected.
3171 static GNUNET_PEER_Id
3172 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3178 tree_notify_connection_broken (t->tree, p1, p2,
3179 &tunnel_notify_client_peer_disconnected,
3181 if (myid != p1 && myid != p2)
3187 if (tree_get_predecessor (t->tree) != 0)
3189 /* We are the peer still connected, notify owner of the disconnection. */
3190 struct GNUNET_MESH_PathBroken msg;
3191 struct GNUNET_PeerIdentity neighbor;
3193 msg.header.size = htons (sizeof (msg));
3194 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3195 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3196 msg.tid = htonl (t->id.tid);
3197 msg.peer1 = my_full_id;
3198 GNUNET_PEER_resolve (pid, &msg.peer2);
3199 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3200 send_message (&msg.header, &neighbor, t);
3208 * Send a multicast packet to a neighbor.
3210 * @param cls Closure (Info about the multicast packet)
3211 * @param neighbor_id Short ID of the neighbor to send the packet to.
3214 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3216 struct MeshData *mdata = cls;
3217 struct MeshTransmissionDescriptor *info;
3218 struct GNUNET_PeerIdentity neighbor;
3220 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3222 info->mesh_data = mdata;
3223 (*(mdata->reference_counter)) ++;
3224 info->destination = neighbor_id;
3225 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3227 GNUNET_i2s (&neighbor));
3228 info->peer = peer_info_get (&neighbor);
3229 GNUNET_assert (NULL != info->peer);
3231 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3232 info->mesh_data->data_len,
3239 * Send a message in a tunnel in multicast, sending a copy to each child node
3240 * down the local one in the tunnel tree.
3242 * @param t Tunnel in which to send the data.
3243 * @param msg Message to be sent.
3244 * @param internal Has the service generated this message?
3247 tunnel_send_multicast (struct MeshTunnel *t,
3248 const struct GNUNET_MessageHeader *msg,
3251 struct MeshData *mdata;
3253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3254 " sending a multicast packet...\n");
3255 mdata = GNUNET_malloc (sizeof (struct MeshData));
3256 mdata->data_len = ntohs (msg->size);
3257 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3259 mdata->data = GNUNET_malloc (mdata->data_len);
3260 memcpy (mdata->data, msg, mdata->data_len);
3261 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3263 struct GNUNET_MESH_Multicast *mcast;
3265 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3266 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3268 ntohl (mcast->ttl));
3272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3274 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3275 && GNUNET_NO == internal)
3277 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3279 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3285 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3286 if (*(mdata->reference_counter) == 0)
3288 GNUNET_free (mdata->data);
3289 GNUNET_free (mdata->reference_counter);
3290 if (NULL != mdata->task)
3292 GNUNET_SCHEDULER_cancel(*(mdata->task));
3293 GNUNET_free (mdata->task);
3294 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3296 // FIXME change order?
3297 GNUNET_free (mdata);
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3300 " sending a multicast packet done\n");
3306 * Increase the SKIP value of all peers that
3307 * have not received a unicast message.
3309 * @param cls Closure (ID of the peer that HAS received the message).
3310 * @param key ID of the neighbor.
3311 * @param value Information about the neighbor.
3313 * @return GNUNET_YES to keep iterating.
3316 tunnel_add_skip (void *cls,
3317 const struct GNUNET_HashCode * key,
3320 struct GNUNET_PeerIdentity *neighbor = cls;
3321 struct MeshTunnelChildInfo *cinfo = value;
3323 /* TODO compare only pointers? key == neighbor? */
3324 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3335 * Iterator to get the appropiate ACK value from all children nodes.
3337 * @param cls Closue (tunnel).
3338 * @param id Id of the child node.
3341 tunnel_get_child_ack (void *cls,
3344 struct GNUNET_PeerIdentity peer_id;
3345 struct MeshTunnelChildInfo *cinfo;
3346 struct MeshTunnelChildIteratorContext *ctx = cls;
3347 struct MeshTunnel *t = ctx->t;
3350 GNUNET_PEER_resolve (id, &peer_id);
3351 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3352 &peer_id.hashPubKey);
3355 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3357 cinfo->pid = t->pid;
3358 cinfo->skip = t->pid;
3359 cinfo->max_pid = ack = t->pid + 1;
3360 GNUNET_assert (GNUNET_OK ==
3361 GNUNET_CONTAINER_multihashmap_put(t->children_fc,
3362 &peer_id.hashPubKey,
3364 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3368 ack = cinfo->max_pid;
3371 if (0 == ctx->max_child_ack)
3372 ctx->max_child_ack = ack;
3374 if (GNUNET_YES == t->speed_min)
3376 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3380 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3387 * Get the maximum PID allowed to transmit to any
3388 * tunnel child of the local peer, depending on the tunnel
3389 * buffering/speed settings.
3393 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3396 tunnel_get_children_ack (struct MeshTunnel *t)
3398 struct MeshTunnelChildIteratorContext ctx;
3400 ctx.max_child_ack = 0;
3402 tree_iterate_children (t->tree, tunnel_get_child_ack, &ctx);
3404 if (0 == ctx.nchildren)
3407 return (int64_t) ctx.max_child_ack;
3412 * Add a client to a tunnel, initializing all needed data structures.
3414 * @param t Tunnel to which add the client.
3415 * @param c Client which to add to the tunnel.
3418 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3422 GNUNET_array_append (t->clients, t->nclients, c);
3425 GNUNET_array_append (t->clients_acks, t->nclients, ack);
3430 * Set the ACK value of a client in a particular tunnel.
3432 * @param t Tunnel affected.
3433 * @param c Client whose ACK to set.
3434 * @param ack ACK value.
3437 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3438 struct MeshClient *c,
3443 for (i = 0; i < t->nclients; i++)
3445 if (t->clients[i] != c)
3447 t->clients_acks[i] = ack;
3455 * Get the ACK value of a client in a particular tunnel.
3457 * @param t Tunnel on which to look.
3458 * @param c Client whose ACK to get.
3460 * @return ACK value.
3462 uint32_t // FIXME static when used!!
3463 tunnel_get_client_ack (struct MeshTunnel *t,
3464 struct MeshClient *c)
3468 for (i = 0; i < t->nclients; i++)
3470 if (t->clients[i] != c)
3472 return t->clients_acks[i];
3475 return t->clients_acks[0];
3480 * Get the highest ACK value of all clients in a particular tunnel,
3481 * according to the buffering/speed settings.
3483 * @param t Tunnel on which to look.
3485 * @return Corresponding ACK value (max uint32_t).
3486 * If no clients are suscribed, -1.
3489 tunnel_get_clients_ack (struct MeshTunnel *t)
3494 for (ack = -1, i = 0; i < t->nclients; i++)
3497 (GNUNET_YES == t->speed_min && t->clients_acks[i] < ack) ||
3498 (GNUNET_NO == t->speed_min && t->clients_acks[i] > ack))
3500 ack = t->clients_acks[i];
3504 if (GNUNET_YES == t->nobuffer && ack > t->pid)
3507 return (uint32_t) ack;
3512 * Get the current ack value for a tunnel, taking in account the tunnel
3513 * mode and the status of all children nodes.
3517 * @return Maximum PID allowed.
3520 tunnel_get_fwd_ack (struct MeshTunnel *t)
3523 uint32_t buffer_free;
3528 count = t->pid - t->skip;
3529 buffer_free = t->queue_max - t->queue_n;
3530 ack = count + buffer_free; // Might overflow 32bits, it's ok!
3531 child_ack = tunnel_get_children_ack (t);
3532 client_ack = tunnel_get_clients_ack (t);
3533 if (-1 == child_ack)
3535 // Node has no children, child_ack AND core buffer are irrelevant.
3536 GNUNET_break (-1 != client_ack); // No children and no clients? Not good!
3537 return (uint32_t) client_ack;
3540 if (GNUNET_YES == t->speed_min)
3542 ack = min_pid (child_ack, ack);
3543 ack = min_pid (client_ack, ack);
3547 ack = max_pid (child_ack, ack);
3548 ack = max_pid (client_ack, ack);
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u\n",
3551 count, buffer_free, child_ack, client_ack);
3556 * Get the current ack value for a tunnel, taking in account the tunnel
3557 * mode and the status of all children nodes.
3561 * @return Maximum PID allowed.
3564 tunnel_get_bck_ack (struct MeshTunnel *t)
3575 * Send an ACK informing the predecessor about the available buffer space.
3576 * In case there is no predecessor, inform the owning client.
3577 * If buffering is off, send only on behalf of children or self if endpoint.
3578 * If buffering is on, send when sent to children and buffer space is free.
3579 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3580 * the ACK itself goes "back" (towards root).
3582 * @param t Tunnel on which to send the ACK.
3583 * @param type Type of message that triggered the ACK transmission.
3586 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3588 struct GNUNET_MESH_ACK msg;
3589 struct GNUNET_PeerIdentity id;
3592 if (NULL != t->owner)
3594 send_client_tunnel_ack (t->owner, t);
3597 /* Is it after unicast / multicast retransmission? */
3598 if (GNUNET_MESSAGE_TYPE_MESH_ACK != type)
3600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ACK via DATA retransmission\n");
3601 if (GNUNET_YES == t->nobuffer)
3603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3606 if (t->queue_max > t->queue_n * 2)
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3613 /* Ok, ACK might be necessary, what PID to ACK? */
3614 ack = tunnel_get_fwd_ack (t);
3616 /* If speed_min and not all children have ack'd, dont send yet */
3617 if (ack == t->last_ack)
3619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, not ready\n");
3624 msg.pid = htonl (ack);
3626 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3628 msg.header.size = htons (sizeof (msg));
3629 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3630 msg.tid = htonl (t->id.tid);
3631 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3632 send_message (&msg.header, &id, t);
3637 * Send an ACK informing the children nodes about the available buffer space.
3638 * In case there is no child node, inform the destination clients.
3639 * If buffering is off, send only on behalf of root (can be self).
3640 * If buffering is on, send when sent to predecessor and buffer space is free.
3641 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3642 * the ACK itself goes "forward" (towards children/clients).
3644 * @param t Tunnel on which to send the ACK.
3645 * @param type Type of message that triggered the ACK transmission.
3648 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3650 struct GNUNET_MESH_ACK msg;
3651 struct GNUNET_PeerIdentity id;
3654 if (NULL != t->owner)
3656 send_client_tunnel_ack (t->owner, t);
3659 /* Is it after unicast / multicast retransmission? */
3660 if (GNUNET_MESSAGE_TYPE_MESH_ACK != type)
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ACK via DATA retransmission\n");
3663 if (GNUNET_YES == t->nobuffer)
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3668 if (t->queue_max > t->queue_n * 2)
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3675 /* Ok, ACK might be necessary, what PID to ACK? */
3676 ack = tunnel_get_bck_ack (t);
3678 /* If speed_min and not all children have ack'd, dont send yet */
3679 if (ack == t->last_ack)
3681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, not ready\n");
3686 msg.pid = htonl (ack);
3688 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3690 msg.header.size = htons (sizeof (msg));
3691 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3692 msg.tid = htonl (t->id.tid);
3693 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3694 send_message (&msg.header, &id, t);
3699 * Send a message to all peers in this tunnel that the tunnel is no longer
3702 * @param t The tunnel whose peers to notify.
3705 tunnel_send_destroy (struct MeshTunnel *t)
3707 struct GNUNET_MESH_TunnelDestroy msg;
3709 msg.header.size = htons (sizeof (msg));
3710 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3711 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3712 msg.tid = htonl (t->id.tid);
3713 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3718 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3720 * @param cls Closure (Tunnel which to cancel).
3721 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3724 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3726 struct MeshTunnel *t = cls;
3727 struct MeshPeerInfo *peer_info;
3728 struct MeshPeerQueue *pq;
3729 struct MeshPeerQueue *next;
3731 peer_info = peer_info_get_short (neighbor_id);
3732 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3735 if (pq->tunnel == t)
3737 queue_destroy (pq, GNUNET_YES);
3740 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3742 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3743 peer_info->core_transmit = NULL;
3748 * Destroy the tunnel and free any allocated resources linked to it.
3750 * @param t the tunnel to destroy
3752 * @return GNUNET_OK on success
3755 tunnel_destroy (struct MeshTunnel *t)
3757 struct MeshClient *c;
3758 struct GNUNET_HashCode hash;
3765 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3771 struct GNUNET_PeerIdentity id;
3773 GNUNET_PEER_resolve (t->id.oid, &id);
3774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3775 GNUNET_i2s (&id), t->id.tid);
3777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3781 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3782 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3787 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3790 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3794 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3795 for (i = 0; i < t->nclients; i++)
3799 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3804 for (i = 0; i < t->nignore; i++)
3808 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3813 if (t->nclients > 0)
3816 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3820 GNUNET_free (t->clients);
3822 if (NULL != t->peers)
3824 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3826 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3829 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3830 &tunnel_destroy_child,
3832 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3834 tree_destroy (t->tree);
3836 if (NULL != t->regex_ctx)
3837 regex_cancel_search (t->regex_ctx);
3838 if (NULL != t->dht_get_type)
3839 GNUNET_DHT_get_stop (t->dht_get_type);
3840 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3841 GNUNET_SCHEDULER_cancel (t->timeout_task);
3842 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3843 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3846 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3847 GNUNET_assert (0 <= n_tunnels);
3854 * Create a new tunnel
3856 * @param owner Who is the owner of the tunnel (short ID).
3857 * @param tid Tunnel Number of the tunnel.
3858 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3859 * @param local Tunnel Number for the tunnel, for the client point of view.
3861 * @return A new initialized tunnel. NULL on error.
3863 static struct MeshTunnel *
3864 tunnel_new (GNUNET_PEER_Id owner,
3865 MESH_TunnelNumber tid,
3866 struct MeshClient *client,
3867 MESH_TunnelNumber local)
3869 struct MeshTunnel *t;
3870 struct GNUNET_HashCode hash;
3872 if (n_tunnels >= max_tunnels && NULL == client)
3875 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3878 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
3879 t->tree = tree_new (owner);
3881 t->root_client_ack = 1;
3882 t->local_tid = local;
3883 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
3885 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3887 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3889 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3890 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3897 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3904 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3906 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3907 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3911 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3921 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3922 * that are no longer needed, as well as nodes of no longer reachable peers.
3923 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3925 * @param t Tunnel from which to remove the path.
3926 * @param peer Short id of the peer which should be removed.
3929 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3931 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3937 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3938 * client when the client disconnects. If the client is not the owner, the
3939 * owner will get notified if no more clients are in the tunnel and the client
3940 * get removed from the tunnel's list.
3942 * @param cls closure (client that is disconnecting)
3943 * @param key the hash of the local tunnel id (used to access the hashmap)
3944 * @param value the value stored at the key (tunnel to destroy)
3946 * @return GNUNET_OK on success
3949 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3951 struct MeshTunnel *t = value;
3952 struct MeshClient *c = cls;
3955 send_client_tunnel_disconnect(t, c);
3958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3959 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3960 tunnel_delete_client(t, c);
3961 client_delete_tunnel(c, t);
3964 tunnel_send_destroy(t);
3965 r = tunnel_destroy (t);
3971 * Timeout function, destroys tunnel if called
3973 * @param cls Closure (tunnel to destroy).
3974 * @param tc TaskContext
3977 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3979 struct MeshTunnel *t = cls;
3981 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3983 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3988 * Resets the tunnel timeout. Starts it if no timeout was running.
3990 * @param t Tunnel whose timeout to reset.
3993 tunnel_reset_timeout (struct MeshTunnel *t)
3995 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3996 GNUNET_SCHEDULER_cancel (t->timeout_task);
3998 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3999 (refresh_path_time, 4), &tunnel_timeout, t);
4003 /******************************************************************************/
4004 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4005 /******************************************************************************/
4008 * Function to send a create path packet to a peer.
4010 * @param cls closure
4011 * @param size number of bytes available in buf
4012 * @param buf where the callee should write the message
4013 * @return number of bytes written to buf
4016 send_core_path_create (void *cls, size_t size, void *buf)
4018 struct MeshPathInfo *info = cls;
4019 struct GNUNET_MESH_ManipulatePath *msg;
4020 struct GNUNET_PeerIdentity *peer_ptr;
4021 struct MeshTunnel *t = info->t;
4022 struct MeshPeerPath *p = info->path;
4027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4029 sizeof (struct GNUNET_MESH_ManipulatePath) +
4030 p->length * sizeof (struct GNUNET_PeerIdentity);
4032 if (size < size_needed || NULL == buf)
4037 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4038 msg->header.size = htons (size_needed);
4039 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4040 msg->tid = ntohl (t->id.tid);
4042 if (GNUNET_YES == t->speed_min)
4043 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4044 if (GNUNET_YES == t->nobuffer)
4045 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4046 msg->opt = htonl(opt);
4048 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4049 for (i = 0; i < p->length; i++)
4051 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4058 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4064 * Fill the core buffer
4066 * @param cls closure (data itself)
4067 * @param size number of bytes available in buf
4068 * @param buf where the callee should write the message
4070 * @return number of bytes written to buf
4073 send_core_data_multicast (void *cls, size_t size, void *buf)
4075 struct MeshTransmissionDescriptor *info = cls;
4078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4079 GNUNET_assert (NULL != info);
4080 GNUNET_assert (NULL != info->peer);
4081 total_size = info->mesh_data->data_len;
4082 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4084 if (total_size > size)
4089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4090 memcpy (buf, info->mesh_data->data, total_size);
4093 struct GNUNET_MESH_Multicast *mc;
4094 struct GNUNET_MessageHeader *mh;
4097 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4099 mc = (struct GNUNET_MESH_Multicast *) mh;
4100 mh = (struct GNUNET_MessageHeader *) &mc[1];
4101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4102 " multicast, payload type %u\n", ntohs (mh->type));
4103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4104 " multicast, payload size %u\n", ntohs (mh->size));
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
4113 data_descriptor_decrement_rc (info->mesh_data);
4114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4122 * Creates a path ack message in buf and frees all unused resources.
4124 * @param cls closure (MeshTransmissionDescriptor)
4125 * @param size number of bytes available in buf
4126 * @param buf where the callee should write the message
4127 * @return number of bytes written to buf
4130 send_core_path_ack (void *cls, size_t size, void *buf)
4132 struct MeshTransmissionDescriptor *info = cls;
4133 struct GNUNET_MESH_PathACK *msg = buf;
4135 GNUNET_assert (NULL != info);
4136 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4141 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4142 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4143 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4144 msg->tid = htonl (info->origin->tid);
4145 msg->peer_id = my_full_id;
4148 /* TODO add signature */
4150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4151 return sizeof (struct GNUNET_MESH_PathACK);
4156 * Free a transmission that was already queued with all resources
4157 * associated to the request.
4159 * @param queue Queue handler to cancel.
4160 * @param clear_cls Is it necessary to free associated cls?
4163 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4165 struct MeshTransmissionDescriptor *dd;
4166 struct MeshPathInfo *path_info;
4168 if (GNUNET_YES == clear_cls)
4170 switch (queue->type)
4172 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4173 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4174 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4177 data_descriptor_decrement_rc (dd->mesh_data);
4179 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4181 path_info = queue->cls;
4182 path_destroy (path_info->path);
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4188 GNUNET_free_non_null (queue->cls);
4190 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4191 queue->peer->queue_tail,
4193 GNUNET_free (queue);
4198 * Core callback to write a queued packet to core buffer
4200 * @param cls Closure (peer info).
4201 * @param size Number of bytes available in buf.
4202 * @param buf Where the to write the message.
4204 * @return number of bytes written to buf
4207 queue_send (void *cls, size_t size, void *buf)
4209 struct MeshPeerInfo *peer = cls;
4210 struct GNUNET_MessageHeader *msg;
4211 struct MeshPeerQueue *queue;
4212 struct MeshTunnel *t;
4215 peer->core_transmit = NULL;
4216 queue = peer->queue_head;
4218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4220 /* If queue is empty, send should have been cancelled */
4226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4228 /* Check if buffer size is enough for the message */
4229 if (queue->size > size)
4231 struct GNUNET_PeerIdentity id;
4233 GNUNET_PEER_resolve (peer->id, &id);
4234 peer->core_transmit =
4235 GNUNET_CORE_notify_transmit_ready(core_handle,
4238 GNUNET_TIME_UNIT_FOREVER_REL,
4245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4251 switch (queue->type)
4253 case 0: // RAW data (preconstructed message, retransmission, etc)
4254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
4255 data_size = send_core_data_raw (queue->cls, size, buf);
4256 msg = (struct GNUNET_MessageHeader *) buf;
4257 switch (ntohs (msg->type)) // Type of payload
4259 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4260 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4262 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4263 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4269 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4271 data_size = send_core_data_multicast(queue->cls, size, buf);
4272 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4274 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4276 data_size = send_core_path_create(queue->cls, size, buf);
4278 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4280 data_size = send_core_path_ack(queue->cls, size, buf);
4284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
4288 /* Free queue, but cls was freed by send_core_* */
4289 queue_destroy (queue, GNUNET_NO);
4291 if (GNUNET_YES == t->destroy && 0 == t->queue_n)
4293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4297 /* If more data in queue, send next */
4298 if (NULL != peer->queue_head)
4300 struct GNUNET_PeerIdentity id;
4302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4303 GNUNET_PEER_resolve (peer->id, &id);
4304 peer->core_transmit =
4305 GNUNET_CORE_notify_transmit_ready(core_handle,
4308 GNUNET_TIME_UNIT_FOREVER_REL,
4310 peer->queue_head->size,
4314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4320 * Queue and pass message to core when possible.
4322 * @param cls Closure (type dependant).
4323 * @param type Type of the message, 0 for a raw message.
4324 * @param size Size of the message.
4325 * @param dst Neighbor to send message to.
4326 * @param t Tunnel this message belongs to.
4329 queue_add (void *cls, uint16_t type, size_t size,
4330 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4332 struct MeshPeerQueue *queue;
4334 if (t->queue_n >= t->queue_max)
4336 if (NULL == t->owner)
4337 GNUNET_break_op(0); // TODO: kill connection?
4340 return; // Drop message
4343 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4349 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4350 if (NULL == dst->core_transmit)
4352 struct GNUNET_PeerIdentity id;
4354 GNUNET_PEER_resolve (dst->id, &id);
4355 dst->core_transmit =
4356 GNUNET_CORE_notify_transmit_ready(core_handle,
4359 GNUNET_TIME_UNIT_FOREVER_REL,
4368 /******************************************************************************/
4369 /******************** MESH NETWORK HANDLERS **************************/
4370 /******************************************************************************/
4374 * Core handler for path creation
4376 * @param cls closure
4377 * @param message message
4378 * @param peer peer identity this notification is about
4379 * @param atsi performance data
4380 * @param atsi_count number of records in 'atsi'
4382 * @return GNUNET_OK to keep the connection open,
4383 * GNUNET_SYSERR to close it (signal serious error)
4386 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4387 const struct GNUNET_MessageHeader *message,
4388 const struct GNUNET_ATS_Information *atsi,
4389 unsigned int atsi_count)
4391 unsigned int own_pos;
4394 MESH_TunnelNumber tid;
4395 struct GNUNET_MESH_ManipulatePath *msg;
4396 struct GNUNET_PeerIdentity *pi;
4397 struct GNUNET_HashCode hash;
4398 struct MeshPeerPath *path;
4399 struct MeshPeerInfo *dest_peer_info;
4400 struct MeshPeerInfo *orig_peer_info;
4401 struct MeshTunnel *t;
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4404 "Received a path create msg [%s]\n",
4405 GNUNET_i2s (&my_full_id));
4406 size = ntohs (message->size);
4407 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4409 GNUNET_break_op (0);
4413 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4414 if (size % sizeof (struct GNUNET_PeerIdentity))
4416 GNUNET_break_op (0);
4419 size /= sizeof (struct GNUNET_PeerIdentity);
4422 GNUNET_break_op (0);
4425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4426 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4428 tid = ntohl (msg->tid);
4429 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4431 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4432 t = tunnel_get (pi, tid);
4433 if (NULL == t) // FIXME only for INCOMING tunnels?
4437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4438 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4441 // FIXME notify failure
4444 opt = ntohl (msg->opt);
4445 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4446 GNUNET_YES : GNUNET_NO;
4447 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4448 GNUNET_YES : GNUNET_NO;
4450 if (GNUNET_YES == t->nobuffer)
4453 while (NULL != tunnel_get_incoming (next_local_tid))
4454 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4455 t->local_tid_dest = next_local_tid++;
4456 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4458 tunnel_reset_timeout (t);
4459 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4461 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4462 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4470 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4471 if (NULL == dest_peer_info)
4473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4474 " Creating PeerInfo for destination.\n");
4475 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4476 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4477 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4479 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4481 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4482 if (NULL == orig_peer_info)
4484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4485 " Creating PeerInfo for origin.\n");
4486 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4487 orig_peer_info->id = GNUNET_PEER_intern (pi);
4488 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4492 path = path_new (size);
4494 for (i = 0; i < size; i++)
4496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4497 GNUNET_i2s (&pi[i]));
4498 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4499 if (path->peers[i] == myid)
4502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4505 /* cannot be self, must be 'not found' */
4506 /* create path: self not found in path through self */
4507 GNUNET_break_op (0);
4508 path_destroy (path);
4509 /* FIXME error. destroy tunnel? leave for timeout? */
4512 path_add_to_peers (path, GNUNET_NO);
4513 tunnel_add_path (t, path, own_pos);
4514 if (own_pos == size - 1)
4516 /* It is for us! Send ack. */
4517 struct MeshTransmissionDescriptor *info;
4519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4520 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4521 if (NULL == t->peers)
4523 /* New tunnel! Notify clients on data. */
4524 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4526 GNUNET_break (GNUNET_SYSERR !=
4527 GNUNET_CONTAINER_multihashmap_put (t->peers,
4528 &my_full_id.hashPubKey,
4531 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4532 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4533 info->origin = &t->id;
4534 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4535 GNUNET_assert (NULL != info->peer);
4537 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4538 sizeof (struct GNUNET_MESH_PathACK),
4544 struct MeshPeerPath *path2;
4546 /* It's for somebody else! Retransmit. */
4547 path2 = path_duplicate (path);
4548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4549 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4550 path2 = path_duplicate (path);
4551 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4552 send_create_path (dest_peer_info, path, t);
4559 * Core handler for path destruction
4561 * @param cls closure
4562 * @param message message
4563 * @param peer peer identity this notification is about
4564 * @param atsi performance data
4565 * @param atsi_count number of records in 'atsi'
4567 * @return GNUNET_OK to keep the connection open,
4568 * GNUNET_SYSERR to close it (signal serious error)
4571 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4572 const struct GNUNET_MessageHeader *message,
4573 const struct GNUNET_ATS_Information *atsi,
4574 unsigned int atsi_count)
4576 struct GNUNET_MESH_ManipulatePath *msg;
4577 struct GNUNET_PeerIdentity *pi;
4578 struct MeshPeerPath *path;
4579 struct MeshTunnel *t;
4580 unsigned int own_pos;
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4585 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4586 size = ntohs (message->size);
4587 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4589 GNUNET_break_op (0);
4593 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4594 if (size % sizeof (struct GNUNET_PeerIdentity))
4596 GNUNET_break_op (0);
4599 size /= sizeof (struct GNUNET_PeerIdentity);
4602 GNUNET_break_op (0);
4605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4607 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4608 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4610 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4612 t = tunnel_get (pi, ntohl (msg->tid));
4615 /* TODO notify back: we don't know this tunnel */
4616 GNUNET_break_op (0);
4619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4620 path = path_new (size);
4622 for (i = 0; i < size; i++)
4624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4625 GNUNET_i2s (&pi[i]));
4626 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4627 if (path->peers[i] == myid)
4630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4631 if (own_pos < path->length - 1)
4632 send_message (message, &pi[own_pos + 1], t);
4634 send_client_tunnel_disconnect(t, NULL);
4636 tunnel_delete_peer (t, path->peers[path->length - 1]);
4637 path_destroy (path);
4643 * Core handler for notifications of broken paths
4645 * @param cls closure
4646 * @param message message
4647 * @param peer peer identity this notification is about
4648 * @param atsi performance data
4649 * @param atsi_count number of records in 'atsi'
4651 * @return GNUNET_OK to keep the connection open,
4652 * GNUNET_SYSERR to close it (signal serious error)
4655 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4656 const struct GNUNET_MessageHeader *message,
4657 const struct GNUNET_ATS_Information *atsi,
4658 unsigned int atsi_count)
4660 struct GNUNET_MESH_PathBroken *msg;
4661 struct MeshTunnel *t;
4663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4664 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4665 msg = (struct GNUNET_MESH_PathBroken *) message;
4666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4667 GNUNET_i2s (&msg->peer1));
4668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4669 GNUNET_i2s (&msg->peer2));
4670 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4673 GNUNET_break_op (0);
4676 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4677 GNUNET_PEER_search (&msg->peer2));
4684 * Core handler for tunnel destruction
4686 * @param cls closure
4687 * @param message message
4688 * @param peer peer identity this notification is about
4689 * @param atsi performance data
4690 * @param atsi_count number of records in 'atsi'
4692 * @return GNUNET_OK to keep the connection open,
4693 * GNUNET_SYSERR to close it (signal serious error)
4696 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4697 const struct GNUNET_MessageHeader *message,
4698 const struct GNUNET_ATS_Information *atsi,
4699 unsigned int atsi_count)
4701 struct GNUNET_MESH_TunnelDestroy *msg;
4702 struct MeshTunnel *t;
4704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4705 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4706 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4708 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4709 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4712 /* Probably already got the message from another path,
4713 * destroyed the tunnel and retransmitted to children.
4716 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4719 if (t->id.oid == myid)
4721 GNUNET_break_op (0);
4724 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4726 /* Tunnel was incoming, notify clients */
4727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4728 t->local_tid, t->local_tid_dest);
4729 send_clients_tunnel_destroy (t);
4731 tunnel_send_destroy (t);
4732 t->destroy = GNUNET_YES;
4733 // TODO: add timeout to destroy the tunnel anyway
4739 * Core handler for mesh network traffic going from the origin to a peer
4741 * @param cls closure
4742 * @param peer peer identity this notification is about
4743 * @param message message
4744 * @param atsi performance data
4745 * @param atsi_count number of records in 'atsi'
4746 * @return GNUNET_OK to keep the connection open,
4747 * GNUNET_SYSERR to close it (signal serious error)
4750 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4751 const struct GNUNET_MessageHeader *message,
4752 const struct GNUNET_ATS_Information *atsi,
4753 unsigned int atsi_count)
4755 struct GNUNET_MESH_Unicast *msg;
4756 struct GNUNET_PeerIdentity *neighbor;
4757 struct MeshTunnelChildInfo *cinfo;
4758 struct MeshTunnel *t;
4759 GNUNET_PEER_Id dest_id;
4764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4766 size = ntohs (message->size);
4768 sizeof (struct GNUNET_MESH_Unicast) +
4769 sizeof (struct GNUNET_MessageHeader))
4774 msg = (struct GNUNET_MESH_Unicast *) message;
4775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4776 ntohs (msg[1].header.type));
4777 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4780 /* TODO notify back: we don't know this tunnel */
4781 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4782 GNUNET_break_op (0);
4785 pid = ntohl (msg->pid);
4788 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4789 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4790 " Already seen pid %u, DROPPING!\n", pid);
4795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4796 " pid %u not seen yet, forwarding\n", pid);
4798 t->skip += (pid - t->pid) - 1;
4800 tunnel_reset_timeout (t);
4801 dest_id = GNUNET_PEER_search (&msg->destination);
4802 if (dest_id == myid)
4804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4805 " it's for us! sending to clients...\n");
4806 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4807 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4808 // FIXME send after client processes the packet
4809 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4812 ttl = ntohl (msg->ttl);
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4816 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4821 " not for us, retransmitting...\n");
4822 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4824 neighbor = tree_get_first_hop (t->tree, dest_id);
4825 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4826 &neighbor->hashPubKey);
4829 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
4830 cinfo->id = GNUNET_PEER_intern (neighbor);
4832 cinfo->max_pid = pid + t->queue_max - t->queue_n; // FIXME review
4834 GNUNET_assert (GNUNET_OK ==
4835 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
4836 &neighbor->hashPubKey,
4838 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
4841 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4844 send_message (message, neighbor, t);
4850 * Core handler for mesh network traffic going from the origin to all peers
4852 * @param cls closure
4853 * @param message message
4854 * @param peer peer identity this notification is about
4855 * @param atsi performance data
4856 * @param atsi_count number of records in 'atsi'
4857 * @return GNUNET_OK to keep the connection open,
4858 * GNUNET_SYSERR to close it (signal serious error)
4860 * TODO: Check who we got this from, to validate route.
4863 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4864 const struct GNUNET_MessageHeader *message,
4865 const struct GNUNET_ATS_Information *atsi,
4866 unsigned int atsi_count)
4868 struct GNUNET_MESH_Multicast *msg;
4869 struct MeshTunnel *t;
4873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
4875 size = ntohs (message->size);
4876 if (sizeof (struct GNUNET_MESH_Multicast) +
4877 sizeof (struct GNUNET_MessageHeader) > size)
4879 GNUNET_break_op (0);
4882 msg = (struct GNUNET_MESH_Multicast *) message;
4883 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4887 /* TODO notify that we dont know that tunnel */
4888 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4889 GNUNET_break_op (0);
4892 pid = ntohl (msg->pid);
4895 /* already seen this packet, drop */
4896 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4897 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4898 " Already seen pid %u, DROPPING!\n", pid);
4903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4904 " pid %u not seen yet, forwarding\n", pid);
4906 t->skip += (pid - t->pid) - 1;
4908 tunnel_reset_timeout (t);
4910 /* Transmit to locally interested clients */
4911 if (NULL != t->peers &&
4912 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
4914 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
4915 send_subscribed_clients (message, &msg[1].header);
4917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
4918 if (ntohl (msg->ttl) == 0)
4920 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4921 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4924 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
4925 tunnel_send_multicast (t, message, GNUNET_NO);
4931 * Core handler for mesh network traffic toward the owner of a tunnel
4933 * @param cls closure
4934 * @param message message
4935 * @param peer peer identity this notification is about
4936 * @param atsi performance data
4937 * @param atsi_count number of records in 'atsi'
4939 * @return GNUNET_OK to keep the connection open,
4940 * GNUNET_SYSERR to close it (signal serious error)
4943 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4944 const struct GNUNET_MessageHeader *message,
4945 const struct GNUNET_ATS_Information *atsi,
4946 unsigned int atsi_count)
4948 struct GNUNET_MESH_ToOrigin *msg;
4949 struct GNUNET_PeerIdentity id;
4950 struct MeshPeerInfo *peer_info;
4951 struct MeshTunnel *t;
4954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4956 size = ntohs (message->size);
4957 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4958 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4960 GNUNET_break_op (0);
4963 msg = (struct GNUNET_MESH_ToOrigin *) message;
4964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4965 ntohs (msg[1].header.type));
4966 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4970 /* TODO notify that we dont know this tunnel (whom)? */
4971 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4972 GNUNET_break_op (0);
4976 if (t->id.oid == myid)
4979 struct GNUNET_MESH_ToOrigin *copy;
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4982 " it's for us! sending to clients...\n");
4983 if (NULL == t->owner)
4985 /* got data packet for ownerless tunnel */
4986 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
4988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4989 GNUNET_break_op (0);
4992 /* TODO signature verification */
4993 memcpy (cbuf, message, size);
4994 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4995 copy->tid = htonl (t->local_tid);
4996 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4997 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4998 ©->header, GNUNET_NO);
5001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5002 " not for us, retransmitting...\n");
5004 peer_info = peer_info_get (&msg->oid);
5005 if (NULL == peer_info)
5007 /* unknown origin of tunnel */
5011 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5012 send_message (message, &id, t);
5013 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5020 * Core handler for mesh network traffic point-to-point acks.
5022 * @param cls closure
5023 * @param message message
5024 * @param peer peer identity this notification is about
5025 * @param atsi performance data
5026 * @param atsi_count number of records in 'atsi'
5028 * @return GNUNET_OK to keep the connection open,
5029 * GNUNET_SYSERR to close it (signal serious error)
5032 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5033 const struct GNUNET_MessageHeader *message,
5034 const struct GNUNET_ATS_Information *atsi,
5035 unsigned int atsi_count)
5037 struct GNUNET_MESH_ACK *msg;
5038 struct MeshTunnelChildInfo *cinfo;
5039 struct MeshTunnel *t;
5042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got an ACK packet from %s\n",
5044 msg = (struct GNUNET_MESH_ACK *) message;
5045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5046 ntohs (msg[1].header.type));
5047 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5051 /* TODO notify that we dont know this tunnel (whom)? */
5052 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5053 GNUNET_break_op (0);
5056 ack = ntohl (msg->pid);
5057 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
5061 GNUNET_break_op (0);
5064 cinfo->max_pid = ack;
5065 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5071 * Core handler for path ACKs
5073 * @param cls closure
5074 * @param message message
5075 * @param peer peer identity this notification is about
5076 * @param atsi performance data
5077 * @param atsi_count number of records in 'atsi'
5079 * @return GNUNET_OK to keep the connection open,
5080 * GNUNET_SYSERR to close it (signal serious error)
5083 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5084 const struct GNUNET_MessageHeader *message,
5085 const struct GNUNET_ATS_Information *atsi,
5086 unsigned int atsi_count)
5088 struct GNUNET_MESH_PathACK *msg;
5089 struct GNUNET_PeerIdentity id;
5090 struct MeshPeerInfo *peer_info;
5091 struct MeshPeerPath *p;
5092 struct MeshTunnel *t;
5094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5095 GNUNET_i2s (&my_full_id));
5096 msg = (struct GNUNET_MESH_PathACK *) message;
5097 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5100 /* TODO notify that we don't know the tunnel */
5101 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5103 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5107 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5109 peer_info = peer_info_get (&msg->peer_id);
5110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5111 GNUNET_i2s (&msg->peer_id));
5112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5115 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5118 "connect_by_string completed, stopping search\n");
5119 regex_cancel_search (t->regex_ctx);
5120 t->regex_ctx = NULL;
5123 /* Add paths to peers? */
5124 p = tree_get_path_to_peer (t->tree, peer_info->id);
5127 path_add_to_peers (p, GNUNET_YES);
5135 /* Message for us? */
5136 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5139 if (NULL == t->owner)
5141 GNUNET_break_op (0);
5144 if (NULL != t->dht_get_type)
5146 GNUNET_DHT_get_stop (t->dht_get_type);
5147 t->dht_get_type = NULL;
5149 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5151 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5152 send_client_peer_connected (t, peer_info->id);
5157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5158 " not for us, retransmitting...\n");
5159 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5160 peer_info = peer_info_get (&msg->oid);
5161 if (NULL == peer_info)
5163 /* If we know the tunnel, we should DEFINITELY know the peer */
5167 send_message (message, &id, t);
5173 * Functions to handle messages from core
5175 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5176 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5177 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5178 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5179 sizeof (struct GNUNET_MESH_PathBroken)},
5180 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5181 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5182 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5183 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5184 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5185 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5186 sizeof (struct GNUNET_MESH_ACK)},
5187 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5188 sizeof (struct GNUNET_MESH_PathACK)},
5194 /******************************************************************************/
5195 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5196 /******************************************************************************/
5199 * deregister_app: iterator for removing each application registered by a client
5201 * @param cls closure
5202 * @param key the hash of the application id (used to access the hashmap)
5203 * @param value the value stored at the key (client)
5205 * @return GNUNET_OK on success
5208 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5210 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5211 GNUNET_break (GNUNET_YES ==
5212 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5218 * notify_client_connection_failure: notify a client that the connection to the
5219 * requested remote peer is not possible (for instance, no route found)
5220 * Function called when the socket is ready to queue more data. "buf" will be
5221 * NULL and "size" zero if the socket was closed for writing in the meantime.
5223 * @param cls closure
5224 * @param size number of bytes available in buf
5225 * @param buf where the callee should write the message
5226 * @return number of bytes written to buf
5229 notify_client_connection_failure (void *cls, size_t size, void *buf)
5232 struct MeshPeerInfo *peer_info;
5233 struct GNUNET_MESH_PeerControl *msg;
5234 struct GNUNET_PeerIdentity id;
5236 if (0 == size && NULL == buf)
5238 // TODO retry? cancel?
5242 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5243 peer_info = (struct MeshPeerInfo *) cls;
5244 msg = (struct GNUNET_MESH_PeerControl *) buf;
5245 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5246 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5247 // msg->tunnel_id = htonl(peer_info->t->tid);
5248 GNUNET_PEER_resolve (peer_info->id, &id);
5249 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5257 * Send keepalive packets for a peer
5259 * @param cls Closure (tunnel for which to send the keepalive).
5260 * @param tc Notification context.
5262 * TODO: implement explicit multicast keepalive?
5265 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5267 struct MeshTunnel *t = cls;
5268 struct GNUNET_MessageHeader *payload;
5269 struct GNUNET_MESH_Multicast *msg;
5271 sizeof (struct GNUNET_MESH_Multicast) +
5272 sizeof (struct GNUNET_MessageHeader);
5275 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5279 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5282 "sending keepalive for tunnel %d\n", t->id.tid);
5284 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5285 msg->header.size = htons (size);
5286 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5287 msg->oid = my_full_id;
5288 msg->tid = htonl (t->id.tid);
5289 msg->ttl = htonl (default_ttl);
5290 msg->pid = htonl (t->pid + 1);
5292 payload = (struct GNUNET_MessageHeader *) &msg[1];
5293 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5294 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5295 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5297 t->path_refresh_task =
5298 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5304 * Function to process paths received for a new peer addition. The recorded
5305 * paths form the initial tunnel, which can be optimized later.
5306 * Called on each result obtained for the DHT search.
5308 * @param cls closure
5309 * @param exp when will this value expire
5310 * @param key key of the result
5311 * @param get_path path of the get request
5312 * @param get_path_length lenght of get_path
5313 * @param put_path path of the put request
5314 * @param put_path_length length of the put_path
5315 * @param type type of the result
5316 * @param size number of bytes in data
5317 * @param data pointer to the result data
5319 * TODO: re-issue the request after certain time? cancel after X results?
5322 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5323 const struct GNUNET_HashCode * key,
5324 const struct GNUNET_PeerIdentity *get_path,
5325 unsigned int get_path_length,
5326 const struct GNUNET_PeerIdentity *put_path,
5327 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5328 size_t size, const void *data)
5330 struct MeshPathInfo *path_info = cls;
5331 struct MeshPeerPath *p;
5332 struct GNUNET_PeerIdentity pi;
5335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5336 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5339 p = path_build_from_dht (get_path, get_path_length, put_path,
5341 path_add_to_peers (p, GNUNET_NO);
5343 for (i = 0; i < path_info->peer->ntunnels; i++)
5345 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5346 peer_info_connect (path_info->peer, path_info->t);
5354 * Function to process paths received for a new peer addition. The recorded
5355 * paths form the initial tunnel, which can be optimized later.
5356 * Called on each result obtained for the DHT search.
5358 * @param cls closure
5359 * @param exp when will this value expire
5360 * @param key key of the result
5361 * @param get_path path of the get request
5362 * @param get_path_length lenght of get_path
5363 * @param put_path path of the put request
5364 * @param put_path_length length of the put_path
5365 * @param type type of the result
5366 * @param size number of bytes in data
5367 * @param data pointer to the result data
5370 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5371 const struct GNUNET_HashCode * key,
5372 const struct GNUNET_PeerIdentity *get_path,
5373 unsigned int get_path_length,
5374 const struct GNUNET_PeerIdentity *put_path,
5375 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5376 size_t size, const void *data)
5378 const struct PBlock *pb = data;
5379 const struct GNUNET_PeerIdentity *pi = &pb->id;
5380 struct MeshTunnel *t = cls;
5381 struct MeshPeerInfo *peer_info;
5382 struct MeshPeerPath *p;
5384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5385 if (size != sizeof (struct PBlock))
5387 GNUNET_break_op (0);
5390 if (ntohl(pb->type) != t->type)
5392 GNUNET_break_op (0);
5395 GNUNET_assert (NULL != t->owner);
5396 peer_info = peer_info_get (pi);
5397 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5399 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5401 p = path_build_from_dht (get_path, get_path_length, put_path,
5403 path_add_to_peers (p, GNUNET_NO);
5405 tunnel_add_peer (t, peer_info);
5406 peer_info_connect (peer_info, t);
5411 * Function to process DHT string to regex matching.
5412 * Called on each result obtained for the DHT search.
5414 * @param cls closure (search context)
5415 * @param exp when will this value expire
5416 * @param key key of the result
5417 * @param get_path path of the get request (not used)
5418 * @param get_path_length lenght of get_path (not used)
5419 * @param put_path path of the put request (not used)
5420 * @param put_path_length length of the put_path (not used)
5421 * @param type type of the result
5422 * @param size number of bytes in data
5423 * @param data pointer to the result data
5426 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5427 const struct GNUNET_HashCode * key,
5428 const struct GNUNET_PeerIdentity *get_path,
5429 unsigned int get_path_length,
5430 const struct GNUNET_PeerIdentity *put_path,
5431 unsigned int put_path_length,
5432 enum GNUNET_BLOCK_Type type,
5433 size_t size, const void *data)
5435 const struct MeshRegexAccept *block = data;
5436 struct MeshRegexSearchContext *ctx = cls;
5437 struct MeshRegexSearchInfo *info = ctx->info;
5438 struct MeshPeerPath *p;
5439 struct MeshPeerInfo *peer_info;
5441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5444 peer_info = peer_info_get(&block->id);
5445 p = path_build_from_dht (get_path, get_path_length, put_path,
5447 path_add_to_peers (p, GNUNET_NO);
5450 tunnel_add_peer (info->t, peer_info);
5451 peer_info_connect (peer_info, info->t);
5452 if (0 == info->peer)
5454 info->peer = peer_info->id;
5458 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5461 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5462 ®ex_connect_timeout,
5470 * Function to process DHT string to regex matching.
5471 * Called on each result obtained for the DHT search.
5473 * @param cls closure (search context)
5474 * @param exp when will this value expire
5475 * @param key key of the result
5476 * @param get_path path of the get request (not used)
5477 * @param get_path_length lenght of get_path (not used)
5478 * @param put_path path of the put request (not used)
5479 * @param put_path_length length of the put_path (not used)
5480 * @param type type of the result
5481 * @param size number of bytes in data
5482 * @param data pointer to the result data
5484 * TODO: re-issue the request after certain time? cancel after X results?
5487 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5488 const struct GNUNET_HashCode * key,
5489 const struct GNUNET_PeerIdentity *get_path,
5490 unsigned int get_path_length,
5491 const struct GNUNET_PeerIdentity *put_path,
5492 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5493 size_t size, const void *data)
5495 const struct MeshRegexBlock *block = data;
5496 struct MeshRegexSearchContext *ctx = cls;
5497 struct MeshRegexSearchInfo *info = ctx->info;
5501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5502 "DHT GET STRING RETURNED RESULTS\n");
5503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5504 " key: %s\n", GNUNET_h2s (key));
5506 copy = GNUNET_malloc (size);
5507 memcpy (copy, data, size);
5508 GNUNET_break (GNUNET_OK ==
5509 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5510 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5511 len = ntohl (block->n_proof);
5513 char proof[len + 1];
5515 memcpy (proof, &block[1], len);
5517 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5519 GNUNET_break_op (0);
5523 len = strlen (info->description);
5524 if (len == ctx->position) // String processed
5526 if (GNUNET_YES == ntohl (block->accepting))
5528 regex_find_path(key, ctx);
5532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5533 // FIXME REGEX this block not successful, wait for more? start timeout?
5537 GNUNET_break (GNUNET_OK ==
5538 GNUNET_MESH_regex_block_iterate (block, size,
5539 ®ex_edge_iterator, ctx));
5543 /******************************************************************************/
5544 /********************* MESH LOCAL HANDLES **************************/
5545 /******************************************************************************/
5549 * Handler for client disconnection
5551 * @param cls closure
5552 * @param client identification of the client; NULL
5553 * for the last call when the server is destroyed
5556 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5558 struct MeshClient *c;
5559 struct MeshClient *next;
5562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5571 if (c->handle != client)
5573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5579 GNUNET_SERVER_client_drop (c->handle);
5580 c->shutting_down = GNUNET_YES;
5581 GNUNET_assert (NULL != c->own_tunnels);
5582 GNUNET_assert (NULL != c->incoming_tunnels);
5583 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5584 &tunnel_destroy_iterator, c);
5585 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5586 &tunnel_destroy_iterator, c);
5587 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5588 &tunnel_destroy_iterator, c);
5589 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5590 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5591 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5593 /* deregister clients applications */
5594 if (NULL != c->apps)
5596 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5597 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5599 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5600 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5602 GNUNET_SCHEDULER_cancel (announce_applications_task);
5603 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5605 if (NULL != c->types)
5606 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5607 for (i = 0; i < c->n_regex; i++)
5609 GNUNET_free (c->regexes[i]);
5611 GNUNET_free_non_null (c->regexes);
5612 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5613 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5615 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5618 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5627 * Handler for new clients
5629 * @param cls closure
5630 * @param client identification of the client
5631 * @param message the actual message, which includes messages the client wants
5634 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5635 const struct GNUNET_MessageHeader *message)
5637 struct GNUNET_MESH_ClientConnect *cc_msg;
5638 struct MeshClient *c;
5639 GNUNET_MESH_ApplicationType *a;
5646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5647 /* Check data sanity */
5648 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5649 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5650 ntypes = ntohs (cc_msg->types);
5651 napps = ntohs (cc_msg->applications);
5653 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5660 /* Create new client structure */
5661 c = GNUNET_malloc (sizeof (struct MeshClient));
5662 c->id = next_client_id++;
5663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5665 GNUNET_SERVER_client_keep (client);
5666 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5669 GNUNET_MESH_ApplicationType at;
5670 struct GNUNET_HashCode hc;
5672 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5673 for (i = 0; i < napps; i++)
5676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5677 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5678 /* store in clients hashmap */
5679 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5680 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5681 /* store in global hashmap, for announcements */
5682 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5683 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5685 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5686 announce_applications_task =
5687 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5693 struct GNUNET_HashCode hc;
5695 t = (uint16_t *) & a[napps];
5696 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5697 for (i = 0; i < ntypes; i++)
5700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5701 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5703 /* store in clients hashmap */
5704 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5705 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5706 /* store in global hashmap */
5707 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5708 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5712 " client has %u+%u subscriptions\n", napps, ntypes);
5714 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5715 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5716 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5717 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5718 GNUNET_SERVER_notification_context_add (nc, client);
5719 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5721 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5727 * Handler for clients announcing available services by a regular expression.
5729 * @param cls closure
5730 * @param client identification of the client
5731 * @param message the actual message, which includes messages the client wants
5734 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5735 const struct GNUNET_MessageHeader *message)
5737 struct MeshClient *c;
5741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5743 /* Sanity check for client registration */
5744 if (NULL == (c = client_get (client)))
5747 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5752 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5753 regex = GNUNET_malloc (len + 1);
5754 memcpy (regex, &message[1], len);
5756 GNUNET_array_append (c->regexes, c->n_regex, regex);
5757 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5759 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5765 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5771 * Handler for requests of new tunnels
5773 * @param cls closure
5774 * @param client identification of the client
5775 * @param message the actual message
5778 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5779 const struct GNUNET_MessageHeader *message)
5781 struct GNUNET_MESH_TunnelMessage *t_msg;
5782 struct MeshTunnel *t;
5783 struct MeshClient *c;
5785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5787 /* Sanity check for client registration */
5788 if (NULL == (c = client_get (client)))
5791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5796 /* Message sanity check */
5797 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5804 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5805 /* Sanity check for tunnel numbering */
5806 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5812 /* Sanity check for duplicate tunnel IDs */
5813 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
5816 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5820 while (NULL != tunnel_get_by_pi (myid, next_tid))
5821 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5822 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
5825 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5827 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5830 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5832 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5833 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
5835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5836 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5842 * Handler for requests of deleting tunnels
5844 * @param cls closure
5845 * @param client identification of the client
5846 * @param message the actual message
5849 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5850 const struct GNUNET_MessageHeader *message)
5852 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5853 struct MeshClient *c;
5854 struct MeshTunnel *t;
5855 MESH_TunnelNumber tid;
5857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5858 "Got a DESTROY TUNNEL from client!\n");
5860 /* Sanity check for client registration */
5861 if (NULL == (c = client_get (client)))
5864 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5867 /* Message sanity check */
5868 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5871 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5875 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5877 /* Retrieve tunnel */
5878 tid = ntohl (tunnel_msg->tunnel_id);
5879 t = tunnel_get_by_local_id(c, tid);
5882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5884 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5887 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5889 client_ignore_tunnel (c, t);
5891 // TODO: when to destroy incoming tunnel?
5892 if (t->nclients == 0)
5894 GNUNET_assert (GNUNET_YES ==
5895 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
5897 GNUNET_assert (GNUNET_YES ==
5898 GNUNET_CONTAINER_multihashmap_remove (t->peers,
5899 &my_full_id.hashPubKey,
5903 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5906 send_client_tunnel_disconnect(t, c);
5907 client_delete_tunnel(c, t);
5909 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5911 tunnel_send_destroy (t);
5912 t->destroy = GNUNET_YES;
5913 // The tunnel will be destroyed when the last message is transmitted.
5914 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5920 * Handler for requests of seeting tunnel's speed.
5922 * @param cls Closure (unused).
5923 * @param client Identification of the client.
5924 * @param message The actual message.
5927 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
5928 const struct GNUNET_MessageHeader *message)
5930 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5931 struct MeshClient *c;
5932 struct MeshTunnel *t;
5933 MESH_TunnelNumber tid;
5935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5936 "Got a SPEED request from client!\n");
5938 /* Sanity check for client registration */
5939 if (NULL == (c = client_get (client)))
5942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5947 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5949 /* Retrieve tunnel */
5950 tid = ntohl (tunnel_msg->tunnel_id);
5951 t = tunnel_get_by_local_id(c, tid);
5954 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
5956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5960 switch (ntohs(message->type))
5962 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
5963 t->speed_min = GNUNET_YES;
5965 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
5966 t->speed_min = GNUNET_NO;
5971 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5976 * Handler for requests of seeting tunnel's buffering policy.
5978 * @param cls Closure (unused).
5979 * @param client Identification of the client.
5980 * @param message The actual message.
5983 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5984 const struct GNUNET_MessageHeader *message)
5986 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5987 struct MeshClient *c;
5988 struct MeshTunnel *t;
5989 MESH_TunnelNumber tid;
5991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5992 "Got a BUFFER request from client!\n");
5994 /* Sanity check for client registration */
5995 if (NULL == (c = client_get (client)))
5998 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6003 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6005 /* Retrieve tunnel */
6006 tid = ntohl (tunnel_msg->tunnel_id);
6007 t = tunnel_get_by_local_id(c, tid);
6010 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6016 switch (ntohs(message->type))
6018 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6019 t->nobuffer = GNUNET_NO;
6021 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6022 t->nobuffer = GNUNET_YES;
6028 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6033 * Handler for connection requests to new peers
6035 * @param cls closure
6036 * @param client identification of the client
6037 * @param message the actual message (PeerControl)
6040 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6041 const struct GNUNET_MessageHeader *message)
6043 struct GNUNET_MESH_PeerControl *peer_msg;
6044 struct MeshPeerInfo *peer_info;
6045 struct MeshClient *c;
6046 struct MeshTunnel *t;
6047 MESH_TunnelNumber tid;
6049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6050 /* Sanity check for client registration */
6051 if (NULL == (c = client_get (client)))
6054 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6058 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6059 /* Sanity check for message size */
6060 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6063 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6067 /* Tunnel exists? */
6068 tid = ntohl (peer_msg->tunnel_id);
6069 t = tunnel_get_by_local_id (c, tid);
6073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6077 /* Does client own tunnel? */
6078 if (t->owner->handle != client)
6081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6085 GNUNET_i2s (&peer_msg->peer));
6086 peer_info = peer_info_get (&peer_msg->peer);
6088 tunnel_add_peer (t, peer_info);
6089 peer_info_connect (peer_info, t);
6091 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6097 * Handler for disconnection requests of peers in a tunnel
6099 * @param cls closure
6100 * @param client identification of the client
6101 * @param message the actual message (PeerControl)
6104 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6105 const struct GNUNET_MessageHeader *message)
6107 struct GNUNET_MESH_PeerControl *peer_msg;
6108 struct MeshPeerInfo *peer_info;
6109 struct MeshClient *c;
6110 struct MeshTunnel *t;
6111 MESH_TunnelNumber tid;
6113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6114 /* Sanity check for client registration */
6115 if (NULL == (c = client_get (client)))
6118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6121 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6122 /* Sanity check for message size */
6123 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6126 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6130 /* Tunnel exists? */
6131 tid = ntohl (peer_msg->tunnel_id);
6132 t = tunnel_get_by_local_id (c, tid);
6136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6141 /* Does client own tunnel? */
6142 if (t->owner->handle != client)
6145 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6150 GNUNET_i2s (&peer_msg->peer));
6151 /* Is the peer in the tunnel? */
6153 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6154 if (NULL == peer_info)
6157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6161 /* Ok, delete peer from tunnel */
6162 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6163 &peer_msg->peer.hashPubKey);
6165 send_destroy_path (t, peer_info->id);
6166 tunnel_delete_peer (t, peer_info->id);
6167 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6172 * Handler for blacklist requests of peers in a tunnel
6174 * @param cls closure
6175 * @param client identification of the client
6176 * @param message the actual message (PeerControl)
6179 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6180 const struct GNUNET_MessageHeader *message)
6182 struct GNUNET_MESH_PeerControl *peer_msg;
6183 struct MeshClient *c;
6184 struct MeshTunnel *t;
6185 MESH_TunnelNumber tid;
6187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6188 /* Sanity check for client registration */
6189 if (NULL == (c = client_get (client)))
6192 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6195 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6197 /* Sanity check for message size */
6198 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6205 /* Tunnel exists? */
6206 tid = ntohl (peer_msg->tunnel_id);
6207 t = tunnel_get_by_local_id (c, tid);
6211 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6216 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6217 GNUNET_PEER_intern(&peer_msg->peer));
6222 * Handler for unblacklist requests of peers in a tunnel
6224 * @param cls closure
6225 * @param client identification of the client
6226 * @param message the actual message (PeerControl)
6229 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6230 const struct GNUNET_MessageHeader *message)
6232 struct GNUNET_MESH_PeerControl *peer_msg;
6233 struct MeshClient *c;
6234 struct MeshTunnel *t;
6235 MESH_TunnelNumber tid;
6239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6240 /* Sanity check for client registration */
6241 if (NULL == (c = client_get (client)))
6244 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6247 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6249 /* Sanity check for message size */
6250 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6253 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6257 /* Tunnel exists? */
6258 tid = ntohl (peer_msg->tunnel_id);
6259 t = tunnel_get_by_local_id (c, tid);
6263 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6268 /* if peer is not known, complain */
6269 pid = GNUNET_PEER_search (&peer_msg->peer);
6276 /* search and remove from list */
6277 for (i = 0; i < t->nblacklisted; i++)
6279 if (t->blacklisted[i] == pid)
6281 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6282 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6287 /* if peer hasn't been blacklisted, complain */
6293 * Handler for connection requests to new peers by type
6295 * @param cls closure
6296 * @param client identification of the client
6297 * @param message the actual message (ConnectPeerByType)
6300 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6301 const struct GNUNET_MessageHeader *message)
6303 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6304 struct MeshClient *c;
6305 struct MeshTunnel *t;
6306 struct GNUNET_HashCode hash;
6307 MESH_TunnelNumber tid;
6309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6310 /* Sanity check for client registration */
6311 if (NULL == (c = client_get (client)))
6314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6318 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6319 /* Sanity check for message size */
6320 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6321 ntohs (connect_msg->header.size))
6324 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6328 /* Tunnel exists? */
6329 tid = ntohl (connect_msg->tunnel_id);
6330 t = tunnel_get_by_local_id (c, tid);
6334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6338 /* Does client own tunnel? */
6339 if (t->owner->handle != client)
6342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6346 /* Do WE have the service? */
6347 t->type = ntohl (connect_msg->type);
6348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6349 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6350 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6353 /* Yes! Fast forward, add ourselves to the tunnel and send the
6354 * good news to the client, and alert the destination client of
6355 * an incoming tunnel.
6357 * FIXME send a path create to self, avoid code duplication
6359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6360 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6361 peer_info_get (&my_full_id),
6362 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6365 send_client_peer_connected (t, myid);
6366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6367 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6369 t->local_tid_dest = next_local_tid++;
6370 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6371 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6372 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6376 /* Ok, lets find a peer offering the service */
6377 if (NULL != t->dht_get_type)
6379 GNUNET_DHT_get_stop (t->dht_get_type);
6381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6382 GNUNET_h2s (&hash));
6384 GNUNET_DHT_get_start (dht_handle,
6385 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6387 dht_replication_level,
6388 GNUNET_DHT_RO_RECORD_ROUTE |
6389 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6391 &dht_get_type_handler, t);
6393 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6399 * Handler for connection requests to new peers by a string service description.
6401 * @param cls closure
6402 * @param client identification of the client
6403 * @param message the actual message, which includes messages the client wants
6406 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6407 const struct GNUNET_MessageHeader *message)
6409 struct GNUNET_MESH_ConnectPeerByString *msg;
6410 struct MeshRegexSearchContext *ctx;
6411 struct MeshRegexSearchInfo *info;
6412 struct GNUNET_DHT_GetHandle *get_h;
6413 struct GNUNET_HashCode key;
6414 struct MeshTunnel *t;
6415 struct MeshClient *c;
6416 MESH_TunnelNumber tid;
6420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6421 "Connect by string started\n");
6422 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6423 size = htons (message->size);
6425 /* Sanity check for client registration */
6426 if (NULL == (c = client_get (client)))
6429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6433 /* Message size sanity check */
6434 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6441 /* Tunnel exists? */
6442 tid = ntohl (msg->tunnel_id);
6443 t = tunnel_get_by_local_id (c, tid);
6447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6451 /* Does client own tunnel? */
6452 if (t->owner->handle != client)
6455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6460 " on tunnel %s [%u]\n",
6461 GNUNET_i2s(&my_full_id),
6464 /* Only one connect_by_string allowed at the same time! */
6465 /* FIXME: allow more, return handle at api level to cancel, document */
6466 if (NULL != t->regex_ctx)
6469 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6473 /* Find string itself */
6474 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6475 string = (const char *) &msg[1];
6477 /* Initialize context */
6478 size = GNUNET_REGEX_get_first_key(string, len, &key);
6479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6480 " consumed %u bits out of %u\n", size, len);
6481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6482 " looking for %s\n", GNUNET_h2s (&key));
6484 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6486 info->description = GNUNET_malloc (len + 1);
6487 memcpy (info->description, string, len);
6488 info->description[len] = '\0';
6489 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6490 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6493 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6494 ctx->position = size;
6498 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6500 /* Start search in DHT */
6501 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6502 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6503 &key, /* key to search */
6504 dht_replication_level, /* replication level */
6505 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6506 NULL, /* xquery */ // FIXME BLOOMFILTER
6507 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6508 &dht_get_string_handler, ctx);
6510 GNUNET_break (GNUNET_OK ==
6511 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6514 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6522 * Handler for client traffic directed to one peer
6524 * @param cls closure
6525 * @param client identification of the client
6526 * @param message the actual message
6529 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6530 const struct GNUNET_MessageHeader *message)
6532 struct MeshClient *c;
6533 struct MeshTunnel *t;
6534 struct MeshPeerInfo *pi;
6535 struct GNUNET_MESH_Unicast *data_msg;
6536 MESH_TunnelNumber tid;
6539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6540 "Got a unicast request from a client!\n");
6542 /* Sanity check for client registration */
6543 if (NULL == (c = client_get (client)))
6546 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6549 data_msg = (struct GNUNET_MESH_Unicast *) message;
6550 /* Sanity check for message size */
6551 size = ntohs (message->size);
6552 if (sizeof (struct GNUNET_MESH_Unicast) +
6553 sizeof (struct GNUNET_MessageHeader) > size)
6556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6560 /* Tunnel exists? */
6561 tid = ntohl (data_msg->tid);
6562 t = tunnel_get_by_local_id (c, tid);
6566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6570 /* Is it a local tunnel? Then, does client own the tunnel? */
6571 if (t->owner->handle != client)
6574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6578 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6579 &data_msg->destination.hashPubKey);
6580 /* Is the selected peer in the tunnel? */
6584 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6588 /* Ok, everything is correct, send the message
6589 * (pretend we got it from a mesh peer)
6592 char buf[ntohs (message->size)] GNUNET_ALIGN;
6593 struct GNUNET_MESH_Unicast *copy;
6595 /* Work around const limitation */
6596 copy = (struct GNUNET_MESH_Unicast *) buf;
6597 memcpy (buf, data_msg, size);
6598 copy->oid = my_full_id;
6599 copy->tid = htonl (t->id.tid);
6600 copy->ttl = htonl (default_ttl);
6601 copy->pid = htonl (t->pid + 1);
6602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6603 " calling generic handler...\n");
6604 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6607 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6613 * Handler for client traffic directed to the origin
6615 * @param cls closure
6616 * @param client identification of the client
6617 * @param message the actual message
6620 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6621 const struct GNUNET_MessageHeader *message)
6623 struct GNUNET_MESH_ToOrigin *data_msg;
6624 struct GNUNET_PeerIdentity id;
6625 struct MeshClient *c;
6626 struct MeshTunnel *t;
6627 MESH_TunnelNumber tid;
6630 /* Sanity check for client registration */
6631 if (NULL == (c = client_get (client)))
6634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6637 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6638 /* Sanity check for message size */
6639 size = ntohs (message->size);
6640 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6641 sizeof (struct GNUNET_MessageHeader) > size)
6644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6648 /* Tunnel exists? */
6649 tid = ntohl (data_msg->tid);
6650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6651 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6652 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6655 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6658 t = tunnel_get_by_local_id (c, tid);
6662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6666 /* It should be sent by someone who has this as incoming tunnel. */
6667 if (-1 == client_knows_tunnel (c, t))
6670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6673 GNUNET_PEER_resolve (t->id.oid, &id);
6675 /* Ok, everything is correct, send the message
6676 * (pretend we got it from a mesh peer)
6679 char buf[ntohs (message->size)] GNUNET_ALIGN;
6680 struct GNUNET_MESH_ToOrigin *copy;
6682 /* Work around const limitation */
6683 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6684 memcpy (buf, data_msg, size);
6686 copy->tid = htonl (t->id.tid);
6687 copy->sender = my_full_id;
6688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6689 " calling generic handler...\n");
6690 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6692 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6698 * Handler for client traffic directed to all peers in a tunnel
6700 * @param cls closure
6701 * @param client identification of the client
6702 * @param message the actual message
6705 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6706 const struct GNUNET_MessageHeader *message)
6708 struct MeshClient *c;
6709 struct MeshTunnel *t;
6710 struct GNUNET_MESH_Multicast *data_msg;
6711 MESH_TunnelNumber tid;
6713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6714 "Got a multicast request from a client!\n");
6716 /* Sanity check for client registration */
6717 if (NULL == (c = client_get (client)))
6720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6723 data_msg = (struct GNUNET_MESH_Multicast *) message;
6724 /* Sanity check for message size */
6725 if (sizeof (struct GNUNET_MESH_Multicast) +
6726 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6729 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6733 /* Tunnel exists? */
6734 tid = ntohl (data_msg->tid);
6735 t = tunnel_get_by_local_id (c, tid);
6739 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6743 /* Does client own tunnel? */
6744 if (t->owner->handle != client)
6747 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6752 char buf[ntohs (message->size)] GNUNET_ALIGN;
6753 struct GNUNET_MESH_Multicast *copy;
6755 copy = (struct GNUNET_MESH_Multicast *) buf;
6756 memcpy (buf, message, ntohs (message->size));
6757 copy->oid = my_full_id;
6758 copy->tid = htonl (t->id.tid);
6759 copy->ttl = htonl (default_ttl);
6760 copy->pid = htonl (t->pid + 1);
6761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6762 " calling generic handler...\n");
6763 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
6766 /* receive done gets called when last copy is sent to a neighbor */
6772 * Handler for client's ACKs for payload traffic.
6774 * @param cls Closure (unused).
6775 * @param client Identification of the client.
6776 * @param message The actual message.
6779 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6780 const struct GNUNET_MessageHeader *message)
6782 struct GNUNET_MESH_LocalAck *msg;
6783 struct MeshTunnel *t;
6784 struct MeshClient *c;
6785 MESH_TunnelNumber tid;
6787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6788 /* Sanity check for client registration */
6789 if (NULL == (c = client_get (client)))
6792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6796 msg = (struct GNUNET_MESH_LocalAck *) message;
6797 /* Tunnel exists? */
6798 tid = ntohl (msg->tunnel_id);
6799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6800 t = tunnel_get_by_local_id (c, tid);
6804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6808 /* Does client own tunnel? Is this and ACK for BCK traffic? */
6809 if (NULL != t->owner && t->owner->handle == client)
6816 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
6817 tunnel_set_client_fwd_ack (t, c, ntohl (msg->max_pid));
6818 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
6821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6827 * Functions to handle messages from clients
6829 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6830 {&handle_local_new_client, NULL,
6831 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6832 {&handle_local_announce_regex, NULL,
6833 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
6834 {&handle_local_tunnel_create, NULL,
6835 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6836 sizeof (struct GNUNET_MESH_TunnelMessage)},
6837 {&handle_local_tunnel_destroy, NULL,
6838 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6839 sizeof (struct GNUNET_MESH_TunnelMessage)},
6840 {&handle_local_tunnel_speed, NULL,
6841 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
6842 sizeof (struct GNUNET_MESH_TunnelMessage)},
6843 {&handle_local_tunnel_speed, NULL,
6844 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
6845 sizeof (struct GNUNET_MESH_TunnelMessage)},
6846 {&handle_local_tunnel_buffer, NULL,
6847 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6848 sizeof (struct GNUNET_MESH_TunnelMessage)},
6849 {&handle_local_tunnel_buffer, NULL,
6850 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6851 sizeof (struct GNUNET_MESH_TunnelMessage)},
6852 {&handle_local_connect_add, NULL,
6853 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
6854 sizeof (struct GNUNET_MESH_PeerControl)},
6855 {&handle_local_connect_del, NULL,
6856 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
6857 sizeof (struct GNUNET_MESH_PeerControl)},
6858 {&handle_local_blacklist, NULL,
6859 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
6860 sizeof (struct GNUNET_MESH_PeerControl)},
6861 {&handle_local_unblacklist, NULL,
6862 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
6863 sizeof (struct GNUNET_MESH_PeerControl)},
6864 {&handle_local_connect_by_type, NULL,
6865 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
6866 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
6867 {&handle_local_connect_by_string, NULL,
6868 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
6869 {&handle_local_unicast, NULL,
6870 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6871 {&handle_local_to_origin, NULL,
6872 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6873 {&handle_local_multicast, NULL,
6874 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6875 {&handle_local_ack, NULL,
6876 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6877 sizeof (struct GNUNET_MESH_LocalAck)},
6883 * To be called on core init/fail.
6885 * @param cls service closure
6886 * @param server handle to the server for this service
6887 * @param identity the public identity of this peer
6890 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6891 const struct GNUNET_PeerIdentity *identity)
6893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6894 core_handle = server;
6895 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6898 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6899 GNUNET_SCHEDULER_shutdown ();
6906 * Method called whenever a given peer connects.
6908 * @param cls closure
6909 * @param peer peer identity this notification is about
6910 * @param atsi performance data for the connection
6911 * @param atsi_count number of records in 'atsi'
6914 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
6915 const struct GNUNET_ATS_Information *atsi,
6916 unsigned int atsi_count)
6918 struct MeshPeerInfo *peer_info;
6919 struct MeshPeerPath *path;
6921 DEBUG_CONN ("Peer connected\n");
6922 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6923 peer_info = peer_info_get (peer);
6924 if (myid == peer_info->id)
6926 DEBUG_CONN (" (self)\n");
6931 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6933 path = path_new (2);
6934 path->peers[0] = myid;
6935 path->peers[1] = peer_info->id;
6936 GNUNET_PEER_change_rc (myid, 1);
6937 GNUNET_PEER_change_rc (peer_info->id, 1);
6938 peer_info_add_path (peer_info, path, GNUNET_YES);
6939 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6945 * Method called whenever a peer disconnects.
6947 * @param cls closure
6948 * @param peer peer identity this notification is about
6951 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6953 struct MeshPeerInfo *pi;
6954 struct MeshPeerQueue *q;
6955 struct MeshPeerQueue *n;
6957 DEBUG_CONN ("Peer disconnected\n");
6958 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6970 /* try to reroute this traffic instead */
6971 queue_destroy(q, GNUNET_YES);
6975 peer_info_remove_path (pi, pi->id, myid);
6978 DEBUG_CONN (" (self)\n");
6980 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6985 /******************************************************************************/
6986 /************************ MAIN FUNCTIONS ****************************/
6987 /******************************************************************************/
6990 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6992 * @param cls closure
6993 * @param key current key code
6994 * @param value value in the hash map
6995 * @return GNUNET_YES if we should continue to iterate,
6999 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7001 struct MeshTunnel *t = value;
7008 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7010 * @param cls closure
7011 * @param key current key code
7012 * @param value value in the hash map
7013 * @return GNUNET_YES if we should continue to iterate,
7017 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7019 struct MeshPeerInfo *p = value;
7020 struct MeshPeerQueue *q;
7021 struct MeshPeerQueue *n;
7029 queue_destroy(q, GNUNET_YES);
7033 peer_info_destroy (p);
7038 * Task run during shutdown.
7044 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7048 if (core_handle != NULL)
7050 GNUNET_CORE_disconnect (core_handle);
7053 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7054 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7055 if (dht_handle != NULL)
7057 GNUNET_DHT_disconnect (dht_handle);
7062 GNUNET_SERVER_notification_context_destroy (nc);
7065 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7067 GNUNET_SCHEDULER_cancel (announce_id_task);
7068 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7074 * Process mesh requests.
7076 * @param cls closure
7077 * @param server the initialized server
7078 * @param c configuration to use
7081 run (void *cls, struct GNUNET_SERVER_Handle *server,
7082 const struct GNUNET_CONFIGURATION_Handle *c)
7084 struct MeshPeerInfo *peer;
7085 struct MeshPeerPath *p;
7088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7089 server_handle = server;
7090 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7091 NULL, /* Closure passed to MESH functions */
7092 &core_init, /* Call core_init once connected */
7093 &core_connect, /* Handle connects */
7094 &core_disconnect, /* remove peers on disconnects */
7095 NULL, /* Don't notify about all incoming messages */
7096 GNUNET_NO, /* For header only in notification */
7097 NULL, /* Don't notify about all outbound messages */
7098 GNUNET_NO, /* For header-only out notification */
7099 core_handlers); /* Register these handlers */
7101 if (core_handle == NULL)
7104 GNUNET_SCHEDULER_shutdown ();
7109 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7114 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7116 GNUNET_SCHEDULER_shutdown ();
7121 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7122 &refresh_path_time))
7124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7126 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7127 "refresh path time");
7128 GNUNET_SCHEDULER_shutdown ();
7133 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7134 &app_announce_time))
7136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7138 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7139 "app announce time");
7140 GNUNET_SCHEDULER_shutdown ();
7145 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7150 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7151 "id announce time");
7152 GNUNET_SCHEDULER_shutdown ();
7157 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7158 &unacknowledged_wait_time))
7160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7162 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7163 "unacknowledged wait time");
7164 GNUNET_SCHEDULER_shutdown ();
7169 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7174 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7176 GNUNET_SCHEDULER_shutdown ();
7181 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7186 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7188 GNUNET_SCHEDULER_shutdown ();
7193 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7198 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7200 GNUNET_SCHEDULER_shutdown ();
7205 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7208 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7210 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7216 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7217 &dht_replication_level))
7219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7221 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7222 "dht replication level", 10);
7223 dht_replication_level = 10;
7227 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7228 GNUNET_free (keyfile);
7229 if (my_private_key == NULL)
7231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7232 _("Mesh service could not access hostkey. Exiting.\n"));
7233 GNUNET_SCHEDULER_shutdown ();
7236 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7237 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7238 &my_full_id.hashPubKey);
7239 myid = GNUNET_PEER_intern (&my_full_id);
7241 // transport_handle = GNUNET_TRANSPORT_connect(c,
7248 dht_handle = GNUNET_DHT_connect (c, 64);
7249 if (dht_handle == NULL)
7254 stats = GNUNET_STATISTICS_create ("mesh", c);
7258 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7260 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7261 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7262 peers = GNUNET_CONTAINER_multihashmap_create (32);
7263 applications = GNUNET_CONTAINER_multihashmap_create (32);
7264 types = GNUNET_CONTAINER_multihashmap_create (32);
7266 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7267 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7268 GNUNET_SERVER_disconnect_notify (server_handle,
7269 &handle_local_client_disconnect, NULL);
7273 clients_tail = NULL;
7276 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7277 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7279 /* Create a peer_info for the local peer */
7280 peer = peer_info_get (&my_full_id);
7283 GNUNET_PEER_change_rc (myid, 1);
7284 peer_info_add_path (peer, p, GNUNET_YES);
7286 /* Scheduled the task to clean up when shutdown is called */
7287 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7294 * The main function for the mesh service.
7296 * @param argc number of arguments from the command line
7297 * @param argv command line arguments
7298 * @return 0 ok, 1 on error
7301 main (int argc, char *const *argv)
7305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7308 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");