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 */
84 * Struct representing a piece of data being sent to other peers
88 /** Tunnel it belongs to. */
91 /** In case of a multicast, task to allow a client to send more data if
92 * some neighbor is too slow. */
93 GNUNET_SCHEDULER_TaskIdentifier *task;
95 /** How many remaining neighbors we need to send this to. */
96 unsigned int *reference_counter;
98 /** Size of the data. */
107 * Struct containing info about a queued transmission to this peer
114 struct MeshPeerQueue *next;
119 struct MeshPeerQueue *prev;
122 * Peer this transmission is directed to.
124 struct MeshPeerInfo *peer;
127 * Tunnel this message belongs to.
129 struct MeshTunnel *tunnel;
132 * Pointer to info stucture used as cls.
142 * Size of the message
149 * Struct containing all info possibly needed to build a package when called
152 struct MeshTransmissionDescriptor
154 /** ID of the tunnel this packet travels in */
155 struct MESH_TunnelID *origin;
157 /** Who was this message being sent to */
158 struct MeshPeerInfo *peer;
160 /** Ultimate destination of the packet */
161 GNUNET_PEER_Id destination;
163 /** Data descriptor */
164 struct MeshData* mesh_data;
169 * Struct containing all information regarding a given peer
179 * Last time we heard from this peer
181 struct GNUNET_TIME_Absolute last_contact;
184 * Number of attempts to reconnect so far
186 int n_reconnect_attempts;
189 * Paths to reach the peer, ordered by ascending hop count
191 struct MeshPeerPath *path_head;
194 * Paths to reach the peer, ordered by ascending hop count
196 struct MeshPeerPath *path_tail;
199 * Handle to stop the DHT search for a path to this peer
201 struct GNUNET_DHT_GetHandle *dhtget;
204 * Closure given to the DHT GET
206 struct MeshPathInfo *dhtgetcls;
209 * Array of tunnels this peer participates in
210 * (most probably a small amount, therefore not a hashmap)
211 * When the path to the peer changes, notify these tunnels to let them
212 * re-adjust their path trees.
214 struct MeshTunnel **tunnels;
217 * Number of tunnels this peers participates in
219 unsigned int ntunnels;
222 * Transmission queue to core DLL head
224 struct MeshPeerQueue *queue_head;
227 * Transmission queue to core DLL tail
229 struct MeshPeerQueue *queue_tail;
232 * How many messages are in the queue to this peer.
234 unsigned int queue_n;
237 * Handle to for queued transmissions
239 struct GNUNET_CORE_TransmitHandle *core_transmit;
244 * Globally unique tunnel identification (owner + number)
245 * DO NOT USE OVER THE NETWORK
250 * Node that owns the tunnel
255 * Tunnel number to differentiate all the tunnels owned by the node oid
256 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
258 MESH_TunnelNumber tid;
262 struct MeshClient; /* FWD declaration */
265 * Struct containing all information regarding a tunnel
266 * For an intermediate node the improtant info used will be:
267 * - id Tunnel unique identification
268 * - paths[0] To know where to send it next
269 * - metainfo: ready, speeds, accounting
276 struct MESH_TunnelID id;
279 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
281 MESH_TunnelNumber local_tid;
284 * Local tunnel number for local destination clients (incoming number)
285 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
288 MESH_TunnelNumber local_tid_dest;
291 * Local count ID of the last packet seen/sent.
296 * SKIP value for this tunnel.
301 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
303 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
306 * Last ACK sent towards the origin.
313 uint32_t max_child_ack;
316 * How many messages are in the queue.
318 unsigned int queue_n;
321 * How many messages do we accept in the queue.
323 unsigned int queue_max;
326 * Is the speed on the tunnel limited to the slowest peer?
331 * Is the tunnel bufferless (minimum latency)?
336 * Flag to signal the destruction of the tunnel.
337 * If this is set GNUNET_YES the tunnel will be destroyed
338 * when the queue is empty.
343 * Last time the tunnel was used
345 struct GNUNET_TIME_Absolute timestamp;
348 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
349 * containing peers added by id or by type, not intermediate peers.
351 struct GNUNET_CONTAINER_MultiHashMap *peers;
354 * Number of peers that are connected and potentially ready to receive data
356 unsigned int peers_ready;
359 * Number of peers that have been added to the tunnel
361 unsigned int peers_total;
364 * Client owner of the tunnel, if any
366 struct MeshClient *owner;
369 * Clients that have been informed about and want to stay in the tunnel.
371 struct MeshClient **clients;
374 * ACK value of each active client.
376 uint32_t *clients_acks;
379 * Number of elements in clients/clients_acks
381 unsigned int nclients;
384 * Clients that have been informed but requested to leave the tunnel.
386 struct MeshClient **ignore;
389 * Number of elements in clients
391 unsigned int nignore;
396 GNUNET_PEER_Id *blacklisted;
399 * Number of elements in blacklisted
401 unsigned int nblacklisted;
404 * Bloomfilter (for peer identities) to stop circular routes
406 char bloomfilter[MESH_BLOOM_SIZE];
411 struct MeshTunnelTree *tree;
414 * Application type we are looking for in this tunnel
416 GNUNET_MESH_ApplicationType type;
419 * Used to search peers offering a service
421 struct GNUNET_DHT_GetHandle *dht_get_type;
424 * Initial context of the regex search for a connect_by_string
426 struct MeshRegexSearchContext *regex_ctx;
429 * Task to keep the used paths alive
431 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
434 * Task to destroy the tunnel after timeout
436 * FIXME: merge the two? a tunnel will have either
437 * a path refresh OR a timeout, never both!
439 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
444 * Info about a child node in a tunnel, needed to perform flow control.
446 struct MeshTunnelChildInfo
449 * ID of the child node.
464 * Maximum PID allowed.
470 * Info needed to work with tunnel paths and peers
477 struct MeshTunnel *t;
480 * Neighbouring peer to whom we send the packet to
482 struct MeshPeerInfo *peer;
487 struct MeshPeerPath *path;
492 * Struct containing information about a client of the service
499 struct MeshClient *next;
504 struct MeshClient *prev;
507 * Tunnels that belong to this client, indexed by local id
509 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
512 * Tunnels this client has accepted, indexed by incoming local id
514 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
517 * Tunnels this client has rejected, indexed by incoming local id
519 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
521 * Handle to communicate with the client
523 struct GNUNET_SERVER_Client *handle;
526 * Applications that this client has claimed to provide
528 struct GNUNET_CONTAINER_MultiHashMap *apps;
531 * Messages that this client has declared interest in
533 struct GNUNET_CONTAINER_MultiHashMap *types;
536 * Whether the client is active or shutting down (don't send confirmations
537 * to a client that is shutting down.
542 * ID of the client, mainly for debug messages
547 * Regular expressions describing the services offered by this client.
549 char **regexes; // FIXME add timeout? API to remove a regex?
552 * Number of regular expressions in regexes.
554 unsigned int n_regex;
557 * Task to refresh all regular expresions in the DHT.
559 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
565 * Struct to keep information of searches of services described by a regex
566 * using a user-provided string service description.
568 struct MeshRegexSearchInfo
571 * Which tunnel is this for
573 struct MeshTunnel *t;
576 * User provided description of the searched service.
581 * Part of the description already consumed by the search.
588 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
591 * Results from running DHT GETs.
593 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
596 * Contexts, for each running DHT GET. Free all on end of search.
598 struct MeshRegexSearchContext **contexts;
601 * Number of contexts (branches/steps in search).
603 unsigned int n_contexts;
606 * Peer that is connecting via connect_by_string. When connected, free ctx.
611 * Other peers that are found but not yet being connected to.
613 GNUNET_PEER_Id *peers;
616 * Number of elements in peers.
618 unsigned int n_peers;
621 * Next peer to try to connect to.
626 * Timeout for a connect attempt.
627 * When reached, try to connect to a different peer, if any. If not,
628 * try the same peer again.
630 GNUNET_SCHEDULER_TaskIdentifier timeout;
635 * Struct to keep state of running searches that have consumed a part of
638 struct MeshRegexSearchContext
641 * Part of the description already consumed by
642 * this particular search branch.
647 * Information about the search.
649 struct MeshRegexSearchInfo *info;
653 /******************************************************************************/
654 /************************ DEBUG FUNCTIONS ****************************/
655 /******************************************************************************/
659 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
660 * @param cls string to print
661 * @param success GNUNET_OK if the PUT was transmitted,
662 * GNUNET_NO on timeout,
663 * GNUNET_SYSERR on disconnect from service
664 * after the PUT message was transmitted
665 * (so we don't know if it was received or not)
670 mesh_debug (void *cls, int success)
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
680 /******************************************************************************/
681 /*********************** GLOBAL VARIABLES ****************************/
682 /******************************************************************************/
686 * Configuration parameters
688 static struct GNUNET_TIME_Relative refresh_path_time;
689 static struct GNUNET_TIME_Relative app_announce_time;
690 static struct GNUNET_TIME_Relative id_announce_time;
691 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
692 static struct GNUNET_TIME_Relative connect_timeout;
693 static long long unsigned int default_ttl;
694 static long long unsigned int dht_replication_level;
695 static long long unsigned int max_tunnels;
696 static long long unsigned int max_msgs_queue;
699 * DLL with all the clients, head.
701 static struct MeshClient *clients;
704 * DLL with all the clients, tail.
706 static struct MeshClient *clients_tail;
709 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
711 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
714 * Number of tunnels known.
716 static unsigned long long n_tunnels;
719 * Tunnels incoming, indexed by MESH_TunnelNumber
720 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
722 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
725 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
727 static struct GNUNET_CONTAINER_MultiHashMap *peers;
730 * Handle to communicate with transport
732 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
735 * Handle to communicate with core.
737 static struct GNUNET_CORE_Handle *core_handle;
742 static struct GNUNET_DHT_Handle *dht_handle;
747 static struct GNUNET_SERVER_Handle *server_handle;
750 * Handle to the statistics service.
752 static struct GNUNET_STATISTICS_Handle *stats;
755 * Notification context, to send messages to local clients.
757 static struct GNUNET_SERVER_NotificationContext *nc;
760 * Local peer own ID (memory efficient handle).
762 static GNUNET_PEER_Id myid;
765 * Local peer own ID (full value).
767 static struct GNUNET_PeerIdentity my_full_id;
772 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
777 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
780 * Tunnel ID for the next created tunnel (global tunnel number).
782 static MESH_TunnelNumber next_tid;
785 * Tunnel ID for the next incoming tunnel (local tunnel number).
787 static MESH_TunnelNumber next_local_tid;
790 * All application types provided by this peer.
792 static struct GNUNET_CONTAINER_MultiHashMap *applications;
795 * All message types clients of this peer are interested in.
797 static struct GNUNET_CONTAINER_MultiHashMap *types;
800 * Task to periodically announce provided applications.
802 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
805 * Task to periodically announce itself in the network.
807 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
810 * Next ID to assign to a client.
812 unsigned int next_client_id;
815 /******************************************************************************/
816 /*********************** DECLARATIONS **************************/
817 /******************************************************************************/
819 /* FIXME move declarations here */
822 * Function to process paths received for a new peer addition. The recorded
823 * paths form the initial tunnel, which can be optimized later.
824 * Called on each result obtained for the DHT search.
827 * @param exp when will this value expire
828 * @param key key of the result
829 * @param type type of the result
830 * @param size number of bytes in data
831 * @param data pointer to the result data
834 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
835 const struct GNUNET_HashCode * key,
836 const struct GNUNET_PeerIdentity *get_path,
837 unsigned int get_path_length,
838 const struct GNUNET_PeerIdentity *put_path,
839 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
840 size_t size, const void *data);
844 * Function to process DHT string to regex matching.
845 * Called on each result obtained for the DHT search.
847 * @param cls closure (search context)
848 * @param exp when will this value expire
849 * @param key key of the result
850 * @param get_path path of the get request (not used)
851 * @param get_path_length lenght of get_path (not used)
852 * @param put_path path of the put request (not used)
853 * @param put_path_length length of the put_path (not used)
854 * @param type type of the result
855 * @param size number of bytes in data
856 * @param data pointer to the result data
858 * TODO: re-issue the request after certain time? cancel after X results?
861 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
862 const struct GNUNET_HashCode * key,
863 const struct GNUNET_PeerIdentity *get_path,
864 unsigned int get_path_length,
865 const struct GNUNET_PeerIdentity *put_path,
866 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
867 size_t size, const void *data);
871 * Function to process DHT string to regex matching.
872 * Called on each result obtained for the DHT search.
874 * @param cls closure (search context)
875 * @param exp when will this value expire
876 * @param key key of the result
877 * @param get_path path of the get request (not used)
878 * @param get_path_length lenght of get_path (not used)
879 * @param put_path path of the put request (not used)
880 * @param put_path_length length of the put_path (not used)
881 * @param type type of the result
882 * @param size number of bytes in data
883 * @param data pointer to the result data
886 dht_get_string_accept_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,
892 enum GNUNET_BLOCK_Type type,
893 size_t size, const void *data);
897 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
898 * and insert it in the appropiate structures if the peer is not known yet.
900 * @param peer Short identity of the peer.
902 * @return Existing or newly created peer info.
904 static struct MeshPeerInfo *
905 peer_info_get_short (const GNUNET_PEER_Id peer);
909 * Try to establish a new connection to this peer.
910 * Use the best path for the given tunnel.
911 * If the peer doesn't have any path to it yet, try to get one.
912 * If the peer already has some path, send a CREATE PATH towards it.
914 * @param peer PeerInfo of the peer.
915 * @param t Tunnel for which to create the path, if possible.
918 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
922 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
924 * If peer already exists, reevaluate shortest path and change if different.
926 * @param t Tunnel we want to add a new peer to
927 * @param peer PeerInfo of the peer being added
931 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
935 * Removes an explicit path from a tunnel, freeing all intermediate nodes
936 * that are no longer needed, as well as nodes of no longer reachable peers.
937 * The tunnel itself is also destoyed if results in a remote empty tunnel.
939 * @param t Tunnel from which to remove the path.
940 * @param peer Short id of the peer which should be removed.
943 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
947 * Search for a tunnel by global ID using full PeerIdentities.
949 * @param oid owner of the tunnel.
950 * @param tid global tunnel number.
952 * @return tunnel handler, NULL if doesn't exist.
954 static struct MeshTunnel *
955 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
959 * Delete an active client from the tunnel.
965 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
968 * Notify a tunnel that a connection has broken that affects at least
971 * @param t Tunnel affected.
972 * @param p1 Peer that got disconnected from p2.
973 * @param p2 Peer that got disconnected from p1.
975 * @return Short ID of the peer disconnected (either p1 or p2).
976 * 0 if the tunnel remained unaffected.
978 static GNUNET_PEER_Id
979 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
984 * Get the current ack value for a tunnel, for data going from root to leaves,
985 * taking in account the tunnel mode and the status of all children and clients.
989 * @return Maximum PID allowed.
992 tunnel_get_fwd_ack (struct MeshTunnel *t);
995 * Get the current ack value for a tunnel, for data going from leaves to root,
996 * taking in account the tunnel mode and the status of all children and clients.
1000 * @return Maximum PID allowed.
1003 tunnel_get_bck_ack (struct MeshTunnel *t);
1006 * Add a client to a tunnel, initializing all needed data structures.
1008 * @param t Tunnel to which add the client.
1009 * @param c Client which to add to the tunnel.
1012 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1016 * Iterator over edges in a regex block retrieved from the DHT.
1018 * @param cls Closure.
1019 * @param token Token that follows to next state.
1020 * @param len Lenght of token.
1021 * @param key Hash of next state.
1023 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1026 regex_edge_iterator (void *cls,
1029 const struct GNUNET_HashCode *key);
1033 * Find a path to a peer that offers a regex servcie compatible
1034 * with a given string.
1036 * @param key The key of the accepting state.
1037 * @param ctx Context containing info about the string, tunnel, etc.
1040 regex_find_path (const struct GNUNET_HashCode *key,
1041 struct MeshRegexSearchContext *ctx);
1045 * Queue and pass message to core when possible.
1047 * @param cls Closure (type dependant).
1048 * @param type Type of the message, 0 for a raw message.
1049 * @param size Size of the message.
1050 * @param dst Neighbor to send message to.
1051 * @param t Tunnel this message belongs to.
1054 queue_add (void *cls, uint16_t type, size_t size,
1055 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1058 * Free a transmission that was already queued with all resources
1059 * associated to the request.
1061 * @param queue Queue handler to cancel.
1062 * @param clear_cls Is it necessary to free associated cls?
1065 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1067 /******************************************************************************/
1068 /************************ ITERATORS ****************************/
1069 /******************************************************************************/
1072 * Iterator over found existing mesh regex blocks that match an ongoing search.
1074 * @param cls closure
1075 * @param key current key code
1076 * @param value value in the hash map
1077 * @return GNUNET_YES if we should continue to iterate,
1081 regex_result_iterator (void *cls,
1082 const struct GNUNET_HashCode * key,
1085 struct MeshRegexBlock *block = value;
1086 struct MeshRegexSearchContext *ctx = cls;
1088 if (GNUNET_YES == ntohl(block->accepting) &&
1089 ctx->position == strlen (ctx->info->description))
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1092 regex_find_path (key, ctx);
1093 return GNUNET_YES; // We found an accept state!
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1098 ctx->position, strlen(ctx->info->description),
1099 ntohl(block->accepting));
1102 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1103 ®ex_edge_iterator, ctx);
1110 * Iterator over edges in a regex block retrieved from the DHT.
1112 * @param cls Closure (context of the search).
1113 * @param token Token that follows to next state.
1114 * @param len Lenght of token.
1115 * @param key Hash of next state.
1117 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1120 regex_edge_iterator (void *cls,
1123 const struct GNUNET_HashCode *key)
1125 struct MeshRegexSearchContext *ctx = cls;
1126 struct MeshRegexSearchContext *new_ctx;
1127 struct MeshRegexSearchInfo *info = ctx->info;
1128 struct GNUNET_DHT_GetHandle *get_h;
1132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1135 current = &info->description[ctx->position];
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1137 current_len = strlen (info->description) - ctx->position;
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1142 if (len > current_len)
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1145 return GNUNET_YES; // Token too long, wont match
1147 if (0 != strncmp (current, token, len))
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1150 return GNUNET_YES; // Token doesn't match
1152 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1153 new_ctx->info = info;
1154 new_ctx->position = ctx->position + len;
1155 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1157 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1160 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1161 ®ex_result_iterator,
1163 return GNUNET_YES; // We are already looking for it
1165 /* Start search in DHT */
1167 GNUNET_DHT_get_start (dht_handle, /* handle */
1168 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1169 key, /* key to search */
1170 dht_replication_level, /* replication level */
1171 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1172 NULL, /* xquery */ // FIXME BLOOMFILTER
1173 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1174 &dht_get_string_handler, new_ctx);
1176 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1177 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1187 * Iterator over hash map entries to cancel DHT GET requests after a
1188 * successful connect_by_string.
1190 * @param cls Closure (unused).
1191 * @param key Current key code (unused).
1192 * @param value Value in the hash map (get handle).
1193 * @return GNUNET_YES if we should continue to iterate,
1197 regex_cancel_dht_get (void *cls,
1198 const struct GNUNET_HashCode * key,
1201 struct GNUNET_DHT_GetHandle *h = value;
1203 GNUNET_DHT_get_stop (h);
1209 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1210 * search for connect_by_string.
1212 * @param cls Closure (unused).
1213 * @param key Current key code (unused).
1214 * @param value MeshRegexBlock in the hash map.
1215 * @return GNUNET_YES if we should continue to iterate,
1219 regex_free_result (void *cls,
1220 const struct GNUNET_HashCode * key,
1224 GNUNET_free (value);
1230 * Regex callback iterator to store own service description in the DHT.
1232 * @param cls closure.
1233 * @param key hash for current state.
1234 * @param proof proof for current state.
1235 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1236 * @param num_edges number of edges leaving current state.
1237 * @param edges edges leaving current state.
1240 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1241 int accepting, unsigned int num_edges,
1242 const struct GNUNET_REGEX_Edge *edges)
1244 struct MeshRegexBlock *block;
1245 struct MeshRegexEdge *block_edge;
1246 enum GNUNET_DHT_RouteOption opt;
1250 unsigned int offset;
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 " regex dht put for state %s\n",
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1263 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1264 if (GNUNET_YES == accepting)
1266 struct MeshRegexAccept block;
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 " state %s is accepting, putting own id\n",
1271 size = sizeof (block);
1273 block.id = my_full_id;
1275 GNUNET_DHT_put(dht_handle, key,
1276 dht_replication_level,
1277 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1278 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1281 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1286 len = strlen(proof);
1287 size = sizeof (struct MeshRegexBlock) + len;
1288 block = GNUNET_malloc (size);
1291 block->n_proof = htonl (len);
1292 block->n_edges = htonl (num_edges);
1293 block->accepting = htonl (accepting);
1295 /* Store the proof at the end of the block. */
1296 aux = (char *) &block[1];
1297 memcpy (aux, proof, len);
1300 /* Store each edge in a variable length MeshEdge struct at the
1301 * very end of the MeshRegexBlock structure.
1303 for (i = 0; i < num_edges; i++)
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306 " edge %s towards %s\n",
1308 GNUNET_h2s(&edges[i].destination));
1310 /* aux points at the end of the last block */
1311 len = strlen (edges[i].label);
1312 size += sizeof (struct MeshRegexEdge) + len;
1313 // Calculate offset FIXME is this ok? use size instead?
1314 offset = aux - (char *) block;
1315 block = GNUNET_realloc (block, size);
1316 aux = &((char *) block)[offset];
1317 block_edge = (struct MeshRegexEdge *) aux;
1318 block_edge->key = edges[i].destination;
1319 block_edge->n_token = htonl (len);
1320 aux = (char *) &block_edge[1];
1321 memcpy (aux, edges[i].label, len);
1325 GNUNET_DHT_put(dht_handle, key,
1326 dht_replication_level,
1328 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1330 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1334 GNUNET_free (block);
1339 * Store the regular expression describing a local service into the DHT.
1341 * @param regex The regular expresion.
1344 regex_put (const char *regex)
1346 struct GNUNET_REGEX_Automaton *dfa;
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1349 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1350 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1351 GNUNET_REGEX_automaton_destroy (dfa);
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1357 * Find a path to a peer that offers a regex servcie compatible
1358 * with a given string.
1360 * @param key The key of the accepting state.
1361 * @param ctx Context containing info about the string, tunnel, etc.
1364 regex_find_path (const struct GNUNET_HashCode *key,
1365 struct MeshRegexSearchContext *ctx)
1367 struct GNUNET_DHT_GetHandle *get_h;
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1370 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1371 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1372 key, /* key to search */
1373 dht_replication_level, /* replication level */
1374 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1375 GNUNET_DHT_RO_RECORD_ROUTE,
1376 NULL, /* xquery */ // FIXME BLOOMFILTER
1377 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1378 &dht_get_string_accept_handler, ctx);
1379 GNUNET_break (GNUNET_OK ==
1380 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1383 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1388 * Function called if the connect attempt to a peer found via
1389 * connect_by_string times out. Try to connect to another peer, if any.
1390 * Otherwise try to reconnect to the same peer.
1392 * @param cls Closure (info about regex search).
1393 * @param tc TaskContext.
1396 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1398 struct MeshRegexSearchInfo *info = cls;
1399 struct MeshPeerInfo *peer_info;
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1404 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1405 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1413 if (0 < info->n_peers)
1415 // Select next peer, put current in that spot.
1416 id = info->peers[info->i_peer];
1417 info->peers[info->i_peer] = info->peer;
1418 info->i_peer = (info->i_peer + 1) % info->n_peers;
1422 // Try to connect to same peer again.
1425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1427 peer_info = peer_info_get_short(id);
1428 tunnel_add_peer (info->t, peer_info);
1430 tunnel_delete_peer (info->t, old);
1431 peer_info_connect (peer_info, info->t);
1432 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1433 ®ex_connect_timeout,
1435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1440 * Cancel an ongoing regex search in the DHT and free all resources.
1442 * @param ctx The search context.
1445 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1447 struct MeshRegexSearchInfo *info = ctx->info;
1450 GNUNET_free (info->description);
1451 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1452 ®ex_cancel_dht_get, NULL);
1453 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1454 ®ex_free_result, NULL);
1455 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1456 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1457 info->t->regex_ctx = NULL;
1458 for (i = 0; i < info->n_contexts; i++)
1460 GNUNET_free (info->contexts[i]);
1462 if (0 < info->n_contexts)
1463 GNUNET_free (info->contexts);
1464 if (0 < info->n_peers)
1465 GNUNET_free (info->peers);
1466 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1468 GNUNET_SCHEDULER_cancel(info->timeout);
1474 /******************************************************************************/
1475 /************************ PERIODIC FUNCTIONS ****************************/
1476 /******************************************************************************/
1479 * Announce iterator over for each application provided by the peer
1481 * @param cls closure
1482 * @param key current key code
1483 * @param value value in the hash map
1484 * @return GNUNET_YES if we should continue to
1489 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1491 struct PBlock block;
1492 struct MeshClient *c;
1494 block.id = my_full_id;
1495 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1496 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1497 if (0 == block.type)
1502 block.type = htonl (block.type);
1504 GNUNET_break (NULL !=
1505 GNUNET_DHT_put (dht_handle, key,
1506 dht_replication_level,
1507 GNUNET_DHT_RO_RECORD_ROUTE |
1508 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1509 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1511 (const char *) &block,
1512 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1514 app_announce_time, NULL, NULL));
1520 * Periodically announce what applications are provided by local clients
1523 * @param cls closure
1524 * @param tc task context
1527 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1529 struct MeshClient *c = cls;
1532 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1534 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1538 DEBUG_DHT ("Starting PUT for regex\n");
1540 for (i = 0; i < c->n_regex; i++)
1542 regex_put (c->regexes[i]);
1544 c->regex_announce_task =
1545 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1546 DEBUG_DHT ("Finished PUT for regex\n");
1553 * Periodically announce what applications are provided by local clients
1556 * @param cls closure
1557 * @param tc task context
1560 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1562 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1564 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1568 DEBUG_DHT ("Starting PUT for apps\n");
1570 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1572 announce_applications_task =
1573 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1575 DEBUG_DHT ("Finished PUT for apps\n");
1582 * Periodically announce self id in the DHT
1584 * @param cls closure
1585 * @param tc task context
1588 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1590 struct PBlock block;
1592 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1594 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1598 * - Set data expiration in function of X
1599 * - Adapt X to churn
1601 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1603 block.id = my_full_id;
1604 block.type = htonl (0);
1605 GNUNET_DHT_put (dht_handle, /* DHT handle */
1606 &my_full_id.hashPubKey, /* Key to use */
1607 dht_replication_level, /* Replication level */
1608 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1609 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1610 sizeof (block), /* Size of the data */
1611 (const char *) &block, /* Data itself */
1612 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1613 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1614 NULL, /* Continuation */
1615 NULL); /* Continuation closure */
1617 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1621 /******************************************************************************/
1622 /****************** GENERAL HELPER FUNCTIONS ************************/
1623 /******************************************************************************/
1627 * Decrements the reference counter and frees all resources if needed
1629 * @param mesh_data Data Descriptor used in a multicast message.
1630 * Freed no longer needed (last message).
1633 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1635 /* Make sure it's a multicast packet */
1636 GNUNET_assert (NULL != mesh_data->reference_counter);
1638 if (0 == --(*(mesh_data->reference_counter)))
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1641 if (NULL != mesh_data->task)
1643 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1645 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1647 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1649 GNUNET_free (mesh_data->task);
1651 GNUNET_free (mesh_data->reference_counter);
1652 GNUNET_free (mesh_data->data);
1653 GNUNET_free (mesh_data);
1659 * Check if client has registered with the service and has not disconnected
1661 * @param client the client to check
1663 * @return non-NULL if client exists in the global DLL
1665 static struct MeshClient *
1666 client_get (struct GNUNET_SERVER_Client *client)
1668 struct MeshClient *c;
1673 if (c->handle == client)
1682 * Checks if a given client has subscribed to certain message type
1684 * @param message_type Type of message to check
1685 * @param c Client to check
1687 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1690 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1692 struct GNUNET_HashCode hc;
1694 if (NULL == c->types)
1696 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1697 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1702 * Allow a client to send more data after transmitting a multicast message
1703 * which some neighbor has not yet accepted altough a reasonable time has
1706 * @param cls Closure (DataDescriptor containing the task identifier)
1707 * @param tc Task Context
1709 * FIXME reference counter cshould be just int
1712 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1714 struct MeshData *mdata = cls;
1716 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1718 GNUNET_assert (NULL != mdata->reference_counter);
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1720 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1721 *(mdata->reference_counter));
1722 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1723 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1728 * Check whether client wants traffic from a tunnel.
1730 * @param c Client to check.
1731 * @param t Tunnel to be found.
1733 * @return GNUNET_YES if client knows tunnel.
1735 * TODO look in client hashmap
1738 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1742 for (i = 0; i < t->nclients; i++)
1743 if (t->clients[i] == c)
1750 * Check whether client has been informed about a tunnel.
1752 * @param c Client to check.
1753 * @param t Tunnel to be found.
1755 * @return GNUNET_YES if client knows tunnel.
1757 * TODO look in client hashmap
1760 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1764 for (i = 0; i < t->nignore; i++)
1765 if (t->ignore[i] == c)
1767 return client_wants_tunnel(c, t);
1772 * Marks a client as uninterested in traffic from the tunnel, updating both
1773 * client and tunnel to reflect this.
1775 * @param c Client that doesn't want traffic anymore.
1776 * @param t Tunnel which should be ignored.
1778 * FIXME when to delete an incoming tunnel?
1781 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1783 struct GNUNET_HashCode hash;
1785 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1786 GNUNET_break (GNUNET_YES ==
1787 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1789 GNUNET_break (GNUNET_YES ==
1790 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1791 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1792 tunnel_delete_active_client (t, c);
1793 GNUNET_array_append (t->ignore, t->nignore, c);
1798 * Deletes a tunnel from a client (either owner or destination). To be used on
1799 * tunnel destroy, otherwise, use client_ignore_tunnel.
1801 * @param c Client whose tunnel to delete.
1802 * @param t Tunnel which should be deleted.
1805 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1807 struct GNUNET_HashCode hash;
1811 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1812 GNUNET_assert (GNUNET_YES ==
1813 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1819 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1821 GNUNET_assert (GNUNET_YES ==
1822 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1826 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1835 * Send the message to all clients that have subscribed to its type
1837 * @param msg Pointer to the message itself
1838 * @param payload Pointer to the payload of the message.
1839 * @return number of clients this message was sent to
1842 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1843 const struct GNUNET_MessageHeader *payload)
1845 struct GNUNET_PeerIdentity *oid;
1846 struct MeshClient *c;
1847 struct MeshTunnel *t;
1848 MESH_TunnelNumber *tid;
1851 char cbuf[htons (msg->size)];
1853 type = ntohs (payload->type);
1854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1857 memcpy (cbuf, msg, sizeof (cbuf));
1858 switch (htons (msg->type))
1860 struct GNUNET_MESH_Unicast *uc;
1861 struct GNUNET_MESH_Multicast *mc;
1862 struct GNUNET_MESH_ToOrigin *to;
1864 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1865 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1869 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1870 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1874 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1875 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1883 t = tunnel_get (oid, ntohl (*tid));
1890 for (count = 0, c = clients; c != NULL; c = c->next)
1892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1893 if (client_is_subscribed (type, c))
1895 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1899 *tid = htonl (t->local_tid);
1903 if (GNUNET_NO == client_knows_tunnel (c, t))
1905 /* This client doesn't know the tunnel */
1906 struct GNUNET_MESH_TunnelNotification tmsg;
1907 struct GNUNET_HashCode hash;
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1910 tmsg.header.size = htons (sizeof (tmsg));
1911 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1912 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1913 tmsg.tunnel_id = htonl (t->local_tid_dest);
1914 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1915 &tmsg.header, GNUNET_NO);
1916 tunnel_add_client (t, c);
1917 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1919 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1920 c->incoming_tunnels, &hash, t,
1921 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1923 *tid = htonl (t->local_tid_dest);
1926 /* Check if the client wants to get traffic from the tunnel */
1927 if (GNUNET_NO == client_wants_tunnel(c, t))
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1931 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1932 (struct GNUNET_MessageHeader
1933 *) cbuf, GNUNET_NO);
1941 * Notify the client that owns the tunnel that a peer has connected to it
1942 * (the requested path to it has been confirmed).
1944 * @param t Tunnel whose owner to notify
1945 * @param id Short id of the peer that has connected
1948 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1950 struct GNUNET_MESH_PeerControl pc;
1952 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1953 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1954 pc.tunnel_id = htonl (t->local_tid);
1955 GNUNET_PEER_resolve (id, &pc.peer);
1956 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1962 * Notify a client about how many more payload packages will we accept
1963 * on a given tunnel.
1969 send_client_tunnel_ack (struct MeshClient *c, struct MeshTunnel *t)
1971 struct GNUNET_MESH_LocalAck msg;
1974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1975 "Sending client ACK on tunnel %X\n",
1980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to client %u\n", c->id);
1982 ack = tunnel_get_fwd_ack (t);
1984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
1985 if (t->last_ack == ack)
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
1990 msg.header.size = htons (sizeof (msg));
1991 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1992 msg.tunnel_id = htonl (t->local_tid);
1993 msg.max_pid = htonl (ack);
1995 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1996 &msg.header, GNUNET_NO);
2001 * Notify all clients (not depending on registration status) that the incoming
2002 * tunnel is no longer valid.
2004 * @param t Tunnel that was destroyed.
2007 send_clients_tunnel_destroy (struct MeshTunnel *t)
2009 struct GNUNET_MESH_TunnelMessage msg;
2011 msg.header.size = htons (sizeof (msg));
2012 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2013 msg.tunnel_id = htonl (t->local_tid_dest);
2014 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2019 * Notify clients of tunnel disconnections, if needed.
2020 * In case the origin disconnects, the destination clients get a tunnel destroy
2021 * notification. If the last destination disconnects (only one remaining client
2022 * in tunnel), the origin gets a (local ID) peer disconnected.
2023 * Note that the function must be called BEFORE removing the client from
2026 * @param t Tunnel that was destroyed.
2027 * @param c Client that disconnected.
2030 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2036 struct GNUNET_MESH_TunnelMessage msg;
2038 msg.header.size = htons (sizeof (msg));
2039 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2040 msg.tunnel_id = htonl (t->local_tid_dest);
2041 for (i = 0; i < t->nclients; i++)
2042 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2043 &msg.header, GNUNET_NO);
2045 // FIXME when to disconnect an incoming tunnel?
2046 else if (1 == t->nclients && NULL != t->owner)
2048 struct GNUNET_MESH_PeerControl msg;
2050 msg.header.size = htons (sizeof (msg));
2051 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2052 msg.tunnel_id = htonl (t->local_tid);
2053 msg.peer = my_full_id;
2054 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2055 &msg.header, GNUNET_NO);
2061 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2062 * and insert it in the appropiate structures if the peer is not known yet.
2064 * @param peer Full identity of the peer.
2066 * @return Existing or newly created peer info.
2068 static struct MeshPeerInfo *
2069 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2071 struct MeshPeerInfo *peer_info;
2073 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2074 if (NULL == peer_info)
2077 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2078 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2079 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2080 peer_info->id = GNUNET_PEER_intern (peer);
2088 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2089 * and insert it in the appropiate structures if the peer is not known yet.
2091 * @param peer Short identity of the peer.
2093 * @return Existing or newly created peer info.
2095 static struct MeshPeerInfo *
2096 peer_info_get_short (const GNUNET_PEER_Id peer)
2098 struct GNUNET_PeerIdentity id;
2100 GNUNET_PEER_resolve (peer, &id);
2101 return peer_info_get (&id);
2106 * Iterator to remove the tunnel from the list of tunnels a peer participates
2109 * @param cls Closure (tunnel info)
2110 * @param key GNUNET_PeerIdentity of the peer (unused)
2111 * @param value PeerInfo of the peer
2113 * @return always GNUNET_YES, to keep iterating
2116 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2118 struct MeshTunnel *t = cls;
2119 struct MeshPeerInfo *peer = value;
2122 for (i = 0; i < peer->ntunnels; i++)
2125 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2128 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2129 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2138 * Core callback to write a pre-constructed data packet to core buffer
2140 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2141 * @param size Number of bytes available in buf.
2142 * @param buf Where the to write the message.
2144 * @return number of bytes written to buf
2147 send_core_data_raw (void *cls, size_t size, void *buf)
2149 struct MeshTransmissionDescriptor *info = cls;
2150 struct GNUNET_MessageHeader *msg;
2153 GNUNET_assert (NULL != info);
2154 GNUNET_assert (NULL != info->mesh_data);
2155 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2156 total_size = ntohs (msg->size);
2158 if (total_size > size)
2163 memcpy (buf, msg, total_size);
2164 data_descriptor_decrement_rc (info->mesh_data);
2171 * Sends an already built non-multicast message to a peer,
2172 * properly registrating all used resources.
2174 * @param message Message to send. Function makes a copy of it.
2175 * @param peer Short ID of the neighbor whom to send the message.
2176 * @param t Tunnel on which this message is transmitted.
2179 send_message (const struct GNUNET_MessageHeader *message,
2180 const struct GNUNET_PeerIdentity *peer,
2181 struct MeshTunnel *t)
2183 struct MeshTransmissionDescriptor *info;
2184 struct MeshPeerInfo *neighbor;
2185 struct MeshPeerPath *p;
2188 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2190 size = ntohs (message->size);
2191 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2192 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2193 info->mesh_data->data = GNUNET_malloc (size);
2194 memcpy (info->mesh_data->data, message, size);
2195 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
2197 struct GNUNET_MESH_Unicast *m;
2199 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2200 m->ttl = htonl (ntohl (m->ttl) - 1);
2202 info->mesh_data->data_len = size;
2203 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2204 *info->mesh_data->reference_counter = 1;
2205 neighbor = peer_info_get (peer);
2206 for (p = neighbor->path_head; NULL != p; p = p->next)
2215 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2216 GNUNET_free (info->mesh_data->data);
2217 GNUNET_free (info->mesh_data);
2221 info->peer = neighbor;
2231 * Sends a CREATE PATH message for a path to a peer, properly registrating
2232 * all used resources.
2234 * @param peer PeerInfo of the final peer for whom this path is being created.
2235 * @param p Path itself.
2236 * @param t Tunnel for which the path is created.
2239 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2240 struct MeshTunnel *t)
2242 struct GNUNET_PeerIdentity id;
2243 struct MeshPathInfo *path_info;
2244 struct MeshPeerInfo *neighbor;
2250 p = tree_get_path_to_peer (t->tree, peer->id);
2257 for (i = 0; i < p->length; i++)
2259 if (p->peers[i] == myid)
2262 if (i >= p->length - 1)
2268 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2270 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2271 path_info->path = p;
2273 neighbor = peer_info_get (&id);
2274 path_info->peer = neighbor;
2275 queue_add (path_info,
2276 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2277 sizeof (struct GNUNET_MESH_ManipulatePath) +
2278 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2285 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2287 * @param t Tunnel whose path to destroy.
2288 * @param destination Short ID of the peer to whom the path to destroy.
2291 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2293 struct MeshPeerPath *p;
2296 p = tree_get_path_to_peer (t->tree, destination);
2302 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2303 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2305 struct GNUNET_MESH_ManipulatePath *msg;
2306 struct GNUNET_PeerIdentity *pi;
2310 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2311 msg->header.size = htons (size);
2312 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2313 msg->tid = htonl (t->id.tid);
2314 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2315 for (i = 0; i < p->length; i++)
2317 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2319 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2326 * Try to establish a new connection to this peer.
2327 * Use the best path for the given tunnel.
2328 * If the peer doesn't have any path to it yet, try to get one.
2329 * If the peer already has some path, send a CREATE PATH towards it.
2331 * @param peer PeerInfo of the peer.
2332 * @param t Tunnel for which to create the path, if possible.
2335 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2337 struct MeshPeerPath *p;
2338 struct MeshPathInfo *path_info;
2340 if (NULL != peer->path_head)
2342 p = tree_get_path_to_peer (t->tree, peer->id);
2349 // FIXME always send create path to self
2352 send_create_path (peer, p, t);
2356 struct GNUNET_HashCode hash;
2359 send_client_peer_connected (t, myid);
2360 t->local_tid_dest = next_local_tid++;
2361 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2364 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2365 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2372 else if (NULL == peer->dhtget)
2374 struct GNUNET_PeerIdentity id;
2376 GNUNET_PEER_resolve (peer->id, &id);
2377 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2378 path_info->peer = peer;
2380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2381 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2382 peer->dhtgetcls = path_info;
2383 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2384 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2385 &id.hashPubKey, /* key to search */
2386 dht_replication_level, /* replication level */
2387 GNUNET_DHT_RO_RECORD_ROUTE |
2388 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2389 NULL, /* xquery */ // FIXME BLOOMFILTER
2390 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2391 &dht_get_id_handler, path_info);
2393 /* Otherwise, there is no path but the DHT get is already started. */
2398 * Task to delay the connection of a peer
2400 * @param cls Closure (path info with tunnel and peer to connect).
2401 * Will be free'd on exection.
2402 * @param tc TaskContext
2405 peer_info_connect_task (void *cls,
2406 const struct GNUNET_SCHEDULER_TaskContext *tc)
2408 struct MeshPathInfo *path_info = cls;
2410 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2415 peer_info_connect (path_info->peer, path_info->t);
2421 * Destroy the peer_info and free any allocated resources linked to it
2423 * @param pi The peer_info to destroy.
2425 * @return GNUNET_OK on success
2428 peer_info_destroy (struct MeshPeerInfo *pi)
2430 struct GNUNET_PeerIdentity id;
2431 struct MeshPeerPath *p;
2432 struct MeshPeerPath *nextp;
2434 GNUNET_PEER_resolve (pi->id, &id);
2435 GNUNET_PEER_change_rc (pi->id, -1);
2438 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2441 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2442 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2444 if (NULL != pi->dhtget)
2446 GNUNET_DHT_get_stop (pi->dhtget);
2447 GNUNET_free (pi->dhtgetcls);
2453 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2463 * Remove all paths that rely on a direct connection between p1 and p2
2464 * from the peer itself and notify all tunnels about it.
2466 * @param peer PeerInfo of affected peer.
2467 * @param p1 GNUNET_PEER_Id of one peer.
2468 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2471 * TODO: optimize (see below)
2474 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2477 struct MeshPeerPath *p;
2478 struct MeshPeerPath *aux;
2479 struct MeshPeerInfo *peer_d;
2481 unsigned int destroyed;
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2488 p = peer->path_head;
2492 for (i = 0; i < (p->length - 1); i++)
2494 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2495 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2497 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2508 for (i = 0; i < peer->ntunnels; i++)
2510 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2514 * Problem: one or more peers have been deleted from the tunnel tree.
2515 * We don't know who they are to try to add them again.
2516 * We need to try to find a new path for each of the disconnected peers.
2517 * Some of them might already have a path to reach them that does not
2518 * involve p1 and p2. Adding all anew might render in a better tree than
2519 * the trivial immediate fix.
2521 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2522 * its children will be reachable trough him.
2524 peer_d = peer_info_get_short (d);
2527 for (p = peer_d->path_head; NULL != p; p = p->next)
2529 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2537 /* No callback, as peer will be already disconnected and a connection
2538 * scheduled by tunnel_notify_connection_broken.
2540 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2544 peer_info_connect (peer_d, peer->tunnels[i]);
2547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2552 * Add the path to the peer and update the path used to reach it in case this
2555 * @param peer_info Destination peer to add the path to.
2556 * @param path New path to add. Last peer must be the peer in arg 1.
2557 * Path will be either used of freed if already known.
2558 * @param trusted Do we trust that this path is real?
2561 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2564 struct MeshPeerPath *aux;
2568 if ((NULL == peer_info) || (NULL == path))
2571 path_destroy (path);
2574 if (path->peers[path->length - 1] != peer_info->id)
2577 path_destroy (path);
2580 if (path->length <= 2 && GNUNET_NO == trusted)
2582 /* Only allow CORE to tell us about direct paths */
2583 path_destroy (path);
2586 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2587 for (l = 1; l < path->length; l++)
2589 if (path->peers[l] == myid)
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2592 for (l2 = 0; l2 < path->length - l; l2++)
2594 path->peers[l2] = path->peers[l + l2];
2599 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2604 struct GNUNET_PeerIdentity id;
2606 GNUNET_PEER_resolve (peer_info->id, &id);
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2608 path->length, GNUNET_i2s (&id));
2611 l = path_get_length (path);
2618 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2619 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2621 l2 = path_get_length (aux);
2624 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2625 peer_info->path_tail, aux, path);
2630 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2632 path_destroy (path);
2637 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2644 * Add the path to the origin peer and update the path used to reach it in case
2645 * this is the shortest.
2646 * The path is given in peer_info -> destination, therefore we turn the path
2647 * upside down first.
2649 * @param peer_info Peer to add the path to, being the origin of the path.
2650 * @param path New path to add after being inversed.
2651 * @param trusted Do we trust that this path is real?
2654 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2655 struct MeshPeerPath *path, int trusted)
2658 peer_info_add_path (peer_info, path, trusted);
2663 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2664 * to obtain a local peer -> destination path and interning the peer ids.
2666 * @return Newly allocated and created path
2668 static struct MeshPeerPath *
2669 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2670 unsigned int get_path_length,
2671 const struct GNUNET_PeerIdentity *put_path,
2672 unsigned int put_path_length)
2674 struct MeshPeerPath *p;
2680 GNUNET_PEER_change_rc (myid, 1);
2681 i = get_path_length;
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2683 for (i--; i >= 0; i--)
2685 id = GNUNET_PEER_intern (&get_path[i]);
2686 if (p->length > 0 && id == p->peers[p->length - 1])
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2689 GNUNET_PEER_change_rc (id, -1);
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2694 GNUNET_i2s (&get_path[i]));
2696 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2697 p->peers[p->length - 1] = id;
2700 i = put_path_length;
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2702 for (i--; i >= 0; i--)
2704 id = GNUNET_PEER_intern (&put_path[i]);
2707 /* PUT path went through us, so discard the path up until now and start
2708 * from here to get a much shorter (and loop-free) path.
2713 if (p->length > 0 && id == p->peers[p->length - 1])
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2716 GNUNET_PEER_change_rc (id, -1);
2720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2721 GNUNET_i2s (&put_path[i]));
2723 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2724 p->peers[p->length - 1] = id;
2728 if (get_path_length > 0)
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2730 GNUNET_i2s (&get_path[0]));
2731 if (put_path_length > 0)
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2733 GNUNET_i2s (&put_path[0]));
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2736 for (i = 0; i < p->length; i++)
2738 struct GNUNET_PeerIdentity peer_id;
2740 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2742 GNUNET_i2s (&peer_id));
2750 * Adds a path to the peer_infos of all the peers in the path
2752 * @param p Path to process.
2753 * @param confirmed Whether we know if the path works or not.
2756 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2760 /* TODO: invert and add */
2761 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2762 for (i++; i < p->length; i++)
2764 struct MeshPeerInfo *aux;
2765 struct MeshPeerPath *copy;
2767 aux = peer_info_get_short (p->peers[i]);
2768 copy = path_duplicate (p);
2769 copy->length = i + 1;
2770 peer_info_add_path (aux, copy, GNUNET_NO);
2776 * Send keepalive packets for a peer
2778 * @param cls Closure (tunnel for which to send the keepalive).
2779 * @param tc Notification context.
2781 * TODO: implement explicit multicast keepalive?
2784 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2788 * Search for a tunnel among the incoming tunnels
2790 * @param tid the local id of the tunnel
2792 * @return tunnel handler, NULL if doesn't exist
2794 static struct MeshTunnel *
2795 tunnel_get_incoming (MESH_TunnelNumber tid)
2797 struct GNUNET_HashCode hash;
2799 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2800 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2801 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2806 * Search for a tunnel among the tunnels for a client
2808 * @param c the client whose tunnels to search in
2809 * @param tid the local id of the tunnel
2811 * @return tunnel handler, NULL if doesn't exist
2813 static struct MeshTunnel *
2814 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2816 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2818 return tunnel_get_incoming (tid);
2822 struct GNUNET_HashCode hash;
2824 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2825 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2831 * Search for a tunnel by global ID using PEER_ID
2833 * @param pi owner of the tunnel
2834 * @param tid global tunnel number
2836 * @return tunnel handler, NULL if doesn't exist
2838 static struct MeshTunnel *
2839 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2841 struct MESH_TunnelID id;
2842 struct GNUNET_HashCode hash;
2847 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2848 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2853 * Search for a tunnel by global ID using full PeerIdentities
2855 * @param oid owner of the tunnel
2856 * @param tid global tunnel number
2858 * @return tunnel handler, NULL if doesn't exist
2860 static struct MeshTunnel *
2861 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2863 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2868 * Delete an active client from the tunnel.
2874 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2878 for (i = 0; i < t->nclients; i++)
2880 if (t->clients[i] == c)
2882 t->clients[i] = t->clients[t->nclients - 1];
2883 t->clients_acks[i] = t->clients_acks[t->nclients - 1];
2884 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2886 GNUNET_array_grow (t->clients_acks, t->nclients, t->nclients - 1);
2894 * Delete an ignored client from the tunnel.
2900 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2904 for (i = 0; i < t->nignore; i++)
2906 if (t->ignore[i] == c)
2908 t->ignore[i] = t->ignore[t->nignore - 1];
2909 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2917 * Delete a client from the tunnel. It should be only done on
2918 * client disconnection, otherwise use client_ignore_tunnel.
2924 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2926 tunnel_delete_ignored_client (t, c);
2927 tunnel_delete_active_client (t, c);
2932 * Iterator to free MeshTunnelChildInfo of tunnel children.
2934 * @param cls Closure (tunnel info).
2935 * @param key Hash of GNUNET_PEER_Id (unused).
2936 * @param value MeshTunnelChildInfo of the child.
2938 * @return always GNUNET_YES, to keep iterating
2941 tunnel_destroy_child (void *cls,
2942 const struct GNUNET_HashCode * key,
2945 GNUNET_free (value);
2951 * Callback used to notify a client owner of a tunnel that a peer has
2952 * disconnected, most likely because of a path change.
2954 * @param cls Closure (tunnel this notification is about).
2955 * @param peer_id Short ID of disconnected peer.
2958 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2960 struct MeshTunnel *t = cls;
2961 struct MeshPeerInfo *peer;
2962 struct MeshPathInfo *path_info;
2964 if (NULL != t->owner && NULL != nc)
2966 struct GNUNET_MESH_PeerControl msg;
2968 msg.header.size = htons (sizeof (msg));
2969 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2970 msg.tunnel_id = htonl (t->local_tid);
2971 GNUNET_PEER_resolve (peer_id, &msg.peer);
2972 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2973 &msg.header, GNUNET_NO);
2975 peer = peer_info_get_short (peer_id);
2976 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2977 path_info->peer = peer;
2979 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2984 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2985 * needed rescources.
2986 * If peer already exists, reevaluate shortest path and change if different.
2988 * @param t Tunnel we want to add a new peer to
2989 * @param peer PeerInfo of the peer being added
2993 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2995 struct GNUNET_PeerIdentity id;
2996 struct MeshPeerPath *best_p;
2997 struct MeshPeerPath *p;
2998 unsigned int best_cost;
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3002 GNUNET_PEER_resolve (peer->id, &id);
3004 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3007 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3008 GNUNET_assert (GNUNET_OK ==
3009 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3011 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3014 if (NULL != (p = peer->path_head))
3017 best_cost = tree_get_path_cost (t->tree, p);
3020 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3027 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3028 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3029 t->path_refresh_task =
3030 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3034 /* Start a DHT get */
3035 peer_info_connect (peer, t);
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3041 * Add a path to a tunnel which we don't own, just to remember the next hop.
3042 * If destination node was already in the tunnel, the first hop information
3043 * will be replaced with the new path.
3045 * @param t Tunnel we want to add a new peer to
3046 * @param p Path to add
3047 * @param own_pos Position of local node in path.
3051 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3052 unsigned int own_pos)
3054 struct GNUNET_PeerIdentity id;
3056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3057 GNUNET_assert (0 != own_pos);
3058 tree_add_path (t->tree, p, NULL, NULL);
3059 if (own_pos < p->length - 1)
3061 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3062 tree_update_first_hops (t->tree, myid, &id);
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3069 * Notifies a tunnel that a connection has broken that affects at least
3070 * some of its peers. Sends a notification towards the root of the tree.
3071 * In case the peer is the owner of the tree, notifies the client that owns
3072 * the tunnel and tries to reconnect.
3074 * @param t Tunnel affected.
3075 * @param p1 Peer that got disconnected from p2.
3076 * @param p2 Peer that got disconnected from p1.
3078 * @return Short ID of the peer disconnected (either p1 or p2).
3079 * 0 if the tunnel remained unaffected.
3081 static GNUNET_PEER_Id
3082 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3088 tree_notify_connection_broken (t->tree, p1, p2,
3089 &tunnel_notify_client_peer_disconnected,
3091 if (myid != p1 && myid != p2)
3097 if (tree_get_predecessor (t->tree) != 0)
3099 /* We are the peer still connected, notify owner of the disconnection. */
3100 struct GNUNET_MESH_PathBroken msg;
3101 struct GNUNET_PeerIdentity neighbor;
3103 msg.header.size = htons (sizeof (msg));
3104 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3105 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3106 msg.tid = htonl (t->id.tid);
3107 msg.peer1 = my_full_id;
3108 GNUNET_PEER_resolve (pid, &msg.peer2);
3109 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3110 send_message (&msg.header, &neighbor, t);
3118 * Send a multicast packet to a neighbor.
3120 * @param cls Closure (Info about the multicast packet)
3121 * @param neighbor_id Short ID of the neighbor to send the packet to.
3124 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3126 struct MeshData *mdata = cls;
3127 struct MeshTransmissionDescriptor *info;
3128 struct GNUNET_PeerIdentity neighbor;
3130 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3132 info->mesh_data = mdata;
3133 (*(mdata->reference_counter)) ++;
3134 info->destination = neighbor_id;
3135 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3137 GNUNET_i2s (&neighbor));
3138 info->peer = peer_info_get (&neighbor);
3139 GNUNET_assert (NULL != info->peer);
3141 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3142 info->mesh_data->data_len,
3149 * Send a message in a tunnel in multicast, sending a copy to each child node
3150 * down the local one in the tunnel tree.
3152 * @param t Tunnel in which to send the data.
3153 * @param msg Message to be sent.
3154 * @param internal Has the service generated this message?
3157 tunnel_send_multicast (struct MeshTunnel *t,
3158 const struct GNUNET_MessageHeader *msg,
3161 struct MeshData *mdata;
3163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3164 " sending a multicast packet...\n");
3165 mdata = GNUNET_malloc (sizeof (struct MeshData));
3166 mdata->data_len = ntohs (msg->size);
3167 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3169 mdata->data = GNUNET_malloc (mdata->data_len);
3170 memcpy (mdata->data, msg, mdata->data_len);
3171 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3173 struct GNUNET_MESH_Multicast *mcast;
3175 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3176 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3178 ntohl (mcast->ttl));
3182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3184 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3185 && GNUNET_NO == internal)
3187 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3189 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3195 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3196 if (*(mdata->reference_counter) == 0)
3198 GNUNET_free (mdata->data);
3199 GNUNET_free (mdata->reference_counter);
3200 if (NULL != mdata->task)
3202 GNUNET_SCHEDULER_cancel(*(mdata->task));
3203 GNUNET_free (mdata->task);
3204 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3206 // FIXME change order?
3207 GNUNET_free (mdata);
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 " sending a multicast packet done\n");
3216 * Increase the SKIP value of all peers that
3217 * have not received a unicast message.
3219 * @param cls Closure (ID of the peer that HAS received the message).
3220 * @param key ID of the neighbor.
3221 * @param value Information about the neighbor.
3223 * @return GNUNET_YES to keep iterating.
3226 tunnel_add_skip (void *cls,
3227 const struct GNUNET_HashCode * key,
3230 struct GNUNET_PeerIdentity *neighbor = cls;
3231 struct MeshTunnelChildInfo *cinfo = value;
3233 /* TODO compare only pointers? key == neighbor? */
3234 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3245 * Iterator to get the appropiate ACK value from all children nodes.
3247 * @param cls Closue (tunnel).
3248 * @param id Id of the child node.
3251 tunnel_get_child_ack (void *cls,
3254 struct GNUNET_PeerIdentity peer_id;
3255 struct MeshTunnelChildInfo *cinfo;
3256 struct MeshTunnel *t = cls;
3259 GNUNET_PEER_resolve (id, &peer_id);
3260 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3261 &peer_id.hashPubKey);
3264 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3266 cinfo->pid = t->pid;
3267 cinfo->skip = t->pid;
3268 cinfo->max_pid = ack = t->pid + 1;
3269 GNUNET_assert (GNUNET_OK ==
3270 GNUNET_CONTAINER_multihashmap_put(t->children_fc,
3271 &peer_id.hashPubKey,
3273 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3277 ack = cinfo->max_pid;
3280 if (0 == t->max_child_ack)
3281 t->max_child_ack = ack;
3283 if (GNUNET_YES == t->speed_min)
3285 t->max_child_ack = t->max_child_ack > ack ? ack : t->max_child_ack;
3289 t->max_child_ack = t->max_child_ack > ack ? t->max_child_ack : ack;
3296 * Get the maximum PID allowed to transmit to any
3297 * tunnel child of the local peer, depending on the tunnel
3298 * buffering/speed settings.
3302 * @return Maximum PID allowed, 0 if node has no children.
3305 tunnel_get_children_ack (struct MeshTunnel *t)
3307 t->max_child_ack = 0;
3308 tree_iterate_children (t->tree, tunnel_get_child_ack, t);
3309 return t->max_child_ack;
3314 * Add a client to a tunnel, initializing all needed data structures.
3316 * @param t Tunnel to which add the client.
3317 * @param c Client which to add to the tunnel.
3320 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3324 GNUNET_array_append (t->clients, t->nclients, c);
3327 GNUNET_array_append (t->clients_acks, t->nclients, ack);
3332 * Set the ACK value of a client in a particular tunnel.
3334 * @param t Tunnel affected.
3335 * @param c Client whose ACK to set.
3336 * @param ack ACK value.
3339 tunnel_set_client_ack (struct MeshTunnel *t,
3340 struct MeshClient *c,
3345 for (i = 0; i < t->nclients; i++)
3347 if (t->clients[i] != c)
3349 t->clients_acks[i] = ack;
3357 * Get the ACK value of a client in a particular tunnel.
3359 * @param t Tunnel on which to look.
3360 * @param c Client whose ACK to get.
3362 * @return ACK value.
3364 uint32_t // FIXME static when used!!
3365 tunnel_get_client_ack (struct MeshTunnel *t,
3366 struct MeshClient *c)
3370 for (i = 0; i < t->nclients; i++)
3372 if (t->clients[i] != c)
3374 return t->clients_acks[i];
3377 return t->clients_acks[0];
3382 * Get the highest ACK value of all clients in a particular tunnel,
3383 * according to the buffering/speed settings.
3385 * @param t Tunnel on which to look.
3387 * @return Corresponding ACK value.
3390 tunnel_get_clients_ack (struct MeshTunnel *t)
3395 for (ack = 0, i = 0; i < t->nclients; i++)
3398 (GNUNET_YES == t->speed_min && t->clients_acks[i] < ack) ||
3399 (GNUNET_NO == t->speed_min && t->clients_acks[i] > ack))
3401 ack = t->clients_acks[i];
3405 if (GNUNET_YES == t->nobuffer && ack > t->pid)
3413 * Get the current ack value for a tunnel, taking in account the tunnel
3414 * mode and the status of all children nodes.
3418 * @return Maximum PID allowed.
3421 tunnel_get_fwd_ack (struct MeshTunnel *t)
3424 uint32_t buffer_free;
3426 uint32_t client_ack;
3429 count = t->pid - t->skip;
3430 buffer_free = t->queue_max - t->queue_n;
3431 ack = count + buffer_free;
3432 child_ack = tunnel_get_children_ack (t);
3434 ack = client_ack = tunnel_get_clients_ack (t);
3436 if (GNUNET_YES == t->speed_min)
3438 ack = child_ack > ack ? ack : child_ack;
3439 ack = client_ack > ack ? ack : client_ack;
3443 ack = child_ack > ack ? child_ack : ack;
3444 ack = client_ack > ack ? client_ack : ack;
3446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u\n",
3447 count, buffer_free, child_ack, client_ack);
3452 * Get the current ack value for a tunnel, taking in account the tunnel
3453 * mode and the status of all children nodes.
3457 * @return Maximum PID allowed.
3460 tunnel_get_bck_ack (struct MeshTunnel *t)
3471 * Send an ACK informing the predecessor about the available buffer space.
3472 * In case there is no predecessor, inform the owning client.
3473 * If buffering is off, send only on behalf of children or self if endpoint.
3474 * If buffering is on, send when sent to children and buffer space is free.
3476 * @param t Tunnel on which to send the ACK.
3477 * @param type Type of message that triggered the ACK transmission.
3480 tunnel_send_ack (struct MeshTunnel *t, uint16_t type)
3482 struct GNUNET_MESH_ACK msg;
3483 struct GNUNET_PeerIdentity id;
3486 if (NULL != t->owner)
3488 send_client_tunnel_ack (t->owner, t);
3491 /* Is it after unicast / multicast retransmission? */
3492 if (GNUNET_MESSAGE_TYPE_MESH_ACK != type)
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ACK via DATA retransmission\n");
3495 if (GNUNET_YES == t->nobuffer)
3497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3500 if (t->queue_max > t->queue_n * 2)
3502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3507 /* Ok, ACK might be necessary, what PID to ACK? */
3508 ack = tunnel_get_fwd_ack (t);
3510 /* If speed_min and not all children have ack'd, dont send yet */
3511 if (ack == t->last_ack)
3513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, not ready\n");
3518 msg.pid = htonl (ack);
3520 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3522 msg.header.size = htons (sizeof (msg));
3523 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3524 msg.tid = htonl (t->id.tid);
3525 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3526 send_message (&msg.header, &id, t);
3531 * Send a message to all peers in this tunnel that the tunnel is no longer
3534 * @param t The tunnel whose peers to notify.
3537 tunnel_send_destroy (struct MeshTunnel *t)
3539 struct GNUNET_MESH_TunnelDestroy msg;
3541 msg.header.size = htons (sizeof (msg));
3542 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3543 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3544 msg.tid = htonl (t->id.tid);
3545 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3550 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3552 * @param cls Closure (Tunnel which to cancel).
3553 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3556 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3558 struct MeshTunnel *t = cls;
3559 struct MeshPeerInfo *peer_info;
3560 struct MeshPeerQueue *pq;
3561 struct MeshPeerQueue *next;
3563 peer_info = peer_info_get_short (neighbor_id);
3564 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3567 if (pq->tunnel == t)
3569 queue_destroy (pq, GNUNET_YES);
3572 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3574 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3575 peer_info->core_transmit = NULL;
3580 * Destroy the tunnel and free any allocated resources linked to it.
3582 * @param t the tunnel to destroy
3584 * @return GNUNET_OK on success
3587 tunnel_destroy (struct MeshTunnel *t)
3589 struct MeshClient *c;
3590 struct GNUNET_HashCode hash;
3597 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3603 struct GNUNET_PeerIdentity id;
3605 GNUNET_PEER_resolve (t->id.oid, &id);
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3607 GNUNET_i2s (&id), t->id.tid);
3609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3613 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3614 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3619 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3622 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3626 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3627 for (i = 0; i < t->nclients; i++)
3631 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3636 for (i = 0; i < t->nignore; i++)
3640 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3645 if (t->nclients > 0)
3648 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3652 GNUNET_free (t->clients);
3654 if (NULL != t->peers)
3656 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3658 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3661 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3662 &tunnel_destroy_child,
3664 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3666 tree_destroy (t->tree);
3668 if (NULL != t->regex_ctx)
3669 regex_cancel_search (t->regex_ctx);
3670 if (NULL != t->dht_get_type)
3671 GNUNET_DHT_get_stop (t->dht_get_type);
3672 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3673 GNUNET_SCHEDULER_cancel (t->timeout_task);
3674 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3675 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3678 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3679 GNUNET_assert (0 <= n_tunnels);
3686 * Create a new tunnel
3688 * @param owner Who is the owner of the tunnel (short ID).
3689 * @param tid Tunnel Number of the tunnel.
3690 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3691 * @param local Tunnel Number for the tunnel, for the client point of view.
3693 * @return A new initialized tunnel. NULL on error.
3695 static struct MeshTunnel *
3696 tunnel_new (GNUNET_PEER_Id owner,
3697 MESH_TunnelNumber tid,
3698 struct MeshClient *client,
3699 MESH_TunnelNumber local)
3701 struct MeshTunnel *t;
3702 struct GNUNET_HashCode hash;
3704 if (n_tunnels >= max_tunnels && NULL == client)
3707 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3710 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
3711 t->tree = tree_new (owner);
3713 t->local_tid = local;
3714 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
3716 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3718 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3720 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3721 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3728 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3735 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3737 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3738 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3742 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3752 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3753 * that are no longer needed, as well as nodes of no longer reachable peers.
3754 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3756 * @param t Tunnel from which to remove the path.
3757 * @param peer Short id of the peer which should be removed.
3760 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3762 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3768 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3769 * client when the client disconnects. If the client is not the owner, the
3770 * owner will get notified if no more clients are in the tunnel and the client
3771 * get removed from the tunnel's list.
3773 * @param cls closure (client that is disconnecting)
3774 * @param key the hash of the local tunnel id (used to access the hashmap)
3775 * @param value the value stored at the key (tunnel to destroy)
3777 * @return GNUNET_OK on success
3780 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3782 struct MeshTunnel *t = value;
3783 struct MeshClient *c = cls;
3786 send_client_tunnel_disconnect(t, c);
3789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3790 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3791 tunnel_delete_client(t, c);
3792 client_delete_tunnel(c, t);
3795 tunnel_send_destroy(t);
3796 r = tunnel_destroy (t);
3802 * Timeout function, destroys tunnel if called
3804 * @param cls Closure (tunnel to destroy).
3805 * @param tc TaskContext
3808 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3810 struct MeshTunnel *t = cls;
3812 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3814 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3819 * Resets the tunnel timeout. Starts it if no timeout was running.
3821 * @param t Tunnel whose timeout to reset.
3824 tunnel_reset_timeout (struct MeshTunnel *t)
3826 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3827 GNUNET_SCHEDULER_cancel (t->timeout_task);
3829 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3830 (refresh_path_time, 4), &tunnel_timeout, t);
3834 /******************************************************************************/
3835 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3836 /******************************************************************************/
3839 * Function to send a create path packet to a peer.
3841 * @param cls closure
3842 * @param size number of bytes available in buf
3843 * @param buf where the callee should write the message
3844 * @return number of bytes written to buf
3847 send_core_path_create (void *cls, size_t size, void *buf)
3849 struct MeshPathInfo *info = cls;
3850 struct GNUNET_MESH_ManipulatePath *msg;
3851 struct GNUNET_PeerIdentity *peer_ptr;
3852 struct MeshTunnel *t = info->t;
3853 struct MeshPeerPath *p = info->path;
3858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3860 sizeof (struct GNUNET_MESH_ManipulatePath) +
3861 p->length * sizeof (struct GNUNET_PeerIdentity);
3863 if (size < size_needed || NULL == buf)
3868 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3869 msg->header.size = htons (size_needed);
3870 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3871 msg->tid = ntohl (t->id.tid);
3873 if (GNUNET_YES == t->speed_min)
3874 opt = MESH_TUNNEL_OPT_SPEED_MIN;
3875 if (GNUNET_YES == t->nobuffer)
3876 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3877 msg->opt = htonl(opt);
3879 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3880 for (i = 0; i < p->length; i++)
3882 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3889 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3895 * Fill the core buffer
3897 * @param cls closure (data itself)
3898 * @param size number of bytes available in buf
3899 * @param buf where the callee should write the message
3901 * @return number of bytes written to buf
3904 send_core_data_multicast (void *cls, size_t size, void *buf)
3906 struct MeshTransmissionDescriptor *info = cls;
3909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3910 GNUNET_assert (NULL != info);
3911 GNUNET_assert (NULL != info->peer);
3912 total_size = info->mesh_data->data_len;
3913 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3915 if (total_size > size)
3920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3921 memcpy (buf, info->mesh_data->data, total_size);
3924 struct GNUNET_MESH_Multicast *mc;
3925 struct GNUNET_MessageHeader *mh;
3928 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3930 mc = (struct GNUNET_MESH_Multicast *) mh;
3931 mh = (struct GNUNET_MessageHeader *) &mc[1];
3932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3933 " multicast, payload type %u\n", ntohs (mh->type));
3934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3935 " multicast, payload size %u\n", ntohs (mh->size));
3939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3944 data_descriptor_decrement_rc (info->mesh_data);
3945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3953 * Creates a path ack message in buf and frees all unused resources.
3955 * @param cls closure (MeshTransmissionDescriptor)
3956 * @param size number of bytes available in buf
3957 * @param buf where the callee should write the message
3958 * @return number of bytes written to buf
3961 send_core_path_ack (void *cls, size_t size, void *buf)
3963 struct MeshTransmissionDescriptor *info = cls;
3964 struct GNUNET_MESH_PathACK *msg = buf;
3966 GNUNET_assert (NULL != info);
3967 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3972 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3973 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3974 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3975 msg->tid = htonl (info->origin->tid);
3976 msg->peer_id = my_full_id;
3979 /* TODO add signature */
3981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3982 return sizeof (struct GNUNET_MESH_PathACK);
3987 * Free a transmission that was already queued with all resources
3988 * associated to the request.
3990 * @param queue Queue handler to cancel.
3991 * @param clear_cls Is it necessary to free associated cls?
3994 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3996 struct MeshTransmissionDescriptor *dd;
3997 struct MeshPathInfo *path_info;
3999 if (GNUNET_YES == clear_cls)
4001 switch (queue->type)
4003 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4004 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4005 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4008 data_descriptor_decrement_rc (dd->mesh_data);
4010 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4012 path_info = queue->cls;
4013 path_destroy (path_info->path);
4017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4019 GNUNET_free_non_null (queue->cls);
4021 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4022 queue->peer->queue_tail,
4024 GNUNET_free (queue);
4029 * Core callback to write a queued packet to core buffer
4031 * @param cls Closure (peer info).
4032 * @param size Number of bytes available in buf.
4033 * @param buf Where the to write the message.
4035 * @return number of bytes written to buf
4038 queue_send (void *cls, size_t size, void *buf)
4040 struct MeshPeerInfo *peer = cls;
4041 struct GNUNET_MessageHeader *msg;
4042 struct MeshPeerQueue *queue;
4043 struct MeshTunnel *t;
4046 peer->core_transmit = NULL;
4047 queue = peer->queue_head;
4049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4051 /* If queue is empty, send should have been cancelled */
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4059 /* Check if buffer size is enough for the message */
4060 if (queue->size > size)
4062 struct GNUNET_PeerIdentity id;
4064 GNUNET_PEER_resolve (peer->id, &id);
4065 peer->core_transmit =
4066 GNUNET_CORE_notify_transmit_ready(core_handle,
4069 GNUNET_TIME_UNIT_FOREVER_REL,
4076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4082 switch (queue->type)
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
4086 data_size = send_core_data_raw (queue->cls, size, buf);
4087 msg = (struct GNUNET_MessageHeader *) buf;
4088 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
4089 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4091 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4093 data_size = send_core_data_multicast(queue->cls, size, buf);
4094 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4096 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4098 data_size = send_core_path_create(queue->cls, size, buf);
4100 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4102 data_size = send_core_path_ack(queue->cls, size, buf);
4106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
4110 /* Free queue, but cls was freed by send_core_* */
4111 queue_destroy (queue, GNUNET_NO);
4113 if (GNUNET_YES == t->destroy && 0 == t->queue_n)
4115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4119 /* If more data in queue, send next */
4120 if (NULL != peer->queue_head)
4122 struct GNUNET_PeerIdentity id;
4124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4125 GNUNET_PEER_resolve (peer->id, &id);
4126 peer->core_transmit =
4127 GNUNET_CORE_notify_transmit_ready(core_handle,
4130 GNUNET_TIME_UNIT_FOREVER_REL,
4132 peer->queue_head->size,
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4142 * Queue and pass message to core when possible.
4144 * @param cls Closure (type dependant).
4145 * @param type Type of the message, 0 for a raw message.
4146 * @param size Size of the message.
4147 * @param dst Neighbor to send message to.
4148 * @param t Tunnel this message belongs to.
4151 queue_add (void *cls, uint16_t type, size_t size,
4152 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4154 struct MeshPeerQueue *queue;
4156 if (t->queue_n >= t->queue_max)
4158 if (NULL == t->owner)
4159 GNUNET_break_op(0); // TODO: kill connection?
4162 return; // Drop message
4165 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4171 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4172 if (NULL == dst->core_transmit)
4174 struct GNUNET_PeerIdentity id;
4176 GNUNET_PEER_resolve (dst->id, &id);
4177 dst->core_transmit =
4178 GNUNET_CORE_notify_transmit_ready(core_handle,
4181 GNUNET_TIME_UNIT_FOREVER_REL,
4190 /******************************************************************************/
4191 /******************** MESH NETWORK HANDLERS **************************/
4192 /******************************************************************************/
4196 * Core handler for path creation
4198 * @param cls closure
4199 * @param message message
4200 * @param peer peer identity this notification is about
4201 * @param atsi performance data
4202 * @param atsi_count number of records in 'atsi'
4204 * @return GNUNET_OK to keep the connection open,
4205 * GNUNET_SYSERR to close it (signal serious error)
4208 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4209 const struct GNUNET_MessageHeader *message,
4210 const struct GNUNET_ATS_Information *atsi,
4211 unsigned int atsi_count)
4213 unsigned int own_pos;
4216 MESH_TunnelNumber tid;
4217 struct GNUNET_MESH_ManipulatePath *msg;
4218 struct GNUNET_PeerIdentity *pi;
4219 struct GNUNET_HashCode hash;
4220 struct MeshPeerPath *path;
4221 struct MeshPeerInfo *dest_peer_info;
4222 struct MeshPeerInfo *orig_peer_info;
4223 struct MeshTunnel *t;
4225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4226 "Received a path create msg [%s]\n",
4227 GNUNET_i2s (&my_full_id));
4228 size = ntohs (message->size);
4229 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4231 GNUNET_break_op (0);
4235 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4236 if (size % sizeof (struct GNUNET_PeerIdentity))
4238 GNUNET_break_op (0);
4241 size /= sizeof (struct GNUNET_PeerIdentity);
4244 GNUNET_break_op (0);
4247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4248 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4250 tid = ntohl (msg->tid);
4251 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4253 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4254 t = tunnel_get (pi, tid);
4255 if (NULL == t) // FIXME only for INCOMING tunnels?
4259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4260 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4263 // FIXME notify failure
4266 opt = ntohl (msg->opt);
4267 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4268 GNUNET_YES : GNUNET_NO;
4269 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4270 GNUNET_YES : GNUNET_NO;
4272 if (GNUNET_YES == t->nobuffer)
4275 while (NULL != tunnel_get_incoming (next_local_tid))
4276 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4277 t->local_tid_dest = next_local_tid++;
4278 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4280 tunnel_reset_timeout (t);
4281 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4283 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4284 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4292 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4293 if (NULL == dest_peer_info)
4295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4296 " Creating PeerInfo for destination.\n");
4297 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4298 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4299 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4303 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4304 if (NULL == orig_peer_info)
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4307 " Creating PeerInfo for origin.\n");
4308 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4309 orig_peer_info->id = GNUNET_PEER_intern (pi);
4310 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4311 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4314 path = path_new (size);
4316 for (i = 0; i < size; i++)
4318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4319 GNUNET_i2s (&pi[i]));
4320 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4321 if (path->peers[i] == myid)
4324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4327 /* cannot be self, must be 'not found' */
4328 /* create path: self not found in path through self */
4329 GNUNET_break_op (0);
4330 path_destroy (path);
4331 /* FIXME error. destroy tunnel? leave for timeout? */
4334 path_add_to_peers (path, GNUNET_NO);
4335 tunnel_add_path (t, path, own_pos);
4336 if (own_pos == size - 1)
4338 /* It is for us! Send ack. */
4339 struct MeshTransmissionDescriptor *info;
4341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4342 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4343 if (NULL == t->peers)
4345 /* New tunnel! Notify clients on data. */
4346 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4348 GNUNET_break (GNUNET_SYSERR !=
4349 GNUNET_CONTAINER_multihashmap_put (t->peers,
4350 &my_full_id.hashPubKey,
4353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4354 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4355 info->origin = &t->id;
4356 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4357 GNUNET_assert (NULL != info->peer);
4359 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4360 sizeof (struct GNUNET_MESH_PathACK),
4366 struct MeshPeerPath *path2;
4368 /* It's for somebody else! Retransmit. */
4369 path2 = path_duplicate (path);
4370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4371 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4372 path2 = path_duplicate (path);
4373 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4374 send_create_path (dest_peer_info, path, t);
4381 * Core handler for path destruction
4383 * @param cls closure
4384 * @param message message
4385 * @param peer peer identity this notification is about
4386 * @param atsi performance data
4387 * @param atsi_count number of records in 'atsi'
4389 * @return GNUNET_OK to keep the connection open,
4390 * GNUNET_SYSERR to close it (signal serious error)
4393 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4394 const struct GNUNET_MessageHeader *message,
4395 const struct GNUNET_ATS_Information *atsi,
4396 unsigned int atsi_count)
4398 struct GNUNET_MESH_ManipulatePath *msg;
4399 struct GNUNET_PeerIdentity *pi;
4400 struct MeshPeerPath *path;
4401 struct MeshTunnel *t;
4402 unsigned int own_pos;
4406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4407 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4408 size = ntohs (message->size);
4409 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4411 GNUNET_break_op (0);
4415 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4416 if (size % sizeof (struct GNUNET_PeerIdentity))
4418 GNUNET_break_op (0);
4421 size /= sizeof (struct GNUNET_PeerIdentity);
4424 GNUNET_break_op (0);
4427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4429 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4430 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4432 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4434 t = tunnel_get (pi, ntohl (msg->tid));
4437 /* TODO notify back: we don't know this tunnel */
4438 GNUNET_break_op (0);
4441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4442 path = path_new (size);
4444 for (i = 0; i < size; i++)
4446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4447 GNUNET_i2s (&pi[i]));
4448 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4449 if (path->peers[i] == myid)
4452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4453 if (own_pos < path->length - 1)
4454 send_message (message, &pi[own_pos + 1], t);
4456 send_client_tunnel_disconnect(t, NULL);
4458 tunnel_delete_peer (t, path->peers[path->length - 1]);
4459 path_destroy (path);
4465 * Core handler for notifications of broken paths
4467 * @param cls closure
4468 * @param message message
4469 * @param peer peer identity this notification is about
4470 * @param atsi performance data
4471 * @param atsi_count number of records in 'atsi'
4473 * @return GNUNET_OK to keep the connection open,
4474 * GNUNET_SYSERR to close it (signal serious error)
4477 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4478 const struct GNUNET_MessageHeader *message,
4479 const struct GNUNET_ATS_Information *atsi,
4480 unsigned int atsi_count)
4482 struct GNUNET_MESH_PathBroken *msg;
4483 struct MeshTunnel *t;
4485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4486 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4487 msg = (struct GNUNET_MESH_PathBroken *) message;
4488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4489 GNUNET_i2s (&msg->peer1));
4490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4491 GNUNET_i2s (&msg->peer2));
4492 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4495 GNUNET_break_op (0);
4498 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4499 GNUNET_PEER_search (&msg->peer2));
4506 * Core handler for tunnel destruction
4508 * @param cls closure
4509 * @param message message
4510 * @param peer peer identity this notification is about
4511 * @param atsi performance data
4512 * @param atsi_count number of records in 'atsi'
4514 * @return GNUNET_OK to keep the connection open,
4515 * GNUNET_SYSERR to close it (signal serious error)
4518 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4519 const struct GNUNET_MessageHeader *message,
4520 const struct GNUNET_ATS_Information *atsi,
4521 unsigned int atsi_count)
4523 struct GNUNET_MESH_TunnelDestroy *msg;
4524 struct MeshTunnel *t;
4526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4527 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4528 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4530 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4531 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4534 /* Probably already got the message from another path,
4535 * destroyed the tunnel and retransmitted to children.
4538 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4541 if (t->id.oid == myid)
4543 GNUNET_break_op (0);
4546 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4548 /* Tunnel was incoming, notify clients */
4549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4550 t->local_tid, t->local_tid_dest);
4551 send_clients_tunnel_destroy (t);
4553 tunnel_send_destroy (t);
4554 t->destroy = GNUNET_YES;
4555 // TODO: add timeout to destroy the tunnel anyway
4561 * Core handler for mesh network traffic going from the origin to a peer
4563 * @param cls closure
4564 * @param peer peer identity this notification is about
4565 * @param message message
4566 * @param atsi performance data
4567 * @param atsi_count number of records in 'atsi'
4568 * @return GNUNET_OK to keep the connection open,
4569 * GNUNET_SYSERR to close it (signal serious error)
4572 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4573 const struct GNUNET_MessageHeader *message,
4574 const struct GNUNET_ATS_Information *atsi,
4575 unsigned int atsi_count)
4577 struct GNUNET_MESH_Unicast *msg;
4578 struct GNUNET_PeerIdentity *neighbor;
4579 struct MeshTunnelChildInfo *cinfo;
4580 struct MeshTunnel *t;
4581 GNUNET_PEER_Id dest_id;
4586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4588 size = ntohs (message->size);
4590 sizeof (struct GNUNET_MESH_Unicast) +
4591 sizeof (struct GNUNET_MessageHeader))
4596 msg = (struct GNUNET_MESH_Unicast *) message;
4597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4598 ntohs (msg[1].header.type));
4599 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4602 /* TODO notify back: we don't know this tunnel */
4603 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4604 GNUNET_break_op (0);
4607 pid = ntohl (msg->pid);
4610 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4611 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4612 " Already seen pid %u, DROPPING!\n", pid);
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4618 " pid %u not seen yet, forwarding\n", pid);
4620 t->skip += (pid - t->pid) - 1;
4622 tunnel_reset_timeout (t);
4623 dest_id = GNUNET_PEER_search (&msg->destination);
4624 if (dest_id == myid)
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 " it's for us! sending to clients...\n");
4628 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4629 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4630 // FIXME send after client processes the packet
4631 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4634 ttl = ntohl (msg->ttl);
4635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4638 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4643 " not for us, retransmitting...\n");
4644 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4646 neighbor = tree_get_first_hop (t->tree, dest_id);
4647 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4648 &neighbor->hashPubKey);
4651 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
4652 cinfo->id = GNUNET_PEER_intern (neighbor);
4654 cinfo->max_pid = pid + t->queue_max - t->queue_n; // FIXME review
4656 GNUNET_assert (GNUNET_OK ==
4657 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
4658 &neighbor->hashPubKey,
4660 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
4663 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4666 send_message (message, neighbor, t);
4672 * Core handler for mesh network traffic going from the origin to all peers
4674 * @param cls closure
4675 * @param message message
4676 * @param peer peer identity this notification is about
4677 * @param atsi performance data
4678 * @param atsi_count number of records in 'atsi'
4679 * @return GNUNET_OK to keep the connection open,
4680 * GNUNET_SYSERR to close it (signal serious error)
4682 * TODO: Check who we got this from, to validate route.
4685 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4686 const struct GNUNET_MessageHeader *message,
4687 const struct GNUNET_ATS_Information *atsi,
4688 unsigned int atsi_count)
4690 struct GNUNET_MESH_Multicast *msg;
4691 struct MeshTunnel *t;
4695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
4697 size = ntohs (message->size);
4698 if (sizeof (struct GNUNET_MESH_Multicast) +
4699 sizeof (struct GNUNET_MessageHeader) > size)
4701 GNUNET_break_op (0);
4704 msg = (struct GNUNET_MESH_Multicast *) message;
4705 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4709 /* TODO notify that we dont know that tunnel */
4710 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4711 GNUNET_break_op (0);
4714 pid = ntohl (msg->pid);
4717 /* already seen this packet, drop */
4718 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4719 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4720 " Already seen pid %u, DROPPING!\n", pid);
4725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4726 " pid %u not seen yet, forwarding\n", pid);
4728 t->skip += (pid - t->pid) - 1;
4730 tunnel_reset_timeout (t);
4732 /* Transmit to locally interested clients */
4733 if (NULL != t->peers &&
4734 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
4736 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
4737 send_subscribed_clients (message, &msg[1].header);
4739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
4740 if (ntohl (msg->ttl) == 0)
4742 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4743 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4746 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
4747 tunnel_send_multicast (t, message, GNUNET_NO);
4753 * Core handler for mesh network traffic toward the owner of a tunnel
4755 * @param cls closure
4756 * @param message message
4757 * @param peer peer identity this notification is about
4758 * @param atsi performance data
4759 * @param atsi_count number of records in 'atsi'
4761 * @return GNUNET_OK to keep the connection open,
4762 * GNUNET_SYSERR to close it (signal serious error)
4765 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4766 const struct GNUNET_MessageHeader *message,
4767 const struct GNUNET_ATS_Information *atsi,
4768 unsigned int atsi_count)
4770 struct GNUNET_MESH_ToOrigin *msg;
4771 struct GNUNET_PeerIdentity id;
4772 struct MeshPeerInfo *peer_info;
4773 struct MeshTunnel *t;
4776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4778 size = ntohs (message->size);
4779 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4780 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4782 GNUNET_break_op (0);
4785 msg = (struct GNUNET_MESH_ToOrigin *) message;
4786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4787 ntohs (msg[1].header.type));
4788 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4792 /* TODO notify that we dont know this tunnel (whom)? */
4793 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4794 GNUNET_break_op (0);
4798 if (t->id.oid == myid)
4801 struct GNUNET_MESH_ToOrigin *copy;
4803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4804 " it's for us! sending to clients...\n");
4805 if (NULL == t->owner)
4807 /* got data packet for ownerless tunnel */
4808 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
4810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4811 GNUNET_break_op (0);
4814 /* TODO signature verification */
4815 memcpy (cbuf, message, size);
4816 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4817 copy->tid = htonl (t->local_tid);
4818 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4819 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4820 ©->header, GNUNET_NO);
4823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4824 " not for us, retransmitting...\n");
4826 peer_info = peer_info_get (&msg->oid);
4827 if (NULL == peer_info)
4829 /* unknown origin of tunnel */
4833 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4834 send_message (message, &id, t);
4835 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4842 * Core handler for mesh network traffic point-to-point acks.
4844 * @param cls closure
4845 * @param message message
4846 * @param peer peer identity this notification is about
4847 * @param atsi performance data
4848 * @param atsi_count number of records in 'atsi'
4850 * @return GNUNET_OK to keep the connection open,
4851 * GNUNET_SYSERR to close it (signal serious error)
4854 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4855 const struct GNUNET_MessageHeader *message,
4856 const struct GNUNET_ATS_Information *atsi,
4857 unsigned int atsi_count)
4859 struct GNUNET_MESH_ACK *msg;
4860 struct MeshTunnelChildInfo *cinfo;
4861 struct MeshTunnel *t;
4864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got an ACK packet from %s\n",
4866 msg = (struct GNUNET_MESH_ACK *) message;
4867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4868 ntohs (msg[1].header.type));
4869 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4873 /* TODO notify that we dont know this tunnel (whom)? */
4874 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4875 GNUNET_break_op (0);
4878 ack = ntohl (msg->pid);
4879 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4883 GNUNET_break_op (0);
4886 cinfo->max_pid = ack;
4887 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4893 * Core handler for path ACKs
4895 * @param cls closure
4896 * @param message message
4897 * @param peer peer identity this notification is about
4898 * @param atsi performance data
4899 * @param atsi_count number of records in 'atsi'
4901 * @return GNUNET_OK to keep the connection open,
4902 * GNUNET_SYSERR to close it (signal serious error)
4905 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4906 const struct GNUNET_MessageHeader *message,
4907 const struct GNUNET_ATS_Information *atsi,
4908 unsigned int atsi_count)
4910 struct GNUNET_MESH_PathACK *msg;
4911 struct GNUNET_PeerIdentity id;
4912 struct MeshPeerInfo *peer_info;
4913 struct MeshPeerPath *p;
4914 struct MeshTunnel *t;
4916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4917 GNUNET_i2s (&my_full_id));
4918 msg = (struct GNUNET_MESH_PathACK *) message;
4919 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4922 /* TODO notify that we don't know the tunnel */
4923 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4925 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4929 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4931 peer_info = peer_info_get (&msg->peer_id);
4932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4933 GNUNET_i2s (&msg->peer_id));
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4937 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
4939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4940 "connect_by_string completed, stopping search\n");
4941 regex_cancel_search (t->regex_ctx);
4942 t->regex_ctx = NULL;
4945 /* Add paths to peers? */
4946 p = tree_get_path_to_peer (t->tree, peer_info->id);
4949 path_add_to_peers (p, GNUNET_YES);
4957 /* Message for us? */
4958 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4961 if (NULL == t->owner)
4963 GNUNET_break_op (0);
4966 if (NULL != t->dht_get_type)
4968 GNUNET_DHT_get_stop (t->dht_get_type);
4969 t->dht_get_type = NULL;
4971 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4973 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4974 send_client_peer_connected (t, peer_info->id);
4979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4980 " not for us, retransmitting...\n");
4981 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4982 peer_info = peer_info_get (&msg->oid);
4983 if (NULL == peer_info)
4985 /* If we know the tunnel, we should DEFINITELY know the peer */
4989 send_message (message, &id, t);
4995 * Functions to handle messages from core
4997 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4998 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4999 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5000 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5001 sizeof (struct GNUNET_MESH_PathBroken)},
5002 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5003 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5004 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5005 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5006 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5007 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5008 sizeof (struct GNUNET_MESH_ACK)},
5009 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5010 sizeof (struct GNUNET_MESH_PathACK)},
5016 /******************************************************************************/
5017 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5018 /******************************************************************************/
5021 * deregister_app: iterator for removing each application registered by a client
5023 * @param cls closure
5024 * @param key the hash of the application id (used to access the hashmap)
5025 * @param value the value stored at the key (client)
5027 * @return GNUNET_OK on success
5030 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5032 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5033 GNUNET_break (GNUNET_YES ==
5034 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5040 * notify_client_connection_failure: notify a client that the connection to the
5041 * requested remote peer is not possible (for instance, no route found)
5042 * Function called when the socket is ready to queue more data. "buf" will be
5043 * NULL and "size" zero if the socket was closed for writing in the meantime.
5045 * @param cls closure
5046 * @param size number of bytes available in buf
5047 * @param buf where the callee should write the message
5048 * @return number of bytes written to buf
5051 notify_client_connection_failure (void *cls, size_t size, void *buf)
5054 struct MeshPeerInfo *peer_info;
5055 struct GNUNET_MESH_PeerControl *msg;
5056 struct GNUNET_PeerIdentity id;
5058 if (0 == size && NULL == buf)
5060 // TODO retry? cancel?
5064 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5065 peer_info = (struct MeshPeerInfo *) cls;
5066 msg = (struct GNUNET_MESH_PeerControl *) buf;
5067 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5068 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5069 // msg->tunnel_id = htonl(peer_info->t->tid);
5070 GNUNET_PEER_resolve (peer_info->id, &id);
5071 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5079 * Send keepalive packets for a peer
5081 * @param cls Closure (tunnel for which to send the keepalive).
5082 * @param tc Notification context.
5084 * TODO: implement explicit multicast keepalive?
5087 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5089 struct MeshTunnel *t = cls;
5090 struct GNUNET_MessageHeader *payload;
5091 struct GNUNET_MESH_Multicast *msg;
5093 sizeof (struct GNUNET_MESH_Multicast) +
5094 sizeof (struct GNUNET_MessageHeader);
5097 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5101 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5104 "sending keepalive for tunnel %d\n", t->id.tid);
5106 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5107 msg->header.size = htons (size);
5108 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5109 msg->oid = my_full_id;
5110 msg->tid = htonl (t->id.tid);
5111 msg->ttl = htonl (default_ttl);
5112 msg->pid = htonl (t->pid + 1);
5114 payload = (struct GNUNET_MessageHeader *) &msg[1];
5115 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5116 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5117 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5119 t->path_refresh_task =
5120 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5126 * Function to process paths received for a new peer addition. The recorded
5127 * paths form the initial tunnel, which can be optimized later.
5128 * Called on each result obtained for the DHT search.
5130 * @param cls closure
5131 * @param exp when will this value expire
5132 * @param key key of the result
5133 * @param get_path path of the get request
5134 * @param get_path_length lenght of get_path
5135 * @param put_path path of the put request
5136 * @param put_path_length length of the put_path
5137 * @param type type of the result
5138 * @param size number of bytes in data
5139 * @param data pointer to the result data
5141 * TODO: re-issue the request after certain time? cancel after X results?
5144 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5145 const struct GNUNET_HashCode * key,
5146 const struct GNUNET_PeerIdentity *get_path,
5147 unsigned int get_path_length,
5148 const struct GNUNET_PeerIdentity *put_path,
5149 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5150 size_t size, const void *data)
5152 struct MeshPathInfo *path_info = cls;
5153 struct MeshPeerPath *p;
5154 struct GNUNET_PeerIdentity pi;
5157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5158 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5161 p = path_build_from_dht (get_path, get_path_length, put_path,
5163 path_add_to_peers (p, GNUNET_NO);
5165 for (i = 0; i < path_info->peer->ntunnels; i++)
5167 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5168 peer_info_connect (path_info->peer, path_info->t);
5176 * Function to process paths received for a new peer addition. The recorded
5177 * paths form the initial tunnel, which can be optimized later.
5178 * Called on each result obtained for the DHT search.
5180 * @param cls closure
5181 * @param exp when will this value expire
5182 * @param key key of the result
5183 * @param get_path path of the get request
5184 * @param get_path_length lenght of get_path
5185 * @param put_path path of the put request
5186 * @param put_path_length length of the put_path
5187 * @param type type of the result
5188 * @param size number of bytes in data
5189 * @param data pointer to the result data
5192 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5193 const struct GNUNET_HashCode * key,
5194 const struct GNUNET_PeerIdentity *get_path,
5195 unsigned int get_path_length,
5196 const struct GNUNET_PeerIdentity *put_path,
5197 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5198 size_t size, const void *data)
5200 const struct PBlock *pb = data;
5201 const struct GNUNET_PeerIdentity *pi = &pb->id;
5202 struct MeshTunnel *t = cls;
5203 struct MeshPeerInfo *peer_info;
5204 struct MeshPeerPath *p;
5206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5207 if (size != sizeof (struct PBlock))
5209 GNUNET_break_op (0);
5212 if (ntohl(pb->type) != t->type)
5214 GNUNET_break_op (0);
5217 GNUNET_assert (NULL != t->owner);
5218 peer_info = peer_info_get (pi);
5219 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5221 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5223 p = path_build_from_dht (get_path, get_path_length, put_path,
5225 path_add_to_peers (p, GNUNET_NO);
5227 tunnel_add_peer (t, peer_info);
5228 peer_info_connect (peer_info, t);
5233 * Function to process DHT string to regex matching.
5234 * Called on each result obtained for the DHT search.
5236 * @param cls closure (search context)
5237 * @param exp when will this value expire
5238 * @param key key of the result
5239 * @param get_path path of the get request (not used)
5240 * @param get_path_length lenght of get_path (not used)
5241 * @param put_path path of the put request (not used)
5242 * @param put_path_length length of the put_path (not used)
5243 * @param type type of the result
5244 * @param size number of bytes in data
5245 * @param data pointer to the result data
5248 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5249 const struct GNUNET_HashCode * key,
5250 const struct GNUNET_PeerIdentity *get_path,
5251 unsigned int get_path_length,
5252 const struct GNUNET_PeerIdentity *put_path,
5253 unsigned int put_path_length,
5254 enum GNUNET_BLOCK_Type type,
5255 size_t size, const void *data)
5257 const struct MeshRegexAccept *block = data;
5258 struct MeshRegexSearchContext *ctx = cls;
5259 struct MeshRegexSearchInfo *info = ctx->info;
5260 struct MeshPeerPath *p;
5261 struct MeshPeerInfo *peer_info;
5263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5266 peer_info = peer_info_get(&block->id);
5267 p = path_build_from_dht (get_path, get_path_length, put_path,
5269 path_add_to_peers (p, GNUNET_NO);
5272 tunnel_add_peer (info->t, peer_info);
5273 peer_info_connect (peer_info, info->t);
5274 if (0 == info->peer)
5276 info->peer = peer_info->id;
5280 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5283 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5284 ®ex_connect_timeout,
5292 * Function to process DHT string to regex matching.
5293 * Called on each result obtained for the DHT search.
5295 * @param cls closure (search context)
5296 * @param exp when will this value expire
5297 * @param key key of the result
5298 * @param get_path path of the get request (not used)
5299 * @param get_path_length lenght of get_path (not used)
5300 * @param put_path path of the put request (not used)
5301 * @param put_path_length length of the put_path (not used)
5302 * @param type type of the result
5303 * @param size number of bytes in data
5304 * @param data pointer to the result data
5306 * TODO: re-issue the request after certain time? cancel after X results?
5309 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5310 const struct GNUNET_HashCode * key,
5311 const struct GNUNET_PeerIdentity *get_path,
5312 unsigned int get_path_length,
5313 const struct GNUNET_PeerIdentity *put_path,
5314 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5315 size_t size, const void *data)
5317 const struct MeshRegexBlock *block = data;
5318 struct MeshRegexSearchContext *ctx = cls;
5319 struct MeshRegexSearchInfo *info = ctx->info;
5323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5324 "DHT GET STRING RETURNED RESULTS\n");
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5326 " key: %s\n", GNUNET_h2s (key));
5328 copy = GNUNET_malloc (size);
5329 memcpy (copy, data, size);
5330 GNUNET_break (GNUNET_OK ==
5331 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5332 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5333 len = ntohl (block->n_proof);
5335 char proof[len + 1];
5337 memcpy (proof, &block[1], len);
5339 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5341 GNUNET_break_op (0);
5345 len = strlen (info->description);
5346 if (len == ctx->position) // String processed
5348 if (GNUNET_YES == ntohl (block->accepting))
5350 regex_find_path(key, ctx);
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5355 // FIXME REGEX this block not successful, wait for more? start timeout?
5359 GNUNET_break (GNUNET_OK ==
5360 GNUNET_MESH_regex_block_iterate (block, size,
5361 ®ex_edge_iterator, ctx));
5365 /******************************************************************************/
5366 /********************* MESH LOCAL HANDLES **************************/
5367 /******************************************************************************/
5371 * Handler for client disconnection
5373 * @param cls closure
5374 * @param client identification of the client; NULL
5375 * for the last call when the server is destroyed
5378 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5380 struct MeshClient *c;
5381 struct MeshClient *next;
5384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5393 if (c->handle != client)
5395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5401 GNUNET_SERVER_client_drop (c->handle);
5402 c->shutting_down = GNUNET_YES;
5403 GNUNET_assert (NULL != c->own_tunnels);
5404 GNUNET_assert (NULL != c->incoming_tunnels);
5405 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5406 &tunnel_destroy_iterator, c);
5407 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5408 &tunnel_destroy_iterator, c);
5409 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5410 &tunnel_destroy_iterator, c);
5411 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5412 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5413 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5415 /* deregister clients applications */
5416 if (NULL != c->apps)
5418 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5419 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5421 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5422 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5424 GNUNET_SCHEDULER_cancel (announce_applications_task);
5425 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5427 if (NULL != c->types)
5428 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5429 for (i = 0; i < c->n_regex; i++)
5431 GNUNET_free (c->regexes[i]);
5433 GNUNET_free_non_null (c->regexes);
5434 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5435 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5437 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5440 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5449 * Handler for new clients
5451 * @param cls closure
5452 * @param client identification of the client
5453 * @param message the actual message, which includes messages the client wants
5456 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5457 const struct GNUNET_MessageHeader *message)
5459 struct GNUNET_MESH_ClientConnect *cc_msg;
5460 struct MeshClient *c;
5461 GNUNET_MESH_ApplicationType *a;
5468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5469 /* Check data sanity */
5470 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5471 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5472 ntypes = ntohs (cc_msg->types);
5473 napps = ntohs (cc_msg->applications);
5475 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5478 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5482 /* Create new client structure */
5483 c = GNUNET_malloc (sizeof (struct MeshClient));
5484 c->id = next_client_id++;
5485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5487 GNUNET_SERVER_client_keep (client);
5488 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5491 GNUNET_MESH_ApplicationType at;
5492 struct GNUNET_HashCode hc;
5494 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5495 for (i = 0; i < napps; i++)
5498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5499 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5500 /* store in clients hashmap */
5501 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5502 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5503 /* store in global hashmap, for announcements */
5504 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5507 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5508 announce_applications_task =
5509 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5515 struct GNUNET_HashCode hc;
5517 t = (uint16_t *) & a[napps];
5518 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5519 for (i = 0; i < ntypes; i++)
5522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5523 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5525 /* store in clients hashmap */
5526 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5527 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5528 /* store in global hashmap */
5529 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5530 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5534 " client has %u+%u subscriptions\n", napps, ntypes);
5536 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5537 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5538 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5539 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5540 GNUNET_SERVER_notification_context_add (nc, client);
5541 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5549 * Handler for clients announcing available services by a regular expression.
5551 * @param cls closure
5552 * @param client identification of the client
5553 * @param message the actual message, which includes messages the client wants
5556 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5557 const struct GNUNET_MessageHeader *message)
5559 struct MeshClient *c;
5563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5565 /* Sanity check for client registration */
5566 if (NULL == (c = client_get (client)))
5569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5574 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5575 regex = GNUNET_malloc (len + 1);
5576 memcpy (regex, &message[1], len);
5578 GNUNET_array_append (c->regexes, c->n_regex, regex);
5579 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5581 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5587 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5593 * Handler for requests of new tunnels
5595 * @param cls closure
5596 * @param client identification of the client
5597 * @param message the actual message
5600 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5601 const struct GNUNET_MessageHeader *message)
5603 struct GNUNET_MESH_TunnelMessage *t_msg;
5604 struct MeshTunnel *t;
5605 struct MeshClient *c;
5607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5609 /* Sanity check for client registration */
5610 if (NULL == (c = client_get (client)))
5613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5618 /* Message sanity check */
5619 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5622 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5626 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5627 /* Sanity check for tunnel numbering */
5628 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5631 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5634 /* Sanity check for duplicate tunnel IDs */
5635 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
5638 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5642 while (NULL != tunnel_get_by_pi (myid, next_tid))
5643 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5644 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
5647 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5649 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5652 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5654 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5655 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
5657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5658 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5664 * Handler for requests of deleting tunnels
5666 * @param cls closure
5667 * @param client identification of the client
5668 * @param message the actual message
5671 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5672 const struct GNUNET_MessageHeader *message)
5674 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5675 struct MeshClient *c;
5676 struct MeshTunnel *t;
5677 MESH_TunnelNumber tid;
5679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5680 "Got a DESTROY TUNNEL from client!\n");
5682 /* Sanity check for client registration */
5683 if (NULL == (c = client_get (client)))
5686 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5689 /* Message sanity check */
5690 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5697 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5699 /* Retrieve tunnel */
5700 tid = ntohl (tunnel_msg->tunnel_id);
5701 t = tunnel_get_by_local_id(c, tid);
5704 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5706 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5709 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5711 client_ignore_tunnel (c, t);
5713 // TODO: when to destroy incoming tunnel?
5714 if (t->nclients == 0)
5716 GNUNET_assert (GNUNET_YES ==
5717 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
5719 GNUNET_assert (GNUNET_YES ==
5720 GNUNET_CONTAINER_multihashmap_remove (t->peers,
5721 &my_full_id.hashPubKey,
5725 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5728 send_client_tunnel_disconnect(t, c);
5729 client_delete_tunnel(c, t);
5731 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5733 tunnel_send_destroy (t);
5734 t->destroy = GNUNET_YES;
5735 // The tunnel will be destroyed when the last message is transmitted.
5736 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5742 * Handler for requests of seeting tunnel's speed.
5744 * @param cls Closure (unused).
5745 * @param client Identification of the client.
5746 * @param message The actual message.
5749 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
5750 const struct GNUNET_MessageHeader *message)
5752 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5753 struct MeshClient *c;
5754 struct MeshTunnel *t;
5755 MESH_TunnelNumber tid;
5757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5758 "Got a SPEED request from client!\n");
5760 /* Sanity check for client registration */
5761 if (NULL == (c = client_get (client)))
5764 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5769 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5771 /* Retrieve tunnel */
5772 tid = ntohl (tunnel_msg->tunnel_id);
5773 t = tunnel_get_by_local_id(c, tid);
5776 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
5778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5782 switch (ntohs(message->type))
5784 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
5785 t->speed_min = GNUNET_YES;
5787 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
5788 t->speed_min = GNUNET_NO;
5793 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5798 * Handler for requests of seeting tunnel's buffering policy.
5800 * @param cls Closure (unused).
5801 * @param client Identification of the client.
5802 * @param message The actual message.
5805 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5806 const struct GNUNET_MessageHeader *message)
5808 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5809 struct MeshClient *c;
5810 struct MeshTunnel *t;
5811 MESH_TunnelNumber tid;
5813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5814 "Got a BUFFER request from client!\n");
5816 /* Sanity check for client registration */
5817 if (NULL == (c = client_get (client)))
5820 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5825 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5827 /* Retrieve tunnel */
5828 tid = ntohl (tunnel_msg->tunnel_id);
5829 t = tunnel_get_by_local_id(c, tid);
5832 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5834 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5838 switch (ntohs(message->type))
5840 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5841 t->nobuffer = GNUNET_NO;
5843 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5844 t->nobuffer = GNUNET_YES;
5850 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5855 * Handler for connection requests to new peers
5857 * @param cls closure
5858 * @param client identification of the client
5859 * @param message the actual message (PeerControl)
5862 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
5863 const struct GNUNET_MessageHeader *message)
5865 struct GNUNET_MESH_PeerControl *peer_msg;
5866 struct MeshPeerInfo *peer_info;
5867 struct MeshClient *c;
5868 struct MeshTunnel *t;
5869 MESH_TunnelNumber tid;
5871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
5872 /* Sanity check for client registration */
5873 if (NULL == (c = client_get (client)))
5876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5880 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5881 /* Sanity check for message size */
5882 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5885 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5889 /* Tunnel exists? */
5890 tid = ntohl (peer_msg->tunnel_id);
5891 t = tunnel_get_by_local_id (c, tid);
5895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5899 /* Does client own tunnel? */
5900 if (t->owner->handle != client)
5903 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
5907 GNUNET_i2s (&peer_msg->peer));
5908 peer_info = peer_info_get (&peer_msg->peer);
5910 tunnel_add_peer (t, peer_info);
5911 peer_info_connect (peer_info, t);
5913 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5919 * Handler for disconnection requests of peers in a tunnel
5921 * @param cls closure
5922 * @param client identification of the client
5923 * @param message the actual message (PeerControl)
5926 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
5927 const struct GNUNET_MessageHeader *message)
5929 struct GNUNET_MESH_PeerControl *peer_msg;
5930 struct MeshPeerInfo *peer_info;
5931 struct MeshClient *c;
5932 struct MeshTunnel *t;
5933 MESH_TunnelNumber tid;
5935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
5936 /* Sanity check for client registration */
5937 if (NULL == (c = client_get (client)))
5940 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5943 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5944 /* Sanity check for message size */
5945 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5948 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5952 /* Tunnel exists? */
5953 tid = ntohl (peer_msg->tunnel_id);
5954 t = tunnel_get_by_local_id (c, tid);
5958 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5963 /* Does client own tunnel? */
5964 if (t->owner->handle != client)
5967 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
5972 GNUNET_i2s (&peer_msg->peer));
5973 /* Is the peer in the tunnel? */
5975 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
5976 if (NULL == peer_info)
5979 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5983 /* Ok, delete peer from tunnel */
5984 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
5985 &peer_msg->peer.hashPubKey);
5987 send_destroy_path (t, peer_info->id);
5988 tunnel_delete_peer (t, peer_info->id);
5989 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5994 * Handler for blacklist requests of peers in a tunnel
5996 * @param cls closure
5997 * @param client identification of the client
5998 * @param message the actual message (PeerControl)
6001 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6002 const struct GNUNET_MessageHeader *message)
6004 struct GNUNET_MESH_PeerControl *peer_msg;
6005 struct MeshClient *c;
6006 struct MeshTunnel *t;
6007 MESH_TunnelNumber tid;
6009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6010 /* Sanity check for client registration */
6011 if (NULL == (c = client_get (client)))
6014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6017 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6019 /* Sanity check for message size */
6020 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6027 /* Tunnel exists? */
6028 tid = ntohl (peer_msg->tunnel_id);
6029 t = tunnel_get_by_local_id (c, tid);
6033 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6038 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6039 GNUNET_PEER_intern(&peer_msg->peer));
6044 * Handler for unblacklist requests of peers in a tunnel
6046 * @param cls closure
6047 * @param client identification of the client
6048 * @param message the actual message (PeerControl)
6051 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6052 const struct GNUNET_MessageHeader *message)
6054 struct GNUNET_MESH_PeerControl *peer_msg;
6055 struct MeshClient *c;
6056 struct MeshTunnel *t;
6057 MESH_TunnelNumber tid;
6061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6062 /* Sanity check for client registration */
6063 if (NULL == (c = client_get (client)))
6066 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6069 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6071 /* Sanity check for message size */
6072 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6075 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6079 /* Tunnel exists? */
6080 tid = ntohl (peer_msg->tunnel_id);
6081 t = tunnel_get_by_local_id (c, tid);
6085 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6090 /* if peer is not known, complain */
6091 pid = GNUNET_PEER_search (&peer_msg->peer);
6098 /* search and remove from list */
6099 for (i = 0; i < t->nblacklisted; i++)
6101 if (t->blacklisted[i] == pid)
6103 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6104 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6109 /* if peer hasn't been blacklisted, complain */
6115 * Handler for connection requests to new peers by type
6117 * @param cls closure
6118 * @param client identification of the client
6119 * @param message the actual message (ConnectPeerByType)
6122 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6123 const struct GNUNET_MessageHeader *message)
6125 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6126 struct MeshClient *c;
6127 struct MeshTunnel *t;
6128 struct GNUNET_HashCode hash;
6129 MESH_TunnelNumber tid;
6131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6132 /* Sanity check for client registration */
6133 if (NULL == (c = client_get (client)))
6136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6140 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6141 /* Sanity check for message size */
6142 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6143 ntohs (connect_msg->header.size))
6146 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6150 /* Tunnel exists? */
6151 tid = ntohl (connect_msg->tunnel_id);
6152 t = tunnel_get_by_local_id (c, tid);
6156 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6160 /* Does client own tunnel? */
6161 if (t->owner->handle != client)
6164 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6168 /* Do WE have the service? */
6169 t->type = ntohl (connect_msg->type);
6170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6171 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6172 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6175 /* Yes! Fast forward, add ourselves to the tunnel and send the
6176 * good news to the client, and alert the destination client of
6177 * an incoming tunnel.
6179 * FIXME send a path create to self, avoid code duplication
6181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6182 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6183 peer_info_get (&my_full_id),
6184 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6187 send_client_peer_connected (t, myid);
6188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6189 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6191 t->local_tid_dest = next_local_tid++;
6192 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6193 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6194 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6198 /* Ok, lets find a peer offering the service */
6199 if (NULL != t->dht_get_type)
6201 GNUNET_DHT_get_stop (t->dht_get_type);
6203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6204 GNUNET_h2s (&hash));
6206 GNUNET_DHT_get_start (dht_handle,
6207 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6209 dht_replication_level,
6210 GNUNET_DHT_RO_RECORD_ROUTE |
6211 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6213 &dht_get_type_handler, t);
6215 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6221 * Handler for connection requests to new peers by a string service description.
6223 * @param cls closure
6224 * @param client identification of the client
6225 * @param message the actual message, which includes messages the client wants
6228 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6229 const struct GNUNET_MessageHeader *message)
6231 struct GNUNET_MESH_ConnectPeerByString *msg;
6232 struct MeshRegexSearchContext *ctx;
6233 struct MeshRegexSearchInfo *info;
6234 struct GNUNET_DHT_GetHandle *get_h;
6235 struct GNUNET_HashCode key;
6236 struct MeshTunnel *t;
6237 struct MeshClient *c;
6238 MESH_TunnelNumber tid;
6242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6243 "Connect by string started\n");
6244 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6245 size = htons (message->size);
6247 /* Sanity check for client registration */
6248 if (NULL == (c = client_get (client)))
6251 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6255 /* Message size sanity check */
6256 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6259 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6263 /* Tunnel exists? */
6264 tid = ntohl (msg->tunnel_id);
6265 t = tunnel_get_by_local_id (c, tid);
6269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6273 /* Does client own tunnel? */
6274 if (t->owner->handle != client)
6277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6282 " on tunnel %s [%u]\n",
6283 GNUNET_i2s(&my_full_id),
6286 /* Only one connect_by_string allowed at the same time! */
6287 /* FIXME: allow more, return handle at api level to cancel, document */
6288 if (NULL != t->regex_ctx)
6291 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6295 /* Find string itself */
6296 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6297 string = (const char *) &msg[1];
6299 /* Initialize context */
6300 size = GNUNET_REGEX_get_first_key(string, len, &key);
6301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6302 " consumed %u bits out of %u\n", size, len);
6303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6304 " looking for %s\n", GNUNET_h2s (&key));
6306 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6308 info->description = GNUNET_malloc (len + 1);
6309 memcpy (info->description, string, len);
6310 info->description[len] = '\0';
6311 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6312 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6315 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6316 ctx->position = size;
6320 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6322 /* Start search in DHT */
6323 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6324 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6325 &key, /* key to search */
6326 dht_replication_level, /* replication level */
6327 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6328 NULL, /* xquery */ // FIXME BLOOMFILTER
6329 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6330 &dht_get_string_handler, ctx);
6332 GNUNET_break (GNUNET_OK ==
6333 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6338 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6344 * Handler for client traffic directed to one peer
6346 * @param cls closure
6347 * @param client identification of the client
6348 * @param message the actual message
6351 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6352 const struct GNUNET_MessageHeader *message)
6354 struct MeshClient *c;
6355 struct MeshTunnel *t;
6356 struct MeshPeerInfo *pi;
6357 struct GNUNET_MESH_Unicast *data_msg;
6358 MESH_TunnelNumber tid;
6361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6362 "Got a unicast request from a client!\n");
6364 /* Sanity check for client registration */
6365 if (NULL == (c = client_get (client)))
6368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6371 data_msg = (struct GNUNET_MESH_Unicast *) message;
6372 /* Sanity check for message size */
6373 size = ntohs (message->size);
6374 if (sizeof (struct GNUNET_MESH_Unicast) +
6375 sizeof (struct GNUNET_MessageHeader) > size)
6378 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6382 /* Tunnel exists? */
6383 tid = ntohl (data_msg->tid);
6384 t = tunnel_get_by_local_id (c, tid);
6388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6392 /* Is it a local tunnel? Then, does client own the tunnel? */
6393 if (t->owner->handle != client)
6396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6400 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6401 &data_msg->destination.hashPubKey);
6402 /* Is the selected peer in the tunnel? */
6406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6410 /* Ok, everything is correct, send the message
6411 * (pretend we got it from a mesh peer)
6414 char buf[ntohs (message->size)] GNUNET_ALIGN;
6415 struct GNUNET_MESH_Unicast *copy;
6417 /* Work around const limitation */
6418 copy = (struct GNUNET_MESH_Unicast *) buf;
6419 memcpy (buf, data_msg, size);
6420 copy->oid = my_full_id;
6421 copy->tid = htonl (t->id.tid);
6422 copy->ttl = htonl (default_ttl);
6423 copy->pid = htonl (t->pid + 1);
6424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6425 " calling generic handler...\n");
6426 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6429 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6435 * Handler for client traffic directed to the origin
6437 * @param cls closure
6438 * @param client identification of the client
6439 * @param message the actual message
6442 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6443 const struct GNUNET_MessageHeader *message)
6445 struct GNUNET_MESH_ToOrigin *data_msg;
6446 struct GNUNET_PeerIdentity id;
6447 struct MeshClient *c;
6448 struct MeshTunnel *t;
6449 MESH_TunnelNumber tid;
6452 /* Sanity check for client registration */
6453 if (NULL == (c = client_get (client)))
6456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6459 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6460 /* Sanity check for message size */
6461 size = ntohs (message->size);
6462 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6463 sizeof (struct GNUNET_MessageHeader) > size)
6466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6470 /* Tunnel exists? */
6471 tid = ntohl (data_msg->tid);
6472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6473 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6474 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6480 t = tunnel_get_by_local_id (c, tid);
6484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6488 /* It should be sent by someone who has this as incoming tunnel. */
6489 if (-1 == client_knows_tunnel (c, t))
6492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6495 GNUNET_PEER_resolve (t->id.oid, &id);
6497 /* Ok, everything is correct, send the message
6498 * (pretend we got it from a mesh peer)
6501 char buf[ntohs (message->size)] GNUNET_ALIGN;
6502 struct GNUNET_MESH_ToOrigin *copy;
6504 /* Work around const limitation */
6505 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6506 memcpy (buf, data_msg, size);
6508 copy->tid = htonl (t->id.tid);
6509 copy->sender = my_full_id;
6510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6511 " calling generic handler...\n");
6512 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6514 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6520 * Handler for client traffic directed to all peers in a tunnel
6522 * @param cls closure
6523 * @param client identification of the client
6524 * @param message the actual message
6527 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6528 const struct GNUNET_MessageHeader *message)
6530 struct MeshClient *c;
6531 struct MeshTunnel *t;
6532 struct GNUNET_MESH_Multicast *data_msg;
6533 MESH_TunnelNumber tid;
6535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6536 "Got a multicast request from a client!\n");
6538 /* Sanity check for client registration */
6539 if (NULL == (c = client_get (client)))
6542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6545 data_msg = (struct GNUNET_MESH_Multicast *) message;
6546 /* Sanity check for message size */
6547 if (sizeof (struct GNUNET_MESH_Multicast) +
6548 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.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 /* Does client own tunnel? */
6566 if (t->owner->handle != client)
6569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6574 char buf[ntohs (message->size)] GNUNET_ALIGN;
6575 struct GNUNET_MESH_Multicast *copy;
6577 copy = (struct GNUNET_MESH_Multicast *) buf;
6578 memcpy (buf, message, ntohs (message->size));
6579 copy->oid = my_full_id;
6580 copy->tid = htonl (t->id.tid);
6581 copy->ttl = htonl (default_ttl);
6582 copy->pid = htonl (t->pid + 1);
6583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6584 " calling generic handler...\n");
6585 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
6588 /* receive done gets called when last copy is sent to a neighbor */
6594 * Handler for client ACKs for payload traffic.
6596 * @param cls Closure (unused).
6597 * @param client Identification of the client.
6598 * @param message The actual message.
6601 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6602 const struct GNUNET_MessageHeader *message)
6604 struct GNUNET_MESH_LocalAck *msg;
6605 struct MeshTunnel *t;
6606 struct MeshClient *c;
6607 MESH_TunnelNumber tid;
6609 /* Sanity check for client registration */
6610 if (NULL == (c = client_get (client)))
6613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6617 msg = (struct GNUNET_MESH_LocalAck *) message;
6618 /* Tunnel exists? */
6619 tid = ntohl (msg->tunnel_id);
6620 t = tunnel_get_by_local_id (c, tid);
6624 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6628 /* Does client own tunnel? */
6629 if (NULL != t->owner && t->owner->handle == client)
6636 tunnel_set_client_ack (t, c, ntohl (msg->max_pid));
6639 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6645 * Functions to handle messages from clients
6647 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6648 {&handle_local_new_client, NULL,
6649 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6650 {&handle_local_announce_regex, NULL,
6651 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
6652 {&handle_local_tunnel_create, NULL,
6653 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6654 sizeof (struct GNUNET_MESH_TunnelMessage)},
6655 {&handle_local_tunnel_destroy, NULL,
6656 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6657 sizeof (struct GNUNET_MESH_TunnelMessage)},
6658 {&handle_local_tunnel_speed, NULL,
6659 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
6660 sizeof (struct GNUNET_MESH_TunnelMessage)},
6661 {&handle_local_tunnel_speed, NULL,
6662 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
6663 sizeof (struct GNUNET_MESH_TunnelMessage)},
6664 {&handle_local_tunnel_buffer, NULL,
6665 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6666 sizeof (struct GNUNET_MESH_TunnelMessage)},
6667 {&handle_local_tunnel_buffer, NULL,
6668 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6669 sizeof (struct GNUNET_MESH_TunnelMessage)},
6670 {&handle_local_connect_add, NULL,
6671 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
6672 sizeof (struct GNUNET_MESH_PeerControl)},
6673 {&handle_local_connect_del, NULL,
6674 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
6675 sizeof (struct GNUNET_MESH_PeerControl)},
6676 {&handle_local_blacklist, NULL,
6677 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
6678 sizeof (struct GNUNET_MESH_PeerControl)},
6679 {&handle_local_unblacklist, NULL,
6680 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
6681 sizeof (struct GNUNET_MESH_PeerControl)},
6682 {&handle_local_connect_by_type, NULL,
6683 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
6684 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
6685 {&handle_local_connect_by_string, NULL,
6686 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
6687 {&handle_local_unicast, NULL,
6688 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6689 {&handle_local_to_origin, NULL,
6690 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6691 {&handle_local_multicast, NULL,
6692 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6693 {&handle_local_ack, NULL,
6694 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6695 sizeof (struct GNUNET_MESH_LocalAck)},
6701 * To be called on core init/fail.
6703 * @param cls service closure
6704 * @param server handle to the server for this service
6705 * @param identity the public identity of this peer
6708 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6709 const struct GNUNET_PeerIdentity *identity)
6711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6712 core_handle = server;
6713 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6716 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6717 GNUNET_SCHEDULER_shutdown ();
6724 * Method called whenever a given peer connects.
6726 * @param cls closure
6727 * @param peer peer identity this notification is about
6728 * @param atsi performance data for the connection
6729 * @param atsi_count number of records in 'atsi'
6732 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
6733 const struct GNUNET_ATS_Information *atsi,
6734 unsigned int atsi_count)
6736 struct MeshPeerInfo *peer_info;
6737 struct MeshPeerPath *path;
6739 DEBUG_CONN ("Peer connected\n");
6740 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6741 peer_info = peer_info_get (peer);
6742 if (myid == peer_info->id)
6744 DEBUG_CONN (" (self)\n");
6749 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6751 path = path_new (2);
6752 path->peers[0] = myid;
6753 path->peers[1] = peer_info->id;
6754 GNUNET_PEER_change_rc (myid, 1);
6755 GNUNET_PEER_change_rc (peer_info->id, 1);
6756 peer_info_add_path (peer_info, path, GNUNET_YES);
6757 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6763 * Method called whenever a peer disconnects.
6765 * @param cls closure
6766 * @param peer peer identity this notification is about
6769 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6771 struct MeshPeerInfo *pi;
6772 struct MeshPeerQueue *q;
6773 struct MeshPeerQueue *n;
6775 DEBUG_CONN ("Peer disconnected\n");
6776 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6788 /* try to reroute this traffic instead */
6789 queue_destroy(q, GNUNET_YES);
6793 peer_info_remove_path (pi, pi->id, myid);
6796 DEBUG_CONN (" (self)\n");
6798 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6803 /******************************************************************************/
6804 /************************ MAIN FUNCTIONS ****************************/
6805 /******************************************************************************/
6808 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6810 * @param cls closure
6811 * @param key current key code
6812 * @param value value in the hash map
6813 * @return GNUNET_YES if we should continue to iterate,
6817 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6819 struct MeshTunnel *t = value;
6826 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
6828 * @param cls closure
6829 * @param key current key code
6830 * @param value value in the hash map
6831 * @return GNUNET_YES if we should continue to iterate,
6835 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
6837 struct MeshPeerInfo *p = value;
6838 struct MeshPeerQueue *q;
6839 struct MeshPeerQueue *n;
6847 queue_destroy(q, GNUNET_YES);
6851 peer_info_destroy (p);
6856 * Task run during shutdown.
6862 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6866 if (core_handle != NULL)
6868 GNUNET_CORE_disconnect (core_handle);
6871 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6872 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6873 if (dht_handle != NULL)
6875 GNUNET_DHT_disconnect (dht_handle);
6880 GNUNET_SERVER_notification_context_destroy (nc);
6883 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6885 GNUNET_SCHEDULER_cancel (announce_id_task);
6886 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6892 * Process mesh requests.
6894 * @param cls closure
6895 * @param server the initialized server
6896 * @param c configuration to use
6899 run (void *cls, struct GNUNET_SERVER_Handle *server,
6900 const struct GNUNET_CONFIGURATION_Handle *c)
6902 struct MeshPeerInfo *peer;
6903 struct MeshPeerPath *p;
6906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6907 server_handle = server;
6908 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6909 NULL, /* Closure passed to MESH functions */
6910 &core_init, /* Call core_init once connected */
6911 &core_connect, /* Handle connects */
6912 &core_disconnect, /* remove peers on disconnects */
6913 NULL, /* Don't notify about all incoming messages */
6914 GNUNET_NO, /* For header only in notification */
6915 NULL, /* Don't notify about all outbound messages */
6916 GNUNET_NO, /* For header-only out notification */
6917 core_handlers); /* Register these handlers */
6919 if (core_handle == NULL)
6922 GNUNET_SCHEDULER_shutdown ();
6927 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
6930 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6932 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6934 GNUNET_SCHEDULER_shutdown ();
6939 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6940 &refresh_path_time))
6942 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6944 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6945 "refresh path time");
6946 GNUNET_SCHEDULER_shutdown ();
6951 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6952 &app_announce_time))
6954 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6956 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6957 "app announce time");
6958 GNUNET_SCHEDULER_shutdown ();
6963 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6968 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6969 "id announce time");
6970 GNUNET_SCHEDULER_shutdown ();
6975 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
6976 &unacknowledged_wait_time))
6978 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6980 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6981 "unacknowledged wait time");
6982 GNUNET_SCHEDULER_shutdown ();
6987 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6990 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6992 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6994 GNUNET_SCHEDULER_shutdown ();
6999 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7002 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7004 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7006 GNUNET_SCHEDULER_shutdown ();
7011 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7014 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7016 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7018 GNUNET_SCHEDULER_shutdown ();
7023 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7026 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7028 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7034 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7035 &dht_replication_level))
7037 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7039 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7040 "dht replication level", 10);
7041 dht_replication_level = 10;
7045 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7046 GNUNET_free (keyfile);
7047 if (my_private_key == NULL)
7049 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7050 _("Mesh service could not access hostkey. Exiting.\n"));
7051 GNUNET_SCHEDULER_shutdown ();
7054 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7055 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7056 &my_full_id.hashPubKey);
7057 myid = GNUNET_PEER_intern (&my_full_id);
7059 // transport_handle = GNUNET_TRANSPORT_connect(c,
7066 dht_handle = GNUNET_DHT_connect (c, 64);
7067 if (dht_handle == NULL)
7072 stats = GNUNET_STATISTICS_create ("mesh", c);
7076 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7078 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7079 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7080 peers = GNUNET_CONTAINER_multihashmap_create (32);
7081 applications = GNUNET_CONTAINER_multihashmap_create (32);
7082 types = GNUNET_CONTAINER_multihashmap_create (32);
7084 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7085 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7086 GNUNET_SERVER_disconnect_notify (server_handle,
7087 &handle_local_client_disconnect, NULL);
7091 clients_tail = NULL;
7094 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7095 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7097 /* Create a peer_info for the local peer */
7098 peer = peer_info_get (&my_full_id);
7101 GNUNET_PEER_change_rc (myid, 1);
7102 peer_info_add_path (peer, p, GNUNET_YES);
7104 /* Scheduled the task to clean up when shutdown is called */
7105 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7112 * The main function for the mesh service.
7114 * @param argc number of arguments from the command line
7115 * @param argv command line arguments
7116 * @return 0 ok, 1 on error
7119 main (int argc, char *const *argv)
7123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7126 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");