2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
44 * - set ttl relative to tree depth
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_YES
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #if MESH_DEBUG_CONNECTION
64 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
66 #define DEBUG_CONN(...)
70 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
72 #define DEBUG_DHT(...)
75 /******************************************************************************/
76 /************************ DATA STRUCTURES ****************************/
77 /******************************************************************************/
79 /** FWD declaration */
85 * Struct representing a piece of data being sent to other peers
89 /** Tunnel it belongs to. */
92 /** In case of a multicast, task to allow a client to send more data if
93 * some neighbor is too slow. */
94 GNUNET_SCHEDULER_TaskIdentifier *task;
96 /** How many remaining neighbors we need to send this to. */
97 unsigned int *reference_counter;
99 /** Size of the data. */
108 * Struct containing info about a queued transmission to this peer
115 struct MeshPeerQueue *next;
120 struct MeshPeerQueue *prev;
123 * Peer this transmission is directed to.
125 struct MeshPeerInfo *peer;
128 * Tunnel this message belongs to.
130 struct MeshTunnel *tunnel;
133 * Pointer to info stucture used as cls.
143 * Size of the message
150 * Struct containing all info possibly needed to build a package when called
153 struct MeshTransmissionDescriptor
155 /** ID of the tunnel this packet travels in */
156 struct MESH_TunnelID *origin;
158 /** Who was this message being sent to */
159 struct MeshPeerInfo *peer;
161 /** Ultimate destination of the packet */
162 GNUNET_PEER_Id destination;
164 /** Data descriptor */
165 struct MeshData* mesh_data;
170 * Struct containing all information regarding a given peer
180 * Last time we heard from this peer
182 struct GNUNET_TIME_Absolute last_contact;
185 * Number of attempts to reconnect so far
187 int n_reconnect_attempts;
190 * Paths to reach the peer, ordered by ascending hop count
192 struct MeshPeerPath *path_head;
195 * Paths to reach the peer, ordered by ascending hop count
197 struct MeshPeerPath *path_tail;
200 * Handle to stop the DHT search for a path to this peer
202 struct GNUNET_DHT_GetHandle *dhtget;
205 * Closure given to the DHT GET
207 struct MeshPathInfo *dhtgetcls;
210 * Array of tunnels this peer participates in
211 * (most probably a small amount, therefore not a hashmap)
212 * When the path to the peer changes, notify these tunnels to let them
213 * re-adjust their path trees.
215 struct MeshTunnel **tunnels;
218 * Number of tunnels this peers participates in
220 unsigned int ntunnels;
223 * Transmission queue to core DLL head
225 struct MeshPeerQueue *queue_head;
228 * Transmission queue to core DLL tail
230 struct MeshPeerQueue *queue_tail;
233 * How many messages are in the queue to this peer.
235 unsigned int queue_n;
238 * Handle to for queued transmissions
240 struct GNUNET_CORE_TransmitHandle *core_transmit;
245 * Globally unique tunnel identification (owner + number)
246 * DO NOT USE OVER THE NETWORK
251 * Node that owns the tunnel
256 * Tunnel number to differentiate all the tunnels owned by the node oid
257 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
259 MESH_TunnelNumber tid;
264 * Info collected during iteration of child nodes in order to get the ACK value
267 struct MeshTunnelChildIteratorContext
270 * Tunnel whose info is being collected.
272 struct MeshTunnel *t;
275 * Maximum child ACK so far.
277 uint32_t max_child_ack;
280 * Number of children nodes
282 unsigned int nchildren;
287 * Struct containing all information regarding a tunnel
288 * For an intermediate node the improtant info used will be:
289 * - id Tunnel unique identification
290 * - paths[0] To know where to send it next
291 * - metainfo: ready, speeds, accounting
298 struct MESH_TunnelID id;
301 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
303 MESH_TunnelNumber local_tid;
306 * Local tunnel number for local destination clients (incoming number)
307 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
310 MESH_TunnelNumber local_tid_dest;
313 * Local count ID of the last packet seen/sent.
318 * SKIP value for this tunnel.
323 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
325 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
328 * Last ACK sent towards the origin.
333 * How many messages are in the queue.
335 unsigned int queue_n;
338 * How many messages do we accept in the queue.
340 unsigned int queue_max;
343 * Is the speed on the tunnel limited to the slowest peer?
348 * Is the tunnel bufferless (minimum latency)?
353 * Flag to signal the destruction of the tunnel.
354 * If this is set GNUNET_YES the tunnel will be destroyed
355 * when the queue is empty.
360 * Last time the tunnel was used
362 struct GNUNET_TIME_Absolute timestamp;
365 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
366 * containing peers added by id or by type, not intermediate peers.
368 struct GNUNET_CONTAINER_MultiHashMap *peers;
371 * Number of peers that are connected and potentially ready to receive data
373 unsigned int peers_ready;
376 * Number of peers that have been added to the tunnel
378 unsigned int peers_total;
381 * Client owner of the tunnel, if any
383 struct MeshClient *owner;
386 * Clients that have been informed about and want to stay in the tunnel.
388 struct MeshClient **clients;
391 * ACK value of each active client.
393 uint32_t *clients_acks;
396 * Number of elements in clients/clients_acks
398 unsigned int nclients;
401 * Clients that have been informed but requested to leave the tunnel.
403 struct MeshClient **ignore;
406 * Number of elements in clients
408 unsigned int nignore;
413 GNUNET_PEER_Id *blacklisted;
416 * Number of elements in blacklisted
418 unsigned int nblacklisted;
421 * Bloomfilter (for peer identities) to stop circular routes
423 char bloomfilter[MESH_BLOOM_SIZE];
428 struct MeshTunnelTree *tree;
431 * Application type we are looking for in this tunnel
433 GNUNET_MESH_ApplicationType type;
436 * Used to search peers offering a service
438 struct GNUNET_DHT_GetHandle *dht_get_type;
441 * Initial context of the regex search for a connect_by_string
443 struct MeshRegexSearchContext *regex_ctx;
446 * Task to keep the used paths alive
448 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
451 * Task to destroy the tunnel after timeout
453 * FIXME: merge the two? a tunnel will have either
454 * a path refresh OR a timeout, never both!
456 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
461 * Info about a child node in a tunnel, needed to perform flow control.
463 struct MeshTunnelChildInfo
466 * ID of the child node.
481 * Maximum PID allowed.
487 * Info needed to work with tunnel paths and peers
494 struct MeshTunnel *t;
497 * Neighbouring peer to whom we send the packet to
499 struct MeshPeerInfo *peer;
504 struct MeshPeerPath *path;
509 * Struct containing information about a client of the service
516 struct MeshClient *next;
521 struct MeshClient *prev;
524 * Tunnels that belong to this client, indexed by local id
526 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
529 * Tunnels this client has accepted, indexed by incoming local id
531 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
534 * Tunnels this client has rejected, indexed by incoming local id
536 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
538 * Handle to communicate with the client
540 struct GNUNET_SERVER_Client *handle;
543 * Applications that this client has claimed to provide
545 struct GNUNET_CONTAINER_MultiHashMap *apps;
548 * Messages that this client has declared interest in
550 struct GNUNET_CONTAINER_MultiHashMap *types;
553 * Whether the client is active or shutting down (don't send confirmations
554 * to a client that is shutting down.
559 * ID of the client, mainly for debug messages
564 * Regular expressions describing the services offered by this client.
566 char **regexes; // FIXME add timeout? API to remove a regex?
569 * Number of regular expressions in regexes.
571 unsigned int n_regex;
574 * Task to refresh all regular expresions in the DHT.
576 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
582 * Struct to keep information of searches of services described by a regex
583 * using a user-provided string service description.
585 struct MeshRegexSearchInfo
588 * Which tunnel is this for
590 struct MeshTunnel *t;
593 * User provided description of the searched service.
598 * Part of the description already consumed by the search.
605 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
608 * Results from running DHT GETs.
610 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
613 * Contexts, for each running DHT GET. Free all on end of search.
615 struct MeshRegexSearchContext **contexts;
618 * Number of contexts (branches/steps in search).
620 unsigned int n_contexts;
623 * Peer that is connecting via connect_by_string. When connected, free ctx.
628 * Other peers that are found but not yet being connected to.
630 GNUNET_PEER_Id *peers;
633 * Number of elements in peers.
635 unsigned int n_peers;
638 * Next peer to try to connect to.
643 * Timeout for a connect attempt.
644 * When reached, try to connect to a different peer, if any. If not,
645 * try the same peer again.
647 GNUNET_SCHEDULER_TaskIdentifier timeout;
652 * Struct to keep state of running searches that have consumed a part of
655 struct MeshRegexSearchContext
658 * Part of the description already consumed by
659 * this particular search branch.
664 * Information about the search.
666 struct MeshRegexSearchInfo *info;
670 /******************************************************************************/
671 /************************ DEBUG FUNCTIONS ****************************/
672 /******************************************************************************/
676 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
677 * @param cls string to print
678 * @param success GNUNET_OK if the PUT was transmitted,
679 * GNUNET_NO on timeout,
680 * GNUNET_SYSERR on disconnect from service
681 * after the PUT message was transmitted
682 * (so we don't know if it was received or not)
687 mesh_debug (void *cls, int success)
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
697 /******************************************************************************/
698 /*********************** GLOBAL VARIABLES ****************************/
699 /******************************************************************************/
703 * Configuration parameters
705 static struct GNUNET_TIME_Relative refresh_path_time;
706 static struct GNUNET_TIME_Relative app_announce_time;
707 static struct GNUNET_TIME_Relative id_announce_time;
708 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
709 static struct GNUNET_TIME_Relative connect_timeout;
710 static long long unsigned int default_ttl;
711 static long long unsigned int dht_replication_level;
712 static long long unsigned int max_tunnels;
713 static long long unsigned int max_msgs_queue;
716 * DLL with all the clients, head.
718 static struct MeshClient *clients;
721 * DLL with all the clients, tail.
723 static struct MeshClient *clients_tail;
726 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
728 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
731 * Number of tunnels known.
733 static unsigned long long n_tunnels;
736 * Tunnels incoming, indexed by MESH_TunnelNumber
737 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
739 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
742 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
744 static struct GNUNET_CONTAINER_MultiHashMap *peers;
747 * Handle to communicate with transport
749 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
752 * Handle to communicate with core.
754 static struct GNUNET_CORE_Handle *core_handle;
759 static struct GNUNET_DHT_Handle *dht_handle;
764 static struct GNUNET_SERVER_Handle *server_handle;
767 * Handle to the statistics service.
769 static struct GNUNET_STATISTICS_Handle *stats;
772 * Notification context, to send messages to local clients.
774 static struct GNUNET_SERVER_NotificationContext *nc;
777 * Local peer own ID (memory efficient handle).
779 static GNUNET_PEER_Id myid;
782 * Local peer own ID (full value).
784 static struct GNUNET_PeerIdentity my_full_id;
789 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
794 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
797 * Tunnel ID for the next created tunnel (global tunnel number).
799 static MESH_TunnelNumber next_tid;
802 * Tunnel ID for the next incoming tunnel (local tunnel number).
804 static MESH_TunnelNumber next_local_tid;
807 * All application types provided by this peer.
809 static struct GNUNET_CONTAINER_MultiHashMap *applications;
812 * All message types clients of this peer are interested in.
814 static struct GNUNET_CONTAINER_MultiHashMap *types;
817 * Task to periodically announce provided applications.
819 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
822 * Task to periodically announce itself in the network.
824 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
827 * Next ID to assign to a client.
829 unsigned int next_client_id;
832 /******************************************************************************/
833 /*********************** DECLARATIONS **************************/
834 /******************************************************************************/
836 /* FIXME move declarations here */
839 * Function to process paths received for a new peer addition. The recorded
840 * paths form the initial tunnel, which can be optimized later.
841 * Called on each result obtained for the DHT search.
844 * @param exp when will this value expire
845 * @param key key of the result
846 * @param type type of the result
847 * @param size number of bytes in data
848 * @param data pointer to the result data
851 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
852 const struct GNUNET_HashCode * key,
853 const struct GNUNET_PeerIdentity *get_path,
854 unsigned int get_path_length,
855 const struct GNUNET_PeerIdentity *put_path,
856 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
857 size_t size, const void *data);
861 * Function to process DHT string to regex matching.
862 * Called on each result obtained for the DHT search.
864 * @param cls closure (search context)
865 * @param exp when will this value expire
866 * @param key key of the result
867 * @param get_path path of the get request (not used)
868 * @param get_path_length lenght of get_path (not used)
869 * @param put_path path of the put request (not used)
870 * @param put_path_length length of the put_path (not used)
871 * @param type type of the result
872 * @param size number of bytes in data
873 * @param data pointer to the result data
875 * TODO: re-issue the request after certain time? cancel after X results?
878 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
879 const struct GNUNET_HashCode * key,
880 const struct GNUNET_PeerIdentity *get_path,
881 unsigned int get_path_length,
882 const struct GNUNET_PeerIdentity *put_path,
883 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
884 size_t size, const void *data);
888 * Function to process DHT string to regex matching.
889 * Called on each result obtained for the DHT search.
891 * @param cls closure (search context)
892 * @param exp when will this value expire
893 * @param key key of the result
894 * @param get_path path of the get request (not used)
895 * @param get_path_length lenght of get_path (not used)
896 * @param put_path path of the put request (not used)
897 * @param put_path_length length of the put_path (not used)
898 * @param type type of the result
899 * @param size number of bytes in data
900 * @param data pointer to the result data
903 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
904 const struct GNUNET_HashCode * key,
905 const struct GNUNET_PeerIdentity *get_path,
906 unsigned int get_path_length,
907 const struct GNUNET_PeerIdentity *put_path,
908 unsigned int put_path_length,
909 enum GNUNET_BLOCK_Type type,
910 size_t size, const void *data);
914 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
915 * and insert it in the appropiate structures if the peer is not known yet.
917 * @param peer Short identity of the peer.
919 * @return Existing or newly created peer info.
921 static struct MeshPeerInfo *
922 peer_info_get_short (const GNUNET_PEER_Id peer);
926 * Try to establish a new connection to this peer.
927 * Use the best path for the given tunnel.
928 * If the peer doesn't have any path to it yet, try to get one.
929 * If the peer already has some path, send a CREATE PATH towards it.
931 * @param peer PeerInfo of the peer.
932 * @param t Tunnel for which to create the path, if possible.
935 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
939 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
941 * If peer already exists, reevaluate shortest path and change if different.
943 * @param t Tunnel we want to add a new peer to
944 * @param peer PeerInfo of the peer being added
948 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
952 * Removes an explicit path from a tunnel, freeing all intermediate nodes
953 * that are no longer needed, as well as nodes of no longer reachable peers.
954 * The tunnel itself is also destoyed if results in a remote empty tunnel.
956 * @param t Tunnel from which to remove the path.
957 * @param peer Short id of the peer which should be removed.
960 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
964 * Search for a tunnel by global ID using full PeerIdentities.
966 * @param oid owner of the tunnel.
967 * @param tid global tunnel number.
969 * @return tunnel handler, NULL if doesn't exist.
971 static struct MeshTunnel *
972 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
976 * Delete an active client from the tunnel.
982 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
985 * Notify a tunnel that a connection has broken that affects at least
988 * @param t Tunnel affected.
989 * @param p1 Peer that got disconnected from p2.
990 * @param p2 Peer that got disconnected from p1.
992 * @return Short ID of the peer disconnected (either p1 or p2).
993 * 0 if the tunnel remained unaffected.
995 static GNUNET_PEER_Id
996 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1001 * Get the current ack value for a tunnel, for data going from root to leaves,
1002 * taking in account the tunnel mode and the status of all children and clients.
1006 * @return Maximum PID allowed.
1009 tunnel_get_fwd_ack (struct MeshTunnel *t);
1012 * Get the current ack value for a tunnel, for data going from leaves to root,
1013 * taking in account the tunnel mode and the status of all children and clients.
1017 * @return Maximum PID allowed.
1020 tunnel_get_bck_ack (struct MeshTunnel *t);
1023 * Add a client to a tunnel, initializing all needed data structures.
1025 * @param t Tunnel to which add the client.
1026 * @param c Client which to add to the tunnel.
1029 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1033 * Iterator over edges in a regex block retrieved from the DHT.
1035 * @param cls Closure.
1036 * @param token Token that follows to next state.
1037 * @param len Lenght of token.
1038 * @param key Hash of next state.
1040 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1043 regex_edge_iterator (void *cls,
1046 const struct GNUNET_HashCode *key);
1050 * Find a path to a peer that offers a regex servcie compatible
1051 * with a given string.
1053 * @param key The key of the accepting state.
1054 * @param ctx Context containing info about the string, tunnel, etc.
1057 regex_find_path (const struct GNUNET_HashCode *key,
1058 struct MeshRegexSearchContext *ctx);
1062 * Queue and pass message to core when possible.
1064 * @param cls Closure (type dependant).
1065 * @param type Type of the message, 0 for a raw message.
1066 * @param size Size of the message.
1067 * @param dst Neighbor to send message to.
1068 * @param t Tunnel this message belongs to.
1071 queue_add (void *cls, uint16_t type, size_t size,
1072 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1075 * Free a transmission that was already queued with all resources
1076 * associated to the request.
1078 * @param queue Queue handler to cancel.
1079 * @param clear_cls Is it necessary to free associated cls?
1082 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1084 /******************************************************************************/
1085 /************************ ITERATORS ****************************/
1086 /******************************************************************************/
1089 * Iterator over found existing mesh regex blocks that match an ongoing search.
1091 * @param cls closure
1092 * @param key current key code
1093 * @param value value in the hash map
1094 * @return GNUNET_YES if we should continue to iterate,
1098 regex_result_iterator (void *cls,
1099 const struct GNUNET_HashCode * key,
1102 struct MeshRegexBlock *block = value;
1103 struct MeshRegexSearchContext *ctx = cls;
1105 if (GNUNET_YES == ntohl(block->accepting) &&
1106 ctx->position == strlen (ctx->info->description))
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1109 regex_find_path (key, ctx);
1110 return GNUNET_YES; // We found an accept state!
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1115 ctx->position, strlen(ctx->info->description),
1116 ntohl(block->accepting));
1119 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1120 ®ex_edge_iterator, ctx);
1127 * Iterator over edges in a regex block retrieved from the DHT.
1129 * @param cls Closure (context of the search).
1130 * @param token Token that follows to next state.
1131 * @param len Lenght of token.
1132 * @param key Hash of next state.
1134 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1137 regex_edge_iterator (void *cls,
1140 const struct GNUNET_HashCode *key)
1142 struct MeshRegexSearchContext *ctx = cls;
1143 struct MeshRegexSearchContext *new_ctx;
1144 struct MeshRegexSearchInfo *info = ctx->info;
1145 struct GNUNET_DHT_GetHandle *get_h;
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1152 current = &info->description[ctx->position];
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1154 current_len = strlen (info->description) - ctx->position;
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1159 if (len > current_len)
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1162 return GNUNET_YES; // Token too long, wont match
1164 if (0 != strncmp (current, token, len))
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1167 return GNUNET_YES; // Token doesn't match
1169 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1170 new_ctx->info = info;
1171 new_ctx->position = ctx->position + len;
1172 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1174 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1177 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1178 ®ex_result_iterator,
1180 return GNUNET_YES; // We are already looking for it
1182 /* Start search in DHT */
1184 GNUNET_DHT_get_start (dht_handle, /* handle */
1185 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1186 key, /* key to search */
1187 dht_replication_level, /* replication level */
1188 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1189 NULL, /* xquery */ // FIXME BLOOMFILTER
1190 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1191 &dht_get_string_handler, new_ctx);
1193 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1194 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1204 * Iterator over hash map entries to cancel DHT GET requests after a
1205 * successful connect_by_string.
1207 * @param cls Closure (unused).
1208 * @param key Current key code (unused).
1209 * @param value Value in the hash map (get handle).
1210 * @return GNUNET_YES if we should continue to iterate,
1214 regex_cancel_dht_get (void *cls,
1215 const struct GNUNET_HashCode * key,
1218 struct GNUNET_DHT_GetHandle *h = value;
1220 GNUNET_DHT_get_stop (h);
1226 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1227 * search for connect_by_string.
1229 * @param cls Closure (unused).
1230 * @param key Current key code (unused).
1231 * @param value MeshRegexBlock in the hash map.
1232 * @return GNUNET_YES if we should continue to iterate,
1236 regex_free_result (void *cls,
1237 const struct GNUNET_HashCode * key,
1241 GNUNET_free (value);
1247 * Regex callback iterator to store own service description in the DHT.
1249 * @param cls closure.
1250 * @param key hash for current state.
1251 * @param proof proof for current state.
1252 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1253 * @param num_edges number of edges leaving current state.
1254 * @param edges edges leaving current state.
1257 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1258 int accepting, unsigned int num_edges,
1259 const struct GNUNET_REGEX_Edge *edges)
1261 struct MeshRegexBlock *block;
1262 struct MeshRegexEdge *block_edge;
1263 enum GNUNET_DHT_RouteOption opt;
1267 unsigned int offset;
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 " regex dht put for state %s\n",
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1280 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1281 if (GNUNET_YES == accepting)
1283 struct MeshRegexAccept block;
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 " state %s is accepting, putting own id\n",
1288 size = sizeof (block);
1290 block.id = my_full_id;
1292 GNUNET_DHT_put(dht_handle, key,
1293 dht_replication_level,
1294 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1295 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1298 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1303 len = strlen(proof);
1304 size = sizeof (struct MeshRegexBlock) + len;
1305 block = GNUNET_malloc (size);
1308 block->n_proof = htonl (len);
1309 block->n_edges = htonl (num_edges);
1310 block->accepting = htonl (accepting);
1312 /* Store the proof at the end of the block. */
1313 aux = (char *) &block[1];
1314 memcpy (aux, proof, len);
1317 /* Store each edge in a variable length MeshEdge struct at the
1318 * very end of the MeshRegexBlock structure.
1320 for (i = 0; i < num_edges; i++)
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 " edge %s towards %s\n",
1325 GNUNET_h2s(&edges[i].destination));
1327 /* aux points at the end of the last block */
1328 len = strlen (edges[i].label);
1329 size += sizeof (struct MeshRegexEdge) + len;
1330 // Calculate offset FIXME is this ok? use size instead?
1331 offset = aux - (char *) block;
1332 block = GNUNET_realloc (block, size);
1333 aux = &((char *) block)[offset];
1334 block_edge = (struct MeshRegexEdge *) aux;
1335 block_edge->key = edges[i].destination;
1336 block_edge->n_token = htonl (len);
1337 aux = (char *) &block_edge[1];
1338 memcpy (aux, edges[i].label, len);
1342 GNUNET_DHT_put(dht_handle, key,
1343 dht_replication_level,
1345 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1347 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1351 GNUNET_free (block);
1356 * Store the regular expression describing a local service into the DHT.
1358 * @param regex The regular expresion.
1361 regex_put (const char *regex)
1363 struct GNUNET_REGEX_Automaton *dfa;
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1366 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1367 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1368 GNUNET_REGEX_automaton_destroy (dfa);
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1374 * Find a path to a peer that offers a regex servcie compatible
1375 * with a given string.
1377 * @param key The key of the accepting state.
1378 * @param ctx Context containing info about the string, tunnel, etc.
1381 regex_find_path (const struct GNUNET_HashCode *key,
1382 struct MeshRegexSearchContext *ctx)
1384 struct GNUNET_DHT_GetHandle *get_h;
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1387 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1388 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1389 key, /* key to search */
1390 dht_replication_level, /* replication level */
1391 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1392 GNUNET_DHT_RO_RECORD_ROUTE,
1393 NULL, /* xquery */ // FIXME BLOOMFILTER
1394 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1395 &dht_get_string_accept_handler, ctx);
1396 GNUNET_break (GNUNET_OK ==
1397 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1400 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1405 * Function called if the connect attempt to a peer found via
1406 * connect_by_string times out. Try to connect to another peer, if any.
1407 * Otherwise try to reconnect to the same peer.
1409 * @param cls Closure (info about regex search).
1410 * @param tc TaskContext.
1413 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1415 struct MeshRegexSearchInfo *info = cls;
1416 struct MeshPeerInfo *peer_info;
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1421 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1422 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1430 if (0 < info->n_peers)
1432 // Select next peer, put current in that spot.
1433 id = info->peers[info->i_peer];
1434 info->peers[info->i_peer] = info->peer;
1435 info->i_peer = (info->i_peer + 1) % info->n_peers;
1439 // Try to connect to same peer again.
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1444 peer_info = peer_info_get_short(id);
1445 tunnel_add_peer (info->t, peer_info);
1447 tunnel_delete_peer (info->t, old);
1448 peer_info_connect (peer_info, info->t);
1449 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1450 ®ex_connect_timeout,
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1457 * Cancel an ongoing regex search in the DHT and free all resources.
1459 * @param ctx The search context.
1462 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1464 struct MeshRegexSearchInfo *info = ctx->info;
1467 GNUNET_free (info->description);
1468 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1469 ®ex_cancel_dht_get, NULL);
1470 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1471 ®ex_free_result, NULL);
1472 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1473 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1474 info->t->regex_ctx = NULL;
1475 for (i = 0; i < info->n_contexts; i++)
1477 GNUNET_free (info->contexts[i]);
1479 if (0 < info->n_contexts)
1480 GNUNET_free (info->contexts);
1481 if (0 < info->n_peers)
1482 GNUNET_free (info->peers);
1483 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1485 GNUNET_SCHEDULER_cancel(info->timeout);
1491 /******************************************************************************/
1492 /************************ PERIODIC FUNCTIONS ****************************/
1493 /******************************************************************************/
1496 * Announce iterator over for each application provided by the peer
1498 * @param cls closure
1499 * @param key current key code
1500 * @param value value in the hash map
1501 * @return GNUNET_YES if we should continue to
1506 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1508 struct PBlock block;
1509 struct MeshClient *c;
1511 block.id = my_full_id;
1512 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1513 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1514 if (0 == block.type)
1519 block.type = htonl (block.type);
1521 GNUNET_break (NULL !=
1522 GNUNET_DHT_put (dht_handle, key,
1523 dht_replication_level,
1524 GNUNET_DHT_RO_RECORD_ROUTE |
1525 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1526 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1528 (const char *) &block,
1529 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1531 app_announce_time, NULL, NULL));
1537 * Periodically announce what applications are provided by local clients
1540 * @param cls closure
1541 * @param tc task context
1544 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1546 struct MeshClient *c = cls;
1549 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1551 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1555 DEBUG_DHT ("Starting PUT for regex\n");
1557 for (i = 0; i < c->n_regex; i++)
1559 regex_put (c->regexes[i]);
1561 c->regex_announce_task =
1562 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1563 DEBUG_DHT ("Finished PUT for regex\n");
1570 * Periodically announce what applications are provided by local clients
1573 * @param cls closure
1574 * @param tc task context
1577 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1579 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1581 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1585 DEBUG_DHT ("Starting PUT for apps\n");
1587 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1589 announce_applications_task =
1590 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1592 DEBUG_DHT ("Finished PUT for apps\n");
1599 * Periodically announce self id in the DHT
1601 * @param cls closure
1602 * @param tc task context
1605 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1607 struct PBlock block;
1609 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1611 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1615 * - Set data expiration in function of X
1616 * - Adapt X to churn
1618 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1620 block.id = my_full_id;
1621 block.type = htonl (0);
1622 GNUNET_DHT_put (dht_handle, /* DHT handle */
1623 &my_full_id.hashPubKey, /* Key to use */
1624 dht_replication_level, /* Replication level */
1625 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1626 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1627 sizeof (block), /* Size of the data */
1628 (const char *) &block, /* Data itself */
1629 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1630 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1631 NULL, /* Continuation */
1632 NULL); /* Continuation closure */
1634 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1638 /******************************************************************************/
1639 /****************** GENERAL HELPER FUNCTIONS ************************/
1640 /******************************************************************************/
1643 max_ack (uint32_t a, uint32_t b)
1648 min_ack (uint32_t a, uint32_t b)
1650 return (max_ack (a, b) == a) ? b : a;
1655 * Decrements the reference counter and frees all resources if needed
1657 * @param mesh_data Data Descriptor used in a multicast message.
1658 * Freed no longer needed (last message).
1661 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1663 /* Make sure it's a multicast packet */
1664 GNUNET_assert (NULL != mesh_data->reference_counter);
1666 if (0 == --(*(mesh_data->reference_counter)))
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1669 if (NULL != mesh_data->task)
1671 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1673 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1675 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1677 GNUNET_free (mesh_data->task);
1679 GNUNET_free (mesh_data->reference_counter);
1680 GNUNET_free (mesh_data->data);
1681 GNUNET_free (mesh_data);
1687 * Check if client has registered with the service and has not disconnected
1689 * @param client the client to check
1691 * @return non-NULL if client exists in the global DLL
1693 static struct MeshClient *
1694 client_get (struct GNUNET_SERVER_Client *client)
1696 struct MeshClient *c;
1701 if (c->handle == client)
1710 * Checks if a given client has subscribed to certain message type
1712 * @param message_type Type of message to check
1713 * @param c Client to check
1715 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1718 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1720 struct GNUNET_HashCode hc;
1722 if (NULL == c->types)
1724 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1725 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1730 * Allow a client to send more data after transmitting a multicast message
1731 * which some neighbor has not yet accepted altough a reasonable time has
1734 * @param cls Closure (DataDescriptor containing the task identifier)
1735 * @param tc Task Context
1737 * FIXME reference counter cshould be just int
1740 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1742 struct MeshData *mdata = cls;
1744 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1746 GNUNET_assert (NULL != mdata->reference_counter);
1747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1749 *(mdata->reference_counter));
1750 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1751 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1756 * Check whether client wants traffic from a tunnel.
1758 * @param c Client to check.
1759 * @param t Tunnel to be found.
1761 * @return GNUNET_YES if client knows tunnel.
1763 * TODO look in client hashmap
1766 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1770 for (i = 0; i < t->nclients; i++)
1771 if (t->clients[i] == c)
1778 * Check whether client has been informed about a tunnel.
1780 * @param c Client to check.
1781 * @param t Tunnel to be found.
1783 * @return GNUNET_YES if client knows tunnel.
1785 * TODO look in client hashmap
1788 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1792 for (i = 0; i < t->nignore; i++)
1793 if (t->ignore[i] == c)
1795 return client_wants_tunnel(c, t);
1800 * Marks a client as uninterested in traffic from the tunnel, updating both
1801 * client and tunnel to reflect this.
1803 * @param c Client that doesn't want traffic anymore.
1804 * @param t Tunnel which should be ignored.
1806 * FIXME when to delete an incoming tunnel?
1809 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1811 struct GNUNET_HashCode hash;
1813 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1814 GNUNET_break (GNUNET_YES ==
1815 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1817 GNUNET_break (GNUNET_YES ==
1818 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1819 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1820 tunnel_delete_active_client (t, c);
1821 GNUNET_array_append (t->ignore, t->nignore, c);
1826 * Deletes a tunnel from a client (either owner or destination). To be used on
1827 * tunnel destroy, otherwise, use client_ignore_tunnel.
1829 * @param c Client whose tunnel to delete.
1830 * @param t Tunnel which should be deleted.
1833 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1835 struct GNUNET_HashCode hash;
1839 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1840 GNUNET_assert (GNUNET_YES ==
1841 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1847 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1849 GNUNET_assert (GNUNET_YES ==
1850 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1854 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1862 * Send the message to all clients that have subscribed to its type
1864 * @param msg Pointer to the message itself
1865 * @param payload Pointer to the payload of the message.
1866 * @return number of clients this message was sent to
1869 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1870 const struct GNUNET_MessageHeader *payload)
1872 struct GNUNET_PeerIdentity *oid;
1873 struct MeshClient *c;
1874 struct MeshTunnel *t;
1875 MESH_TunnelNumber *tid;
1878 char cbuf[htons (msg->size)];
1880 type = ntohs (payload->type);
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1884 memcpy (cbuf, msg, sizeof (cbuf));
1885 switch (htons (msg->type))
1887 struct GNUNET_MESH_Unicast *uc;
1888 struct GNUNET_MESH_Multicast *mc;
1889 struct GNUNET_MESH_ToOrigin *to;
1891 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1892 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1896 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1897 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1901 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1902 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1910 t = tunnel_get (oid, ntohl (*tid));
1917 for (count = 0, c = clients; c != NULL; c = c->next)
1919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1920 if (client_is_subscribed (type, c))
1922 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1926 *tid = htonl (t->local_tid);
1930 if (GNUNET_NO == client_knows_tunnel (c, t))
1932 /* This client doesn't know the tunnel */
1933 struct GNUNET_MESH_TunnelNotification tmsg;
1934 struct GNUNET_HashCode hash;
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1937 tmsg.header.size = htons (sizeof (tmsg));
1938 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1939 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1940 tmsg.tunnel_id = htonl (t->local_tid_dest);
1941 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1942 &tmsg.header, GNUNET_NO);
1943 tunnel_add_client (t, c);
1944 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1946 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1947 c->incoming_tunnels, &hash, t,
1948 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1950 *tid = htonl (t->local_tid_dest);
1953 /* Check if the client wants to get traffic from the tunnel */
1954 if (GNUNET_NO == client_wants_tunnel(c, t))
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1958 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1959 (struct GNUNET_MessageHeader
1960 *) cbuf, GNUNET_NO);
1968 * Notify the client that owns the tunnel that a peer has connected to it
1969 * (the requested path to it has been confirmed).
1971 * @param t Tunnel whose owner to notify
1972 * @param id Short id of the peer that has connected
1975 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1977 struct GNUNET_MESH_PeerControl pc;
1979 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1980 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1981 pc.tunnel_id = htonl (t->local_tid);
1982 GNUNET_PEER_resolve (id, &pc.peer);
1983 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1989 * Notify a client about how many more payload packages will we accept
1990 * on a given tunnel.
1996 send_client_tunnel_ack (struct MeshClient *c, struct MeshTunnel *t)
1998 struct GNUNET_MESH_LocalAck msg;
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2002 "Sending client ACK on tunnel %X\n",
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to client %u\n", c->id);
2009 ack = tunnel_get_fwd_ack (t);
2011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2012 if (t->last_ack == ack)
2015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2017 msg.header.size = htons (sizeof (msg));
2018 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2019 msg.tunnel_id = htonl (t->local_tid);
2020 msg.max_pid = htonl (ack);
2022 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2023 &msg.header, GNUNET_NO);
2028 * Notify all clients (not depending on registration status) that the incoming
2029 * tunnel is no longer valid.
2031 * @param t Tunnel that was destroyed.
2034 send_clients_tunnel_destroy (struct MeshTunnel *t)
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 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2046 * Notify clients of tunnel disconnections, if needed.
2047 * In case the origin disconnects, the destination clients get a tunnel destroy
2048 * notification. If the last destination disconnects (only one remaining client
2049 * in tunnel), the origin gets a (local ID) peer disconnected.
2050 * Note that the function must be called BEFORE removing the client from
2053 * @param t Tunnel that was destroyed.
2054 * @param c Client that disconnected.
2057 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2063 struct GNUNET_MESH_TunnelMessage msg;
2065 msg.header.size = htons (sizeof (msg));
2066 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2067 msg.tunnel_id = htonl (t->local_tid_dest);
2068 for (i = 0; i < t->nclients; i++)
2069 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2070 &msg.header, GNUNET_NO);
2072 // FIXME when to disconnect an incoming tunnel?
2073 else if (1 == t->nclients && NULL != t->owner)
2075 struct GNUNET_MESH_PeerControl msg;
2077 msg.header.size = htons (sizeof (msg));
2078 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2079 msg.tunnel_id = htonl (t->local_tid);
2080 msg.peer = my_full_id;
2081 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2082 &msg.header, GNUNET_NO);
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 Full identity of the peer.
2093 * @return Existing or newly created peer info.
2095 static struct MeshPeerInfo *
2096 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2098 struct MeshPeerInfo *peer_info;
2100 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2101 if (NULL == peer_info)
2104 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2105 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2106 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2107 peer_info->id = GNUNET_PEER_intern (peer);
2115 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2116 * and insert it in the appropiate structures if the peer is not known yet.
2118 * @param peer Short identity of the peer.
2120 * @return Existing or newly created peer info.
2122 static struct MeshPeerInfo *
2123 peer_info_get_short (const GNUNET_PEER_Id peer)
2125 struct GNUNET_PeerIdentity id;
2127 GNUNET_PEER_resolve (peer, &id);
2128 return peer_info_get (&id);
2133 * Iterator to remove the tunnel from the list of tunnels a peer participates
2136 * @param cls Closure (tunnel info)
2137 * @param key GNUNET_PeerIdentity of the peer (unused)
2138 * @param value PeerInfo of the peer
2140 * @return always GNUNET_YES, to keep iterating
2143 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2145 struct MeshTunnel *t = cls;
2146 struct MeshPeerInfo *peer = value;
2149 for (i = 0; i < peer->ntunnels; i++)
2152 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2155 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2156 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2165 * Core callback to write a pre-constructed data packet to core buffer
2167 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2168 * @param size Number of bytes available in buf.
2169 * @param buf Where the to write the message.
2171 * @return number of bytes written to buf
2174 send_core_data_raw (void *cls, size_t size, void *buf)
2176 struct MeshTransmissionDescriptor *info = cls;
2177 struct GNUNET_MessageHeader *msg;
2180 GNUNET_assert (NULL != info);
2181 GNUNET_assert (NULL != info->mesh_data);
2182 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2183 total_size = ntohs (msg->size);
2185 if (total_size > size)
2190 memcpy (buf, msg, total_size);
2191 data_descriptor_decrement_rc (info->mesh_data);
2198 * Sends an already built non-multicast message to a peer,
2199 * properly registrating all used resources.
2201 * @param message Message to send. Function makes a copy of it.
2202 * @param peer Short ID of the neighbor whom to send the message.
2203 * @param t Tunnel on which this message is transmitted.
2206 send_message (const struct GNUNET_MessageHeader *message,
2207 const struct GNUNET_PeerIdentity *peer,
2208 struct MeshTunnel *t)
2210 struct MeshTransmissionDescriptor *info;
2211 struct MeshPeerInfo *neighbor;
2212 struct MeshPeerPath *p;
2215 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2217 size = ntohs (message->size);
2218 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2219 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2220 info->mesh_data->data = GNUNET_malloc (size);
2221 memcpy (info->mesh_data->data, message, size);
2222 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
2224 struct GNUNET_MESH_Unicast *m;
2226 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2227 m->ttl = htonl (ntohl (m->ttl) - 1);
2229 info->mesh_data->data_len = size;
2230 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2231 *info->mesh_data->reference_counter = 1;
2232 neighbor = peer_info_get (peer);
2233 for (p = neighbor->path_head; NULL != p; p = p->next)
2242 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2243 GNUNET_free (info->mesh_data->data);
2244 GNUNET_free (info->mesh_data);
2248 info->peer = neighbor;
2258 * Sends a CREATE PATH message for a path to a peer, properly registrating
2259 * all used resources.
2261 * @param peer PeerInfo of the final peer for whom this path is being created.
2262 * @param p Path itself.
2263 * @param t Tunnel for which the path is created.
2266 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2267 struct MeshTunnel *t)
2269 struct GNUNET_PeerIdentity id;
2270 struct MeshPathInfo *path_info;
2271 struct MeshPeerInfo *neighbor;
2277 p = tree_get_path_to_peer (t->tree, peer->id);
2284 for (i = 0; i < p->length; i++)
2286 if (p->peers[i] == myid)
2289 if (i >= p->length - 1)
2295 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2297 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2298 path_info->path = p;
2300 neighbor = peer_info_get (&id);
2301 path_info->peer = neighbor;
2302 queue_add (path_info,
2303 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2304 sizeof (struct GNUNET_MESH_ManipulatePath) +
2305 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2312 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2314 * @param t Tunnel whose path to destroy.
2315 * @param destination Short ID of the peer to whom the path to destroy.
2318 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2320 struct MeshPeerPath *p;
2323 p = tree_get_path_to_peer (t->tree, destination);
2329 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2330 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2332 struct GNUNET_MESH_ManipulatePath *msg;
2333 struct GNUNET_PeerIdentity *pi;
2337 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2338 msg->header.size = htons (size);
2339 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2340 msg->tid = htonl (t->id.tid);
2341 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2342 for (i = 0; i < p->length; i++)
2344 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2346 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2353 * Try to establish a new connection to this peer.
2354 * Use the best path for the given tunnel.
2355 * If the peer doesn't have any path to it yet, try to get one.
2356 * If the peer already has some path, send a CREATE PATH towards it.
2358 * @param peer PeerInfo of the peer.
2359 * @param t Tunnel for which to create the path, if possible.
2362 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2364 struct MeshPeerPath *p;
2365 struct MeshPathInfo *path_info;
2367 if (NULL != peer->path_head)
2369 p = tree_get_path_to_peer (t->tree, peer->id);
2376 // FIXME always send create path to self
2379 send_create_path (peer, p, t);
2383 struct GNUNET_HashCode hash;
2386 send_client_peer_connected (t, myid);
2387 t->local_tid_dest = next_local_tid++;
2388 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2391 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2392 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2399 else if (NULL == peer->dhtget)
2401 struct GNUNET_PeerIdentity id;
2403 GNUNET_PEER_resolve (peer->id, &id);
2404 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2405 path_info->peer = peer;
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2408 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2409 peer->dhtgetcls = path_info;
2410 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2411 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2412 &id.hashPubKey, /* key to search */
2413 dht_replication_level, /* replication level */
2414 GNUNET_DHT_RO_RECORD_ROUTE |
2415 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2416 NULL, /* xquery */ // FIXME BLOOMFILTER
2417 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2418 &dht_get_id_handler, path_info);
2420 /* Otherwise, there is no path but the DHT get is already started. */
2425 * Task to delay the connection of a peer
2427 * @param cls Closure (path info with tunnel and peer to connect).
2428 * Will be free'd on exection.
2429 * @param tc TaskContext
2432 peer_info_connect_task (void *cls,
2433 const struct GNUNET_SCHEDULER_TaskContext *tc)
2435 struct MeshPathInfo *path_info = cls;
2437 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2442 peer_info_connect (path_info->peer, path_info->t);
2448 * Destroy the peer_info and free any allocated resources linked to it
2450 * @param pi The peer_info to destroy.
2452 * @return GNUNET_OK on success
2455 peer_info_destroy (struct MeshPeerInfo *pi)
2457 struct GNUNET_PeerIdentity id;
2458 struct MeshPeerPath *p;
2459 struct MeshPeerPath *nextp;
2461 GNUNET_PEER_resolve (pi->id, &id);
2462 GNUNET_PEER_change_rc (pi->id, -1);
2465 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2468 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2469 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2471 if (NULL != pi->dhtget)
2473 GNUNET_DHT_get_stop (pi->dhtget);
2474 GNUNET_free (pi->dhtgetcls);
2480 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2490 * Remove all paths that rely on a direct connection between p1 and p2
2491 * from the peer itself and notify all tunnels about it.
2493 * @param peer PeerInfo of affected peer.
2494 * @param p1 GNUNET_PEER_Id of one peer.
2495 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2498 * TODO: optimize (see below)
2501 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2504 struct MeshPeerPath *p;
2505 struct MeshPeerPath *aux;
2506 struct MeshPeerInfo *peer_d;
2508 unsigned int destroyed;
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2515 p = peer->path_head;
2519 for (i = 0; i < (p->length - 1); i++)
2521 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2522 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2524 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2535 for (i = 0; i < peer->ntunnels; i++)
2537 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2541 * Problem: one or more peers have been deleted from the tunnel tree.
2542 * We don't know who they are to try to add them again.
2543 * We need to try to find a new path for each of the disconnected peers.
2544 * Some of them might already have a path to reach them that does not
2545 * involve p1 and p2. Adding all anew might render in a better tree than
2546 * the trivial immediate fix.
2548 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2549 * its children will be reachable trough him.
2551 peer_d = peer_info_get_short (d);
2554 for (p = peer_d->path_head; NULL != p; p = p->next)
2556 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2564 /* No callback, as peer will be already disconnected and a connection
2565 * scheduled by tunnel_notify_connection_broken.
2567 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2571 peer_info_connect (peer_d, peer->tunnels[i]);
2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2579 * Add the path to the peer and update the path used to reach it in case this
2582 * @param peer_info Destination peer to add the path to.
2583 * @param path New path to add. Last peer must be the peer in arg 1.
2584 * Path will be either used of freed if already known.
2585 * @param trusted Do we trust that this path is real?
2588 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2591 struct MeshPeerPath *aux;
2595 if ((NULL == peer_info) || (NULL == path))
2598 path_destroy (path);
2601 if (path->peers[path->length - 1] != peer_info->id)
2604 path_destroy (path);
2607 if (path->length <= 2 && GNUNET_NO == trusted)
2609 /* Only allow CORE to tell us about direct paths */
2610 path_destroy (path);
2613 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2614 for (l = 1; l < path->length; l++)
2616 if (path->peers[l] == myid)
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2619 for (l2 = 0; l2 < path->length - l; l2++)
2621 path->peers[l2] = path->peers[l + l2];
2626 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2631 struct GNUNET_PeerIdentity id;
2633 GNUNET_PEER_resolve (peer_info->id, &id);
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2635 path->length, GNUNET_i2s (&id));
2638 l = path_get_length (path);
2645 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2646 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2648 l2 = path_get_length (aux);
2651 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2652 peer_info->path_tail, aux, path);
2657 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2659 path_destroy (path);
2664 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2671 * Add the path to the origin peer and update the path used to reach it in case
2672 * this is the shortest.
2673 * The path is given in peer_info -> destination, therefore we turn the path
2674 * upside down first.
2676 * @param peer_info Peer to add the path to, being the origin of the path.
2677 * @param path New path to add after being inversed.
2678 * @param trusted Do we trust that this path is real?
2681 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2682 struct MeshPeerPath *path, int trusted)
2685 peer_info_add_path (peer_info, path, trusted);
2690 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2691 * to obtain a local peer -> destination path and interning the peer ids.
2693 * @return Newly allocated and created path
2695 static struct MeshPeerPath *
2696 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2697 unsigned int get_path_length,
2698 const struct GNUNET_PeerIdentity *put_path,
2699 unsigned int put_path_length)
2701 struct MeshPeerPath *p;
2707 GNUNET_PEER_change_rc (myid, 1);
2708 i = get_path_length;
2709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2710 for (i--; i >= 0; i--)
2712 id = GNUNET_PEER_intern (&get_path[i]);
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 GET: %s.\n",
2721 GNUNET_i2s (&get_path[i]));
2723 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2724 p->peers[p->length - 1] = id;
2727 i = put_path_length;
2728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2729 for (i--; i >= 0; i--)
2731 id = GNUNET_PEER_intern (&put_path[i]);
2734 /* PUT path went through us, so discard the path up until now and start
2735 * from here to get a much shorter (and loop-free) path.
2740 if (p->length > 0 && id == p->peers[p->length - 1])
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2743 GNUNET_PEER_change_rc (id, -1);
2747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2748 GNUNET_i2s (&put_path[i]));
2750 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2751 p->peers[p->length - 1] = id;
2755 if (get_path_length > 0)
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2757 GNUNET_i2s (&get_path[0]));
2758 if (put_path_length > 0)
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2760 GNUNET_i2s (&put_path[0]));
2761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2763 for (i = 0; i < p->length; i++)
2765 struct GNUNET_PeerIdentity peer_id;
2767 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2769 GNUNET_i2s (&peer_id));
2777 * Adds a path to the peer_infos of all the peers in the path
2779 * @param p Path to process.
2780 * @param confirmed Whether we know if the path works or not.
2783 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2787 /* TODO: invert and add */
2788 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2789 for (i++; i < p->length; i++)
2791 struct MeshPeerInfo *aux;
2792 struct MeshPeerPath *copy;
2794 aux = peer_info_get_short (p->peers[i]);
2795 copy = path_duplicate (p);
2796 copy->length = i + 1;
2797 peer_info_add_path (aux, copy, GNUNET_NO);
2803 * Send keepalive packets for a peer
2805 * @param cls Closure (tunnel for which to send the keepalive).
2806 * @param tc Notification context.
2808 * TODO: implement explicit multicast keepalive?
2811 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2815 * Search for a tunnel among the incoming tunnels
2817 * @param tid the local id of the tunnel
2819 * @return tunnel handler, NULL if doesn't exist
2821 static struct MeshTunnel *
2822 tunnel_get_incoming (MESH_TunnelNumber tid)
2824 struct GNUNET_HashCode hash;
2826 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2827 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2828 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2833 * Search for a tunnel among the tunnels for a client
2835 * @param c the client whose tunnels to search in
2836 * @param tid the local id of the tunnel
2838 * @return tunnel handler, NULL if doesn't exist
2840 static struct MeshTunnel *
2841 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2843 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2845 return tunnel_get_incoming (tid);
2849 struct GNUNET_HashCode hash;
2851 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2852 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2858 * Search for a tunnel by global ID using PEER_ID
2860 * @param pi owner of the tunnel
2861 * @param tid global tunnel number
2863 * @return tunnel handler, NULL if doesn't exist
2865 static struct MeshTunnel *
2866 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2868 struct MESH_TunnelID id;
2869 struct GNUNET_HashCode hash;
2874 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2875 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2880 * Search for a tunnel by global ID using full PeerIdentities
2882 * @param oid owner of the tunnel
2883 * @param tid global tunnel number
2885 * @return tunnel handler, NULL if doesn't exist
2887 static struct MeshTunnel *
2888 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2890 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2895 * Delete an active client from the tunnel.
2901 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2905 for (i = 0; i < t->nclients; i++)
2907 if (t->clients[i] == c)
2909 t->clients[i] = t->clients[t->nclients - 1];
2910 t->clients_acks[i] = t->clients_acks[t->nclients - 1];
2911 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2913 GNUNET_array_grow (t->clients_acks, t->nclients, t->nclients - 1);
2921 * Delete an ignored client from the tunnel.
2927 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2931 for (i = 0; i < t->nignore; i++)
2933 if (t->ignore[i] == c)
2935 t->ignore[i] = t->ignore[t->nignore - 1];
2936 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2944 * Delete a client from the tunnel. It should be only done on
2945 * client disconnection, otherwise use client_ignore_tunnel.
2951 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2953 tunnel_delete_ignored_client (t, c);
2954 tunnel_delete_active_client (t, c);
2959 * Iterator to free MeshTunnelChildInfo of tunnel children.
2961 * @param cls Closure (tunnel info).
2962 * @param key Hash of GNUNET_PEER_Id (unused).
2963 * @param value MeshTunnelChildInfo of the child.
2965 * @return always GNUNET_YES, to keep iterating
2968 tunnel_destroy_child (void *cls,
2969 const struct GNUNET_HashCode * key,
2972 GNUNET_free (value);
2978 * Callback used to notify a client owner of a tunnel that a peer has
2979 * disconnected, most likely because of a path change.
2981 * @param cls Closure (tunnel this notification is about).
2982 * @param peer_id Short ID of disconnected peer.
2985 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2987 struct MeshTunnel *t = cls;
2988 struct MeshPeerInfo *peer;
2989 struct MeshPathInfo *path_info;
2991 if (NULL != t->owner && NULL != nc)
2993 struct GNUNET_MESH_PeerControl msg;
2995 msg.header.size = htons (sizeof (msg));
2996 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2997 msg.tunnel_id = htonl (t->local_tid);
2998 GNUNET_PEER_resolve (peer_id, &msg.peer);
2999 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3000 &msg.header, GNUNET_NO);
3002 peer = peer_info_get_short (peer_id);
3003 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3004 path_info->peer = peer;
3006 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3011 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3012 * needed rescources.
3013 * If peer already exists, reevaluate shortest path and change if different.
3015 * @param t Tunnel we want to add a new peer to
3016 * @param peer PeerInfo of the peer being added
3020 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3022 struct GNUNET_PeerIdentity id;
3023 struct MeshPeerPath *best_p;
3024 struct MeshPeerPath *p;
3025 unsigned int best_cost;
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3029 GNUNET_PEER_resolve (peer->id, &id);
3031 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3034 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3035 GNUNET_assert (GNUNET_OK ==
3036 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3038 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3041 if (NULL != (p = peer->path_head))
3044 best_cost = tree_get_path_cost (t->tree, p);
3047 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3054 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3055 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3056 t->path_refresh_task =
3057 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3061 /* Start a DHT get */
3062 peer_info_connect (peer, t);
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3068 * Add a path to a tunnel which we don't own, just to remember the next hop.
3069 * If destination node was already in the tunnel, the first hop information
3070 * will be replaced with the new path.
3072 * @param t Tunnel we want to add a new peer to
3073 * @param p Path to add
3074 * @param own_pos Position of local node in path.
3078 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3079 unsigned int own_pos)
3081 struct GNUNET_PeerIdentity id;
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3084 GNUNET_assert (0 != own_pos);
3085 tree_add_path (t->tree, p, NULL, NULL);
3086 if (own_pos < p->length - 1)
3088 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3089 tree_update_first_hops (t->tree, myid, &id);
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3096 * Notifies a tunnel that a connection has broken that affects at least
3097 * some of its peers. Sends a notification towards the root of the tree.
3098 * In case the peer is the owner of the tree, notifies the client that owns
3099 * the tunnel and tries to reconnect.
3101 * @param t Tunnel affected.
3102 * @param p1 Peer that got disconnected from p2.
3103 * @param p2 Peer that got disconnected from p1.
3105 * @return Short ID of the peer disconnected (either p1 or p2).
3106 * 0 if the tunnel remained unaffected.
3108 static GNUNET_PEER_Id
3109 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3115 tree_notify_connection_broken (t->tree, p1, p2,
3116 &tunnel_notify_client_peer_disconnected,
3118 if (myid != p1 && myid != p2)
3124 if (tree_get_predecessor (t->tree) != 0)
3126 /* We are the peer still connected, notify owner of the disconnection. */
3127 struct GNUNET_MESH_PathBroken msg;
3128 struct GNUNET_PeerIdentity neighbor;
3130 msg.header.size = htons (sizeof (msg));
3131 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3132 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3133 msg.tid = htonl (t->id.tid);
3134 msg.peer1 = my_full_id;
3135 GNUNET_PEER_resolve (pid, &msg.peer2);
3136 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3137 send_message (&msg.header, &neighbor, t);
3145 * Send a multicast packet to a neighbor.
3147 * @param cls Closure (Info about the multicast packet)
3148 * @param neighbor_id Short ID of the neighbor to send the packet to.
3151 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3153 struct MeshData *mdata = cls;
3154 struct MeshTransmissionDescriptor *info;
3155 struct GNUNET_PeerIdentity neighbor;
3157 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3159 info->mesh_data = mdata;
3160 (*(mdata->reference_counter)) ++;
3161 info->destination = neighbor_id;
3162 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3164 GNUNET_i2s (&neighbor));
3165 info->peer = peer_info_get (&neighbor);
3166 GNUNET_assert (NULL != info->peer);
3168 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3169 info->mesh_data->data_len,
3176 * Send a message in a tunnel in multicast, sending a copy to each child node
3177 * down the local one in the tunnel tree.
3179 * @param t Tunnel in which to send the data.
3180 * @param msg Message to be sent.
3181 * @param internal Has the service generated this message?
3184 tunnel_send_multicast (struct MeshTunnel *t,
3185 const struct GNUNET_MessageHeader *msg,
3188 struct MeshData *mdata;
3190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3191 " sending a multicast packet...\n");
3192 mdata = GNUNET_malloc (sizeof (struct MeshData));
3193 mdata->data_len = ntohs (msg->size);
3194 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3196 mdata->data = GNUNET_malloc (mdata->data_len);
3197 memcpy (mdata->data, msg, mdata->data_len);
3198 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3200 struct GNUNET_MESH_Multicast *mcast;
3202 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3203 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3205 ntohl (mcast->ttl));
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3211 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3212 && GNUNET_NO == internal)
3214 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3216 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3222 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3223 if (*(mdata->reference_counter) == 0)
3225 GNUNET_free (mdata->data);
3226 GNUNET_free (mdata->reference_counter);
3227 if (NULL != mdata->task)
3229 GNUNET_SCHEDULER_cancel(*(mdata->task));
3230 GNUNET_free (mdata->task);
3231 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3233 // FIXME change order?
3234 GNUNET_free (mdata);
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3237 " sending a multicast packet done\n");
3243 * Increase the SKIP value of all peers that
3244 * have not received a unicast message.
3246 * @param cls Closure (ID of the peer that HAS received the message).
3247 * @param key ID of the neighbor.
3248 * @param value Information about the neighbor.
3250 * @return GNUNET_YES to keep iterating.
3253 tunnel_add_skip (void *cls,
3254 const struct GNUNET_HashCode * key,
3257 struct GNUNET_PeerIdentity *neighbor = cls;
3258 struct MeshTunnelChildInfo *cinfo = value;
3260 /* TODO compare only pointers? key == neighbor? */
3261 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3272 * Iterator to get the appropiate ACK value from all children nodes.
3274 * @param cls Closue (tunnel).
3275 * @param id Id of the child node.
3278 tunnel_get_child_ack (void *cls,
3281 struct GNUNET_PeerIdentity peer_id;
3282 struct MeshTunnelChildInfo *cinfo;
3283 struct MeshTunnelChildIteratorContext *ctx = cls;
3284 struct MeshTunnel *t = ctx->t;
3287 GNUNET_PEER_resolve (id, &peer_id);
3288 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3289 &peer_id.hashPubKey);
3292 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3294 cinfo->pid = t->pid;
3295 cinfo->skip = t->pid;
3296 cinfo->max_pid = ack = t->pid + 1;
3297 GNUNET_assert (GNUNET_OK ==
3298 GNUNET_CONTAINER_multihashmap_put(t->children_fc,
3299 &peer_id.hashPubKey,
3301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3305 ack = cinfo->max_pid;
3308 if (0 == ctx->max_child_ack)
3309 ctx->max_child_ack = ack;
3311 if (GNUNET_YES == t->speed_min)
3313 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3317 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3324 * Get the maximum PID allowed to transmit to any
3325 * tunnel child of the local peer, depending on the tunnel
3326 * buffering/speed settings.
3330 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3333 tunnel_get_children_ack (struct MeshTunnel *t)
3335 struct MeshTunnelChildIteratorContext ctx;
3337 ctx.max_child_ack = 0;
3339 tree_iterate_children (t->tree, tunnel_get_child_ack, &ctx);
3341 if (0 == ctx.nchildren)
3344 return (int64_t) ctx.max_child_ack;
3349 * Add a client to a tunnel, initializing all needed data structures.
3351 * @param t Tunnel to which add the client.
3352 * @param c Client which to add to the tunnel.
3355 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3359 GNUNET_array_append (t->clients, t->nclients, c);
3362 GNUNET_array_append (t->clients_acks, t->nclients, ack);
3367 * Set the ACK value of a client in a particular tunnel.
3369 * @param t Tunnel affected.
3370 * @param c Client whose ACK to set.
3371 * @param ack ACK value.
3374 tunnel_set_client_ack (struct MeshTunnel *t,
3375 struct MeshClient *c,
3380 for (i = 0; i < t->nclients; i++)
3382 if (t->clients[i] != c)
3384 t->clients_acks[i] = ack;
3392 * Get the ACK value of a client in a particular tunnel.
3394 * @param t Tunnel on which to look.
3395 * @param c Client whose ACK to get.
3397 * @return ACK value.
3399 uint32_t // FIXME static when used!!
3400 tunnel_get_client_ack (struct MeshTunnel *t,
3401 struct MeshClient *c)
3405 for (i = 0; i < t->nclients; i++)
3407 if (t->clients[i] != c)
3409 return t->clients_acks[i];
3412 return t->clients_acks[0];
3417 * Get the highest ACK value of all clients in a particular tunnel,
3418 * according to the buffering/speed settings.
3420 * @param t Tunnel on which to look.
3422 * @return Corresponding ACK value.
3425 tunnel_get_clients_ack (struct MeshTunnel *t)
3430 for (ack = 0, i = 0; i < t->nclients; i++)
3433 (GNUNET_YES == t->speed_min && t->clients_acks[i] < ack) ||
3434 (GNUNET_NO == t->speed_min && t->clients_acks[i] > ack))
3436 ack = t->clients_acks[i];
3440 if (GNUNET_YES == t->nobuffer && ack > t->pid)
3448 * Get the current ack value for a tunnel, taking in account the tunnel
3449 * mode and the status of all children nodes.
3453 * @return Maximum PID allowed.
3456 tunnel_get_fwd_ack (struct MeshTunnel *t)
3459 uint32_t buffer_free;
3461 uint32_t client_ack;
3464 count = t->pid - t->skip;
3465 buffer_free = t->queue_max - t->queue_n;
3466 ack = count + buffer_free;
3467 child_ack = tunnel_get_children_ack (t);
3469 ack = client_ack = tunnel_get_clients_ack (t);
3471 if (GNUNET_YES == t->speed_min)
3473 ack = child_ack > ack ? ack : child_ack;
3474 ack = client_ack > ack ? ack : client_ack;
3478 ack = child_ack > ack ? child_ack : ack;
3479 ack = client_ack > ack ? client_ack : ack;
3481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u\n",
3482 count, buffer_free, child_ack, client_ack);
3487 * Get the current ack value for a tunnel, taking in account the tunnel
3488 * mode and the status of all children nodes.
3492 * @return Maximum PID allowed.
3495 tunnel_get_bck_ack (struct MeshTunnel *t)
3506 * Send an ACK informing the predecessor about the available buffer space.
3507 * In case there is no predecessor, inform the owning client.
3508 * If buffering is off, send only on behalf of children or self if endpoint.
3509 * If buffering is on, send when sent to children and buffer space is free.
3511 * @param t Tunnel on which to send the ACK.
3512 * @param type Type of message that triggered the ACK transmission.
3515 tunnel_send_ack (struct MeshTunnel *t, uint16_t type)
3517 struct GNUNET_MESH_ACK msg;
3518 struct GNUNET_PeerIdentity id;
3521 if (NULL != t->owner)
3523 send_client_tunnel_ack (t->owner, t);
3526 /* Is it after unicast / multicast retransmission? */
3527 if (GNUNET_MESSAGE_TYPE_MESH_ACK != type)
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ACK via DATA retransmission\n");
3530 if (GNUNET_YES == t->nobuffer)
3532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3535 if (t->queue_max > t->queue_n * 2)
3537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3542 /* Ok, ACK might be necessary, what PID to ACK? */
3543 ack = tunnel_get_fwd_ack (t);
3545 /* If speed_min and not all children have ack'd, dont send yet */
3546 if (ack == t->last_ack)
3548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, not ready\n");
3553 msg.pid = htonl (ack);
3555 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3557 msg.header.size = htons (sizeof (msg));
3558 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3559 msg.tid = htonl (t->id.tid);
3560 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3561 send_message (&msg.header, &id, t);
3566 * Send a message to all peers in this tunnel that the tunnel is no longer
3569 * @param t The tunnel whose peers to notify.
3572 tunnel_send_destroy (struct MeshTunnel *t)
3574 struct GNUNET_MESH_TunnelDestroy msg;
3576 msg.header.size = htons (sizeof (msg));
3577 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3578 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3579 msg.tid = htonl (t->id.tid);
3580 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3585 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3587 * @param cls Closure (Tunnel which to cancel).
3588 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3591 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3593 struct MeshTunnel *t = cls;
3594 struct MeshPeerInfo *peer_info;
3595 struct MeshPeerQueue *pq;
3596 struct MeshPeerQueue *next;
3598 peer_info = peer_info_get_short (neighbor_id);
3599 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3602 if (pq->tunnel == t)
3604 queue_destroy (pq, GNUNET_YES);
3607 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3609 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3610 peer_info->core_transmit = NULL;
3615 * Destroy the tunnel and free any allocated resources linked to it.
3617 * @param t the tunnel to destroy
3619 * @return GNUNET_OK on success
3622 tunnel_destroy (struct MeshTunnel *t)
3624 struct MeshClient *c;
3625 struct GNUNET_HashCode hash;
3632 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3638 struct GNUNET_PeerIdentity id;
3640 GNUNET_PEER_resolve (t->id.oid, &id);
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3642 GNUNET_i2s (&id), t->id.tid);
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3648 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3649 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3654 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3657 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3661 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3662 for (i = 0; i < t->nclients; i++)
3666 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3671 for (i = 0; i < t->nignore; i++)
3675 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3680 if (t->nclients > 0)
3683 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3687 GNUNET_free (t->clients);
3689 if (NULL != t->peers)
3691 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3693 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3696 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3697 &tunnel_destroy_child,
3699 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3701 tree_destroy (t->tree);
3703 if (NULL != t->regex_ctx)
3704 regex_cancel_search (t->regex_ctx);
3705 if (NULL != t->dht_get_type)
3706 GNUNET_DHT_get_stop (t->dht_get_type);
3707 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3708 GNUNET_SCHEDULER_cancel (t->timeout_task);
3709 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3710 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3713 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3714 GNUNET_assert (0 <= n_tunnels);
3721 * Create a new tunnel
3723 * @param owner Who is the owner of the tunnel (short ID).
3724 * @param tid Tunnel Number of the tunnel.
3725 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3726 * @param local Tunnel Number for the tunnel, for the client point of view.
3728 * @return A new initialized tunnel. NULL on error.
3730 static struct MeshTunnel *
3731 tunnel_new (GNUNET_PEER_Id owner,
3732 MESH_TunnelNumber tid,
3733 struct MeshClient *client,
3734 MESH_TunnelNumber local)
3736 struct MeshTunnel *t;
3737 struct GNUNET_HashCode hash;
3739 if (n_tunnels >= max_tunnels && NULL == client)
3742 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3745 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
3746 t->tree = tree_new (owner);
3748 t->local_tid = local;
3749 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
3751 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3753 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3755 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3756 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3763 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3770 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3772 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3773 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3777 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3787 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3788 * that are no longer needed, as well as nodes of no longer reachable peers.
3789 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3791 * @param t Tunnel from which to remove the path.
3792 * @param peer Short id of the peer which should be removed.
3795 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3797 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3803 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3804 * client when the client disconnects. If the client is not the owner, the
3805 * owner will get notified if no more clients are in the tunnel and the client
3806 * get removed from the tunnel's list.
3808 * @param cls closure (client that is disconnecting)
3809 * @param key the hash of the local tunnel id (used to access the hashmap)
3810 * @param value the value stored at the key (tunnel to destroy)
3812 * @return GNUNET_OK on success
3815 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3817 struct MeshTunnel *t = value;
3818 struct MeshClient *c = cls;
3821 send_client_tunnel_disconnect(t, c);
3824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3825 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3826 tunnel_delete_client(t, c);
3827 client_delete_tunnel(c, t);
3830 tunnel_send_destroy(t);
3831 r = tunnel_destroy (t);
3837 * Timeout function, destroys tunnel if called
3839 * @param cls Closure (tunnel to destroy).
3840 * @param tc TaskContext
3843 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3845 struct MeshTunnel *t = cls;
3847 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3849 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3854 * Resets the tunnel timeout. Starts it if no timeout was running.
3856 * @param t Tunnel whose timeout to reset.
3859 tunnel_reset_timeout (struct MeshTunnel *t)
3861 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3862 GNUNET_SCHEDULER_cancel (t->timeout_task);
3864 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3865 (refresh_path_time, 4), &tunnel_timeout, t);
3869 /******************************************************************************/
3870 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3871 /******************************************************************************/
3874 * Function to send a create path packet to a peer.
3876 * @param cls closure
3877 * @param size number of bytes available in buf
3878 * @param buf where the callee should write the message
3879 * @return number of bytes written to buf
3882 send_core_path_create (void *cls, size_t size, void *buf)
3884 struct MeshPathInfo *info = cls;
3885 struct GNUNET_MESH_ManipulatePath *msg;
3886 struct GNUNET_PeerIdentity *peer_ptr;
3887 struct MeshTunnel *t = info->t;
3888 struct MeshPeerPath *p = info->path;
3893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3895 sizeof (struct GNUNET_MESH_ManipulatePath) +
3896 p->length * sizeof (struct GNUNET_PeerIdentity);
3898 if (size < size_needed || NULL == buf)
3903 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3904 msg->header.size = htons (size_needed);
3905 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3906 msg->tid = ntohl (t->id.tid);
3908 if (GNUNET_YES == t->speed_min)
3909 opt = MESH_TUNNEL_OPT_SPEED_MIN;
3910 if (GNUNET_YES == t->nobuffer)
3911 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3912 msg->opt = htonl(opt);
3914 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3915 for (i = 0; i < p->length; i++)
3917 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3924 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3930 * Fill the core buffer
3932 * @param cls closure (data itself)
3933 * @param size number of bytes available in buf
3934 * @param buf where the callee should write the message
3936 * @return number of bytes written to buf
3939 send_core_data_multicast (void *cls, size_t size, void *buf)
3941 struct MeshTransmissionDescriptor *info = cls;
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3945 GNUNET_assert (NULL != info);
3946 GNUNET_assert (NULL != info->peer);
3947 total_size = info->mesh_data->data_len;
3948 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3950 if (total_size > size)
3955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3956 memcpy (buf, info->mesh_data->data, total_size);
3959 struct GNUNET_MESH_Multicast *mc;
3960 struct GNUNET_MessageHeader *mh;
3963 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3965 mc = (struct GNUNET_MESH_Multicast *) mh;
3966 mh = (struct GNUNET_MessageHeader *) &mc[1];
3967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3968 " multicast, payload type %u\n", ntohs (mh->type));
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3970 " multicast, payload size %u\n", ntohs (mh->size));
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3979 data_descriptor_decrement_rc (info->mesh_data);
3980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3988 * Creates a path ack message in buf and frees all unused resources.
3990 * @param cls closure (MeshTransmissionDescriptor)
3991 * @param size number of bytes available in buf
3992 * @param buf where the callee should write the message
3993 * @return number of bytes written to buf
3996 send_core_path_ack (void *cls, size_t size, void *buf)
3998 struct MeshTransmissionDescriptor *info = cls;
3999 struct GNUNET_MESH_PathACK *msg = buf;
4001 GNUNET_assert (NULL != info);
4002 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4007 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4008 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4009 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4010 msg->tid = htonl (info->origin->tid);
4011 msg->peer_id = my_full_id;
4014 /* TODO add signature */
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4017 return sizeof (struct GNUNET_MESH_PathACK);
4022 * Free a transmission that was already queued with all resources
4023 * associated to the request.
4025 * @param queue Queue handler to cancel.
4026 * @param clear_cls Is it necessary to free associated cls?
4029 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4031 struct MeshTransmissionDescriptor *dd;
4032 struct MeshPathInfo *path_info;
4034 if (GNUNET_YES == clear_cls)
4036 switch (queue->type)
4038 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4039 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4040 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4043 data_descriptor_decrement_rc (dd->mesh_data);
4045 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4047 path_info = queue->cls;
4048 path_destroy (path_info->path);
4052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4054 GNUNET_free_non_null (queue->cls);
4056 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4057 queue->peer->queue_tail,
4059 GNUNET_free (queue);
4064 * Core callback to write a queued packet to core buffer
4066 * @param cls Closure (peer info).
4067 * @param size Number of bytes available in buf.
4068 * @param buf Where the to write the message.
4070 * @return number of bytes written to buf
4073 queue_send (void *cls, size_t size, void *buf)
4075 struct MeshPeerInfo *peer = cls;
4076 struct GNUNET_MessageHeader *msg;
4077 struct MeshPeerQueue *queue;
4078 struct MeshTunnel *t;
4081 peer->core_transmit = NULL;
4082 queue = peer->queue_head;
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4086 /* If queue is empty, send should have been cancelled */
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4094 /* Check if buffer size is enough for the message */
4095 if (queue->size > size)
4097 struct GNUNET_PeerIdentity id;
4099 GNUNET_PEER_resolve (peer->id, &id);
4100 peer->core_transmit =
4101 GNUNET_CORE_notify_transmit_ready(core_handle,
4104 GNUNET_TIME_UNIT_FOREVER_REL,
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4117 switch (queue->type)
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
4121 data_size = send_core_data_raw (queue->cls, size, buf);
4122 msg = (struct GNUNET_MessageHeader *) buf;
4123 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
4124 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4126 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4128 data_size = send_core_data_multicast(queue->cls, size, buf);
4129 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4131 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4133 data_size = send_core_path_create(queue->cls, size, buf);
4135 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4137 data_size = send_core_path_ack(queue->cls, size, buf);
4141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
4145 /* Free queue, but cls was freed by send_core_* */
4146 queue_destroy (queue, GNUNET_NO);
4148 if (GNUNET_YES == t->destroy && 0 == t->queue_n)
4150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4154 /* If more data in queue, send next */
4155 if (NULL != peer->queue_head)
4157 struct GNUNET_PeerIdentity id;
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4160 GNUNET_PEER_resolve (peer->id, &id);
4161 peer->core_transmit =
4162 GNUNET_CORE_notify_transmit_ready(core_handle,
4165 GNUNET_TIME_UNIT_FOREVER_REL,
4167 peer->queue_head->size,
4171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4177 * Queue and pass message to core when possible.
4179 * @param cls Closure (type dependant).
4180 * @param type Type of the message, 0 for a raw message.
4181 * @param size Size of the message.
4182 * @param dst Neighbor to send message to.
4183 * @param t Tunnel this message belongs to.
4186 queue_add (void *cls, uint16_t type, size_t size,
4187 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4189 struct MeshPeerQueue *queue;
4191 if (t->queue_n >= t->queue_max)
4193 if (NULL == t->owner)
4194 GNUNET_break_op(0); // TODO: kill connection?
4197 return; // Drop message
4200 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4206 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4207 if (NULL == dst->core_transmit)
4209 struct GNUNET_PeerIdentity id;
4211 GNUNET_PEER_resolve (dst->id, &id);
4212 dst->core_transmit =
4213 GNUNET_CORE_notify_transmit_ready(core_handle,
4216 GNUNET_TIME_UNIT_FOREVER_REL,
4225 /******************************************************************************/
4226 /******************** MESH NETWORK HANDLERS **************************/
4227 /******************************************************************************/
4231 * Core handler for path creation
4233 * @param cls closure
4234 * @param message message
4235 * @param peer peer identity this notification is about
4236 * @param atsi performance data
4237 * @param atsi_count number of records in 'atsi'
4239 * @return GNUNET_OK to keep the connection open,
4240 * GNUNET_SYSERR to close it (signal serious error)
4243 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4244 const struct GNUNET_MessageHeader *message,
4245 const struct GNUNET_ATS_Information *atsi,
4246 unsigned int atsi_count)
4248 unsigned int own_pos;
4251 MESH_TunnelNumber tid;
4252 struct GNUNET_MESH_ManipulatePath *msg;
4253 struct GNUNET_PeerIdentity *pi;
4254 struct GNUNET_HashCode hash;
4255 struct MeshPeerPath *path;
4256 struct MeshPeerInfo *dest_peer_info;
4257 struct MeshPeerInfo *orig_peer_info;
4258 struct MeshTunnel *t;
4260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4261 "Received a path create msg [%s]\n",
4262 GNUNET_i2s (&my_full_id));
4263 size = ntohs (message->size);
4264 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4266 GNUNET_break_op (0);
4270 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4271 if (size % sizeof (struct GNUNET_PeerIdentity))
4273 GNUNET_break_op (0);
4276 size /= sizeof (struct GNUNET_PeerIdentity);
4279 GNUNET_break_op (0);
4282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4283 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4285 tid = ntohl (msg->tid);
4286 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4288 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4289 t = tunnel_get (pi, tid);
4290 if (NULL == t) // FIXME only for INCOMING tunnels?
4294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4295 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4298 // FIXME notify failure
4301 opt = ntohl (msg->opt);
4302 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4303 GNUNET_YES : GNUNET_NO;
4304 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4305 GNUNET_YES : GNUNET_NO;
4307 if (GNUNET_YES == t->nobuffer)
4310 while (NULL != tunnel_get_incoming (next_local_tid))
4311 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4312 t->local_tid_dest = next_local_tid++;
4313 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4315 tunnel_reset_timeout (t);
4316 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4318 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4319 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4327 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4328 if (NULL == dest_peer_info)
4330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4331 " Creating PeerInfo for destination.\n");
4332 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4333 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4334 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4338 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4339 if (NULL == orig_peer_info)
4341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4342 " Creating PeerInfo for origin.\n");
4343 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4344 orig_peer_info->id = GNUNET_PEER_intern (pi);
4345 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4346 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4349 path = path_new (size);
4351 for (i = 0; i < size; i++)
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4354 GNUNET_i2s (&pi[i]));
4355 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4356 if (path->peers[i] == myid)
4359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4362 /* cannot be self, must be 'not found' */
4363 /* create path: self not found in path through self */
4364 GNUNET_break_op (0);
4365 path_destroy (path);
4366 /* FIXME error. destroy tunnel? leave for timeout? */
4369 path_add_to_peers (path, GNUNET_NO);
4370 tunnel_add_path (t, path, own_pos);
4371 if (own_pos == size - 1)
4373 /* It is for us! Send ack. */
4374 struct MeshTransmissionDescriptor *info;
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4377 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4378 if (NULL == t->peers)
4380 /* New tunnel! Notify clients on data. */
4381 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4383 GNUNET_break (GNUNET_SYSERR !=
4384 GNUNET_CONTAINER_multihashmap_put (t->peers,
4385 &my_full_id.hashPubKey,
4388 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4389 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4390 info->origin = &t->id;
4391 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4392 GNUNET_assert (NULL != info->peer);
4394 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4395 sizeof (struct GNUNET_MESH_PathACK),
4401 struct MeshPeerPath *path2;
4403 /* It's for somebody else! Retransmit. */
4404 path2 = path_duplicate (path);
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4406 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4407 path2 = path_duplicate (path);
4408 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4409 send_create_path (dest_peer_info, path, t);
4416 * Core handler for path destruction
4418 * @param cls closure
4419 * @param message message
4420 * @param peer peer identity this notification is about
4421 * @param atsi performance data
4422 * @param atsi_count number of records in 'atsi'
4424 * @return GNUNET_OK to keep the connection open,
4425 * GNUNET_SYSERR to close it (signal serious error)
4428 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4429 const struct GNUNET_MessageHeader *message,
4430 const struct GNUNET_ATS_Information *atsi,
4431 unsigned int atsi_count)
4433 struct GNUNET_MESH_ManipulatePath *msg;
4434 struct GNUNET_PeerIdentity *pi;
4435 struct MeshPeerPath *path;
4436 struct MeshTunnel *t;
4437 unsigned int own_pos;
4441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4442 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4443 size = ntohs (message->size);
4444 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4446 GNUNET_break_op (0);
4450 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4451 if (size % sizeof (struct GNUNET_PeerIdentity))
4453 GNUNET_break_op (0);
4456 size /= sizeof (struct GNUNET_PeerIdentity);
4459 GNUNET_break_op (0);
4462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4464 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4465 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4467 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4469 t = tunnel_get (pi, ntohl (msg->tid));
4472 /* TODO notify back: we don't know this tunnel */
4473 GNUNET_break_op (0);
4476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4477 path = path_new (size);
4479 for (i = 0; i < size; i++)
4481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4482 GNUNET_i2s (&pi[i]));
4483 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4484 if (path->peers[i] == myid)
4487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4488 if (own_pos < path->length - 1)
4489 send_message (message, &pi[own_pos + 1], t);
4491 send_client_tunnel_disconnect(t, NULL);
4493 tunnel_delete_peer (t, path->peers[path->length - 1]);
4494 path_destroy (path);
4500 * Core handler for notifications of broken paths
4502 * @param cls closure
4503 * @param message message
4504 * @param peer peer identity this notification is about
4505 * @param atsi performance data
4506 * @param atsi_count number of records in 'atsi'
4508 * @return GNUNET_OK to keep the connection open,
4509 * GNUNET_SYSERR to close it (signal serious error)
4512 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4513 const struct GNUNET_MessageHeader *message,
4514 const struct GNUNET_ATS_Information *atsi,
4515 unsigned int atsi_count)
4517 struct GNUNET_MESH_PathBroken *msg;
4518 struct MeshTunnel *t;
4520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4521 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4522 msg = (struct GNUNET_MESH_PathBroken *) message;
4523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4524 GNUNET_i2s (&msg->peer1));
4525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4526 GNUNET_i2s (&msg->peer2));
4527 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4530 GNUNET_break_op (0);
4533 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4534 GNUNET_PEER_search (&msg->peer2));
4541 * Core handler for tunnel destruction
4543 * @param cls closure
4544 * @param message message
4545 * @param peer peer identity this notification is about
4546 * @param atsi performance data
4547 * @param atsi_count number of records in 'atsi'
4549 * @return GNUNET_OK to keep the connection open,
4550 * GNUNET_SYSERR to close it (signal serious error)
4553 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4554 const struct GNUNET_MessageHeader *message,
4555 const struct GNUNET_ATS_Information *atsi,
4556 unsigned int atsi_count)
4558 struct GNUNET_MESH_TunnelDestroy *msg;
4559 struct MeshTunnel *t;
4561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4562 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4563 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4565 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4566 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4569 /* Probably already got the message from another path,
4570 * destroyed the tunnel and retransmitted to children.
4573 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4576 if (t->id.oid == myid)
4578 GNUNET_break_op (0);
4581 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4583 /* Tunnel was incoming, notify clients */
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4585 t->local_tid, t->local_tid_dest);
4586 send_clients_tunnel_destroy (t);
4588 tunnel_send_destroy (t);
4589 t->destroy = GNUNET_YES;
4590 // TODO: add timeout to destroy the tunnel anyway
4596 * Core handler for mesh network traffic going from the origin to a peer
4598 * @param cls closure
4599 * @param peer peer identity this notification is about
4600 * @param message message
4601 * @param atsi performance data
4602 * @param atsi_count number of records in 'atsi'
4603 * @return GNUNET_OK to keep the connection open,
4604 * GNUNET_SYSERR to close it (signal serious error)
4607 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4608 const struct GNUNET_MessageHeader *message,
4609 const struct GNUNET_ATS_Information *atsi,
4610 unsigned int atsi_count)
4612 struct GNUNET_MESH_Unicast *msg;
4613 struct GNUNET_PeerIdentity *neighbor;
4614 struct MeshTunnelChildInfo *cinfo;
4615 struct MeshTunnel *t;
4616 GNUNET_PEER_Id dest_id;
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4623 size = ntohs (message->size);
4625 sizeof (struct GNUNET_MESH_Unicast) +
4626 sizeof (struct GNUNET_MessageHeader))
4631 msg = (struct GNUNET_MESH_Unicast *) message;
4632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4633 ntohs (msg[1].header.type));
4634 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4637 /* TODO notify back: we don't know this tunnel */
4638 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4639 GNUNET_break_op (0);
4642 pid = ntohl (msg->pid);
4645 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4646 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4647 " Already seen pid %u, DROPPING!\n", pid);
4652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4653 " pid %u not seen yet, forwarding\n", pid);
4655 t->skip += (pid - t->pid) - 1;
4657 tunnel_reset_timeout (t);
4658 dest_id = GNUNET_PEER_search (&msg->destination);
4659 if (dest_id == myid)
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4662 " it's for us! sending to clients...\n");
4663 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4664 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4665 // FIXME send after client processes the packet
4666 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4669 ttl = ntohl (msg->ttl);
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4673 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4674 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4678 " not for us, retransmitting...\n");
4679 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4681 neighbor = tree_get_first_hop (t->tree, dest_id);
4682 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4683 &neighbor->hashPubKey);
4686 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
4687 cinfo->id = GNUNET_PEER_intern (neighbor);
4689 cinfo->max_pid = pid + t->queue_max - t->queue_n; // FIXME review
4691 GNUNET_assert (GNUNET_OK ==
4692 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
4693 &neighbor->hashPubKey,
4695 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
4698 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4701 send_message (message, neighbor, t);
4707 * Core handler for mesh network traffic going from the origin to all peers
4709 * @param cls closure
4710 * @param message message
4711 * @param peer peer identity this notification is about
4712 * @param atsi performance data
4713 * @param atsi_count number of records in 'atsi'
4714 * @return GNUNET_OK to keep the connection open,
4715 * GNUNET_SYSERR to close it (signal serious error)
4717 * TODO: Check who we got this from, to validate route.
4720 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4721 const struct GNUNET_MessageHeader *message,
4722 const struct GNUNET_ATS_Information *atsi,
4723 unsigned int atsi_count)
4725 struct GNUNET_MESH_Multicast *msg;
4726 struct MeshTunnel *t;
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
4732 size = ntohs (message->size);
4733 if (sizeof (struct GNUNET_MESH_Multicast) +
4734 sizeof (struct GNUNET_MessageHeader) > size)
4736 GNUNET_break_op (0);
4739 msg = (struct GNUNET_MESH_Multicast *) message;
4740 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4744 /* TODO notify that we dont know that tunnel */
4745 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4746 GNUNET_break_op (0);
4749 pid = ntohl (msg->pid);
4752 /* already seen this packet, drop */
4753 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4754 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4755 " Already seen pid %u, DROPPING!\n", pid);
4760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4761 " pid %u not seen yet, forwarding\n", pid);
4763 t->skip += (pid - t->pid) - 1;
4765 tunnel_reset_timeout (t);
4767 /* Transmit to locally interested clients */
4768 if (NULL != t->peers &&
4769 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
4771 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
4772 send_subscribed_clients (message, &msg[1].header);
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
4775 if (ntohl (msg->ttl) == 0)
4777 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4778 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4781 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
4782 tunnel_send_multicast (t, message, GNUNET_NO);
4788 * Core handler for mesh network traffic toward the owner of a tunnel
4790 * @param cls closure
4791 * @param message message
4792 * @param peer peer identity this notification is about
4793 * @param atsi performance data
4794 * @param atsi_count number of records in 'atsi'
4796 * @return GNUNET_OK to keep the connection open,
4797 * GNUNET_SYSERR to close it (signal serious error)
4800 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4801 const struct GNUNET_MessageHeader *message,
4802 const struct GNUNET_ATS_Information *atsi,
4803 unsigned int atsi_count)
4805 struct GNUNET_MESH_ToOrigin *msg;
4806 struct GNUNET_PeerIdentity id;
4807 struct MeshPeerInfo *peer_info;
4808 struct MeshTunnel *t;
4811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4813 size = ntohs (message->size);
4814 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4815 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4817 GNUNET_break_op (0);
4820 msg = (struct GNUNET_MESH_ToOrigin *) message;
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4822 ntohs (msg[1].header.type));
4823 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4827 /* TODO notify that we dont know this tunnel (whom)? */
4828 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4829 GNUNET_break_op (0);
4833 if (t->id.oid == myid)
4836 struct GNUNET_MESH_ToOrigin *copy;
4838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4839 " it's for us! sending to clients...\n");
4840 if (NULL == t->owner)
4842 /* got data packet for ownerless tunnel */
4843 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
4845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4846 GNUNET_break_op (0);
4849 /* TODO signature verification */
4850 memcpy (cbuf, message, size);
4851 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4852 copy->tid = htonl (t->local_tid);
4853 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4854 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4855 ©->header, GNUNET_NO);
4858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4859 " not for us, retransmitting...\n");
4861 peer_info = peer_info_get (&msg->oid);
4862 if (NULL == peer_info)
4864 /* unknown origin of tunnel */
4868 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4869 send_message (message, &id, t);
4870 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4877 * Core handler for mesh network traffic point-to-point acks.
4879 * @param cls closure
4880 * @param message message
4881 * @param peer peer identity this notification is about
4882 * @param atsi performance data
4883 * @param atsi_count number of records in 'atsi'
4885 * @return GNUNET_OK to keep the connection open,
4886 * GNUNET_SYSERR to close it (signal serious error)
4889 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4890 const struct GNUNET_MessageHeader *message,
4891 const struct GNUNET_ATS_Information *atsi,
4892 unsigned int atsi_count)
4894 struct GNUNET_MESH_ACK *msg;
4895 struct MeshTunnelChildInfo *cinfo;
4896 struct MeshTunnel *t;
4899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got an ACK packet from %s\n",
4901 msg = (struct GNUNET_MESH_ACK *) message;
4902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4903 ntohs (msg[1].header.type));
4904 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4908 /* TODO notify that we dont know this tunnel (whom)? */
4909 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4910 GNUNET_break_op (0);
4913 ack = ntohl (msg->pid);
4914 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4918 GNUNET_break_op (0);
4921 cinfo->max_pid = ack;
4922 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4928 * Core handler for path ACKs
4930 * @param cls closure
4931 * @param message message
4932 * @param peer peer identity this notification is about
4933 * @param atsi performance data
4934 * @param atsi_count number of records in 'atsi'
4936 * @return GNUNET_OK to keep the connection open,
4937 * GNUNET_SYSERR to close it (signal serious error)
4940 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4941 const struct GNUNET_MessageHeader *message,
4942 const struct GNUNET_ATS_Information *atsi,
4943 unsigned int atsi_count)
4945 struct GNUNET_MESH_PathACK *msg;
4946 struct GNUNET_PeerIdentity id;
4947 struct MeshPeerInfo *peer_info;
4948 struct MeshPeerPath *p;
4949 struct MeshTunnel *t;
4951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4952 GNUNET_i2s (&my_full_id));
4953 msg = (struct GNUNET_MESH_PathACK *) message;
4954 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4957 /* TODO notify that we don't know the tunnel */
4958 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4960 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4964 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4966 peer_info = peer_info_get (&msg->peer_id);
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4968 GNUNET_i2s (&msg->peer_id));
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4972 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
4974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4975 "connect_by_string completed, stopping search\n");
4976 regex_cancel_search (t->regex_ctx);
4977 t->regex_ctx = NULL;
4980 /* Add paths to peers? */
4981 p = tree_get_path_to_peer (t->tree, peer_info->id);
4984 path_add_to_peers (p, GNUNET_YES);
4992 /* Message for us? */
4993 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4996 if (NULL == t->owner)
4998 GNUNET_break_op (0);
5001 if (NULL != t->dht_get_type)
5003 GNUNET_DHT_get_stop (t->dht_get_type);
5004 t->dht_get_type = NULL;
5006 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5008 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5009 send_client_peer_connected (t, peer_info->id);
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5015 " not for us, retransmitting...\n");
5016 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5017 peer_info = peer_info_get (&msg->oid);
5018 if (NULL == peer_info)
5020 /* If we know the tunnel, we should DEFINITELY know the peer */
5024 send_message (message, &id, t);
5030 * Functions to handle messages from core
5032 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5033 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5034 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5035 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5036 sizeof (struct GNUNET_MESH_PathBroken)},
5037 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5038 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5039 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5040 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5041 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5042 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5043 sizeof (struct GNUNET_MESH_ACK)},
5044 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5045 sizeof (struct GNUNET_MESH_PathACK)},
5051 /******************************************************************************/
5052 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5053 /******************************************************************************/
5056 * deregister_app: iterator for removing each application registered by a client
5058 * @param cls closure
5059 * @param key the hash of the application id (used to access the hashmap)
5060 * @param value the value stored at the key (client)
5062 * @return GNUNET_OK on success
5065 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5067 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5068 GNUNET_break (GNUNET_YES ==
5069 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5075 * notify_client_connection_failure: notify a client that the connection to the
5076 * requested remote peer is not possible (for instance, no route found)
5077 * Function called when the socket is ready to queue more data. "buf" will be
5078 * NULL and "size" zero if the socket was closed for writing in the meantime.
5080 * @param cls closure
5081 * @param size number of bytes available in buf
5082 * @param buf where the callee should write the message
5083 * @return number of bytes written to buf
5086 notify_client_connection_failure (void *cls, size_t size, void *buf)
5089 struct MeshPeerInfo *peer_info;
5090 struct GNUNET_MESH_PeerControl *msg;
5091 struct GNUNET_PeerIdentity id;
5093 if (0 == size && NULL == buf)
5095 // TODO retry? cancel?
5099 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5100 peer_info = (struct MeshPeerInfo *) cls;
5101 msg = (struct GNUNET_MESH_PeerControl *) buf;
5102 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5103 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5104 // msg->tunnel_id = htonl(peer_info->t->tid);
5105 GNUNET_PEER_resolve (peer_info->id, &id);
5106 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5114 * Send keepalive packets for a peer
5116 * @param cls Closure (tunnel for which to send the keepalive).
5117 * @param tc Notification context.
5119 * TODO: implement explicit multicast keepalive?
5122 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5124 struct MeshTunnel *t = cls;
5125 struct GNUNET_MessageHeader *payload;
5126 struct GNUNET_MESH_Multicast *msg;
5128 sizeof (struct GNUNET_MESH_Multicast) +
5129 sizeof (struct GNUNET_MessageHeader);
5132 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5136 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5139 "sending keepalive for tunnel %d\n", t->id.tid);
5141 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5142 msg->header.size = htons (size);
5143 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5144 msg->oid = my_full_id;
5145 msg->tid = htonl (t->id.tid);
5146 msg->ttl = htonl (default_ttl);
5147 msg->pid = htonl (t->pid + 1);
5149 payload = (struct GNUNET_MessageHeader *) &msg[1];
5150 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5151 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5152 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5154 t->path_refresh_task =
5155 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5161 * Function to process paths received for a new peer addition. The recorded
5162 * paths form the initial tunnel, which can be optimized later.
5163 * Called on each result obtained for the DHT search.
5165 * @param cls closure
5166 * @param exp when will this value expire
5167 * @param key key of the result
5168 * @param get_path path of the get request
5169 * @param get_path_length lenght of get_path
5170 * @param put_path path of the put request
5171 * @param put_path_length length of the put_path
5172 * @param type type of the result
5173 * @param size number of bytes in data
5174 * @param data pointer to the result data
5176 * TODO: re-issue the request after certain time? cancel after X results?
5179 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5180 const struct GNUNET_HashCode * key,
5181 const struct GNUNET_PeerIdentity *get_path,
5182 unsigned int get_path_length,
5183 const struct GNUNET_PeerIdentity *put_path,
5184 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5185 size_t size, const void *data)
5187 struct MeshPathInfo *path_info = cls;
5188 struct MeshPeerPath *p;
5189 struct GNUNET_PeerIdentity pi;
5192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5193 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5196 p = path_build_from_dht (get_path, get_path_length, put_path,
5198 path_add_to_peers (p, GNUNET_NO);
5200 for (i = 0; i < path_info->peer->ntunnels; i++)
5202 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5203 peer_info_connect (path_info->peer, path_info->t);
5211 * Function to process paths received for a new peer addition. The recorded
5212 * paths form the initial tunnel, which can be optimized later.
5213 * Called on each result obtained for the DHT search.
5215 * @param cls closure
5216 * @param exp when will this value expire
5217 * @param key key of the result
5218 * @param get_path path of the get request
5219 * @param get_path_length lenght of get_path
5220 * @param put_path path of the put request
5221 * @param put_path_length length of the put_path
5222 * @param type type of the result
5223 * @param size number of bytes in data
5224 * @param data pointer to the result data
5227 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5228 const struct GNUNET_HashCode * key,
5229 const struct GNUNET_PeerIdentity *get_path,
5230 unsigned int get_path_length,
5231 const struct GNUNET_PeerIdentity *put_path,
5232 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5233 size_t size, const void *data)
5235 const struct PBlock *pb = data;
5236 const struct GNUNET_PeerIdentity *pi = &pb->id;
5237 struct MeshTunnel *t = cls;
5238 struct MeshPeerInfo *peer_info;
5239 struct MeshPeerPath *p;
5241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5242 if (size != sizeof (struct PBlock))
5244 GNUNET_break_op (0);
5247 if (ntohl(pb->type) != t->type)
5249 GNUNET_break_op (0);
5252 GNUNET_assert (NULL != t->owner);
5253 peer_info = peer_info_get (pi);
5254 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5258 p = path_build_from_dht (get_path, get_path_length, put_path,
5260 path_add_to_peers (p, GNUNET_NO);
5262 tunnel_add_peer (t, peer_info);
5263 peer_info_connect (peer_info, t);
5268 * Function to process DHT string to regex matching.
5269 * Called on each result obtained for the DHT search.
5271 * @param cls closure (search context)
5272 * @param exp when will this value expire
5273 * @param key key of the result
5274 * @param get_path path of the get request (not used)
5275 * @param get_path_length lenght of get_path (not used)
5276 * @param put_path path of the put request (not used)
5277 * @param put_path_length length of the put_path (not used)
5278 * @param type type of the result
5279 * @param size number of bytes in data
5280 * @param data pointer to the result data
5283 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5284 const struct GNUNET_HashCode * key,
5285 const struct GNUNET_PeerIdentity *get_path,
5286 unsigned int get_path_length,
5287 const struct GNUNET_PeerIdentity *put_path,
5288 unsigned int put_path_length,
5289 enum GNUNET_BLOCK_Type type,
5290 size_t size, const void *data)
5292 const struct MeshRegexAccept *block = data;
5293 struct MeshRegexSearchContext *ctx = cls;
5294 struct MeshRegexSearchInfo *info = ctx->info;
5295 struct MeshPeerPath *p;
5296 struct MeshPeerInfo *peer_info;
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5301 peer_info = peer_info_get(&block->id);
5302 p = path_build_from_dht (get_path, get_path_length, put_path,
5304 path_add_to_peers (p, GNUNET_NO);
5307 tunnel_add_peer (info->t, peer_info);
5308 peer_info_connect (peer_info, info->t);
5309 if (0 == info->peer)
5311 info->peer = peer_info->id;
5315 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5318 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5319 ®ex_connect_timeout,
5327 * Function to process DHT string to regex matching.
5328 * Called on each result obtained for the DHT search.
5330 * @param cls closure (search context)
5331 * @param exp when will this value expire
5332 * @param key key of the result
5333 * @param get_path path of the get request (not used)
5334 * @param get_path_length lenght of get_path (not used)
5335 * @param put_path path of the put request (not used)
5336 * @param put_path_length length of the put_path (not used)
5337 * @param type type of the result
5338 * @param size number of bytes in data
5339 * @param data pointer to the result data
5341 * TODO: re-issue the request after certain time? cancel after X results?
5344 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5345 const struct GNUNET_HashCode * key,
5346 const struct GNUNET_PeerIdentity *get_path,
5347 unsigned int get_path_length,
5348 const struct GNUNET_PeerIdentity *put_path,
5349 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5350 size_t size, const void *data)
5352 const struct MeshRegexBlock *block = data;
5353 struct MeshRegexSearchContext *ctx = cls;
5354 struct MeshRegexSearchInfo *info = ctx->info;
5358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5359 "DHT GET STRING RETURNED RESULTS\n");
5360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5361 " key: %s\n", GNUNET_h2s (key));
5363 copy = GNUNET_malloc (size);
5364 memcpy (copy, data, size);
5365 GNUNET_break (GNUNET_OK ==
5366 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5367 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5368 len = ntohl (block->n_proof);
5370 char proof[len + 1];
5372 memcpy (proof, &block[1], len);
5374 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5376 GNUNET_break_op (0);
5380 len = strlen (info->description);
5381 if (len == ctx->position) // String processed
5383 if (GNUNET_YES == ntohl (block->accepting))
5385 regex_find_path(key, ctx);
5389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5390 // FIXME REGEX this block not successful, wait for more? start timeout?
5394 GNUNET_break (GNUNET_OK ==
5395 GNUNET_MESH_regex_block_iterate (block, size,
5396 ®ex_edge_iterator, ctx));
5400 /******************************************************************************/
5401 /********************* MESH LOCAL HANDLES **************************/
5402 /******************************************************************************/
5406 * Handler for client disconnection
5408 * @param cls closure
5409 * @param client identification of the client; NULL
5410 * for the last call when the server is destroyed
5413 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5415 struct MeshClient *c;
5416 struct MeshClient *next;
5419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5428 if (c->handle != client)
5430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5436 GNUNET_SERVER_client_drop (c->handle);
5437 c->shutting_down = GNUNET_YES;
5438 GNUNET_assert (NULL != c->own_tunnels);
5439 GNUNET_assert (NULL != c->incoming_tunnels);
5440 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5441 &tunnel_destroy_iterator, c);
5442 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5443 &tunnel_destroy_iterator, c);
5444 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5445 &tunnel_destroy_iterator, c);
5446 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5447 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5448 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5450 /* deregister clients applications */
5451 if (NULL != c->apps)
5453 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5454 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5456 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5457 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5459 GNUNET_SCHEDULER_cancel (announce_applications_task);
5460 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5462 if (NULL != c->types)
5463 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5464 for (i = 0; i < c->n_regex; i++)
5466 GNUNET_free (c->regexes[i]);
5468 GNUNET_free_non_null (c->regexes);
5469 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5470 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5472 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5475 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5484 * Handler for new clients
5486 * @param cls closure
5487 * @param client identification of the client
5488 * @param message the actual message, which includes messages the client wants
5491 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5492 const struct GNUNET_MessageHeader *message)
5494 struct GNUNET_MESH_ClientConnect *cc_msg;
5495 struct MeshClient *c;
5496 GNUNET_MESH_ApplicationType *a;
5503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5504 /* Check data sanity */
5505 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5506 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5507 ntypes = ntohs (cc_msg->types);
5508 napps = ntohs (cc_msg->applications);
5510 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5513 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5517 /* Create new client structure */
5518 c = GNUNET_malloc (sizeof (struct MeshClient));
5519 c->id = next_client_id++;
5520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5522 GNUNET_SERVER_client_keep (client);
5523 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5526 GNUNET_MESH_ApplicationType at;
5527 struct GNUNET_HashCode hc;
5529 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5530 for (i = 0; i < napps; i++)
5533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5534 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5535 /* store in clients hashmap */
5536 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5537 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5538 /* store in global hashmap, for announcements */
5539 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5540 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5542 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5543 announce_applications_task =
5544 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5550 struct GNUNET_HashCode hc;
5552 t = (uint16_t *) & a[napps];
5553 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5554 for (i = 0; i < ntypes; i++)
5557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5558 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5560 /* store in clients hashmap */
5561 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5562 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5563 /* store in global hashmap */
5564 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5565 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5569 " client has %u+%u subscriptions\n", napps, ntypes);
5571 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5572 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5573 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5574 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5575 GNUNET_SERVER_notification_context_add (nc, client);
5576 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5578 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5584 * Handler for clients announcing available services by a regular expression.
5586 * @param cls closure
5587 * @param client identification of the client
5588 * @param message the actual message, which includes messages the client wants
5591 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5592 const struct GNUNET_MessageHeader *message)
5594 struct MeshClient *c;
5598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5600 /* Sanity check for client registration */
5601 if (NULL == (c = client_get (client)))
5604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5609 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5610 regex = GNUNET_malloc (len + 1);
5611 memcpy (regex, &message[1], len);
5613 GNUNET_array_append (c->regexes, c->n_regex, regex);
5614 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5616 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5622 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5628 * Handler for requests of new tunnels
5630 * @param cls closure
5631 * @param client identification of the client
5632 * @param message the actual message
5635 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5636 const struct GNUNET_MessageHeader *message)
5638 struct GNUNET_MESH_TunnelMessage *t_msg;
5639 struct MeshTunnel *t;
5640 struct MeshClient *c;
5642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5644 /* Sanity check for client registration */
5645 if (NULL == (c = client_get (client)))
5648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5653 /* Message sanity check */
5654 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5661 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5662 /* Sanity check for tunnel numbering */
5663 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5666 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5669 /* Sanity check for duplicate tunnel IDs */
5670 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
5673 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5677 while (NULL != tunnel_get_by_pi (myid, next_tid))
5678 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5679 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
5682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5684 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5687 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5689 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5690 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
5692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5693 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5699 * Handler for requests of deleting tunnels
5701 * @param cls closure
5702 * @param client identification of the client
5703 * @param message the actual message
5706 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5707 const struct GNUNET_MessageHeader *message)
5709 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5710 struct MeshClient *c;
5711 struct MeshTunnel *t;
5712 MESH_TunnelNumber tid;
5714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5715 "Got a DESTROY TUNNEL from client!\n");
5717 /* Sanity check for client registration */
5718 if (NULL == (c = client_get (client)))
5721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5724 /* Message sanity check */
5725 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5732 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5734 /* Retrieve tunnel */
5735 tid = ntohl (tunnel_msg->tunnel_id);
5736 t = tunnel_get_by_local_id(c, tid);
5739 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5741 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5744 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5746 client_ignore_tunnel (c, t);
5748 // TODO: when to destroy incoming tunnel?
5749 if (t->nclients == 0)
5751 GNUNET_assert (GNUNET_YES ==
5752 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
5754 GNUNET_assert (GNUNET_YES ==
5755 GNUNET_CONTAINER_multihashmap_remove (t->peers,
5756 &my_full_id.hashPubKey,
5760 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5763 send_client_tunnel_disconnect(t, c);
5764 client_delete_tunnel(c, t);
5766 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5768 tunnel_send_destroy (t);
5769 t->destroy = GNUNET_YES;
5770 // The tunnel will be destroyed when the last message is transmitted.
5771 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5777 * Handler for requests of seeting tunnel's speed.
5779 * @param cls Closure (unused).
5780 * @param client Identification of the client.
5781 * @param message The actual message.
5784 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
5785 const struct GNUNET_MessageHeader *message)
5787 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5788 struct MeshClient *c;
5789 struct MeshTunnel *t;
5790 MESH_TunnelNumber tid;
5792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5793 "Got a SPEED request from client!\n");
5795 /* Sanity check for client registration */
5796 if (NULL == (c = client_get (client)))
5799 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5804 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5806 /* Retrieve tunnel */
5807 tid = ntohl (tunnel_msg->tunnel_id);
5808 t = tunnel_get_by_local_id(c, tid);
5811 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
5813 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5817 switch (ntohs(message->type))
5819 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
5820 t->speed_min = GNUNET_YES;
5822 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
5823 t->speed_min = GNUNET_NO;
5828 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5833 * Handler for requests of seeting tunnel's buffering policy.
5835 * @param cls Closure (unused).
5836 * @param client Identification of the client.
5837 * @param message The actual message.
5840 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5841 const struct GNUNET_MessageHeader *message)
5843 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5844 struct MeshClient *c;
5845 struct MeshTunnel *t;
5846 MESH_TunnelNumber tid;
5848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5849 "Got a BUFFER request from client!\n");
5851 /* Sanity check for client registration */
5852 if (NULL == (c = client_get (client)))
5855 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5860 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5862 /* Retrieve tunnel */
5863 tid = ntohl (tunnel_msg->tunnel_id);
5864 t = tunnel_get_by_local_id(c, tid);
5867 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5873 switch (ntohs(message->type))
5875 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5876 t->nobuffer = GNUNET_NO;
5878 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5879 t->nobuffer = GNUNET_YES;
5885 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5890 * Handler for connection requests to new peers
5892 * @param cls closure
5893 * @param client identification of the client
5894 * @param message the actual message (PeerControl)
5897 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
5898 const struct GNUNET_MessageHeader *message)
5900 struct GNUNET_MESH_PeerControl *peer_msg;
5901 struct MeshPeerInfo *peer_info;
5902 struct MeshClient *c;
5903 struct MeshTunnel *t;
5904 MESH_TunnelNumber tid;
5906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
5907 /* Sanity check for client registration */
5908 if (NULL == (c = client_get (client)))
5911 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5915 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5916 /* Sanity check for message size */
5917 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5920 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5924 /* Tunnel exists? */
5925 tid = ntohl (peer_msg->tunnel_id);
5926 t = tunnel_get_by_local_id (c, tid);
5930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5934 /* Does client own tunnel? */
5935 if (t->owner->handle != client)
5938 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
5942 GNUNET_i2s (&peer_msg->peer));
5943 peer_info = peer_info_get (&peer_msg->peer);
5945 tunnel_add_peer (t, peer_info);
5946 peer_info_connect (peer_info, t);
5948 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5954 * Handler for disconnection requests of peers in a tunnel
5956 * @param cls closure
5957 * @param client identification of the client
5958 * @param message the actual message (PeerControl)
5961 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
5962 const struct GNUNET_MessageHeader *message)
5964 struct GNUNET_MESH_PeerControl *peer_msg;
5965 struct MeshPeerInfo *peer_info;
5966 struct MeshClient *c;
5967 struct MeshTunnel *t;
5968 MESH_TunnelNumber tid;
5970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
5971 /* Sanity check for client registration */
5972 if (NULL == (c = client_get (client)))
5975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5978 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5979 /* Sanity check for message size */
5980 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5987 /* Tunnel exists? */
5988 tid = ntohl (peer_msg->tunnel_id);
5989 t = tunnel_get_by_local_id (c, tid);
5993 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5998 /* Does client own tunnel? */
5999 if (t->owner->handle != client)
6002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6007 GNUNET_i2s (&peer_msg->peer));
6008 /* Is the peer in the tunnel? */
6010 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6011 if (NULL == peer_info)
6014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6018 /* Ok, delete peer from tunnel */
6019 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6020 &peer_msg->peer.hashPubKey);
6022 send_destroy_path (t, peer_info->id);
6023 tunnel_delete_peer (t, peer_info->id);
6024 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6029 * Handler for blacklist requests of peers in a tunnel
6031 * @param cls closure
6032 * @param client identification of the client
6033 * @param message the actual message (PeerControl)
6036 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6037 const struct GNUNET_MessageHeader *message)
6039 struct GNUNET_MESH_PeerControl *peer_msg;
6040 struct MeshClient *c;
6041 struct MeshTunnel *t;
6042 MESH_TunnelNumber tid;
6044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6045 /* Sanity check for client registration */
6046 if (NULL == (c = client_get (client)))
6049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6052 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6054 /* Sanity check for message size */
6055 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6062 /* Tunnel exists? */
6063 tid = ntohl (peer_msg->tunnel_id);
6064 t = tunnel_get_by_local_id (c, tid);
6068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6073 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6074 GNUNET_PEER_intern(&peer_msg->peer));
6079 * Handler for unblacklist requests of peers in a tunnel
6081 * @param cls closure
6082 * @param client identification of the client
6083 * @param message the actual message (PeerControl)
6086 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6087 const struct GNUNET_MessageHeader *message)
6089 struct GNUNET_MESH_PeerControl *peer_msg;
6090 struct MeshClient *c;
6091 struct MeshTunnel *t;
6092 MESH_TunnelNumber tid;
6096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6097 /* Sanity check for client registration */
6098 if (NULL == (c = client_get (client)))
6101 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6104 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6106 /* Sanity check for message size */
6107 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6114 /* Tunnel exists? */
6115 tid = ntohl (peer_msg->tunnel_id);
6116 t = tunnel_get_by_local_id (c, tid);
6120 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6125 /* if peer is not known, complain */
6126 pid = GNUNET_PEER_search (&peer_msg->peer);
6133 /* search and remove from list */
6134 for (i = 0; i < t->nblacklisted; i++)
6136 if (t->blacklisted[i] == pid)
6138 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6139 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6144 /* if peer hasn't been blacklisted, complain */
6150 * Handler for connection requests to new peers by type
6152 * @param cls closure
6153 * @param client identification of the client
6154 * @param message the actual message (ConnectPeerByType)
6157 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6158 const struct GNUNET_MessageHeader *message)
6160 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6161 struct MeshClient *c;
6162 struct MeshTunnel *t;
6163 struct GNUNET_HashCode hash;
6164 MESH_TunnelNumber tid;
6166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6167 /* Sanity check for client registration */
6168 if (NULL == (c = client_get (client)))
6171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6175 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6176 /* Sanity check for message size */
6177 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6178 ntohs (connect_msg->header.size))
6181 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6185 /* Tunnel exists? */
6186 tid = ntohl (connect_msg->tunnel_id);
6187 t = tunnel_get_by_local_id (c, tid);
6191 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6195 /* Does client own tunnel? */
6196 if (t->owner->handle != client)
6199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6203 /* Do WE have the service? */
6204 t->type = ntohl (connect_msg->type);
6205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6206 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6207 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6210 /* Yes! Fast forward, add ourselves to the tunnel and send the
6211 * good news to the client, and alert the destination client of
6212 * an incoming tunnel.
6214 * FIXME send a path create to self, avoid code duplication
6216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6217 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6218 peer_info_get (&my_full_id),
6219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6222 send_client_peer_connected (t, myid);
6223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6224 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6226 t->local_tid_dest = next_local_tid++;
6227 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6228 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6233 /* Ok, lets find a peer offering the service */
6234 if (NULL != t->dht_get_type)
6236 GNUNET_DHT_get_stop (t->dht_get_type);
6238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6239 GNUNET_h2s (&hash));
6241 GNUNET_DHT_get_start (dht_handle,
6242 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6244 dht_replication_level,
6245 GNUNET_DHT_RO_RECORD_ROUTE |
6246 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6248 &dht_get_type_handler, t);
6250 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6256 * Handler for connection requests to new peers by a string service description.
6258 * @param cls closure
6259 * @param client identification of the client
6260 * @param message the actual message, which includes messages the client wants
6263 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6264 const struct GNUNET_MessageHeader *message)
6266 struct GNUNET_MESH_ConnectPeerByString *msg;
6267 struct MeshRegexSearchContext *ctx;
6268 struct MeshRegexSearchInfo *info;
6269 struct GNUNET_DHT_GetHandle *get_h;
6270 struct GNUNET_HashCode key;
6271 struct MeshTunnel *t;
6272 struct MeshClient *c;
6273 MESH_TunnelNumber tid;
6277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6278 "Connect by string started\n");
6279 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6280 size = htons (message->size);
6282 /* Sanity check for client registration */
6283 if (NULL == (c = client_get (client)))
6286 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6290 /* Message size sanity check */
6291 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6294 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6298 /* Tunnel exists? */
6299 tid = ntohl (msg->tunnel_id);
6300 t = tunnel_get_by_local_id (c, tid);
6304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6308 /* Does client own tunnel? */
6309 if (t->owner->handle != client)
6312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6317 " on tunnel %s [%u]\n",
6318 GNUNET_i2s(&my_full_id),
6321 /* Only one connect_by_string allowed at the same time! */
6322 /* FIXME: allow more, return handle at api level to cancel, document */
6323 if (NULL != t->regex_ctx)
6326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6330 /* Find string itself */
6331 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6332 string = (const char *) &msg[1];
6334 /* Initialize context */
6335 size = GNUNET_REGEX_get_first_key(string, len, &key);
6336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6337 " consumed %u bits out of %u\n", size, len);
6338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6339 " looking for %s\n", GNUNET_h2s (&key));
6341 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6343 info->description = GNUNET_malloc (len + 1);
6344 memcpy (info->description, string, len);
6345 info->description[len] = '\0';
6346 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6347 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6350 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6351 ctx->position = size;
6355 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6357 /* Start search in DHT */
6358 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6359 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6360 &key, /* key to search */
6361 dht_replication_level, /* replication level */
6362 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6363 NULL, /* xquery */ // FIXME BLOOMFILTER
6364 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6365 &dht_get_string_handler, ctx);
6367 GNUNET_break (GNUNET_OK ==
6368 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6371 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6373 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6379 * Handler for client traffic directed to one peer
6381 * @param cls closure
6382 * @param client identification of the client
6383 * @param message the actual message
6386 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6387 const struct GNUNET_MessageHeader *message)
6389 struct MeshClient *c;
6390 struct MeshTunnel *t;
6391 struct MeshPeerInfo *pi;
6392 struct GNUNET_MESH_Unicast *data_msg;
6393 MESH_TunnelNumber tid;
6396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6397 "Got a unicast request from a client!\n");
6399 /* Sanity check for client registration */
6400 if (NULL == (c = client_get (client)))
6403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6406 data_msg = (struct GNUNET_MESH_Unicast *) message;
6407 /* Sanity check for message size */
6408 size = ntohs (message->size);
6409 if (sizeof (struct GNUNET_MESH_Unicast) +
6410 sizeof (struct GNUNET_MessageHeader) > size)
6413 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6417 /* Tunnel exists? */
6418 tid = ntohl (data_msg->tid);
6419 t = tunnel_get_by_local_id (c, tid);
6423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6427 /* Is it a local tunnel? Then, does client own the tunnel? */
6428 if (t->owner->handle != client)
6431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6435 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6436 &data_msg->destination.hashPubKey);
6437 /* Is the selected peer in the tunnel? */
6441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6445 /* Ok, everything is correct, send the message
6446 * (pretend we got it from a mesh peer)
6449 char buf[ntohs (message->size)] GNUNET_ALIGN;
6450 struct GNUNET_MESH_Unicast *copy;
6452 /* Work around const limitation */
6453 copy = (struct GNUNET_MESH_Unicast *) buf;
6454 memcpy (buf, data_msg, size);
6455 copy->oid = my_full_id;
6456 copy->tid = htonl (t->id.tid);
6457 copy->ttl = htonl (default_ttl);
6458 copy->pid = htonl (t->pid + 1);
6459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6460 " calling generic handler...\n");
6461 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6464 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6470 * Handler for client traffic directed to the origin
6472 * @param cls closure
6473 * @param client identification of the client
6474 * @param message the actual message
6477 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6478 const struct GNUNET_MessageHeader *message)
6480 struct GNUNET_MESH_ToOrigin *data_msg;
6481 struct GNUNET_PeerIdentity id;
6482 struct MeshClient *c;
6483 struct MeshTunnel *t;
6484 MESH_TunnelNumber tid;
6487 /* Sanity check for client registration */
6488 if (NULL == (c = client_get (client)))
6491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6494 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6495 /* Sanity check for message size */
6496 size = ntohs (message->size);
6497 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6498 sizeof (struct GNUNET_MessageHeader) > size)
6501 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6505 /* Tunnel exists? */
6506 tid = ntohl (data_msg->tid);
6507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6508 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6509 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6515 t = tunnel_get_by_local_id (c, tid);
6519 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6523 /* It should be sent by someone who has this as incoming tunnel. */
6524 if (-1 == client_knows_tunnel (c, t))
6527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6530 GNUNET_PEER_resolve (t->id.oid, &id);
6532 /* Ok, everything is correct, send the message
6533 * (pretend we got it from a mesh peer)
6536 char buf[ntohs (message->size)] GNUNET_ALIGN;
6537 struct GNUNET_MESH_ToOrigin *copy;
6539 /* Work around const limitation */
6540 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6541 memcpy (buf, data_msg, size);
6543 copy->tid = htonl (t->id.tid);
6544 copy->sender = my_full_id;
6545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6546 " calling generic handler...\n");
6547 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6549 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6555 * Handler for client traffic directed to all peers in a tunnel
6557 * @param cls closure
6558 * @param client identification of the client
6559 * @param message the actual message
6562 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6563 const struct GNUNET_MessageHeader *message)
6565 struct MeshClient *c;
6566 struct MeshTunnel *t;
6567 struct GNUNET_MESH_Multicast *data_msg;
6568 MESH_TunnelNumber tid;
6570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6571 "Got a multicast request from a client!\n");
6573 /* Sanity check for client registration */
6574 if (NULL == (c = client_get (client)))
6577 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6580 data_msg = (struct GNUNET_MESH_Multicast *) message;
6581 /* Sanity check for message size */
6582 if (sizeof (struct GNUNET_MESH_Multicast) +
6583 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6586 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6590 /* Tunnel exists? */
6591 tid = ntohl (data_msg->tid);
6592 t = tunnel_get_by_local_id (c, tid);
6596 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6600 /* Does client own tunnel? */
6601 if (t->owner->handle != client)
6604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6609 char buf[ntohs (message->size)] GNUNET_ALIGN;
6610 struct GNUNET_MESH_Multicast *copy;
6612 copy = (struct GNUNET_MESH_Multicast *) buf;
6613 memcpy (buf, message, ntohs (message->size));
6614 copy->oid = my_full_id;
6615 copy->tid = htonl (t->id.tid);
6616 copy->ttl = htonl (default_ttl);
6617 copy->pid = htonl (t->pid + 1);
6618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6619 " calling generic handler...\n");
6620 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
6623 /* receive done gets called when last copy is sent to a neighbor */
6629 * Handler for client ACKs for payload traffic.
6631 * @param cls Closure (unused).
6632 * @param client Identification of the client.
6633 * @param message The actual message.
6636 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6637 const struct GNUNET_MessageHeader *message)
6639 struct GNUNET_MESH_LocalAck *msg;
6640 struct MeshTunnel *t;
6641 struct MeshClient *c;
6642 MESH_TunnelNumber tid;
6644 /* Sanity check for client registration */
6645 if (NULL == (c = client_get (client)))
6648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6652 msg = (struct GNUNET_MESH_LocalAck *) message;
6653 /* Tunnel exists? */
6654 tid = ntohl (msg->tunnel_id);
6655 t = tunnel_get_by_local_id (c, tid);
6659 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6663 /* Does client own tunnel? */
6664 if (NULL != t->owner && t->owner->handle == client)
6671 tunnel_set_client_ack (t, c, ntohl (msg->max_pid));
6674 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6680 * Functions to handle messages from clients
6682 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6683 {&handle_local_new_client, NULL,
6684 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6685 {&handle_local_announce_regex, NULL,
6686 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
6687 {&handle_local_tunnel_create, NULL,
6688 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6689 sizeof (struct GNUNET_MESH_TunnelMessage)},
6690 {&handle_local_tunnel_destroy, NULL,
6691 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6692 sizeof (struct GNUNET_MESH_TunnelMessage)},
6693 {&handle_local_tunnel_speed, NULL,
6694 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
6695 sizeof (struct GNUNET_MESH_TunnelMessage)},
6696 {&handle_local_tunnel_speed, NULL,
6697 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
6698 sizeof (struct GNUNET_MESH_TunnelMessage)},
6699 {&handle_local_tunnel_buffer, NULL,
6700 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6701 sizeof (struct GNUNET_MESH_TunnelMessage)},
6702 {&handle_local_tunnel_buffer, NULL,
6703 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6704 sizeof (struct GNUNET_MESH_TunnelMessage)},
6705 {&handle_local_connect_add, NULL,
6706 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
6707 sizeof (struct GNUNET_MESH_PeerControl)},
6708 {&handle_local_connect_del, NULL,
6709 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
6710 sizeof (struct GNUNET_MESH_PeerControl)},
6711 {&handle_local_blacklist, NULL,
6712 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
6713 sizeof (struct GNUNET_MESH_PeerControl)},
6714 {&handle_local_unblacklist, NULL,
6715 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
6716 sizeof (struct GNUNET_MESH_PeerControl)},
6717 {&handle_local_connect_by_type, NULL,
6718 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
6719 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
6720 {&handle_local_connect_by_string, NULL,
6721 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
6722 {&handle_local_unicast, NULL,
6723 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6724 {&handle_local_to_origin, NULL,
6725 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6726 {&handle_local_multicast, NULL,
6727 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6728 {&handle_local_ack, NULL,
6729 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6730 sizeof (struct GNUNET_MESH_LocalAck)},
6736 * To be called on core init/fail.
6738 * @param cls service closure
6739 * @param server handle to the server for this service
6740 * @param identity the public identity of this peer
6743 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6744 const struct GNUNET_PeerIdentity *identity)
6746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6747 core_handle = server;
6748 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6751 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6752 GNUNET_SCHEDULER_shutdown ();
6759 * Method called whenever a given peer connects.
6761 * @param cls closure
6762 * @param peer peer identity this notification is about
6763 * @param atsi performance data for the connection
6764 * @param atsi_count number of records in 'atsi'
6767 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
6768 const struct GNUNET_ATS_Information *atsi,
6769 unsigned int atsi_count)
6771 struct MeshPeerInfo *peer_info;
6772 struct MeshPeerPath *path;
6774 DEBUG_CONN ("Peer connected\n");
6775 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6776 peer_info = peer_info_get (peer);
6777 if (myid == peer_info->id)
6779 DEBUG_CONN (" (self)\n");
6784 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6786 path = path_new (2);
6787 path->peers[0] = myid;
6788 path->peers[1] = peer_info->id;
6789 GNUNET_PEER_change_rc (myid, 1);
6790 GNUNET_PEER_change_rc (peer_info->id, 1);
6791 peer_info_add_path (peer_info, path, GNUNET_YES);
6792 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6798 * Method called whenever a peer disconnects.
6800 * @param cls closure
6801 * @param peer peer identity this notification is about
6804 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6806 struct MeshPeerInfo *pi;
6807 struct MeshPeerQueue *q;
6808 struct MeshPeerQueue *n;
6810 DEBUG_CONN ("Peer disconnected\n");
6811 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6823 /* try to reroute this traffic instead */
6824 queue_destroy(q, GNUNET_YES);
6828 peer_info_remove_path (pi, pi->id, myid);
6831 DEBUG_CONN (" (self)\n");
6833 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6838 /******************************************************************************/
6839 /************************ MAIN FUNCTIONS ****************************/
6840 /******************************************************************************/
6843 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6845 * @param cls closure
6846 * @param key current key code
6847 * @param value value in the hash map
6848 * @return GNUNET_YES if we should continue to iterate,
6852 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6854 struct MeshTunnel *t = value;
6861 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
6863 * @param cls closure
6864 * @param key current key code
6865 * @param value value in the hash map
6866 * @return GNUNET_YES if we should continue to iterate,
6870 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
6872 struct MeshPeerInfo *p = value;
6873 struct MeshPeerQueue *q;
6874 struct MeshPeerQueue *n;
6882 queue_destroy(q, GNUNET_YES);
6886 peer_info_destroy (p);
6891 * Task run during shutdown.
6897 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6901 if (core_handle != NULL)
6903 GNUNET_CORE_disconnect (core_handle);
6906 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6907 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6908 if (dht_handle != NULL)
6910 GNUNET_DHT_disconnect (dht_handle);
6915 GNUNET_SERVER_notification_context_destroy (nc);
6918 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6920 GNUNET_SCHEDULER_cancel (announce_id_task);
6921 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6927 * Process mesh requests.
6929 * @param cls closure
6930 * @param server the initialized server
6931 * @param c configuration to use
6934 run (void *cls, struct GNUNET_SERVER_Handle *server,
6935 const struct GNUNET_CONFIGURATION_Handle *c)
6937 struct MeshPeerInfo *peer;
6938 struct MeshPeerPath *p;
6941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6942 server_handle = server;
6943 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6944 NULL, /* Closure passed to MESH functions */
6945 &core_init, /* Call core_init once connected */
6946 &core_connect, /* Handle connects */
6947 &core_disconnect, /* remove peers on disconnects */
6948 NULL, /* Don't notify about all incoming messages */
6949 GNUNET_NO, /* For header only in notification */
6950 NULL, /* Don't notify about all outbound messages */
6951 GNUNET_NO, /* For header-only out notification */
6952 core_handlers); /* Register these handlers */
6954 if (core_handle == NULL)
6957 GNUNET_SCHEDULER_shutdown ();
6962 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
6965 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6967 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6969 GNUNET_SCHEDULER_shutdown ();
6974 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6975 &refresh_path_time))
6977 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6979 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6980 "refresh path time");
6981 GNUNET_SCHEDULER_shutdown ();
6986 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6987 &app_announce_time))
6989 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6991 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6992 "app announce time");
6993 GNUNET_SCHEDULER_shutdown ();
6998 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7003 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7004 "id announce time");
7005 GNUNET_SCHEDULER_shutdown ();
7010 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7011 &unacknowledged_wait_time))
7013 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7015 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7016 "unacknowledged wait time");
7017 GNUNET_SCHEDULER_shutdown ();
7022 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7025 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7027 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7029 GNUNET_SCHEDULER_shutdown ();
7034 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7039 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7041 GNUNET_SCHEDULER_shutdown ();
7046 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7049 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7051 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7053 GNUNET_SCHEDULER_shutdown ();
7058 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7063 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7069 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7070 &dht_replication_level))
7072 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7074 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7075 "dht replication level", 10);
7076 dht_replication_level = 10;
7080 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7081 GNUNET_free (keyfile);
7082 if (my_private_key == NULL)
7084 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7085 _("Mesh service could not access hostkey. Exiting.\n"));
7086 GNUNET_SCHEDULER_shutdown ();
7089 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7090 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7091 &my_full_id.hashPubKey);
7092 myid = GNUNET_PEER_intern (&my_full_id);
7094 // transport_handle = GNUNET_TRANSPORT_connect(c,
7101 dht_handle = GNUNET_DHT_connect (c, 64);
7102 if (dht_handle == NULL)
7107 stats = GNUNET_STATISTICS_create ("mesh", c);
7111 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7113 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7114 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7115 peers = GNUNET_CONTAINER_multihashmap_create (32);
7116 applications = GNUNET_CONTAINER_multihashmap_create (32);
7117 types = GNUNET_CONTAINER_multihashmap_create (32);
7119 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7120 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7121 GNUNET_SERVER_disconnect_notify (server_handle,
7122 &handle_local_client_disconnect, NULL);
7126 clients_tail = NULL;
7129 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7130 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7132 /* Create a peer_info for the local peer */
7133 peer = peer_info_get (&my_full_id);
7136 GNUNET_PEER_change_rc (myid, 1);
7137 peer_info_add_path (peer, p, GNUNET_YES);
7139 /* Scheduled the task to clean up when shutdown is called */
7140 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7147 * The main function for the mesh service.
7149 * @param argc number of arguments from the command line
7150 * @param argv command line arguments
7151 * @return 0 ok, 1 on error
7154 main (int argc, char *const *argv)
7158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7161 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");