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.
472 * Info collected during iteration of child nodes in order to get the ACK value
475 struct MeshTunnelChildIteratorContext
478 * Tunnel whose info is being collected.
480 struct MeshTunnel *t;
483 * Maximum child ACK so far.
485 uint32_t max_child_ack;
488 * Number of children nodes
490 unsigned int nchildren;
495 * Info needed to work with tunnel paths and peers
502 struct MeshTunnel *t;
505 * Neighbouring peer to whom we send the packet to
507 struct MeshPeerInfo *peer;
512 struct MeshPeerPath *path;
517 * Struct containing information about a client of the service
524 struct MeshClient *next;
529 struct MeshClient *prev;
532 * Tunnels that belong to this client, indexed by local id
534 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
537 * Tunnels this client has accepted, indexed by incoming local id
539 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
542 * Tunnels this client has rejected, indexed by incoming local id
544 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
546 * Handle to communicate with the client
548 struct GNUNET_SERVER_Client *handle;
551 * Applications that this client has claimed to provide
553 struct GNUNET_CONTAINER_MultiHashMap *apps;
556 * Messages that this client has declared interest in
558 struct GNUNET_CONTAINER_MultiHashMap *types;
561 * Whether the client is active or shutting down (don't send confirmations
562 * to a client that is shutting down.
567 * ID of the client, mainly for debug messages
572 * Regular expressions describing the services offered by this client.
574 char **regexes; // FIXME add timeout? API to remove a regex?
577 * Number of regular expressions in regexes.
579 unsigned int n_regex;
582 * Task to refresh all regular expresions in the DHT.
584 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
590 * Struct to keep information of searches of services described by a regex
591 * using a user-provided string service description.
593 struct MeshRegexSearchInfo
596 * Which tunnel is this for
598 struct MeshTunnel *t;
601 * User provided description of the searched service.
606 * Part of the description already consumed by the search.
613 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
616 * Results from running DHT GETs.
618 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
621 * Contexts, for each running DHT GET. Free all on end of search.
623 struct MeshRegexSearchContext **contexts;
626 * Number of contexts (branches/steps in search).
628 unsigned int n_contexts;
631 * Peer that is connecting via connect_by_string. When connected, free ctx.
636 * Other peers that are found but not yet being connected to.
638 GNUNET_PEER_Id *peers;
641 * Number of elements in peers.
643 unsigned int n_peers;
646 * Next peer to try to connect to.
651 * Timeout for a connect attempt.
652 * When reached, try to connect to a different peer, if any. If not,
653 * try the same peer again.
655 GNUNET_SCHEDULER_TaskIdentifier timeout;
660 * Struct to keep state of running searches that have consumed a part of
663 struct MeshRegexSearchContext
666 * Part of the description already consumed by
667 * this particular search branch.
672 * Information about the search.
674 struct MeshRegexSearchInfo *info;
678 /******************************************************************************/
679 /************************ DEBUG FUNCTIONS ****************************/
680 /******************************************************************************/
684 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
685 * @param cls string to print
686 * @param success GNUNET_OK if the PUT was transmitted,
687 * GNUNET_NO on timeout,
688 * GNUNET_SYSERR on disconnect from service
689 * after the PUT message was transmitted
690 * (so we don't know if it was received or not)
695 mesh_debug (void *cls, int success)
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
705 /******************************************************************************/
706 /*********************** GLOBAL VARIABLES ****************************/
707 /******************************************************************************/
711 * Configuration parameters
713 static struct GNUNET_TIME_Relative refresh_path_time;
714 static struct GNUNET_TIME_Relative app_announce_time;
715 static struct GNUNET_TIME_Relative id_announce_time;
716 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
717 static struct GNUNET_TIME_Relative connect_timeout;
718 static long long unsigned int default_ttl;
719 static long long unsigned int dht_replication_level;
720 static long long unsigned int max_tunnels;
721 static long long unsigned int max_msgs_queue;
724 * DLL with all the clients, head.
726 static struct MeshClient *clients;
729 * DLL with all the clients, tail.
731 static struct MeshClient *clients_tail;
734 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
736 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
739 * Number of tunnels known.
741 static unsigned long long n_tunnels;
744 * Tunnels incoming, indexed by MESH_TunnelNumber
745 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
747 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
750 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
752 static struct GNUNET_CONTAINER_MultiHashMap *peers;
755 * Handle to communicate with transport
757 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
760 * Handle to communicate with core.
762 static struct GNUNET_CORE_Handle *core_handle;
767 static struct GNUNET_DHT_Handle *dht_handle;
772 static struct GNUNET_SERVER_Handle *server_handle;
775 * Handle to the statistics service.
777 static struct GNUNET_STATISTICS_Handle *stats;
780 * Notification context, to send messages to local clients.
782 static struct GNUNET_SERVER_NotificationContext *nc;
785 * Local peer own ID (memory efficient handle).
787 static GNUNET_PEER_Id myid;
790 * Local peer own ID (full value).
792 static struct GNUNET_PeerIdentity my_full_id;
797 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
802 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
805 * Tunnel ID for the next created tunnel (global tunnel number).
807 static MESH_TunnelNumber next_tid;
810 * Tunnel ID for the next incoming tunnel (local tunnel number).
812 static MESH_TunnelNumber next_local_tid;
815 * All application types provided by this peer.
817 static struct GNUNET_CONTAINER_MultiHashMap *applications;
820 * All message types clients of this peer are interested in.
822 static struct GNUNET_CONTAINER_MultiHashMap *types;
825 * Task to periodically announce provided applications.
827 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
830 * Task to periodically announce itself in the network.
832 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
835 * Next ID to assign to a client.
837 unsigned int next_client_id;
840 /******************************************************************************/
841 /*********************** DECLARATIONS **************************/
842 /******************************************************************************/
844 /* FIXME move declarations here */
847 * Function to process paths received for a new peer addition. The recorded
848 * paths form the initial tunnel, which can be optimized later.
849 * Called on each result obtained for the DHT search.
852 * @param exp when will this value expire
853 * @param key key of the result
854 * @param type type of the result
855 * @param size number of bytes in data
856 * @param data pointer to the result data
859 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
860 const struct GNUNET_HashCode * key,
861 const struct GNUNET_PeerIdentity *get_path,
862 unsigned int get_path_length,
863 const struct GNUNET_PeerIdentity *put_path,
864 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
865 size_t size, const void *data);
869 * Function to process DHT string to regex matching.
870 * Called on each result obtained for the DHT search.
872 * @param cls closure (search context)
873 * @param exp when will this value expire
874 * @param key key of the result
875 * @param get_path path of the get request (not used)
876 * @param get_path_length lenght of get_path (not used)
877 * @param put_path path of the put request (not used)
878 * @param put_path_length length of the put_path (not used)
879 * @param type type of the result
880 * @param size number of bytes in data
881 * @param data pointer to the result data
883 * TODO: re-issue the request after certain time? cancel after X results?
886 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
887 const struct GNUNET_HashCode * key,
888 const struct GNUNET_PeerIdentity *get_path,
889 unsigned int get_path_length,
890 const struct GNUNET_PeerIdentity *put_path,
891 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
892 size_t size, const void *data);
896 * Function to process DHT string to regex matching.
897 * Called on each result obtained for the DHT search.
899 * @param cls closure (search context)
900 * @param exp when will this value expire
901 * @param key key of the result
902 * @param get_path path of the get request (not used)
903 * @param get_path_length lenght of get_path (not used)
904 * @param put_path path of the put request (not used)
905 * @param put_path_length length of the put_path (not used)
906 * @param type type of the result
907 * @param size number of bytes in data
908 * @param data pointer to the result data
911 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
912 const struct GNUNET_HashCode * key,
913 const struct GNUNET_PeerIdentity *get_path,
914 unsigned int get_path_length,
915 const struct GNUNET_PeerIdentity *put_path,
916 unsigned int put_path_length,
917 enum GNUNET_BLOCK_Type type,
918 size_t size, const void *data);
922 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
923 * and insert it in the appropiate structures if the peer is not known yet.
925 * @param peer Short identity of the peer.
927 * @return Existing or newly created peer info.
929 static struct MeshPeerInfo *
930 peer_info_get_short (const GNUNET_PEER_Id peer);
934 * Try to establish a new connection to this peer.
935 * Use the best path for the given tunnel.
936 * If the peer doesn't have any path to it yet, try to get one.
937 * If the peer already has some path, send a CREATE PATH towards it.
939 * @param peer PeerInfo of the peer.
940 * @param t Tunnel for which to create the path, if possible.
943 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
947 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
949 * If peer already exists, reevaluate shortest path and change if different.
951 * @param t Tunnel we want to add a new peer to
952 * @param peer PeerInfo of the peer being added
956 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
960 * Removes an explicit path from a tunnel, freeing all intermediate nodes
961 * that are no longer needed, as well as nodes of no longer reachable peers.
962 * The tunnel itself is also destoyed if results in a remote empty tunnel.
964 * @param t Tunnel from which to remove the path.
965 * @param peer Short id of the peer which should be removed.
968 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
972 * Search for a tunnel by global ID using full PeerIdentities.
974 * @param oid owner of the tunnel.
975 * @param tid global tunnel number.
977 * @return tunnel handler, NULL if doesn't exist.
979 static struct MeshTunnel *
980 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
984 * Delete an active client from the tunnel.
990 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
993 * Notify a tunnel that a connection has broken that affects at least
996 * @param t Tunnel affected.
997 * @param p1 Peer that got disconnected from p2.
998 * @param p2 Peer that got disconnected from p1.
1000 * @return Short ID of the peer disconnected (either p1 or p2).
1001 * 0 if the tunnel remained unaffected.
1003 static GNUNET_PEER_Id
1004 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1009 * Get the current ack value for a tunnel, for data going from root to leaves,
1010 * taking in account the tunnel mode and the status of all children and clients.
1014 * @return Maximum PID allowed.
1017 tunnel_get_fwd_ack (struct MeshTunnel *t);
1020 * Get the current ack value for a tunnel, for data going from leaves to root,
1021 * taking in account the tunnel mode and the status of all children and clients.
1025 * @return Maximum PID allowed.
1028 tunnel_get_bck_ack (struct MeshTunnel *t);
1031 * Add a client to a tunnel, initializing all needed data structures.
1033 * @param t Tunnel to which add the client.
1034 * @param c Client which to add to the tunnel.
1037 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1041 * Iterator over edges in a regex block retrieved from the DHT.
1043 * @param cls Closure.
1044 * @param token Token that follows to next state.
1045 * @param len Lenght of token.
1046 * @param key Hash of next state.
1048 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1051 regex_edge_iterator (void *cls,
1054 const struct GNUNET_HashCode *key);
1058 * Find a path to a peer that offers a regex servcie compatible
1059 * with a given string.
1061 * @param key The key of the accepting state.
1062 * @param ctx Context containing info about the string, tunnel, etc.
1065 regex_find_path (const struct GNUNET_HashCode *key,
1066 struct MeshRegexSearchContext *ctx);
1070 * Queue and pass message to core when possible.
1072 * @param cls Closure (type dependant).
1073 * @param type Type of the message, 0 for a raw message.
1074 * @param size Size of the message.
1075 * @param dst Neighbor to send message to.
1076 * @param t Tunnel this message belongs to.
1079 queue_add (void *cls, uint16_t type, size_t size,
1080 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1083 * Free a transmission that was already queued with all resources
1084 * associated to the request.
1086 * @param queue Queue handler to cancel.
1087 * @param clear_cls Is it necessary to free associated cls?
1090 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1092 /******************************************************************************/
1093 /************************ ITERATORS ****************************/
1094 /******************************************************************************/
1097 * Iterator over found existing mesh regex blocks that match an ongoing search.
1099 * @param cls closure
1100 * @param key current key code
1101 * @param value value in the hash map
1102 * @return GNUNET_YES if we should continue to iterate,
1106 regex_result_iterator (void *cls,
1107 const struct GNUNET_HashCode * key,
1110 struct MeshRegexBlock *block = value;
1111 struct MeshRegexSearchContext *ctx = cls;
1113 if (GNUNET_YES == ntohl(block->accepting) &&
1114 ctx->position == strlen (ctx->info->description))
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1117 regex_find_path (key, ctx);
1118 return GNUNET_YES; // We found an accept state!
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1123 ctx->position, strlen(ctx->info->description),
1124 ntohl(block->accepting));
1127 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1128 ®ex_edge_iterator, ctx);
1135 * Iterator over edges in a regex block retrieved from the DHT.
1137 * @param cls Closure (context of the search).
1138 * @param token Token that follows to next state.
1139 * @param len Lenght of token.
1140 * @param key Hash of next state.
1142 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1145 regex_edge_iterator (void *cls,
1148 const struct GNUNET_HashCode *key)
1150 struct MeshRegexSearchContext *ctx = cls;
1151 struct MeshRegexSearchContext *new_ctx;
1152 struct MeshRegexSearchInfo *info = ctx->info;
1153 struct GNUNET_DHT_GetHandle *get_h;
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1160 current = &info->description[ctx->position];
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1162 current_len = strlen (info->description) - ctx->position;
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1167 if (len > current_len)
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1170 return GNUNET_YES; // Token too long, wont match
1172 if (0 != strncmp (current, token, len))
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1175 return GNUNET_YES; // Token doesn't match
1177 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1178 new_ctx->info = info;
1179 new_ctx->position = ctx->position + len;
1180 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1182 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1185 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1186 ®ex_result_iterator,
1188 return GNUNET_YES; // We are already looking for it
1190 /* Start search in DHT */
1192 GNUNET_DHT_get_start (dht_handle, /* handle */
1193 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1194 key, /* key to search */
1195 dht_replication_level, /* replication level */
1196 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1197 NULL, /* xquery */ // FIXME BLOOMFILTER
1198 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1199 &dht_get_string_handler, new_ctx);
1201 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1202 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1212 * Iterator over hash map entries to cancel DHT GET requests after a
1213 * successful connect_by_string.
1215 * @param cls Closure (unused).
1216 * @param key Current key code (unused).
1217 * @param value Value in the hash map (get handle).
1218 * @return GNUNET_YES if we should continue to iterate,
1222 regex_cancel_dht_get (void *cls,
1223 const struct GNUNET_HashCode * key,
1226 struct GNUNET_DHT_GetHandle *h = value;
1228 GNUNET_DHT_get_stop (h);
1234 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1235 * search for connect_by_string.
1237 * @param cls Closure (unused).
1238 * @param key Current key code (unused).
1239 * @param value MeshRegexBlock in the hash map.
1240 * @return GNUNET_YES if we should continue to iterate,
1244 regex_free_result (void *cls,
1245 const struct GNUNET_HashCode * key,
1249 GNUNET_free (value);
1255 * Regex callback iterator to store own service description in the DHT.
1257 * @param cls closure.
1258 * @param key hash for current state.
1259 * @param proof proof for current state.
1260 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1261 * @param num_edges number of edges leaving current state.
1262 * @param edges edges leaving current state.
1265 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1266 int accepting, unsigned int num_edges,
1267 const struct GNUNET_REGEX_Edge *edges)
1269 struct MeshRegexBlock *block;
1270 struct MeshRegexEdge *block_edge;
1271 enum GNUNET_DHT_RouteOption opt;
1275 unsigned int offset;
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 " regex dht put for state %s\n",
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1289 if (GNUNET_YES == accepting)
1291 struct MeshRegexAccept block;
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 " state %s is accepting, putting own id\n",
1296 size = sizeof (block);
1298 block.id = my_full_id;
1300 GNUNET_DHT_put(dht_handle, key,
1301 dht_replication_level,
1302 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1303 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1306 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1311 len = strlen(proof);
1312 size = sizeof (struct MeshRegexBlock) + len;
1313 block = GNUNET_malloc (size);
1316 block->n_proof = htonl (len);
1317 block->n_edges = htonl (num_edges);
1318 block->accepting = htonl (accepting);
1320 /* Store the proof at the end of the block. */
1321 aux = (char *) &block[1];
1322 memcpy (aux, proof, len);
1325 /* Store each edge in a variable length MeshEdge struct at the
1326 * very end of the MeshRegexBlock structure.
1328 for (i = 0; i < num_edges; i++)
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 " edge %s towards %s\n",
1333 GNUNET_h2s(&edges[i].destination));
1335 /* aux points at the end of the last block */
1336 len = strlen (edges[i].label);
1337 size += sizeof (struct MeshRegexEdge) + len;
1338 // Calculate offset FIXME is this ok? use size instead?
1339 offset = aux - (char *) block;
1340 block = GNUNET_realloc (block, size);
1341 aux = &((char *) block)[offset];
1342 block_edge = (struct MeshRegexEdge *) aux;
1343 block_edge->key = edges[i].destination;
1344 block_edge->n_token = htonl (len);
1345 aux = (char *) &block_edge[1];
1346 memcpy (aux, edges[i].label, len);
1350 GNUNET_DHT_put(dht_handle, key,
1351 dht_replication_level,
1353 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1355 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1359 GNUNET_free (block);
1364 * Store the regular expression describing a local service into the DHT.
1366 * @param regex The regular expresion.
1369 regex_put (const char *regex)
1371 struct GNUNET_REGEX_Automaton *dfa;
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1374 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1375 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1376 GNUNET_REGEX_automaton_destroy (dfa);
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1382 * Find a path to a peer that offers a regex servcie compatible
1383 * with a given string.
1385 * @param key The key of the accepting state.
1386 * @param ctx Context containing info about the string, tunnel, etc.
1389 regex_find_path (const struct GNUNET_HashCode *key,
1390 struct MeshRegexSearchContext *ctx)
1392 struct GNUNET_DHT_GetHandle *get_h;
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1395 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1396 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1397 key, /* key to search */
1398 dht_replication_level, /* replication level */
1399 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1400 GNUNET_DHT_RO_RECORD_ROUTE,
1401 NULL, /* xquery */ // FIXME BLOOMFILTER
1402 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1403 &dht_get_string_accept_handler, ctx);
1404 GNUNET_break (GNUNET_OK ==
1405 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1408 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1413 * Function called if the connect attempt to a peer found via
1414 * connect_by_string times out. Try to connect to another peer, if any.
1415 * Otherwise try to reconnect to the same peer.
1417 * @param cls Closure (info about regex search).
1418 * @param tc TaskContext.
1421 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1423 struct MeshRegexSearchInfo *info = cls;
1424 struct MeshPeerInfo *peer_info;
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1429 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1430 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1438 if (0 < info->n_peers)
1440 // Select next peer, put current in that spot.
1441 id = info->peers[info->i_peer];
1442 info->peers[info->i_peer] = info->peer;
1443 info->i_peer = (info->i_peer + 1) % info->n_peers;
1447 // Try to connect to same peer again.
1450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1452 peer_info = peer_info_get_short(id);
1453 tunnel_add_peer (info->t, peer_info);
1455 tunnel_delete_peer (info->t, old);
1456 peer_info_connect (peer_info, info->t);
1457 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1458 ®ex_connect_timeout,
1460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1465 * Cancel an ongoing regex search in the DHT and free all resources.
1467 * @param ctx The search context.
1470 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1472 struct MeshRegexSearchInfo *info = ctx->info;
1475 GNUNET_free (info->description);
1476 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1477 ®ex_cancel_dht_get, NULL);
1478 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1479 ®ex_free_result, NULL);
1480 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1481 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1482 info->t->regex_ctx = NULL;
1483 for (i = 0; i < info->n_contexts; i++)
1485 GNUNET_free (info->contexts[i]);
1487 if (0 < info->n_contexts)
1488 GNUNET_free (info->contexts);
1489 if (0 < info->n_peers)
1490 GNUNET_free (info->peers);
1491 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1493 GNUNET_SCHEDULER_cancel(info->timeout);
1499 /******************************************************************************/
1500 /************************ PERIODIC FUNCTIONS ****************************/
1501 /******************************************************************************/
1504 * Announce iterator over for each application provided by the peer
1506 * @param cls closure
1507 * @param key current key code
1508 * @param value value in the hash map
1509 * @return GNUNET_YES if we should continue to
1514 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1516 struct PBlock block;
1517 struct MeshClient *c;
1519 block.id = my_full_id;
1520 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1521 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1522 if (0 == block.type)
1527 block.type = htonl (block.type);
1529 GNUNET_break (NULL !=
1530 GNUNET_DHT_put (dht_handle, key,
1531 dht_replication_level,
1532 GNUNET_DHT_RO_RECORD_ROUTE |
1533 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1534 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1536 (const char *) &block,
1537 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1539 app_announce_time, NULL, NULL));
1545 * Periodically announce what applications are provided by local clients
1548 * @param cls closure
1549 * @param tc task context
1552 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1554 struct MeshClient *c = cls;
1557 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1559 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1563 DEBUG_DHT ("Starting PUT for regex\n");
1565 for (i = 0; i < c->n_regex; i++)
1567 regex_put (c->regexes[i]);
1569 c->regex_announce_task =
1570 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1571 DEBUG_DHT ("Finished PUT for regex\n");
1578 * Periodically announce what applications are provided by local clients
1581 * @param cls closure
1582 * @param tc task context
1585 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1587 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1589 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1593 DEBUG_DHT ("Starting PUT for apps\n");
1595 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1597 announce_applications_task =
1598 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1600 DEBUG_DHT ("Finished PUT for apps\n");
1607 * Periodically announce self id in the DHT
1609 * @param cls closure
1610 * @param tc task context
1613 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1615 struct PBlock block;
1617 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1619 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1623 * - Set data expiration in function of X
1624 * - Adapt X to churn
1626 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1628 block.id = my_full_id;
1629 block.type = htonl (0);
1630 GNUNET_DHT_put (dht_handle, /* DHT handle */
1631 &my_full_id.hashPubKey, /* Key to use */
1632 dht_replication_level, /* Replication level */
1633 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1634 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1635 sizeof (block), /* Size of the data */
1636 (const char *) &block, /* Data itself */
1637 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1638 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1639 NULL, /* Continuation */
1640 NULL); /* Continuation closure */
1642 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1646 /******************************************************************************/
1647 /****************** GENERAL HELPER FUNCTIONS ************************/
1648 /******************************************************************************/
1651 * Check if one pid is bigger than other, accounting for overflow.
1653 * @param big Argument that should be bigger.
1654 * @param small Argument that should be smaller.
1656 * @return True if big is bigger than small
1659 is_pid_bigger (uint32_t big, uint32_t small)
1661 return (PID_OVERFLOW(small, big) ||
1662 (big > small && !PID_OVERFLOW(big, small)));
1666 * Get the higher ACK value out of two values, taking in account overflow.
1668 * @param a First ACK value.
1669 * @param b Second ACK value.
1671 * @return Highest ACK value from the two.
1674 max_pid (uint32_t a, uint32_t b)
1676 if (is_pid_bigger(a, b))
1683 * Get the lower ACK value out of two values, taking in account overflow.
1685 * @param a First ACK value.
1686 * @param b Second ACK value.
1688 * @return Lowest ACK value from the two.
1691 min_pid (uint32_t a, uint32_t b)
1693 if (is_pid_bigger(a, b))
1700 * Decrements the reference counter and frees all resources if needed
1702 * @param mesh_data Data Descriptor used in a multicast message.
1703 * Freed no longer needed (last message).
1706 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1708 /* Make sure it's a multicast packet */
1709 GNUNET_assert (NULL != mesh_data->reference_counter);
1711 if (0 == --(*(mesh_data->reference_counter)))
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1714 if (NULL != mesh_data->task)
1716 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1718 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1720 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1722 GNUNET_free (mesh_data->task);
1724 GNUNET_free (mesh_data->reference_counter);
1725 GNUNET_free (mesh_data->data);
1726 GNUNET_free (mesh_data);
1732 * Check if client has registered with the service and has not disconnected
1734 * @param client the client to check
1736 * @return non-NULL if client exists in the global DLL
1738 static struct MeshClient *
1739 client_get (struct GNUNET_SERVER_Client *client)
1741 struct MeshClient *c;
1746 if (c->handle == client)
1755 * Checks if a given client has subscribed to certain message type
1757 * @param message_type Type of message to check
1758 * @param c Client to check
1760 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1763 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1765 struct GNUNET_HashCode hc;
1767 if (NULL == c->types)
1769 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1770 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1775 * Allow a client to send more data after transmitting a multicast message
1776 * which some neighbor has not yet accepted altough a reasonable time has
1779 * @param cls Closure (DataDescriptor containing the task identifier)
1780 * @param tc Task Context
1782 * FIXME reference counter cshould be just int
1785 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1787 struct MeshData *mdata = cls;
1789 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1791 GNUNET_assert (NULL != mdata->reference_counter);
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1793 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1794 *(mdata->reference_counter));
1795 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1796 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1801 * Check whether client wants traffic from a tunnel.
1803 * @param c Client to check.
1804 * @param t Tunnel to be found.
1806 * @return GNUNET_YES if client knows tunnel.
1808 * TODO look in client hashmap
1811 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1815 for (i = 0; i < t->nclients; i++)
1816 if (t->clients[i] == c)
1823 * Check whether client has been informed about a tunnel.
1825 * @param c Client to check.
1826 * @param t Tunnel to be found.
1828 * @return GNUNET_YES if client knows tunnel.
1830 * TODO look in client hashmap
1833 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1837 for (i = 0; i < t->nignore; i++)
1838 if (t->ignore[i] == c)
1840 return client_wants_tunnel(c, t);
1845 * Marks a client as uninterested in traffic from the tunnel, updating both
1846 * client and tunnel to reflect this.
1848 * @param c Client that doesn't want traffic anymore.
1849 * @param t Tunnel which should be ignored.
1851 * FIXME when to delete an incoming tunnel?
1854 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1856 struct GNUNET_HashCode hash;
1858 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1859 GNUNET_break (GNUNET_YES ==
1860 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1862 GNUNET_break (GNUNET_YES ==
1863 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1864 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1865 tunnel_delete_active_client (t, c);
1866 GNUNET_array_append (t->ignore, t->nignore, c);
1871 * Deletes a tunnel from a client (either owner or destination). To be used on
1872 * tunnel destroy, otherwise, use client_ignore_tunnel.
1874 * @param c Client whose tunnel to delete.
1875 * @param t Tunnel which should be deleted.
1878 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1880 struct GNUNET_HashCode hash;
1884 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1885 GNUNET_assert (GNUNET_YES ==
1886 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1892 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1894 GNUNET_assert (GNUNET_YES ==
1895 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1899 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1907 * Send the message to all clients that have subscribed to its type
1909 * @param msg Pointer to the message itself
1910 * @param payload Pointer to the payload of the message.
1911 * @return number of clients this message was sent to
1914 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1915 const struct GNUNET_MessageHeader *payload)
1917 struct GNUNET_PeerIdentity *oid;
1918 struct MeshClient *c;
1919 struct MeshTunnel *t;
1920 MESH_TunnelNumber *tid;
1923 char cbuf[htons (msg->size)];
1925 type = ntohs (payload->type);
1926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1929 memcpy (cbuf, msg, sizeof (cbuf));
1930 switch (htons (msg->type))
1932 struct GNUNET_MESH_Unicast *uc;
1933 struct GNUNET_MESH_Multicast *mc;
1934 struct GNUNET_MESH_ToOrigin *to;
1936 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1937 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1941 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1942 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1946 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1947 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1955 t = tunnel_get (oid, ntohl (*tid));
1962 for (count = 0, c = clients; c != NULL; c = c->next)
1964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1965 if (client_is_subscribed (type, c))
1967 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1971 *tid = htonl (t->local_tid);
1975 if (GNUNET_NO == client_knows_tunnel (c, t))
1977 /* This client doesn't know the tunnel */
1978 struct GNUNET_MESH_TunnelNotification tmsg;
1979 struct GNUNET_HashCode hash;
1981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1982 tmsg.header.size = htons (sizeof (tmsg));
1983 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1984 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1985 tmsg.tunnel_id = htonl (t->local_tid_dest);
1986 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1987 &tmsg.header, GNUNET_NO);
1988 tunnel_add_client (t, c);
1989 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1991 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1992 c->incoming_tunnels, &hash, t,
1993 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1995 *tid = htonl (t->local_tid_dest);
1998 /* Check if the client wants to get traffic from the tunnel */
1999 if (GNUNET_NO == client_wants_tunnel(c, t))
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2003 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2004 (struct GNUNET_MessageHeader
2005 *) cbuf, GNUNET_NO);
2013 * Notify the client that owns the tunnel that a peer has connected to it
2014 * (the requested path to it has been confirmed).
2016 * @param t Tunnel whose owner to notify
2017 * @param id Short id of the peer that has connected
2020 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2022 struct GNUNET_MESH_PeerControl pc;
2024 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2025 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2026 pc.tunnel_id = htonl (t->local_tid);
2027 GNUNET_PEER_resolve (id, &pc.peer);
2028 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2034 * Notify a the client of a tunnel about how many more
2035 * payload packages will we accept on a given tunnel,
2036 * distinguiching between root and leaf clients.
2038 * @param c Client whom to send the ACK.
2039 * @param t Tunnel on which to send the ACK.
2042 send_client_tunnel_ack (struct MeshClient *c, struct MeshTunnel *t)
2044 struct GNUNET_MESH_LocalAck msg;
2045 MESH_TunnelNumber tid;
2048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2049 "Sending client ACK on tunnel %X\n",
2054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to client %u\n", c->id);
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (owner, FWD ACK)\n");
2059 ack = tunnel_get_fwd_ack (t);
2064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (leaf, BCK ACK)\n");
2065 ack = tunnel_get_bck_ack (t);
2066 tid = t->local_tid_dest;
2069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2070 if (t->last_ack == ack)
2073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2075 msg.header.size = htons (sizeof (msg));
2076 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2077 msg.tunnel_id = htonl (tid);
2078 msg.max_pid = htonl (ack);
2080 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2081 &msg.header, GNUNET_NO);
2086 * Notify all clients (not depending on registration status) that the incoming
2087 * tunnel is no longer valid.
2089 * @param t Tunnel that was destroyed.
2092 send_clients_tunnel_destroy (struct MeshTunnel *t)
2094 struct GNUNET_MESH_TunnelMessage msg;
2096 msg.header.size = htons (sizeof (msg));
2097 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2098 msg.tunnel_id = htonl (t->local_tid_dest);
2099 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2104 * Notify clients of tunnel disconnections, if needed.
2105 * In case the origin disconnects, the destination clients get a tunnel destroy
2106 * notification. If the last destination disconnects (only one remaining client
2107 * in tunnel), the origin gets a (local ID) peer disconnected.
2108 * Note that the function must be called BEFORE removing the client from
2111 * @param t Tunnel that was destroyed.
2112 * @param c Client that disconnected.
2115 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2121 struct GNUNET_MESH_TunnelMessage msg;
2123 msg.header.size = htons (sizeof (msg));
2124 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2125 msg.tunnel_id = htonl (t->local_tid_dest);
2126 for (i = 0; i < t->nclients; i++)
2127 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2128 &msg.header, GNUNET_NO);
2130 // FIXME when to disconnect an incoming tunnel?
2131 else if (1 == t->nclients && NULL != t->owner)
2133 struct GNUNET_MESH_PeerControl msg;
2135 msg.header.size = htons (sizeof (msg));
2136 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2137 msg.tunnel_id = htonl (t->local_tid);
2138 msg.peer = my_full_id;
2139 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2140 &msg.header, GNUNET_NO);
2146 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2147 * and insert it in the appropiate structures if the peer is not known yet.
2149 * @param peer Full identity of the peer.
2151 * @return Existing or newly created peer info.
2153 static struct MeshPeerInfo *
2154 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2156 struct MeshPeerInfo *peer_info;
2158 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2159 if (NULL == peer_info)
2162 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2163 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2164 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2165 peer_info->id = GNUNET_PEER_intern (peer);
2173 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2174 * and insert it in the appropiate structures if the peer is not known yet.
2176 * @param peer Short identity of the peer.
2178 * @return Existing or newly created peer info.
2180 static struct MeshPeerInfo *
2181 peer_info_get_short (const GNUNET_PEER_Id peer)
2183 struct GNUNET_PeerIdentity id;
2185 GNUNET_PEER_resolve (peer, &id);
2186 return peer_info_get (&id);
2191 * Iterator to remove the tunnel from the list of tunnels a peer participates
2194 * @param cls Closure (tunnel info)
2195 * @param key GNUNET_PeerIdentity of the peer (unused)
2196 * @param value PeerInfo of the peer
2198 * @return always GNUNET_YES, to keep iterating
2201 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2203 struct MeshTunnel *t = cls;
2204 struct MeshPeerInfo *peer = value;
2207 for (i = 0; i < peer->ntunnels; i++)
2210 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2213 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2214 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2223 * Core callback to write a pre-constructed data packet to core buffer
2225 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2226 * @param size Number of bytes available in buf.
2227 * @param buf Where the to write the message.
2229 * @return number of bytes written to buf
2232 send_core_data_raw (void *cls, size_t size, void *buf)
2234 struct MeshTransmissionDescriptor *info = cls;
2235 struct GNUNET_MessageHeader *msg;
2238 GNUNET_assert (NULL != info);
2239 GNUNET_assert (NULL != info->mesh_data);
2240 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2241 total_size = ntohs (msg->size);
2243 if (total_size > size)
2248 memcpy (buf, msg, total_size);
2249 data_descriptor_decrement_rc (info->mesh_data);
2256 * Sends an already built non-multicast message to a peer,
2257 * properly registrating all used resources.
2259 * @param message Message to send. Function makes a copy of it.
2260 * @param peer Short ID of the neighbor whom to send the message.
2261 * @param t Tunnel on which this message is transmitted.
2264 send_message (const struct GNUNET_MessageHeader *message,
2265 const struct GNUNET_PeerIdentity *peer,
2266 struct MeshTunnel *t)
2268 struct MeshTransmissionDescriptor *info;
2269 struct MeshPeerInfo *neighbor;
2270 struct MeshPeerPath *p;
2273 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2275 size = ntohs (message->size);
2276 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2277 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2278 info->mesh_data->data = GNUNET_malloc (size);
2279 memcpy (info->mesh_data->data, message, size);
2280 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
2282 struct GNUNET_MESH_Unicast *m;
2284 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2285 m->ttl = htonl (ntohl (m->ttl) - 1);
2287 info->mesh_data->data_len = size;
2288 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2289 *info->mesh_data->reference_counter = 1;
2290 neighbor = peer_info_get (peer);
2291 for (p = neighbor->path_head; NULL != p; p = p->next)
2300 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2301 GNUNET_free (info->mesh_data->data);
2302 GNUNET_free (info->mesh_data);
2306 info->peer = neighbor;
2316 * Sends a CREATE PATH message for a path to a peer, properly registrating
2317 * all used resources.
2319 * @param peer PeerInfo of the final peer for whom this path is being created.
2320 * @param p Path itself.
2321 * @param t Tunnel for which the path is created.
2324 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2325 struct MeshTunnel *t)
2327 struct GNUNET_PeerIdentity id;
2328 struct MeshPathInfo *path_info;
2329 struct MeshPeerInfo *neighbor;
2335 p = tree_get_path_to_peer (t->tree, peer->id);
2342 for (i = 0; i < p->length; i++)
2344 if (p->peers[i] == myid)
2347 if (i >= p->length - 1)
2353 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2355 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2356 path_info->path = p;
2358 neighbor = peer_info_get (&id);
2359 path_info->peer = neighbor;
2360 queue_add (path_info,
2361 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2362 sizeof (struct GNUNET_MESH_ManipulatePath) +
2363 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2370 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2372 * @param t Tunnel whose path to destroy.
2373 * @param destination Short ID of the peer to whom the path to destroy.
2376 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2378 struct MeshPeerPath *p;
2381 p = tree_get_path_to_peer (t->tree, destination);
2387 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2388 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2390 struct GNUNET_MESH_ManipulatePath *msg;
2391 struct GNUNET_PeerIdentity *pi;
2395 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2396 msg->header.size = htons (size);
2397 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2398 msg->tid = htonl (t->id.tid);
2399 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2400 for (i = 0; i < p->length; i++)
2402 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2404 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2411 * Try to establish a new connection to this peer.
2412 * Use the best path for the given tunnel.
2413 * If the peer doesn't have any path to it yet, try to get one.
2414 * If the peer already has some path, send a CREATE PATH towards it.
2416 * @param peer PeerInfo of the peer.
2417 * @param t Tunnel for which to create the path, if possible.
2420 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2422 struct MeshPeerPath *p;
2423 struct MeshPathInfo *path_info;
2425 if (NULL != peer->path_head)
2427 p = tree_get_path_to_peer (t->tree, peer->id);
2434 // FIXME always send create path to self
2437 send_create_path (peer, p, t);
2441 struct GNUNET_HashCode hash;
2444 send_client_peer_connected (t, myid);
2445 t->local_tid_dest = next_local_tid++;
2446 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2449 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2450 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2457 else if (NULL == peer->dhtget)
2459 struct GNUNET_PeerIdentity id;
2461 GNUNET_PEER_resolve (peer->id, &id);
2462 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2463 path_info->peer = peer;
2465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2466 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2467 peer->dhtgetcls = path_info;
2468 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2469 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2470 &id.hashPubKey, /* key to search */
2471 dht_replication_level, /* replication level */
2472 GNUNET_DHT_RO_RECORD_ROUTE |
2473 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2474 NULL, /* xquery */ // FIXME BLOOMFILTER
2475 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2476 &dht_get_id_handler, path_info);
2478 /* Otherwise, there is no path but the DHT get is already started. */
2483 * Task to delay the connection of a peer
2485 * @param cls Closure (path info with tunnel and peer to connect).
2486 * Will be free'd on exection.
2487 * @param tc TaskContext
2490 peer_info_connect_task (void *cls,
2491 const struct GNUNET_SCHEDULER_TaskContext *tc)
2493 struct MeshPathInfo *path_info = cls;
2495 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2500 peer_info_connect (path_info->peer, path_info->t);
2506 * Destroy the peer_info and free any allocated resources linked to it
2508 * @param pi The peer_info to destroy.
2510 * @return GNUNET_OK on success
2513 peer_info_destroy (struct MeshPeerInfo *pi)
2515 struct GNUNET_PeerIdentity id;
2516 struct MeshPeerPath *p;
2517 struct MeshPeerPath *nextp;
2519 GNUNET_PEER_resolve (pi->id, &id);
2520 GNUNET_PEER_change_rc (pi->id, -1);
2523 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2526 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2527 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2529 if (NULL != pi->dhtget)
2531 GNUNET_DHT_get_stop (pi->dhtget);
2532 GNUNET_free (pi->dhtgetcls);
2538 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2548 * Remove all paths that rely on a direct connection between p1 and p2
2549 * from the peer itself and notify all tunnels about it.
2551 * @param peer PeerInfo of affected peer.
2552 * @param p1 GNUNET_PEER_Id of one peer.
2553 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2556 * TODO: optimize (see below)
2559 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2562 struct MeshPeerPath *p;
2563 struct MeshPeerPath *aux;
2564 struct MeshPeerInfo *peer_d;
2566 unsigned int destroyed;
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2573 p = peer->path_head;
2577 for (i = 0; i < (p->length - 1); i++)
2579 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2580 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2582 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2593 for (i = 0; i < peer->ntunnels; i++)
2595 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2599 * Problem: one or more peers have been deleted from the tunnel tree.
2600 * We don't know who they are to try to add them again.
2601 * We need to try to find a new path for each of the disconnected peers.
2602 * Some of them might already have a path to reach them that does not
2603 * involve p1 and p2. Adding all anew might render in a better tree than
2604 * the trivial immediate fix.
2606 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2607 * its children will be reachable trough him.
2609 peer_d = peer_info_get_short (d);
2612 for (p = peer_d->path_head; NULL != p; p = p->next)
2614 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2622 /* No callback, as peer will be already disconnected and a connection
2623 * scheduled by tunnel_notify_connection_broken.
2625 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2629 peer_info_connect (peer_d, peer->tunnels[i]);
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2637 * Add the path to the peer and update the path used to reach it in case this
2640 * @param peer_info Destination peer to add the path to.
2641 * @param path New path to add. Last peer must be the peer in arg 1.
2642 * Path will be either used of freed if already known.
2643 * @param trusted Do we trust that this path is real?
2646 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2649 struct MeshPeerPath *aux;
2653 if ((NULL == peer_info) || (NULL == path))
2656 path_destroy (path);
2659 if (path->peers[path->length - 1] != peer_info->id)
2662 path_destroy (path);
2665 if (path->length <= 2 && GNUNET_NO == trusted)
2667 /* Only allow CORE to tell us about direct paths */
2668 path_destroy (path);
2671 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2672 for (l = 1; l < path->length; l++)
2674 if (path->peers[l] == myid)
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2677 for (l2 = 0; l2 < path->length - l; l2++)
2679 path->peers[l2] = path->peers[l + l2];
2684 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2689 struct GNUNET_PeerIdentity id;
2691 GNUNET_PEER_resolve (peer_info->id, &id);
2692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2693 path->length, GNUNET_i2s (&id));
2696 l = path_get_length (path);
2703 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2704 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2706 l2 = path_get_length (aux);
2709 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2710 peer_info->path_tail, aux, path);
2715 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2717 path_destroy (path);
2722 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2729 * Add the path to the origin peer and update the path used to reach it in case
2730 * this is the shortest.
2731 * The path is given in peer_info -> destination, therefore we turn the path
2732 * upside down first.
2734 * @param peer_info Peer to add the path to, being the origin of the path.
2735 * @param path New path to add after being inversed.
2736 * @param trusted Do we trust that this path is real?
2739 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2740 struct MeshPeerPath *path, int trusted)
2743 peer_info_add_path (peer_info, path, trusted);
2748 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2749 * to obtain a local peer -> destination path and interning the peer ids.
2751 * @return Newly allocated and created path
2753 static struct MeshPeerPath *
2754 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2755 unsigned int get_path_length,
2756 const struct GNUNET_PeerIdentity *put_path,
2757 unsigned int put_path_length)
2759 struct MeshPeerPath *p;
2765 GNUNET_PEER_change_rc (myid, 1);
2766 i = get_path_length;
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2768 for (i--; i >= 0; i--)
2770 id = GNUNET_PEER_intern (&get_path[i]);
2771 if (p->length > 0 && id == p->peers[p->length - 1])
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2774 GNUNET_PEER_change_rc (id, -1);
2778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2779 GNUNET_i2s (&get_path[i]));
2781 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2782 p->peers[p->length - 1] = id;
2785 i = put_path_length;
2786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2787 for (i--; i >= 0; i--)
2789 id = GNUNET_PEER_intern (&put_path[i]);
2792 /* PUT path went through us, so discard the path up until now and start
2793 * from here to get a much shorter (and loop-free) path.
2798 if (p->length > 0 && id == p->peers[p->length - 1])
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2801 GNUNET_PEER_change_rc (id, -1);
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2806 GNUNET_i2s (&put_path[i]));
2808 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2809 p->peers[p->length - 1] = id;
2813 if (get_path_length > 0)
2814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2815 GNUNET_i2s (&get_path[0]));
2816 if (put_path_length > 0)
2817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2818 GNUNET_i2s (&put_path[0]));
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2821 for (i = 0; i < p->length; i++)
2823 struct GNUNET_PeerIdentity peer_id;
2825 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2827 GNUNET_i2s (&peer_id));
2835 * Adds a path to the peer_infos of all the peers in the path
2837 * @param p Path to process.
2838 * @param confirmed Whether we know if the path works or not.
2841 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2845 /* TODO: invert and add */
2846 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2847 for (i++; i < p->length; i++)
2849 struct MeshPeerInfo *aux;
2850 struct MeshPeerPath *copy;
2852 aux = peer_info_get_short (p->peers[i]);
2853 copy = path_duplicate (p);
2854 copy->length = i + 1;
2855 peer_info_add_path (aux, copy, GNUNET_NO);
2861 * Send keepalive packets for a peer
2863 * @param cls Closure (tunnel for which to send the keepalive).
2864 * @param tc Notification context.
2866 * TODO: implement explicit multicast keepalive?
2869 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2873 * Search for a tunnel among the incoming tunnels
2875 * @param tid the local id of the tunnel
2877 * @return tunnel handler, NULL if doesn't exist
2879 static struct MeshTunnel *
2880 tunnel_get_incoming (MESH_TunnelNumber tid)
2882 struct GNUNET_HashCode hash;
2884 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2885 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2886 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2891 * Search for a tunnel among the tunnels for a client
2893 * @param c the client whose tunnels to search in
2894 * @param tid the local id of the tunnel
2896 * @return tunnel handler, NULL if doesn't exist
2898 static struct MeshTunnel *
2899 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2901 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2903 return tunnel_get_incoming (tid);
2907 struct GNUNET_HashCode hash;
2909 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2910 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2916 * Search for a tunnel by global ID using PEER_ID
2918 * @param pi owner of the tunnel
2919 * @param tid global tunnel number
2921 * @return tunnel handler, NULL if doesn't exist
2923 static struct MeshTunnel *
2924 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2926 struct MESH_TunnelID id;
2927 struct GNUNET_HashCode hash;
2932 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2933 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2938 * Search for a tunnel by global ID using full PeerIdentities
2940 * @param oid owner of the tunnel
2941 * @param tid global tunnel number
2943 * @return tunnel handler, NULL if doesn't exist
2945 static struct MeshTunnel *
2946 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2948 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2953 * Delete an active client from the tunnel.
2959 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2963 for (i = 0; i < t->nclients; i++)
2965 if (t->clients[i] == c)
2967 t->clients[i] = t->clients[t->nclients - 1];
2968 t->clients_acks[i] = t->clients_acks[t->nclients - 1];
2969 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2971 GNUNET_array_grow (t->clients_acks, t->nclients, t->nclients - 1);
2979 * Delete an ignored client from the tunnel.
2985 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2989 for (i = 0; i < t->nignore; i++)
2991 if (t->ignore[i] == c)
2993 t->ignore[i] = t->ignore[t->nignore - 1];
2994 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3002 * Delete a client from the tunnel. It should be only done on
3003 * client disconnection, otherwise use client_ignore_tunnel.
3009 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3011 tunnel_delete_ignored_client (t, c);
3012 tunnel_delete_active_client (t, c);
3017 * Iterator to free MeshTunnelChildInfo of tunnel children.
3019 * @param cls Closure (tunnel info).
3020 * @param key Hash of GNUNET_PEER_Id (unused).
3021 * @param value MeshTunnelChildInfo of the child.
3023 * @return always GNUNET_YES, to keep iterating
3026 tunnel_destroy_child (void *cls,
3027 const struct GNUNET_HashCode * key,
3030 GNUNET_free (value);
3036 * Callback used to notify a client owner of a tunnel that a peer has
3037 * disconnected, most likely because of a path change.
3039 * @param cls Closure (tunnel this notification is about).
3040 * @param peer_id Short ID of disconnected peer.
3043 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3045 struct MeshTunnel *t = cls;
3046 struct MeshPeerInfo *peer;
3047 struct MeshPathInfo *path_info;
3049 if (NULL != t->owner && NULL != nc)
3051 struct GNUNET_MESH_PeerControl msg;
3053 msg.header.size = htons (sizeof (msg));
3054 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3055 msg.tunnel_id = htonl (t->local_tid);
3056 GNUNET_PEER_resolve (peer_id, &msg.peer);
3057 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3058 &msg.header, GNUNET_NO);
3060 peer = peer_info_get_short (peer_id);
3061 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3062 path_info->peer = peer;
3064 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3069 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3070 * needed rescources.
3071 * If peer already exists, reevaluate shortest path and change if different.
3073 * @param t Tunnel we want to add a new peer to
3074 * @param peer PeerInfo of the peer being added
3078 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3080 struct GNUNET_PeerIdentity id;
3081 struct MeshPeerPath *best_p;
3082 struct MeshPeerPath *p;
3083 unsigned int best_cost;
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3087 GNUNET_PEER_resolve (peer->id, &id);
3089 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3092 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3093 GNUNET_assert (GNUNET_OK ==
3094 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3096 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3099 if (NULL != (p = peer->path_head))
3102 best_cost = tree_get_path_cost (t->tree, p);
3105 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3112 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3113 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3114 t->path_refresh_task =
3115 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3119 /* Start a DHT get */
3120 peer_info_connect (peer, t);
3122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3126 * Add a path to a tunnel which we don't own, just to remember the next hop.
3127 * If destination node was already in the tunnel, the first hop information
3128 * will be replaced with the new path.
3130 * @param t Tunnel we want to add a new peer to
3131 * @param p Path to add
3132 * @param own_pos Position of local node in path.
3136 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3137 unsigned int own_pos)
3139 struct GNUNET_PeerIdentity id;
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3142 GNUNET_assert (0 != own_pos);
3143 tree_add_path (t->tree, p, NULL, NULL);
3144 if (own_pos < p->length - 1)
3146 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3147 tree_update_first_hops (t->tree, myid, &id);
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3154 * Notifies a tunnel that a connection has broken that affects at least
3155 * some of its peers. Sends a notification towards the root of the tree.
3156 * In case the peer is the owner of the tree, notifies the client that owns
3157 * the tunnel and tries to reconnect.
3159 * @param t Tunnel affected.
3160 * @param p1 Peer that got disconnected from p2.
3161 * @param p2 Peer that got disconnected from p1.
3163 * @return Short ID of the peer disconnected (either p1 or p2).
3164 * 0 if the tunnel remained unaffected.
3166 static GNUNET_PEER_Id
3167 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3173 tree_notify_connection_broken (t->tree, p1, p2,
3174 &tunnel_notify_client_peer_disconnected,
3176 if (myid != p1 && myid != p2)
3182 if (tree_get_predecessor (t->tree) != 0)
3184 /* We are the peer still connected, notify owner of the disconnection. */
3185 struct GNUNET_MESH_PathBroken msg;
3186 struct GNUNET_PeerIdentity neighbor;
3188 msg.header.size = htons (sizeof (msg));
3189 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3190 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3191 msg.tid = htonl (t->id.tid);
3192 msg.peer1 = my_full_id;
3193 GNUNET_PEER_resolve (pid, &msg.peer2);
3194 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3195 send_message (&msg.header, &neighbor, t);
3203 * Send a multicast packet to a neighbor.
3205 * @param cls Closure (Info about the multicast packet)
3206 * @param neighbor_id Short ID of the neighbor to send the packet to.
3209 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3211 struct MeshData *mdata = cls;
3212 struct MeshTransmissionDescriptor *info;
3213 struct GNUNET_PeerIdentity neighbor;
3215 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3217 info->mesh_data = mdata;
3218 (*(mdata->reference_counter)) ++;
3219 info->destination = neighbor_id;
3220 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3222 GNUNET_i2s (&neighbor));
3223 info->peer = peer_info_get (&neighbor);
3224 GNUNET_assert (NULL != info->peer);
3226 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3227 info->mesh_data->data_len,
3234 * Send a message in a tunnel in multicast, sending a copy to each child node
3235 * down the local one in the tunnel tree.
3237 * @param t Tunnel in which to send the data.
3238 * @param msg Message to be sent.
3239 * @param internal Has the service generated this message?
3242 tunnel_send_multicast (struct MeshTunnel *t,
3243 const struct GNUNET_MessageHeader *msg,
3246 struct MeshData *mdata;
3248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3249 " sending a multicast packet...\n");
3250 mdata = GNUNET_malloc (sizeof (struct MeshData));
3251 mdata->data_len = ntohs (msg->size);
3252 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3254 mdata->data = GNUNET_malloc (mdata->data_len);
3255 memcpy (mdata->data, msg, mdata->data_len);
3256 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3258 struct GNUNET_MESH_Multicast *mcast;
3260 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3261 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3263 ntohl (mcast->ttl));
3267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3269 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3270 && GNUNET_NO == internal)
3272 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3274 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3280 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3281 if (*(mdata->reference_counter) == 0)
3283 GNUNET_free (mdata->data);
3284 GNUNET_free (mdata->reference_counter);
3285 if (NULL != mdata->task)
3287 GNUNET_SCHEDULER_cancel(*(mdata->task));
3288 GNUNET_free (mdata->task);
3289 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3291 // FIXME change order?
3292 GNUNET_free (mdata);
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3295 " sending a multicast packet done\n");
3301 * Increase the SKIP value of all peers that
3302 * have not received a unicast message.
3304 * @param cls Closure (ID of the peer that HAS received the message).
3305 * @param key ID of the neighbor.
3306 * @param value Information about the neighbor.
3308 * @return GNUNET_YES to keep iterating.
3311 tunnel_add_skip (void *cls,
3312 const struct GNUNET_HashCode * key,
3315 struct GNUNET_PeerIdentity *neighbor = cls;
3316 struct MeshTunnelChildInfo *cinfo = value;
3318 /* TODO compare only pointers? key == neighbor? */
3319 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3330 * Iterator to get the appropiate ACK value from all children nodes.
3332 * @param cls Closue (tunnel).
3333 * @param id Id of the child node.
3336 tunnel_get_child_ack (void *cls,
3339 struct GNUNET_PeerIdentity peer_id;
3340 struct MeshTunnelChildInfo *cinfo;
3341 struct MeshTunnelChildIteratorContext *ctx = cls;
3342 struct MeshTunnel *t = ctx->t;
3345 GNUNET_PEER_resolve (id, &peer_id);
3346 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3347 &peer_id.hashPubKey);
3350 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3352 cinfo->pid = t->pid;
3353 cinfo->skip = t->pid;
3354 cinfo->max_pid = ack = t->pid + 1;
3355 GNUNET_assert (GNUNET_OK ==
3356 GNUNET_CONTAINER_multihashmap_put(t->children_fc,
3357 &peer_id.hashPubKey,
3359 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3363 ack = cinfo->max_pid;
3366 if (0 == ctx->max_child_ack)
3367 ctx->max_child_ack = ack;
3369 if (GNUNET_YES == t->speed_min)
3371 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3375 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3382 * Get the maximum PID allowed to transmit to any
3383 * tunnel child of the local peer, depending on the tunnel
3384 * buffering/speed settings.
3388 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3391 tunnel_get_children_ack (struct MeshTunnel *t)
3393 struct MeshTunnelChildIteratorContext ctx;
3395 ctx.max_child_ack = 0;
3397 tree_iterate_children (t->tree, tunnel_get_child_ack, &ctx);
3399 if (0 == ctx.nchildren)
3402 return (int64_t) ctx.max_child_ack;
3407 * Add a client to a tunnel, initializing all needed data structures.
3409 * @param t Tunnel to which add the client.
3410 * @param c Client which to add to the tunnel.
3413 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3417 GNUNET_array_append (t->clients, t->nclients, c);
3420 GNUNET_array_append (t->clients_acks, t->nclients, ack);
3425 * Set the ACK value of a client in a particular tunnel.
3427 * @param t Tunnel affected.
3428 * @param c Client whose ACK to set.
3429 * @param ack ACK value.
3432 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3433 struct MeshClient *c,
3438 for (i = 0; i < t->nclients; i++)
3440 if (t->clients[i] != c)
3442 t->clients_acks[i] = ack;
3450 * Get the ACK value of a client in a particular tunnel.
3452 * @param t Tunnel on which to look.
3453 * @param c Client whose ACK to get.
3455 * @return ACK value.
3457 uint32_t // FIXME static when used!!
3458 tunnel_get_client_ack (struct MeshTunnel *t,
3459 struct MeshClient *c)
3463 for (i = 0; i < t->nclients; i++)
3465 if (t->clients[i] != c)
3467 return t->clients_acks[i];
3470 return t->clients_acks[0];
3475 * Get the highest ACK value of all clients in a particular tunnel,
3476 * according to the buffering/speed settings.
3478 * @param t Tunnel on which to look.
3480 * @return Corresponding ACK value (max uint32_t).
3481 * If no clients are suscribed, -1.
3484 tunnel_get_clients_ack (struct MeshTunnel *t)
3489 for (ack = -1, i = 0; i < t->nclients; i++)
3492 (GNUNET_YES == t->speed_min && t->clients_acks[i] < ack) ||
3493 (GNUNET_NO == t->speed_min && t->clients_acks[i] > ack))
3495 ack = t->clients_acks[i];
3499 if (GNUNET_YES == t->nobuffer && ack > t->pid)
3502 return (uint32_t) ack;
3507 * Get the current ack value for a tunnel, taking in account the tunnel
3508 * mode and the status of all children nodes.
3512 * @return Maximum PID allowed.
3515 tunnel_get_fwd_ack (struct MeshTunnel *t)
3518 uint32_t buffer_free;
3523 count = t->pid - t->skip;
3524 buffer_free = t->queue_max - t->queue_n;
3525 ack = count + buffer_free; // Might overflow 32bits, it's ok!
3526 child_ack = tunnel_get_children_ack (t);
3527 client_ack = tunnel_get_clients_ack (t);
3528 if (-1 == child_ack)
3530 // Node has no children, child_ack AND core buffer are irrelevant.
3531 GNUNET_break (-1 != client_ack); // No children and no clients? Not good!
3532 return (uint32_t) client_ack;
3535 if (GNUNET_YES == t->speed_min)
3537 ack = min_pid (child_ack, ack);
3538 ack = min_pid (client_ack, ack);
3542 ack = max_pid (child_ack, ack);
3543 ack = max_pid (client_ack, ack);
3545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u\n",
3546 count, buffer_free, child_ack, client_ack);
3551 * Get the current ack value for a tunnel, taking in account the tunnel
3552 * mode and the status of all children nodes.
3556 * @return Maximum PID allowed.
3559 tunnel_get_bck_ack (struct MeshTunnel *t)
3570 * Send an ACK informing the predecessor about the available buffer space.
3571 * In case there is no predecessor, inform the owning client.
3572 * If buffering is off, send only on behalf of children or self if endpoint.
3573 * If buffering is on, send when sent to children and buffer space is free.
3574 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3575 * the ACK itself goes "back" (towards root).
3577 * @param t Tunnel on which to send the ACK.
3578 * @param type Type of message that triggered the ACK transmission.
3581 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3583 struct GNUNET_MESH_ACK msg;
3584 struct GNUNET_PeerIdentity id;
3587 if (NULL != t->owner)
3589 send_client_tunnel_ack (t->owner, t);
3592 /* Is it after unicast / multicast retransmission? */
3593 if (GNUNET_MESSAGE_TYPE_MESH_ACK != type)
3595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ACK via DATA retransmission\n");
3596 if (GNUNET_YES == t->nobuffer)
3598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3601 if (t->queue_max > t->queue_n * 2)
3603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3608 /* Ok, ACK might be necessary, what PID to ACK? */
3609 ack = tunnel_get_fwd_ack (t);
3611 /* If speed_min and not all children have ack'd, dont send yet */
3612 if (ack == t->last_ack)
3614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, not ready\n");
3619 msg.pid = htonl (ack);
3621 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3623 msg.header.size = htons (sizeof (msg));
3624 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3625 msg.tid = htonl (t->id.tid);
3626 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3627 send_message (&msg.header, &id, t);
3632 * Send an ACK informing the children nodes about the available buffer space.
3633 * In case there is no child node, inform the destination clients.
3634 * If buffering is off, send only on behalf of root (can be self).
3635 * If buffering is on, send when sent to predecessor and buffer space is free.
3636 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3637 * the ACK itself goes "forward" (towards children/clients).
3639 * @param t Tunnel on which to send the ACK.
3640 * @param type Type of message that triggered the ACK transmission.
3643 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3645 struct GNUNET_MESH_ACK msg;
3646 struct GNUNET_PeerIdentity id;
3649 if (NULL != t->owner)
3651 send_client_tunnel_ack (t->owner, t);
3654 /* Is it after unicast / multicast retransmission? */
3655 if (GNUNET_MESSAGE_TYPE_MESH_ACK != type)
3657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ACK via DATA retransmission\n");
3658 if (GNUNET_YES == t->nobuffer)
3660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3663 if (t->queue_max > t->queue_n * 2)
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3670 /* Ok, ACK might be necessary, what PID to ACK? */
3671 ack = tunnel_get_bck_ack (t);
3673 /* If speed_min and not all children have ack'd, dont send yet */
3674 if (ack == t->last_ack)
3676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, not ready\n");
3681 msg.pid = htonl (ack);
3683 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3685 msg.header.size = htons (sizeof (msg));
3686 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3687 msg.tid = htonl (t->id.tid);
3688 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3689 send_message (&msg.header, &id, t);
3694 * Send a message to all peers in this tunnel that the tunnel is no longer
3697 * @param t The tunnel whose peers to notify.
3700 tunnel_send_destroy (struct MeshTunnel *t)
3702 struct GNUNET_MESH_TunnelDestroy msg;
3704 msg.header.size = htons (sizeof (msg));
3705 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3706 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3707 msg.tid = htonl (t->id.tid);
3708 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3713 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3715 * @param cls Closure (Tunnel which to cancel).
3716 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3719 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3721 struct MeshTunnel *t = cls;
3722 struct MeshPeerInfo *peer_info;
3723 struct MeshPeerQueue *pq;
3724 struct MeshPeerQueue *next;
3726 peer_info = peer_info_get_short (neighbor_id);
3727 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3730 if (pq->tunnel == t)
3732 queue_destroy (pq, GNUNET_YES);
3735 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3737 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3738 peer_info->core_transmit = NULL;
3743 * Destroy the tunnel and free any allocated resources linked to it.
3745 * @param t the tunnel to destroy
3747 * @return GNUNET_OK on success
3750 tunnel_destroy (struct MeshTunnel *t)
3752 struct MeshClient *c;
3753 struct GNUNET_HashCode hash;
3760 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3766 struct GNUNET_PeerIdentity id;
3768 GNUNET_PEER_resolve (t->id.oid, &id);
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3770 GNUNET_i2s (&id), t->id.tid);
3772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3776 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3777 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3782 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3785 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3789 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3790 for (i = 0; i < t->nclients; i++)
3794 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3799 for (i = 0; i < t->nignore; i++)
3803 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3808 if (t->nclients > 0)
3811 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3815 GNUNET_free (t->clients);
3817 if (NULL != t->peers)
3819 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3821 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3824 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3825 &tunnel_destroy_child,
3827 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3829 tree_destroy (t->tree);
3831 if (NULL != t->regex_ctx)
3832 regex_cancel_search (t->regex_ctx);
3833 if (NULL != t->dht_get_type)
3834 GNUNET_DHT_get_stop (t->dht_get_type);
3835 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3836 GNUNET_SCHEDULER_cancel (t->timeout_task);
3837 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3838 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3841 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3842 GNUNET_assert (0 <= n_tunnels);
3849 * Create a new tunnel
3851 * @param owner Who is the owner of the tunnel (short ID).
3852 * @param tid Tunnel Number of the tunnel.
3853 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3854 * @param local Tunnel Number for the tunnel, for the client point of view.
3856 * @return A new initialized tunnel. NULL on error.
3858 static struct MeshTunnel *
3859 tunnel_new (GNUNET_PEER_Id owner,
3860 MESH_TunnelNumber tid,
3861 struct MeshClient *client,
3862 MESH_TunnelNumber local)
3864 struct MeshTunnel *t;
3865 struct GNUNET_HashCode hash;
3867 if (n_tunnels >= max_tunnels && NULL == client)
3870 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3873 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
3874 t->tree = tree_new (owner);
3876 t->root_client_ack = 1;
3877 t->local_tid = local;
3878 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
3880 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3882 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3884 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3885 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3892 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3899 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3901 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3902 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3906 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3916 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3917 * that are no longer needed, as well as nodes of no longer reachable peers.
3918 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3920 * @param t Tunnel from which to remove the path.
3921 * @param peer Short id of the peer which should be removed.
3924 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3926 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3932 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3933 * client when the client disconnects. If the client is not the owner, the
3934 * owner will get notified if no more clients are in the tunnel and the client
3935 * get removed from the tunnel's list.
3937 * @param cls closure (client that is disconnecting)
3938 * @param key the hash of the local tunnel id (used to access the hashmap)
3939 * @param value the value stored at the key (tunnel to destroy)
3941 * @return GNUNET_OK on success
3944 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3946 struct MeshTunnel *t = value;
3947 struct MeshClient *c = cls;
3950 send_client_tunnel_disconnect(t, c);
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3954 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3955 tunnel_delete_client(t, c);
3956 client_delete_tunnel(c, t);
3959 tunnel_send_destroy(t);
3960 r = tunnel_destroy (t);
3966 * Timeout function, destroys tunnel if called
3968 * @param cls Closure (tunnel to destroy).
3969 * @param tc TaskContext
3972 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3974 struct MeshTunnel *t = cls;
3976 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3978 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3983 * Resets the tunnel timeout. Starts it if no timeout was running.
3985 * @param t Tunnel whose timeout to reset.
3988 tunnel_reset_timeout (struct MeshTunnel *t)
3990 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3991 GNUNET_SCHEDULER_cancel (t->timeout_task);
3993 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3994 (refresh_path_time, 4), &tunnel_timeout, t);
3998 /******************************************************************************/
3999 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4000 /******************************************************************************/
4003 * Function to send a create path packet to a peer.
4005 * @param cls closure
4006 * @param size number of bytes available in buf
4007 * @param buf where the callee should write the message
4008 * @return number of bytes written to buf
4011 send_core_path_create (void *cls, size_t size, void *buf)
4013 struct MeshPathInfo *info = cls;
4014 struct GNUNET_MESH_ManipulatePath *msg;
4015 struct GNUNET_PeerIdentity *peer_ptr;
4016 struct MeshTunnel *t = info->t;
4017 struct MeshPeerPath *p = info->path;
4022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4024 sizeof (struct GNUNET_MESH_ManipulatePath) +
4025 p->length * sizeof (struct GNUNET_PeerIdentity);
4027 if (size < size_needed || NULL == buf)
4032 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4033 msg->header.size = htons (size_needed);
4034 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4035 msg->tid = ntohl (t->id.tid);
4037 if (GNUNET_YES == t->speed_min)
4038 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4039 if (GNUNET_YES == t->nobuffer)
4040 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4041 msg->opt = htonl(opt);
4043 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4044 for (i = 0; i < p->length; i++)
4046 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4053 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4059 * Fill the core buffer
4061 * @param cls closure (data itself)
4062 * @param size number of bytes available in buf
4063 * @param buf where the callee should write the message
4065 * @return number of bytes written to buf
4068 send_core_data_multicast (void *cls, size_t size, void *buf)
4070 struct MeshTransmissionDescriptor *info = cls;
4073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4074 GNUNET_assert (NULL != info);
4075 GNUNET_assert (NULL != info->peer);
4076 total_size = info->mesh_data->data_len;
4077 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4079 if (total_size > size)
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4085 memcpy (buf, info->mesh_data->data, total_size);
4088 struct GNUNET_MESH_Multicast *mc;
4089 struct GNUNET_MessageHeader *mh;
4092 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4094 mc = (struct GNUNET_MESH_Multicast *) mh;
4095 mh = (struct GNUNET_MessageHeader *) &mc[1];
4096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4097 " multicast, payload type %u\n", ntohs (mh->type));
4098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4099 " multicast, payload size %u\n", ntohs (mh->size));
4103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
4108 data_descriptor_decrement_rc (info->mesh_data);
4109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4117 * Creates a path ack message in buf and frees all unused resources.
4119 * @param cls closure (MeshTransmissionDescriptor)
4120 * @param size number of bytes available in buf
4121 * @param buf where the callee should write the message
4122 * @return number of bytes written to buf
4125 send_core_path_ack (void *cls, size_t size, void *buf)
4127 struct MeshTransmissionDescriptor *info = cls;
4128 struct GNUNET_MESH_PathACK *msg = buf;
4130 GNUNET_assert (NULL != info);
4131 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4136 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4137 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4138 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4139 msg->tid = htonl (info->origin->tid);
4140 msg->peer_id = my_full_id;
4143 /* TODO add signature */
4145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4146 return sizeof (struct GNUNET_MESH_PathACK);
4151 * Free a transmission that was already queued with all resources
4152 * associated to the request.
4154 * @param queue Queue handler to cancel.
4155 * @param clear_cls Is it necessary to free associated cls?
4158 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4160 struct MeshTransmissionDescriptor *dd;
4161 struct MeshPathInfo *path_info;
4163 if (GNUNET_YES == clear_cls)
4165 switch (queue->type)
4167 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4168 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4169 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4172 data_descriptor_decrement_rc (dd->mesh_data);
4174 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4176 path_info = queue->cls;
4177 path_destroy (path_info->path);
4181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4183 GNUNET_free_non_null (queue->cls);
4185 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4186 queue->peer->queue_tail,
4188 GNUNET_free (queue);
4193 * Core callback to write a queued packet to core buffer
4195 * @param cls Closure (peer info).
4196 * @param size Number of bytes available in buf.
4197 * @param buf Where the to write the message.
4199 * @return number of bytes written to buf
4202 queue_send (void *cls, size_t size, void *buf)
4204 struct MeshPeerInfo *peer = cls;
4205 struct GNUNET_MessageHeader *msg;
4206 struct MeshPeerQueue *queue;
4207 struct MeshTunnel *t;
4210 peer->core_transmit = NULL;
4211 queue = peer->queue_head;
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4215 /* If queue is empty, send should have been cancelled */
4221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4223 /* Check if buffer size is enough for the message */
4224 if (queue->size > size)
4226 struct GNUNET_PeerIdentity id;
4228 GNUNET_PEER_resolve (peer->id, &id);
4229 peer->core_transmit =
4230 GNUNET_CORE_notify_transmit_ready(core_handle,
4233 GNUNET_TIME_UNIT_FOREVER_REL,
4240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4246 switch (queue->type)
4248 case 0: // RAW data (preconstructed message, retransmission, etc)
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
4250 data_size = send_core_data_raw (queue->cls, size, buf);
4251 msg = (struct GNUNET_MessageHeader *) buf;
4252 switch (ntohs (msg->type)) // Type of payload
4254 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4255 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4257 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4258 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4264 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4266 data_size = send_core_data_multicast(queue->cls, size, buf);
4267 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4269 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4271 data_size = send_core_path_create(queue->cls, size, buf);
4273 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4275 data_size = send_core_path_ack(queue->cls, size, buf);
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
4283 /* Free queue, but cls was freed by send_core_* */
4284 queue_destroy (queue, GNUNET_NO);
4286 if (GNUNET_YES == t->destroy && 0 == t->queue_n)
4288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4292 /* If more data in queue, send next */
4293 if (NULL != peer->queue_head)
4295 struct GNUNET_PeerIdentity id;
4297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4298 GNUNET_PEER_resolve (peer->id, &id);
4299 peer->core_transmit =
4300 GNUNET_CORE_notify_transmit_ready(core_handle,
4303 GNUNET_TIME_UNIT_FOREVER_REL,
4305 peer->queue_head->size,
4309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4315 * Queue and pass message to core when possible.
4317 * @param cls Closure (type dependant).
4318 * @param type Type of the message, 0 for a raw message.
4319 * @param size Size of the message.
4320 * @param dst Neighbor to send message to.
4321 * @param t Tunnel this message belongs to.
4324 queue_add (void *cls, uint16_t type, size_t size,
4325 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4327 struct MeshPeerQueue *queue;
4329 if (t->queue_n >= t->queue_max)
4331 if (NULL == t->owner)
4332 GNUNET_break_op(0); // TODO: kill connection?
4335 return; // Drop message
4338 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4344 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4345 if (NULL == dst->core_transmit)
4347 struct GNUNET_PeerIdentity id;
4349 GNUNET_PEER_resolve (dst->id, &id);
4350 dst->core_transmit =
4351 GNUNET_CORE_notify_transmit_ready(core_handle,
4354 GNUNET_TIME_UNIT_FOREVER_REL,
4363 /******************************************************************************/
4364 /******************** MESH NETWORK HANDLERS **************************/
4365 /******************************************************************************/
4369 * Core handler for path creation
4371 * @param cls closure
4372 * @param message message
4373 * @param peer peer identity this notification is about
4374 * @param atsi performance data
4375 * @param atsi_count number of records in 'atsi'
4377 * @return GNUNET_OK to keep the connection open,
4378 * GNUNET_SYSERR to close it (signal serious error)
4381 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4382 const struct GNUNET_MessageHeader *message,
4383 const struct GNUNET_ATS_Information *atsi,
4384 unsigned int atsi_count)
4386 unsigned int own_pos;
4389 MESH_TunnelNumber tid;
4390 struct GNUNET_MESH_ManipulatePath *msg;
4391 struct GNUNET_PeerIdentity *pi;
4392 struct GNUNET_HashCode hash;
4393 struct MeshPeerPath *path;
4394 struct MeshPeerInfo *dest_peer_info;
4395 struct MeshPeerInfo *orig_peer_info;
4396 struct MeshTunnel *t;
4398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4399 "Received a path create msg [%s]\n",
4400 GNUNET_i2s (&my_full_id));
4401 size = ntohs (message->size);
4402 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4404 GNUNET_break_op (0);
4408 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4409 if (size % sizeof (struct GNUNET_PeerIdentity))
4411 GNUNET_break_op (0);
4414 size /= sizeof (struct GNUNET_PeerIdentity);
4417 GNUNET_break_op (0);
4420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4421 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4423 tid = ntohl (msg->tid);
4424 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4426 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4427 t = tunnel_get (pi, tid);
4428 if (NULL == t) // FIXME only for INCOMING tunnels?
4432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4433 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4436 // FIXME notify failure
4439 opt = ntohl (msg->opt);
4440 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4441 GNUNET_YES : GNUNET_NO;
4442 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4443 GNUNET_YES : GNUNET_NO;
4445 if (GNUNET_YES == t->nobuffer)
4448 while (NULL != tunnel_get_incoming (next_local_tid))
4449 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4450 t->local_tid_dest = next_local_tid++;
4451 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4453 tunnel_reset_timeout (t);
4454 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4456 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4465 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4466 if (NULL == dest_peer_info)
4468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4469 " Creating PeerInfo for destination.\n");
4470 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4471 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4472 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4474 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4476 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4477 if (NULL == orig_peer_info)
4479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4480 " Creating PeerInfo for origin.\n");
4481 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4482 orig_peer_info->id = GNUNET_PEER_intern (pi);
4483 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4484 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4487 path = path_new (size);
4489 for (i = 0; i < size; i++)
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4492 GNUNET_i2s (&pi[i]));
4493 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4494 if (path->peers[i] == myid)
4497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4500 /* cannot be self, must be 'not found' */
4501 /* create path: self not found in path through self */
4502 GNUNET_break_op (0);
4503 path_destroy (path);
4504 /* FIXME error. destroy tunnel? leave for timeout? */
4507 path_add_to_peers (path, GNUNET_NO);
4508 tunnel_add_path (t, path, own_pos);
4509 if (own_pos == size - 1)
4511 /* It is for us! Send ack. */
4512 struct MeshTransmissionDescriptor *info;
4514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4515 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4516 if (NULL == t->peers)
4518 /* New tunnel! Notify clients on data. */
4519 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4521 GNUNET_break (GNUNET_SYSERR !=
4522 GNUNET_CONTAINER_multihashmap_put (t->peers,
4523 &my_full_id.hashPubKey,
4526 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4527 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4528 info->origin = &t->id;
4529 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4530 GNUNET_assert (NULL != info->peer);
4532 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4533 sizeof (struct GNUNET_MESH_PathACK),
4539 struct MeshPeerPath *path2;
4541 /* It's for somebody else! Retransmit. */
4542 path2 = path_duplicate (path);
4543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4544 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4545 path2 = path_duplicate (path);
4546 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4547 send_create_path (dest_peer_info, path, t);
4554 * Core handler for path destruction
4556 * @param cls closure
4557 * @param message message
4558 * @param peer peer identity this notification is about
4559 * @param atsi performance data
4560 * @param atsi_count number of records in 'atsi'
4562 * @return GNUNET_OK to keep the connection open,
4563 * GNUNET_SYSERR to close it (signal serious error)
4566 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4567 const struct GNUNET_MessageHeader *message,
4568 const struct GNUNET_ATS_Information *atsi,
4569 unsigned int atsi_count)
4571 struct GNUNET_MESH_ManipulatePath *msg;
4572 struct GNUNET_PeerIdentity *pi;
4573 struct MeshPeerPath *path;
4574 struct MeshTunnel *t;
4575 unsigned int own_pos;
4579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4580 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4581 size = ntohs (message->size);
4582 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4584 GNUNET_break_op (0);
4588 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4589 if (size % sizeof (struct GNUNET_PeerIdentity))
4591 GNUNET_break_op (0);
4594 size /= sizeof (struct GNUNET_PeerIdentity);
4597 GNUNET_break_op (0);
4600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4602 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4603 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4605 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4607 t = tunnel_get (pi, ntohl (msg->tid));
4610 /* TODO notify back: we don't know this tunnel */
4611 GNUNET_break_op (0);
4614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4615 path = path_new (size);
4617 for (i = 0; i < size; i++)
4619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4620 GNUNET_i2s (&pi[i]));
4621 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4622 if (path->peers[i] == myid)
4625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4626 if (own_pos < path->length - 1)
4627 send_message (message, &pi[own_pos + 1], t);
4629 send_client_tunnel_disconnect(t, NULL);
4631 tunnel_delete_peer (t, path->peers[path->length - 1]);
4632 path_destroy (path);
4638 * Core handler for notifications of broken paths
4640 * @param cls closure
4641 * @param message message
4642 * @param peer peer identity this notification is about
4643 * @param atsi performance data
4644 * @param atsi_count number of records in 'atsi'
4646 * @return GNUNET_OK to keep the connection open,
4647 * GNUNET_SYSERR to close it (signal serious error)
4650 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4651 const struct GNUNET_MessageHeader *message,
4652 const struct GNUNET_ATS_Information *atsi,
4653 unsigned int atsi_count)
4655 struct GNUNET_MESH_PathBroken *msg;
4656 struct MeshTunnel *t;
4658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4659 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4660 msg = (struct GNUNET_MESH_PathBroken *) message;
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4662 GNUNET_i2s (&msg->peer1));
4663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4664 GNUNET_i2s (&msg->peer2));
4665 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4668 GNUNET_break_op (0);
4671 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4672 GNUNET_PEER_search (&msg->peer2));
4679 * Core handler for tunnel destruction
4681 * @param cls closure
4682 * @param message message
4683 * @param peer peer identity this notification is about
4684 * @param atsi performance data
4685 * @param atsi_count number of records in 'atsi'
4687 * @return GNUNET_OK to keep the connection open,
4688 * GNUNET_SYSERR to close it (signal serious error)
4691 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4692 const struct GNUNET_MessageHeader *message,
4693 const struct GNUNET_ATS_Information *atsi,
4694 unsigned int atsi_count)
4696 struct GNUNET_MESH_TunnelDestroy *msg;
4697 struct MeshTunnel *t;
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4700 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4701 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4703 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4704 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4707 /* Probably already got the message from another path,
4708 * destroyed the tunnel and retransmitted to children.
4711 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4714 if (t->id.oid == myid)
4716 GNUNET_break_op (0);
4719 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4721 /* Tunnel was incoming, notify clients */
4722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4723 t->local_tid, t->local_tid_dest);
4724 send_clients_tunnel_destroy (t);
4726 tunnel_send_destroy (t);
4727 t->destroy = GNUNET_YES;
4728 // TODO: add timeout to destroy the tunnel anyway
4734 * Core handler for mesh network traffic going from the origin to a peer
4736 * @param cls closure
4737 * @param peer peer identity this notification is about
4738 * @param message message
4739 * @param atsi performance data
4740 * @param atsi_count number of records in 'atsi'
4741 * @return GNUNET_OK to keep the connection open,
4742 * GNUNET_SYSERR to close it (signal serious error)
4745 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4746 const struct GNUNET_MessageHeader *message,
4747 const struct GNUNET_ATS_Information *atsi,
4748 unsigned int atsi_count)
4750 struct GNUNET_MESH_Unicast *msg;
4751 struct GNUNET_PeerIdentity *neighbor;
4752 struct MeshTunnelChildInfo *cinfo;
4753 struct MeshTunnel *t;
4754 GNUNET_PEER_Id dest_id;
4759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4761 size = ntohs (message->size);
4763 sizeof (struct GNUNET_MESH_Unicast) +
4764 sizeof (struct GNUNET_MessageHeader))
4769 msg = (struct GNUNET_MESH_Unicast *) message;
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4771 ntohs (msg[1].header.type));
4772 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4775 /* TODO notify back: we don't know this tunnel */
4776 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4777 GNUNET_break_op (0);
4780 pid = ntohl (msg->pid);
4783 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4784 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4785 " Already seen pid %u, DROPPING!\n", pid);
4790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4791 " pid %u not seen yet, forwarding\n", pid);
4793 t->skip += (pid - t->pid) - 1;
4795 tunnel_reset_timeout (t);
4796 dest_id = GNUNET_PEER_search (&msg->destination);
4797 if (dest_id == myid)
4799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4800 " it's for us! sending to clients...\n");
4801 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4802 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4803 // FIXME send after client processes the packet
4804 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4807 ttl = ntohl (msg->ttl);
4808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4811 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4812 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4816 " not for us, retransmitting...\n");
4817 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4819 neighbor = tree_get_first_hop (t->tree, dest_id);
4820 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4821 &neighbor->hashPubKey);
4824 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
4825 cinfo->id = GNUNET_PEER_intern (neighbor);
4827 cinfo->max_pid = pid + t->queue_max - t->queue_n; // FIXME review
4829 GNUNET_assert (GNUNET_OK ==
4830 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
4831 &neighbor->hashPubKey,
4833 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
4836 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4839 send_message (message, neighbor, t);
4845 * Core handler for mesh network traffic going from the origin to all peers
4847 * @param cls closure
4848 * @param message message
4849 * @param peer peer identity this notification is about
4850 * @param atsi performance data
4851 * @param atsi_count number of records in 'atsi'
4852 * @return GNUNET_OK to keep the connection open,
4853 * GNUNET_SYSERR to close it (signal serious error)
4855 * TODO: Check who we got this from, to validate route.
4858 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4859 const struct GNUNET_MessageHeader *message,
4860 const struct GNUNET_ATS_Information *atsi,
4861 unsigned int atsi_count)
4863 struct GNUNET_MESH_Multicast *msg;
4864 struct MeshTunnel *t;
4868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
4870 size = ntohs (message->size);
4871 if (sizeof (struct GNUNET_MESH_Multicast) +
4872 sizeof (struct GNUNET_MessageHeader) > size)
4874 GNUNET_break_op (0);
4877 msg = (struct GNUNET_MESH_Multicast *) message;
4878 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4882 /* TODO notify that we dont know that tunnel */
4883 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4884 GNUNET_break_op (0);
4887 pid = ntohl (msg->pid);
4890 /* already seen this packet, drop */
4891 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4892 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4893 " Already seen pid %u, DROPPING!\n", pid);
4898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4899 " pid %u not seen yet, forwarding\n", pid);
4901 t->skip += (pid - t->pid) - 1;
4903 tunnel_reset_timeout (t);
4905 /* Transmit to locally interested clients */
4906 if (NULL != t->peers &&
4907 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
4909 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
4910 send_subscribed_clients (message, &msg[1].header);
4912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
4913 if (ntohl (msg->ttl) == 0)
4915 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4916 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4919 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
4920 tunnel_send_multicast (t, message, GNUNET_NO);
4926 * Core handler for mesh network traffic toward the owner of a tunnel
4928 * @param cls closure
4929 * @param message message
4930 * @param peer peer identity this notification is about
4931 * @param atsi performance data
4932 * @param atsi_count number of records in 'atsi'
4934 * @return GNUNET_OK to keep the connection open,
4935 * GNUNET_SYSERR to close it (signal serious error)
4938 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4939 const struct GNUNET_MessageHeader *message,
4940 const struct GNUNET_ATS_Information *atsi,
4941 unsigned int atsi_count)
4943 struct GNUNET_MESH_ToOrigin *msg;
4944 struct GNUNET_PeerIdentity id;
4945 struct MeshPeerInfo *peer_info;
4946 struct MeshTunnel *t;
4949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4951 size = ntohs (message->size);
4952 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4953 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4955 GNUNET_break_op (0);
4958 msg = (struct GNUNET_MESH_ToOrigin *) message;
4959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4960 ntohs (msg[1].header.type));
4961 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4965 /* TODO notify that we dont know this tunnel (whom)? */
4966 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4967 GNUNET_break_op (0);
4971 if (t->id.oid == myid)
4974 struct GNUNET_MESH_ToOrigin *copy;
4976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4977 " it's for us! sending to clients...\n");
4978 if (NULL == t->owner)
4980 /* got data packet for ownerless tunnel */
4981 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
4983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4984 GNUNET_break_op (0);
4987 /* TODO signature verification */
4988 memcpy (cbuf, message, size);
4989 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4990 copy->tid = htonl (t->local_tid);
4991 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4992 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4993 ©->header, GNUNET_NO);
4996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4997 " not for us, retransmitting...\n");
4999 peer_info = peer_info_get (&msg->oid);
5000 if (NULL == peer_info)
5002 /* unknown origin of tunnel */
5006 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5007 send_message (message, &id, t);
5008 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5015 * Core handler for mesh network traffic point-to-point acks.
5017 * @param cls closure
5018 * @param message message
5019 * @param peer peer identity this notification is about
5020 * @param atsi performance data
5021 * @param atsi_count number of records in 'atsi'
5023 * @return GNUNET_OK to keep the connection open,
5024 * GNUNET_SYSERR to close it (signal serious error)
5027 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5028 const struct GNUNET_MessageHeader *message,
5029 const struct GNUNET_ATS_Information *atsi,
5030 unsigned int atsi_count)
5032 struct GNUNET_MESH_ACK *msg;
5033 struct MeshTunnelChildInfo *cinfo;
5034 struct MeshTunnel *t;
5037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got an ACK packet from %s\n",
5039 msg = (struct GNUNET_MESH_ACK *) message;
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5041 ntohs (msg[1].header.type));
5042 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5046 /* TODO notify that we dont know this tunnel (whom)? */
5047 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5048 GNUNET_break_op (0);
5051 ack = ntohl (msg->pid);
5052 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
5056 GNUNET_break_op (0);
5059 cinfo->max_pid = ack;
5060 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5066 * Core handler for path ACKs
5068 * @param cls closure
5069 * @param message message
5070 * @param peer peer identity this notification is about
5071 * @param atsi performance data
5072 * @param atsi_count number of records in 'atsi'
5074 * @return GNUNET_OK to keep the connection open,
5075 * GNUNET_SYSERR to close it (signal serious error)
5078 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5079 const struct GNUNET_MessageHeader *message,
5080 const struct GNUNET_ATS_Information *atsi,
5081 unsigned int atsi_count)
5083 struct GNUNET_MESH_PathACK *msg;
5084 struct GNUNET_PeerIdentity id;
5085 struct MeshPeerInfo *peer_info;
5086 struct MeshPeerPath *p;
5087 struct MeshTunnel *t;
5089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5090 GNUNET_i2s (&my_full_id));
5091 msg = (struct GNUNET_MESH_PathACK *) message;
5092 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5095 /* TODO notify that we don't know the tunnel */
5096 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5098 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5102 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5104 peer_info = peer_info_get (&msg->peer_id);
5105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5106 GNUNET_i2s (&msg->peer_id));
5107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5110 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5113 "connect_by_string completed, stopping search\n");
5114 regex_cancel_search (t->regex_ctx);
5115 t->regex_ctx = NULL;
5118 /* Add paths to peers? */
5119 p = tree_get_path_to_peer (t->tree, peer_info->id);
5122 path_add_to_peers (p, GNUNET_YES);
5130 /* Message for us? */
5131 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5134 if (NULL == t->owner)
5136 GNUNET_break_op (0);
5139 if (NULL != t->dht_get_type)
5141 GNUNET_DHT_get_stop (t->dht_get_type);
5142 t->dht_get_type = NULL;
5144 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5146 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5147 send_client_peer_connected (t, peer_info->id);
5152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5153 " not for us, retransmitting...\n");
5154 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5155 peer_info = peer_info_get (&msg->oid);
5156 if (NULL == peer_info)
5158 /* If we know the tunnel, we should DEFINITELY know the peer */
5162 send_message (message, &id, t);
5168 * Functions to handle messages from core
5170 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5171 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5172 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5173 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5174 sizeof (struct GNUNET_MESH_PathBroken)},
5175 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5176 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5177 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5178 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5179 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5180 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5181 sizeof (struct GNUNET_MESH_ACK)},
5182 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5183 sizeof (struct GNUNET_MESH_PathACK)},
5189 /******************************************************************************/
5190 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5191 /******************************************************************************/
5194 * deregister_app: iterator for removing each application registered by a client
5196 * @param cls closure
5197 * @param key the hash of the application id (used to access the hashmap)
5198 * @param value the value stored at the key (client)
5200 * @return GNUNET_OK on success
5203 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5205 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5206 GNUNET_break (GNUNET_YES ==
5207 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5213 * notify_client_connection_failure: notify a client that the connection to the
5214 * requested remote peer is not possible (for instance, no route found)
5215 * Function called when the socket is ready to queue more data. "buf" will be
5216 * NULL and "size" zero if the socket was closed for writing in the meantime.
5218 * @param cls closure
5219 * @param size number of bytes available in buf
5220 * @param buf where the callee should write the message
5221 * @return number of bytes written to buf
5224 notify_client_connection_failure (void *cls, size_t size, void *buf)
5227 struct MeshPeerInfo *peer_info;
5228 struct GNUNET_MESH_PeerControl *msg;
5229 struct GNUNET_PeerIdentity id;
5231 if (0 == size && NULL == buf)
5233 // TODO retry? cancel?
5237 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5238 peer_info = (struct MeshPeerInfo *) cls;
5239 msg = (struct GNUNET_MESH_PeerControl *) buf;
5240 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5241 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5242 // msg->tunnel_id = htonl(peer_info->t->tid);
5243 GNUNET_PEER_resolve (peer_info->id, &id);
5244 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5252 * Send keepalive packets for a peer
5254 * @param cls Closure (tunnel for which to send the keepalive).
5255 * @param tc Notification context.
5257 * TODO: implement explicit multicast keepalive?
5260 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5262 struct MeshTunnel *t = cls;
5263 struct GNUNET_MessageHeader *payload;
5264 struct GNUNET_MESH_Multicast *msg;
5266 sizeof (struct GNUNET_MESH_Multicast) +
5267 sizeof (struct GNUNET_MessageHeader);
5270 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5274 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5277 "sending keepalive for tunnel %d\n", t->id.tid);
5279 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5280 msg->header.size = htons (size);
5281 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5282 msg->oid = my_full_id;
5283 msg->tid = htonl (t->id.tid);
5284 msg->ttl = htonl (default_ttl);
5285 msg->pid = htonl (t->pid + 1);
5287 payload = (struct GNUNET_MessageHeader *) &msg[1];
5288 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5289 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5290 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5292 t->path_refresh_task =
5293 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5299 * Function to process paths received for a new peer addition. The recorded
5300 * paths form the initial tunnel, which can be optimized later.
5301 * Called on each result obtained for the DHT search.
5303 * @param cls closure
5304 * @param exp when will this value expire
5305 * @param key key of the result
5306 * @param get_path path of the get request
5307 * @param get_path_length lenght of get_path
5308 * @param put_path path of the put request
5309 * @param put_path_length length of the put_path
5310 * @param type type of the result
5311 * @param size number of bytes in data
5312 * @param data pointer to the result data
5314 * TODO: re-issue the request after certain time? cancel after X results?
5317 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5318 const struct GNUNET_HashCode * key,
5319 const struct GNUNET_PeerIdentity *get_path,
5320 unsigned int get_path_length,
5321 const struct GNUNET_PeerIdentity *put_path,
5322 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5323 size_t size, const void *data)
5325 struct MeshPathInfo *path_info = cls;
5326 struct MeshPeerPath *p;
5327 struct GNUNET_PeerIdentity pi;
5330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5331 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5334 p = path_build_from_dht (get_path, get_path_length, put_path,
5336 path_add_to_peers (p, GNUNET_NO);
5338 for (i = 0; i < path_info->peer->ntunnels; i++)
5340 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5341 peer_info_connect (path_info->peer, path_info->t);
5349 * Function to process paths received for a new peer addition. The recorded
5350 * paths form the initial tunnel, which can be optimized later.
5351 * Called on each result obtained for the DHT search.
5353 * @param cls closure
5354 * @param exp when will this value expire
5355 * @param key key of the result
5356 * @param get_path path of the get request
5357 * @param get_path_length lenght of get_path
5358 * @param put_path path of the put request
5359 * @param put_path_length length of the put_path
5360 * @param type type of the result
5361 * @param size number of bytes in data
5362 * @param data pointer to the result data
5365 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5366 const struct GNUNET_HashCode * key,
5367 const struct GNUNET_PeerIdentity *get_path,
5368 unsigned int get_path_length,
5369 const struct GNUNET_PeerIdentity *put_path,
5370 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5371 size_t size, const void *data)
5373 const struct PBlock *pb = data;
5374 const struct GNUNET_PeerIdentity *pi = &pb->id;
5375 struct MeshTunnel *t = cls;
5376 struct MeshPeerInfo *peer_info;
5377 struct MeshPeerPath *p;
5379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5380 if (size != sizeof (struct PBlock))
5382 GNUNET_break_op (0);
5385 if (ntohl(pb->type) != t->type)
5387 GNUNET_break_op (0);
5390 GNUNET_assert (NULL != t->owner);
5391 peer_info = peer_info_get (pi);
5392 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5394 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5396 p = path_build_from_dht (get_path, get_path_length, put_path,
5398 path_add_to_peers (p, GNUNET_NO);
5400 tunnel_add_peer (t, peer_info);
5401 peer_info_connect (peer_info, t);
5406 * Function to process DHT string to regex matching.
5407 * Called on each result obtained for the DHT search.
5409 * @param cls closure (search context)
5410 * @param exp when will this value expire
5411 * @param key key of the result
5412 * @param get_path path of the get request (not used)
5413 * @param get_path_length lenght of get_path (not used)
5414 * @param put_path path of the put request (not used)
5415 * @param put_path_length length of the put_path (not used)
5416 * @param type type of the result
5417 * @param size number of bytes in data
5418 * @param data pointer to the result data
5421 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5422 const struct GNUNET_HashCode * key,
5423 const struct GNUNET_PeerIdentity *get_path,
5424 unsigned int get_path_length,
5425 const struct GNUNET_PeerIdentity *put_path,
5426 unsigned int put_path_length,
5427 enum GNUNET_BLOCK_Type type,
5428 size_t size, const void *data)
5430 const struct MeshRegexAccept *block = data;
5431 struct MeshRegexSearchContext *ctx = cls;
5432 struct MeshRegexSearchInfo *info = ctx->info;
5433 struct MeshPeerPath *p;
5434 struct MeshPeerInfo *peer_info;
5436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5439 peer_info = peer_info_get(&block->id);
5440 p = path_build_from_dht (get_path, get_path_length, put_path,
5442 path_add_to_peers (p, GNUNET_NO);
5445 tunnel_add_peer (info->t, peer_info);
5446 peer_info_connect (peer_info, info->t);
5447 if (0 == info->peer)
5449 info->peer = peer_info->id;
5453 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5456 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5457 ®ex_connect_timeout,
5465 * Function to process DHT string to regex matching.
5466 * Called on each result obtained for the DHT search.
5468 * @param cls closure (search context)
5469 * @param exp when will this value expire
5470 * @param key key of the result
5471 * @param get_path path of the get request (not used)
5472 * @param get_path_length lenght of get_path (not used)
5473 * @param put_path path of the put request (not used)
5474 * @param put_path_length length of the put_path (not used)
5475 * @param type type of the result
5476 * @param size number of bytes in data
5477 * @param data pointer to the result data
5479 * TODO: re-issue the request after certain time? cancel after X results?
5482 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5483 const struct GNUNET_HashCode * key,
5484 const struct GNUNET_PeerIdentity *get_path,
5485 unsigned int get_path_length,
5486 const struct GNUNET_PeerIdentity *put_path,
5487 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5488 size_t size, const void *data)
5490 const struct MeshRegexBlock *block = data;
5491 struct MeshRegexSearchContext *ctx = cls;
5492 struct MeshRegexSearchInfo *info = ctx->info;
5496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5497 "DHT GET STRING RETURNED RESULTS\n");
5498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5499 " key: %s\n", GNUNET_h2s (key));
5501 copy = GNUNET_malloc (size);
5502 memcpy (copy, data, size);
5503 GNUNET_break (GNUNET_OK ==
5504 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5506 len = ntohl (block->n_proof);
5508 char proof[len + 1];
5510 memcpy (proof, &block[1], len);
5512 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5514 GNUNET_break_op (0);
5518 len = strlen (info->description);
5519 if (len == ctx->position) // String processed
5521 if (GNUNET_YES == ntohl (block->accepting))
5523 regex_find_path(key, ctx);
5527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5528 // FIXME REGEX this block not successful, wait for more? start timeout?
5532 GNUNET_break (GNUNET_OK ==
5533 GNUNET_MESH_regex_block_iterate (block, size,
5534 ®ex_edge_iterator, ctx));
5538 /******************************************************************************/
5539 /********************* MESH LOCAL HANDLES **************************/
5540 /******************************************************************************/
5544 * Handler for client disconnection
5546 * @param cls closure
5547 * @param client identification of the client; NULL
5548 * for the last call when the server is destroyed
5551 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5553 struct MeshClient *c;
5554 struct MeshClient *next;
5557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5566 if (c->handle != client)
5568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5574 GNUNET_SERVER_client_drop (c->handle);
5575 c->shutting_down = GNUNET_YES;
5576 GNUNET_assert (NULL != c->own_tunnels);
5577 GNUNET_assert (NULL != c->incoming_tunnels);
5578 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5579 &tunnel_destroy_iterator, c);
5580 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5581 &tunnel_destroy_iterator, c);
5582 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5583 &tunnel_destroy_iterator, c);
5584 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5585 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5586 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5588 /* deregister clients applications */
5589 if (NULL != c->apps)
5591 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5592 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5594 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5595 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5597 GNUNET_SCHEDULER_cancel (announce_applications_task);
5598 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5600 if (NULL != c->types)
5601 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5602 for (i = 0; i < c->n_regex; i++)
5604 GNUNET_free (c->regexes[i]);
5606 GNUNET_free_non_null (c->regexes);
5607 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5608 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5610 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5613 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5622 * Handler for new clients
5624 * @param cls closure
5625 * @param client identification of the client
5626 * @param message the actual message, which includes messages the client wants
5629 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5630 const struct GNUNET_MessageHeader *message)
5632 struct GNUNET_MESH_ClientConnect *cc_msg;
5633 struct MeshClient *c;
5634 GNUNET_MESH_ApplicationType *a;
5641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5642 /* Check data sanity */
5643 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5644 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5645 ntypes = ntohs (cc_msg->types);
5646 napps = ntohs (cc_msg->applications);
5648 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5651 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5655 /* Create new client structure */
5656 c = GNUNET_malloc (sizeof (struct MeshClient));
5657 c->id = next_client_id++;
5658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5660 GNUNET_SERVER_client_keep (client);
5661 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5664 GNUNET_MESH_ApplicationType at;
5665 struct GNUNET_HashCode hc;
5667 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5668 for (i = 0; i < napps; i++)
5671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5672 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5673 /* store in clients hashmap */
5674 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5676 /* store in global hashmap, for announcements */
5677 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5678 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5680 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5681 announce_applications_task =
5682 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5688 struct GNUNET_HashCode hc;
5690 t = (uint16_t *) & a[napps];
5691 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5692 for (i = 0; i < ntypes; i++)
5695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5696 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5698 /* store in clients hashmap */
5699 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5700 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5701 /* store in global hashmap */
5702 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5703 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5707 " client has %u+%u subscriptions\n", napps, ntypes);
5709 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5710 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5711 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5712 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5713 GNUNET_SERVER_notification_context_add (nc, client);
5714 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5716 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5722 * Handler for clients announcing available services by a regular expression.
5724 * @param cls closure
5725 * @param client identification of the client
5726 * @param message the actual message, which includes messages the client wants
5729 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5730 const struct GNUNET_MessageHeader *message)
5732 struct MeshClient *c;
5736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5738 /* Sanity check for client registration */
5739 if (NULL == (c = client_get (client)))
5742 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5747 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5748 regex = GNUNET_malloc (len + 1);
5749 memcpy (regex, &message[1], len);
5751 GNUNET_array_append (c->regexes, c->n_regex, regex);
5752 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5754 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5760 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5766 * Handler for requests of new tunnels
5768 * @param cls closure
5769 * @param client identification of the client
5770 * @param message the actual message
5773 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5774 const struct GNUNET_MessageHeader *message)
5776 struct GNUNET_MESH_TunnelMessage *t_msg;
5777 struct MeshTunnel *t;
5778 struct MeshClient *c;
5780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5782 /* Sanity check for client registration */
5783 if (NULL == (c = client_get (client)))
5786 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5791 /* Message sanity check */
5792 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5799 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5800 /* Sanity check for tunnel numbering */
5801 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5807 /* Sanity check for duplicate tunnel IDs */
5808 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
5811 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5815 while (NULL != tunnel_get_by_pi (myid, next_tid))
5816 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5817 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
5820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5825 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5827 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5828 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
5830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5831 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5837 * Handler for requests of deleting tunnels
5839 * @param cls closure
5840 * @param client identification of the client
5841 * @param message the actual message
5844 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5845 const struct GNUNET_MessageHeader *message)
5847 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5848 struct MeshClient *c;
5849 struct MeshTunnel *t;
5850 MESH_TunnelNumber tid;
5852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5853 "Got a DESTROY TUNNEL from client!\n");
5855 /* Sanity check for client registration */
5856 if (NULL == (c = client_get (client)))
5859 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5862 /* Message sanity check */
5863 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5866 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5870 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5872 /* Retrieve tunnel */
5873 tid = ntohl (tunnel_msg->tunnel_id);
5874 t = tunnel_get_by_local_id(c, tid);
5877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5879 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5882 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5884 client_ignore_tunnel (c, t);
5886 // TODO: when to destroy incoming tunnel?
5887 if (t->nclients == 0)
5889 GNUNET_assert (GNUNET_YES ==
5890 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
5892 GNUNET_assert (GNUNET_YES ==
5893 GNUNET_CONTAINER_multihashmap_remove (t->peers,
5894 &my_full_id.hashPubKey,
5898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5901 send_client_tunnel_disconnect(t, c);
5902 client_delete_tunnel(c, t);
5904 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5906 tunnel_send_destroy (t);
5907 t->destroy = GNUNET_YES;
5908 // The tunnel will be destroyed when the last message is transmitted.
5909 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5915 * Handler for requests of seeting tunnel's speed.
5917 * @param cls Closure (unused).
5918 * @param client Identification of the client.
5919 * @param message The actual message.
5922 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
5923 const struct GNUNET_MessageHeader *message)
5925 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5926 struct MeshClient *c;
5927 struct MeshTunnel *t;
5928 MESH_TunnelNumber tid;
5930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5931 "Got a SPEED request from client!\n");
5933 /* Sanity check for client registration */
5934 if (NULL == (c = client_get (client)))
5937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5942 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5944 /* Retrieve tunnel */
5945 tid = ntohl (tunnel_msg->tunnel_id);
5946 t = tunnel_get_by_local_id(c, tid);
5949 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
5951 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5955 switch (ntohs(message->type))
5957 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
5958 t->speed_min = GNUNET_YES;
5960 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
5961 t->speed_min = GNUNET_NO;
5966 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5971 * Handler for requests of seeting tunnel's buffering policy.
5973 * @param cls Closure (unused).
5974 * @param client Identification of the client.
5975 * @param message The actual message.
5978 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5979 const struct GNUNET_MessageHeader *message)
5981 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5982 struct MeshClient *c;
5983 struct MeshTunnel *t;
5984 MESH_TunnelNumber tid;
5986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5987 "Got a BUFFER request from client!\n");
5989 /* Sanity check for client registration */
5990 if (NULL == (c = client_get (client)))
5993 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5998 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6000 /* Retrieve tunnel */
6001 tid = ntohl (tunnel_msg->tunnel_id);
6002 t = tunnel_get_by_local_id(c, tid);
6005 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6007 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6011 switch (ntohs(message->type))
6013 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6014 t->nobuffer = GNUNET_NO;
6016 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6017 t->nobuffer = GNUNET_YES;
6023 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6028 * Handler for connection requests to new peers
6030 * @param cls closure
6031 * @param client identification of the client
6032 * @param message the actual message (PeerControl)
6035 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6036 const struct GNUNET_MessageHeader *message)
6038 struct GNUNET_MESH_PeerControl *peer_msg;
6039 struct MeshPeerInfo *peer_info;
6040 struct MeshClient *c;
6041 struct MeshTunnel *t;
6042 MESH_TunnelNumber tid;
6044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6045 /* Sanity check for client registration */
6046 if (NULL == (c = client_get (client)))
6049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6053 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6054 /* Sanity check for message size */
6055 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6062 /* Tunnel exists? */
6063 tid = ntohl (peer_msg->tunnel_id);
6064 t = tunnel_get_by_local_id (c, tid);
6068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6072 /* Does client own tunnel? */
6073 if (t->owner->handle != client)
6076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6080 GNUNET_i2s (&peer_msg->peer));
6081 peer_info = peer_info_get (&peer_msg->peer);
6083 tunnel_add_peer (t, peer_info);
6084 peer_info_connect (peer_info, t);
6086 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6092 * Handler for disconnection requests of peers in a tunnel
6094 * @param cls closure
6095 * @param client identification of the client
6096 * @param message the actual message (PeerControl)
6099 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6100 const struct GNUNET_MessageHeader *message)
6102 struct GNUNET_MESH_PeerControl *peer_msg;
6103 struct MeshPeerInfo *peer_info;
6104 struct MeshClient *c;
6105 struct MeshTunnel *t;
6106 MESH_TunnelNumber tid;
6108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6109 /* Sanity check for client registration */
6110 if (NULL == (c = client_get (client)))
6113 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6116 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6117 /* Sanity check for message size */
6118 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6125 /* Tunnel exists? */
6126 tid = ntohl (peer_msg->tunnel_id);
6127 t = tunnel_get_by_local_id (c, tid);
6131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6136 /* Does client own tunnel? */
6137 if (t->owner->handle != client)
6140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6145 GNUNET_i2s (&peer_msg->peer));
6146 /* Is the peer in the tunnel? */
6148 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6149 if (NULL == peer_info)
6152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6156 /* Ok, delete peer from tunnel */
6157 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6158 &peer_msg->peer.hashPubKey);
6160 send_destroy_path (t, peer_info->id);
6161 tunnel_delete_peer (t, peer_info->id);
6162 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6167 * Handler for blacklist requests of peers in a tunnel
6169 * @param cls closure
6170 * @param client identification of the client
6171 * @param message the actual message (PeerControl)
6174 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6175 const struct GNUNET_MessageHeader *message)
6177 struct GNUNET_MESH_PeerControl *peer_msg;
6178 struct MeshClient *c;
6179 struct MeshTunnel *t;
6180 MESH_TunnelNumber tid;
6182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6183 /* Sanity check for client registration */
6184 if (NULL == (c = client_get (client)))
6187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6190 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6192 /* Sanity check for message size */
6193 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6196 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6200 /* Tunnel exists? */
6201 tid = ntohl (peer_msg->tunnel_id);
6202 t = tunnel_get_by_local_id (c, tid);
6206 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6211 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6212 GNUNET_PEER_intern(&peer_msg->peer));
6217 * Handler for unblacklist requests of peers in a tunnel
6219 * @param cls closure
6220 * @param client identification of the client
6221 * @param message the actual message (PeerControl)
6224 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6225 const struct GNUNET_MessageHeader *message)
6227 struct GNUNET_MESH_PeerControl *peer_msg;
6228 struct MeshClient *c;
6229 struct MeshTunnel *t;
6230 MESH_TunnelNumber tid;
6234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6235 /* Sanity check for client registration */
6236 if (NULL == (c = client_get (client)))
6239 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6242 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6244 /* Sanity check for message size */
6245 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6252 /* Tunnel exists? */
6253 tid = ntohl (peer_msg->tunnel_id);
6254 t = tunnel_get_by_local_id (c, tid);
6258 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6263 /* if peer is not known, complain */
6264 pid = GNUNET_PEER_search (&peer_msg->peer);
6271 /* search and remove from list */
6272 for (i = 0; i < t->nblacklisted; i++)
6274 if (t->blacklisted[i] == pid)
6276 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6277 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6282 /* if peer hasn't been blacklisted, complain */
6288 * Handler for connection requests to new peers by type
6290 * @param cls closure
6291 * @param client identification of the client
6292 * @param message the actual message (ConnectPeerByType)
6295 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6296 const struct GNUNET_MessageHeader *message)
6298 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6299 struct MeshClient *c;
6300 struct MeshTunnel *t;
6301 struct GNUNET_HashCode hash;
6302 MESH_TunnelNumber tid;
6304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6305 /* Sanity check for client registration */
6306 if (NULL == (c = client_get (client)))
6309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6313 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6314 /* Sanity check for message size */
6315 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6316 ntohs (connect_msg->header.size))
6319 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6323 /* Tunnel exists? */
6324 tid = ntohl (connect_msg->tunnel_id);
6325 t = tunnel_get_by_local_id (c, tid);
6329 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6333 /* Does client own tunnel? */
6334 if (t->owner->handle != client)
6337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6341 /* Do WE have the service? */
6342 t->type = ntohl (connect_msg->type);
6343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6344 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6345 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6348 /* Yes! Fast forward, add ourselves to the tunnel and send the
6349 * good news to the client, and alert the destination client of
6350 * an incoming tunnel.
6352 * FIXME send a path create to self, avoid code duplication
6354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6355 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6356 peer_info_get (&my_full_id),
6357 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6360 send_client_peer_connected (t, myid);
6361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6362 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6364 t->local_tid_dest = next_local_tid++;
6365 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6366 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6367 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6371 /* Ok, lets find a peer offering the service */
6372 if (NULL != t->dht_get_type)
6374 GNUNET_DHT_get_stop (t->dht_get_type);
6376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6377 GNUNET_h2s (&hash));
6379 GNUNET_DHT_get_start (dht_handle,
6380 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6382 dht_replication_level,
6383 GNUNET_DHT_RO_RECORD_ROUTE |
6384 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6386 &dht_get_type_handler, t);
6388 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6394 * Handler for connection requests to new peers by a string service description.
6396 * @param cls closure
6397 * @param client identification of the client
6398 * @param message the actual message, which includes messages the client wants
6401 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6402 const struct GNUNET_MessageHeader *message)
6404 struct GNUNET_MESH_ConnectPeerByString *msg;
6405 struct MeshRegexSearchContext *ctx;
6406 struct MeshRegexSearchInfo *info;
6407 struct GNUNET_DHT_GetHandle *get_h;
6408 struct GNUNET_HashCode key;
6409 struct MeshTunnel *t;
6410 struct MeshClient *c;
6411 MESH_TunnelNumber tid;
6415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6416 "Connect by string started\n");
6417 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6418 size = htons (message->size);
6420 /* Sanity check for client registration */
6421 if (NULL == (c = client_get (client)))
6424 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6428 /* Message size sanity check */
6429 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6436 /* Tunnel exists? */
6437 tid = ntohl (msg->tunnel_id);
6438 t = tunnel_get_by_local_id (c, tid);
6442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6446 /* Does client own tunnel? */
6447 if (t->owner->handle != client)
6450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6455 " on tunnel %s [%u]\n",
6456 GNUNET_i2s(&my_full_id),
6459 /* Only one connect_by_string allowed at the same time! */
6460 /* FIXME: allow more, return handle at api level to cancel, document */
6461 if (NULL != t->regex_ctx)
6464 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6468 /* Find string itself */
6469 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6470 string = (const char *) &msg[1];
6472 /* Initialize context */
6473 size = GNUNET_REGEX_get_first_key(string, len, &key);
6474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6475 " consumed %u bits out of %u\n", size, len);
6476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6477 " looking for %s\n", GNUNET_h2s (&key));
6479 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6481 info->description = GNUNET_malloc (len + 1);
6482 memcpy (info->description, string, len);
6483 info->description[len] = '\0';
6484 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6485 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6488 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6489 ctx->position = size;
6493 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6495 /* Start search in DHT */
6496 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6497 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6498 &key, /* key to search */
6499 dht_replication_level, /* replication level */
6500 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6501 NULL, /* xquery */ // FIXME BLOOMFILTER
6502 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6503 &dht_get_string_handler, ctx);
6505 GNUNET_break (GNUNET_OK ==
6506 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6509 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6511 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6517 * Handler for client traffic directed to one peer
6519 * @param cls closure
6520 * @param client identification of the client
6521 * @param message the actual message
6524 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6525 const struct GNUNET_MessageHeader *message)
6527 struct MeshClient *c;
6528 struct MeshTunnel *t;
6529 struct MeshPeerInfo *pi;
6530 struct GNUNET_MESH_Unicast *data_msg;
6531 MESH_TunnelNumber tid;
6534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6535 "Got a unicast request from a client!\n");
6537 /* Sanity check for client registration */
6538 if (NULL == (c = client_get (client)))
6541 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6544 data_msg = (struct GNUNET_MESH_Unicast *) message;
6545 /* Sanity check for message size */
6546 size = ntohs (message->size);
6547 if (sizeof (struct GNUNET_MESH_Unicast) +
6548 sizeof (struct GNUNET_MessageHeader) > size)
6551 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6555 /* Tunnel exists? */
6556 tid = ntohl (data_msg->tid);
6557 t = tunnel_get_by_local_id (c, tid);
6561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6565 /* Is it a local tunnel? Then, does client own the tunnel? */
6566 if (t->owner->handle != client)
6569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6573 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6574 &data_msg->destination.hashPubKey);
6575 /* Is the selected peer in the tunnel? */
6579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6583 /* Ok, everything is correct, send the message
6584 * (pretend we got it from a mesh peer)
6587 char buf[ntohs (message->size)] GNUNET_ALIGN;
6588 struct GNUNET_MESH_Unicast *copy;
6590 /* Work around const limitation */
6591 copy = (struct GNUNET_MESH_Unicast *) buf;
6592 memcpy (buf, data_msg, size);
6593 copy->oid = my_full_id;
6594 copy->tid = htonl (t->id.tid);
6595 copy->ttl = htonl (default_ttl);
6596 copy->pid = htonl (t->pid + 1);
6597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6598 " calling generic handler...\n");
6599 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6602 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6608 * Handler for client traffic directed to the origin
6610 * @param cls closure
6611 * @param client identification of the client
6612 * @param message the actual message
6615 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6616 const struct GNUNET_MessageHeader *message)
6618 struct GNUNET_MESH_ToOrigin *data_msg;
6619 struct GNUNET_PeerIdentity id;
6620 struct MeshClient *c;
6621 struct MeshTunnel *t;
6622 MESH_TunnelNumber tid;
6625 /* Sanity check for client registration */
6626 if (NULL == (c = client_get (client)))
6629 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6632 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6633 /* Sanity check for message size */
6634 size = ntohs (message->size);
6635 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6636 sizeof (struct GNUNET_MessageHeader) > size)
6639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6643 /* Tunnel exists? */
6644 tid = ntohl (data_msg->tid);
6645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6646 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6647 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6650 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6653 t = tunnel_get_by_local_id (c, tid);
6657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6661 /* It should be sent by someone who has this as incoming tunnel. */
6662 if (-1 == client_knows_tunnel (c, t))
6665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6668 GNUNET_PEER_resolve (t->id.oid, &id);
6670 /* Ok, everything is correct, send the message
6671 * (pretend we got it from a mesh peer)
6674 char buf[ntohs (message->size)] GNUNET_ALIGN;
6675 struct GNUNET_MESH_ToOrigin *copy;
6677 /* Work around const limitation */
6678 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6679 memcpy (buf, data_msg, size);
6681 copy->tid = htonl (t->id.tid);
6682 copy->sender = my_full_id;
6683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6684 " calling generic handler...\n");
6685 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6687 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6693 * Handler for client traffic directed to all peers in a tunnel
6695 * @param cls closure
6696 * @param client identification of the client
6697 * @param message the actual message
6700 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6701 const struct GNUNET_MessageHeader *message)
6703 struct MeshClient *c;
6704 struct MeshTunnel *t;
6705 struct GNUNET_MESH_Multicast *data_msg;
6706 MESH_TunnelNumber tid;
6708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6709 "Got a multicast request from a client!\n");
6711 /* Sanity check for client registration */
6712 if (NULL == (c = client_get (client)))
6715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6718 data_msg = (struct GNUNET_MESH_Multicast *) message;
6719 /* Sanity check for message size */
6720 if (sizeof (struct GNUNET_MESH_Multicast) +
6721 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6728 /* Tunnel exists? */
6729 tid = ntohl (data_msg->tid);
6730 t = tunnel_get_by_local_id (c, tid);
6734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6738 /* Does client own tunnel? */
6739 if (t->owner->handle != client)
6742 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6747 char buf[ntohs (message->size)] GNUNET_ALIGN;
6748 struct GNUNET_MESH_Multicast *copy;
6750 copy = (struct GNUNET_MESH_Multicast *) buf;
6751 memcpy (buf, message, ntohs (message->size));
6752 copy->oid = my_full_id;
6753 copy->tid = htonl (t->id.tid);
6754 copy->ttl = htonl (default_ttl);
6755 copy->pid = htonl (t->pid + 1);
6756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6757 " calling generic handler...\n");
6758 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
6761 /* receive done gets called when last copy is sent to a neighbor */
6767 * Handler for client's ACKs for payload traffic.
6769 * @param cls Closure (unused).
6770 * @param client Identification of the client.
6771 * @param message The actual message.
6774 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6775 const struct GNUNET_MessageHeader *message)
6777 struct GNUNET_MESH_LocalAck *msg;
6778 struct MeshTunnel *t;
6779 struct MeshClient *c;
6780 MESH_TunnelNumber tid;
6782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6783 /* Sanity check for client registration */
6784 if (NULL == (c = client_get (client)))
6787 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6791 msg = (struct GNUNET_MESH_LocalAck *) message;
6792 /* Tunnel exists? */
6793 tid = ntohl (msg->tunnel_id);
6794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6795 t = tunnel_get_by_local_id (c, tid);
6799 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6803 /* Does client own tunnel? Is this and ACK for BCK traffic? */
6804 if (NULL != t->owner && t->owner->handle == client)
6811 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
6812 tunnel_set_client_fwd_ack (t, c, ntohl (msg->max_pid));
6813 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
6816 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6822 * Functions to handle messages from clients
6824 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6825 {&handle_local_new_client, NULL,
6826 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6827 {&handle_local_announce_regex, NULL,
6828 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
6829 {&handle_local_tunnel_create, NULL,
6830 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6831 sizeof (struct GNUNET_MESH_TunnelMessage)},
6832 {&handle_local_tunnel_destroy, NULL,
6833 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6834 sizeof (struct GNUNET_MESH_TunnelMessage)},
6835 {&handle_local_tunnel_speed, NULL,
6836 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
6837 sizeof (struct GNUNET_MESH_TunnelMessage)},
6838 {&handle_local_tunnel_speed, NULL,
6839 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
6840 sizeof (struct GNUNET_MESH_TunnelMessage)},
6841 {&handle_local_tunnel_buffer, NULL,
6842 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6843 sizeof (struct GNUNET_MESH_TunnelMessage)},
6844 {&handle_local_tunnel_buffer, NULL,
6845 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6846 sizeof (struct GNUNET_MESH_TunnelMessage)},
6847 {&handle_local_connect_add, NULL,
6848 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
6849 sizeof (struct GNUNET_MESH_PeerControl)},
6850 {&handle_local_connect_del, NULL,
6851 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
6852 sizeof (struct GNUNET_MESH_PeerControl)},
6853 {&handle_local_blacklist, NULL,
6854 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
6855 sizeof (struct GNUNET_MESH_PeerControl)},
6856 {&handle_local_unblacklist, NULL,
6857 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
6858 sizeof (struct GNUNET_MESH_PeerControl)},
6859 {&handle_local_connect_by_type, NULL,
6860 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
6861 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
6862 {&handle_local_connect_by_string, NULL,
6863 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
6864 {&handle_local_unicast, NULL,
6865 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6866 {&handle_local_to_origin, NULL,
6867 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6868 {&handle_local_multicast, NULL,
6869 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6870 {&handle_local_ack, NULL,
6871 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6872 sizeof (struct GNUNET_MESH_LocalAck)},
6878 * To be called on core init/fail.
6880 * @param cls service closure
6881 * @param server handle to the server for this service
6882 * @param identity the public identity of this peer
6885 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6886 const struct GNUNET_PeerIdentity *identity)
6888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6889 core_handle = server;
6890 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6893 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6894 GNUNET_SCHEDULER_shutdown ();
6901 * Method called whenever a given peer connects.
6903 * @param cls closure
6904 * @param peer peer identity this notification is about
6905 * @param atsi performance data for the connection
6906 * @param atsi_count number of records in 'atsi'
6909 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
6910 const struct GNUNET_ATS_Information *atsi,
6911 unsigned int atsi_count)
6913 struct MeshPeerInfo *peer_info;
6914 struct MeshPeerPath *path;
6916 DEBUG_CONN ("Peer connected\n");
6917 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6918 peer_info = peer_info_get (peer);
6919 if (myid == peer_info->id)
6921 DEBUG_CONN (" (self)\n");
6926 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6928 path = path_new (2);
6929 path->peers[0] = myid;
6930 path->peers[1] = peer_info->id;
6931 GNUNET_PEER_change_rc (myid, 1);
6932 GNUNET_PEER_change_rc (peer_info->id, 1);
6933 peer_info_add_path (peer_info, path, GNUNET_YES);
6934 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6940 * Method called whenever a peer disconnects.
6942 * @param cls closure
6943 * @param peer peer identity this notification is about
6946 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6948 struct MeshPeerInfo *pi;
6949 struct MeshPeerQueue *q;
6950 struct MeshPeerQueue *n;
6952 DEBUG_CONN ("Peer disconnected\n");
6953 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6965 /* try to reroute this traffic instead */
6966 queue_destroy(q, GNUNET_YES);
6970 peer_info_remove_path (pi, pi->id, myid);
6973 DEBUG_CONN (" (self)\n");
6975 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6980 /******************************************************************************/
6981 /************************ MAIN FUNCTIONS ****************************/
6982 /******************************************************************************/
6985 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6987 * @param cls closure
6988 * @param key current key code
6989 * @param value value in the hash map
6990 * @return GNUNET_YES if we should continue to iterate,
6994 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6996 struct MeshTunnel *t = value;
7003 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7005 * @param cls closure
7006 * @param key current key code
7007 * @param value value in the hash map
7008 * @return GNUNET_YES if we should continue to iterate,
7012 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7014 struct MeshPeerInfo *p = value;
7015 struct MeshPeerQueue *q;
7016 struct MeshPeerQueue *n;
7024 queue_destroy(q, GNUNET_YES);
7028 peer_info_destroy (p);
7033 * Task run during shutdown.
7039 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7043 if (core_handle != NULL)
7045 GNUNET_CORE_disconnect (core_handle);
7048 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7049 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7050 if (dht_handle != NULL)
7052 GNUNET_DHT_disconnect (dht_handle);
7057 GNUNET_SERVER_notification_context_destroy (nc);
7060 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7062 GNUNET_SCHEDULER_cancel (announce_id_task);
7063 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7069 * Process mesh requests.
7071 * @param cls closure
7072 * @param server the initialized server
7073 * @param c configuration to use
7076 run (void *cls, struct GNUNET_SERVER_Handle *server,
7077 const struct GNUNET_CONFIGURATION_Handle *c)
7079 struct MeshPeerInfo *peer;
7080 struct MeshPeerPath *p;
7083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7084 server_handle = server;
7085 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7086 NULL, /* Closure passed to MESH functions */
7087 &core_init, /* Call core_init once connected */
7088 &core_connect, /* Handle connects */
7089 &core_disconnect, /* remove peers on disconnects */
7090 NULL, /* Don't notify about all incoming messages */
7091 GNUNET_NO, /* For header only in notification */
7092 NULL, /* Don't notify about all outbound messages */
7093 GNUNET_NO, /* For header-only out notification */
7094 core_handlers); /* Register these handlers */
7096 if (core_handle == NULL)
7099 GNUNET_SCHEDULER_shutdown ();
7104 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7109 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7111 GNUNET_SCHEDULER_shutdown ();
7116 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7117 &refresh_path_time))
7119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7121 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7122 "refresh path time");
7123 GNUNET_SCHEDULER_shutdown ();
7128 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7129 &app_announce_time))
7131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7133 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7134 "app announce time");
7135 GNUNET_SCHEDULER_shutdown ();
7140 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7145 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7146 "id announce time");
7147 GNUNET_SCHEDULER_shutdown ();
7152 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7153 &unacknowledged_wait_time))
7155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7157 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7158 "unacknowledged wait time");
7159 GNUNET_SCHEDULER_shutdown ();
7164 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7169 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7171 GNUNET_SCHEDULER_shutdown ();
7176 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7179 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7181 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7183 GNUNET_SCHEDULER_shutdown ();
7188 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7193 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7195 GNUNET_SCHEDULER_shutdown ();
7200 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7205 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7211 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7212 &dht_replication_level))
7214 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7216 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7217 "dht replication level", 10);
7218 dht_replication_level = 10;
7222 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7223 GNUNET_free (keyfile);
7224 if (my_private_key == NULL)
7226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7227 _("Mesh service could not access hostkey. Exiting.\n"));
7228 GNUNET_SCHEDULER_shutdown ();
7231 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7232 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7233 &my_full_id.hashPubKey);
7234 myid = GNUNET_PEER_intern (&my_full_id);
7236 // transport_handle = GNUNET_TRANSPORT_connect(c,
7243 dht_handle = GNUNET_DHT_connect (c, 64);
7244 if (dht_handle == NULL)
7249 stats = GNUNET_STATISTICS_create ("mesh", c);
7253 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7255 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7256 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7257 peers = GNUNET_CONTAINER_multihashmap_create (32);
7258 applications = GNUNET_CONTAINER_multihashmap_create (32);
7259 types = GNUNET_CONTAINER_multihashmap_create (32);
7261 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7262 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7263 GNUNET_SERVER_disconnect_notify (server_handle,
7264 &handle_local_client_disconnect, NULL);
7268 clients_tail = NULL;
7271 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7272 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7274 /* Create a peer_info for the local peer */
7275 peer = peer_info_get (&my_full_id);
7278 GNUNET_PEER_change_rc (myid, 1);
7279 peer_info_add_path (peer, p, GNUNET_YES);
7281 /* Scheduled the task to clean up when shutdown is called */
7282 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7289 * The main function for the mesh service.
7291 * @param argc number of arguments from the command line
7292 * @param argv command line arguments
7293 * @return 0 ok, 1 on error
7296 main (int argc, char *const *argv)
7300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7303 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");