2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
42 * - relay corking down to core
43 * - set ttl relative to tree depth
44 * - Add data ACK count in path ACK
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_YES
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #if MESH_DEBUG_CONNECTION
64 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
66 #define DEBUG_CONN(...)
70 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
72 #define DEBUG_DHT(...)
75 /******************************************************************************/
76 /************************ DATA STRUCTURES ****************************/
77 /******************************************************************************/
79 /** FWD declaration */
85 * Struct representing a piece of data being sent to other peers
89 /** Tunnel it belongs to. */
92 /** In case of a multicast, task to allow a client to send more data if
93 * some neighbor is too slow. */
94 GNUNET_SCHEDULER_TaskIdentifier *task;
96 /** How many remaining neighbors we need to send this to. */
97 unsigned int *reference_counter;
99 /** Size of the data. */
108 * Struct containing info about a queued transmission to this peer
115 struct MeshPeerQueue *next;
120 struct MeshPeerQueue *prev;
123 * Peer this transmission is directed to.
125 struct MeshPeerInfo *peer;
128 * Tunnel this message belongs to.
130 struct MeshTunnel *tunnel;
133 * Pointer to info stucture used as cls.
143 * Size of the message
150 * Struct containing all info possibly needed to build a package when called
153 struct MeshTransmissionDescriptor
155 /** ID of the tunnel this packet travels in */
156 struct MESH_TunnelID *origin;
158 /** Who was this message being sent to */
159 struct MeshPeerInfo *peer;
161 /** Ultimate destination of the packet */
162 GNUNET_PEER_Id destination;
164 /** Data descriptor */
165 struct MeshData* mesh_data;
170 * Struct containing all information regarding a given peer
180 * Last time we heard from this peer
182 struct GNUNET_TIME_Absolute last_contact;
185 * Number of attempts to reconnect so far
187 int n_reconnect_attempts;
190 * Paths to reach the peer, ordered by ascending hop count
192 struct MeshPeerPath *path_head;
195 * Paths to reach the peer, ordered by ascending hop count
197 struct MeshPeerPath *path_tail;
200 * Handle to stop the DHT search for a path to this peer
202 struct GNUNET_DHT_GetHandle *dhtget;
205 * Closure given to the DHT GET
207 struct MeshPathInfo *dhtgetcls;
210 * Array of tunnels this peer participates in
211 * (most probably a small amount, therefore not a hashmap)
212 * When the path to the peer changes, notify these tunnels to let them
213 * re-adjust their path trees.
215 struct MeshTunnel **tunnels;
218 * Number of tunnels this peers participates in
220 unsigned int ntunnels;
223 * Transmission queue to core DLL head
225 struct MeshPeerQueue *queue_head;
228 * Transmission queue to core DLL tail
230 struct MeshPeerQueue *queue_tail;
233 * How many messages are in the queue to this peer.
235 unsigned int queue_n;
238 * Handle to for queued transmissions
240 struct GNUNET_CORE_TransmitHandle *core_transmit;
245 * Globally unique tunnel identification (owner + number)
246 * DO NOT USE OVER THE NETWORK
251 * Node that owns the tunnel
256 * Tunnel number to differentiate all the tunnels owned by the node oid
257 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
259 MESH_TunnelNumber tid;
264 * Struct containing all information regarding a tunnel
265 * For an intermediate node the improtant info used will be:
266 * - id Tunnel unique identification
267 * - paths[0] To know where to send it next
268 * - metainfo: ready, speeds, accounting
275 struct MESH_TunnelID id;
278 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
280 MESH_TunnelNumber local_tid;
283 * Local tunnel number for local destination clients (incoming number)
284 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
287 MESH_TunnelNumber local_tid_dest;
290 * Is the speed on the tunnel limited to the slowest peer?
295 * Is the tunnel bufferless (minimum latency)?
300 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
305 * Packet ID of the last bck packet sent (unique counter per hop).
310 * SKIP value for this tunnel.
315 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
316 * Contains the Flow Control info: FWD ACK value received,
317 * last BCK ACK sent, PID and SKIP values.
319 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
322 * Last ACK sent towards the origin (for traffic towards leaf node).
324 uint32_t last_fwd_ack;
327 * BCK ACK value received from the hop towards the owner of the tunnel,
328 * (previous node / owner): up to what message PID can we sent back to him.
333 * How many messages are in the forward queue (towards leaves).
335 unsigned int fwd_queue_n;
338 * How many messages do we accept in the forward queue.
340 unsigned int fwd_queue_max;
343 * How many messages are in the backward queue (towards origin).
345 unsigned int bck_queue_n;
348 * How many messages do we accept in the backward queue.
350 unsigned int bck_queue_max;
353 * Last time the tunnel was used
355 struct GNUNET_TIME_Absolute timestamp;
358 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
359 * containing peers added by id or by type, not intermediate peers.
361 struct GNUNET_CONTAINER_MultiHashMap *peers;
364 * Number of peers that are connected and potentially ready to receive data
366 unsigned int peers_ready;
369 * Number of peers that have been added to the tunnel
371 unsigned int peers_total;
374 * Client owner of the tunnel, if any
376 struct MeshClient *owner;
379 * Clients that have been informed about and want to stay in the tunnel.
381 struct MeshClient **clients;
384 * Flow control info for each client.
386 struct MeshTunnelClientInfo *clients_fc;
389 * Number of elements in clients/clients_fc
391 unsigned int nclients;
394 * Clients that have been informed but requested to leave the tunnel.
396 struct MeshClient **ignore;
399 * Number of elements in clients
401 unsigned int nignore;
406 GNUNET_PEER_Id *blacklisted;
409 * Number of elements in blacklisted
411 unsigned int nblacklisted;
414 * Bloomfilter (for peer identities) to stop circular routes
416 char bloomfilter[MESH_BLOOM_SIZE];
421 struct MeshTunnelTree *tree;
424 * Application type we are looking for in this tunnel
426 GNUNET_MESH_ApplicationType type;
429 * Used to search peers offering a service
431 struct GNUNET_DHT_GetHandle *dht_get_type;
434 * Initial context of the regex search for a connect_by_string
436 struct MeshRegexSearchContext *regex_ctx;
439 * Task to keep the used paths alive
441 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
444 * Task to destroy the tunnel after timeout
446 * FIXME: merge the two? a tunnel will have either
447 * a path refresh OR a timeout, never both!
449 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
452 * Flag to signal the destruction of the tunnel.
453 * If this is set GNUNET_YES the tunnel will be destroyed
454 * when the queue is empty.
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 (FWD ACK received).
486 * Last ACK sent to that child (BCK ACK).
493 * Info about a leaf client of a tunnel, needed to perform flow control.
495 struct MeshTunnelClientInfo
498 * PID of the last packet sent to the client (FWD).
503 * PID of the last packet received from the client (BCK).
508 * Maximum PID allowed (FWD ACK received).
513 * Last ACK sent to that child (BCK ACK).
521 * Info collected during iteration of child nodes in order to get the ACK value
524 struct MeshTunnelChildIteratorContext
527 * Tunnel whose info is being collected.
529 struct MeshTunnel *t;
532 * Is this context initialized? Is the value in max_child_ack valid?
537 * Maximum child ACK so far.
539 uint32_t max_child_ack;
542 * Number of children nodes
544 unsigned int nchildren;
549 * Info needed to work with tunnel paths and peers
556 struct MeshTunnel *t;
559 * Neighbouring peer to whom we send the packet to
561 struct MeshPeerInfo *peer;
566 struct MeshPeerPath *path;
571 * Struct containing information about a client of the service
578 struct MeshClient *next;
583 struct MeshClient *prev;
586 * Tunnels that belong to this client, indexed by local id
588 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
591 * Tunnels this client has accepted, indexed by incoming local id
593 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
596 * Tunnels this client has rejected, indexed by incoming local id
598 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
600 * Handle to communicate with the client
602 struct GNUNET_SERVER_Client *handle;
605 * Applications that this client has claimed to provide
607 struct GNUNET_CONTAINER_MultiHashMap *apps;
610 * Messages that this client has declared interest in
612 struct GNUNET_CONTAINER_MultiHashMap *types;
615 * Whether the client is active or shutting down (don't send confirmations
616 * to a client that is shutting down.
621 * ID of the client, mainly for debug messages
626 * Regular expressions describing the services offered by this client.
628 char **regexes; // FIXME add timeout? API to remove a regex?
631 * Number of regular expressions in regexes.
633 unsigned int n_regex;
636 * Task to refresh all regular expresions in the DHT.
638 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
644 * Struct to keep information of searches of services described by a regex
645 * using a user-provided string service description.
647 struct MeshRegexSearchInfo
650 * Which tunnel is this for
652 struct MeshTunnel *t;
655 * User provided description of the searched service.
660 * Part of the description already consumed by the search.
667 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
670 * Results from running DHT GETs.
672 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
675 * Contexts, for each running DHT GET. Free all on end of search.
677 struct MeshRegexSearchContext **contexts;
680 * Number of contexts (branches/steps in search).
682 unsigned int n_contexts;
685 * Peer that is connecting via connect_by_string. When connected, free ctx.
690 * Other peers that are found but not yet being connected to.
692 GNUNET_PEER_Id *peers;
695 * Number of elements in peers.
697 unsigned int n_peers;
700 * Next peer to try to connect to.
705 * Timeout for a connect attempt.
706 * When reached, try to connect to a different peer, if any. If not,
707 * try the same peer again.
709 GNUNET_SCHEDULER_TaskIdentifier timeout;
714 * Struct to keep state of running searches that have consumed a part of
717 struct MeshRegexSearchContext
720 * Part of the description already consumed by
721 * this particular search branch.
726 * Information about the search.
728 struct MeshRegexSearchInfo *info;
732 /******************************************************************************/
733 /************************ DEBUG FUNCTIONS ****************************/
734 /******************************************************************************/
738 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
739 * @param cls string to print
740 * @param success GNUNET_OK if the PUT was transmitted,
741 * GNUNET_NO on timeout,
742 * GNUNET_SYSERR on disconnect from service
743 * after the PUT message was transmitted
744 * (so we don't know if it was received or not)
749 mesh_debug (void *cls, int success)
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
759 /******************************************************************************/
760 /*********************** GLOBAL VARIABLES ****************************/
761 /******************************************************************************/
765 * Configuration parameters
767 static struct GNUNET_TIME_Relative refresh_path_time;
768 static struct GNUNET_TIME_Relative app_announce_time;
769 static struct GNUNET_TIME_Relative id_announce_time;
770 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
771 static struct GNUNET_TIME_Relative connect_timeout;
772 static long long unsigned int default_ttl;
773 static long long unsigned int dht_replication_level;
774 static long long unsigned int max_tunnels;
775 static long long unsigned int max_msgs_queue;
778 * DLL with all the clients, head.
780 static struct MeshClient *clients;
783 * DLL with all the clients, tail.
785 static struct MeshClient *clients_tail;
788 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
790 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
793 * Number of tunnels known.
795 static unsigned long long n_tunnels;
798 * Tunnels incoming, indexed by MESH_TunnelNumber
799 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
801 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
804 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
806 static struct GNUNET_CONTAINER_MultiHashMap *peers;
809 * Handle to communicate with transport
811 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
814 * Handle to communicate with core.
816 static struct GNUNET_CORE_Handle *core_handle;
821 static struct GNUNET_DHT_Handle *dht_handle;
826 static struct GNUNET_SERVER_Handle *server_handle;
829 * Handle to the statistics service.
831 static struct GNUNET_STATISTICS_Handle *stats;
834 * Notification context, to send messages to local clients.
836 static struct GNUNET_SERVER_NotificationContext *nc;
839 * Local peer own ID (memory efficient handle).
841 static GNUNET_PEER_Id myid;
844 * Local peer own ID (full value).
846 static struct GNUNET_PeerIdentity my_full_id;
851 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
856 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
859 * Tunnel ID for the next created tunnel (global tunnel number).
861 static MESH_TunnelNumber next_tid;
864 * Tunnel ID for the next incoming tunnel (local tunnel number).
866 static MESH_TunnelNumber next_local_tid;
869 * All application types provided by this peer.
871 static struct GNUNET_CONTAINER_MultiHashMap *applications;
874 * All message types clients of this peer are interested in.
876 static struct GNUNET_CONTAINER_MultiHashMap *types;
879 * Task to periodically announce provided applications.
881 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
884 * Task to periodically announce itself in the network.
886 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
889 * Next ID to assign to a client.
891 unsigned int next_client_id;
894 /******************************************************************************/
895 /*********************** DECLARATIONS **************************/
896 /******************************************************************************/
898 /* FIXME move declarations here */
901 * Function to process paths received for a new peer addition. The recorded
902 * paths form the initial tunnel, which can be optimized later.
903 * Called on each result obtained for the DHT search.
906 * @param exp when will this value expire
907 * @param key key of the result
908 * @param type type of the result
909 * @param size number of bytes in data
910 * @param data pointer to the result data
913 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
914 const struct GNUNET_HashCode * key,
915 const struct GNUNET_PeerIdentity *get_path,
916 unsigned int get_path_length,
917 const struct GNUNET_PeerIdentity *put_path,
918 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
919 size_t size, const void *data);
923 * Function to process DHT string to regex matching.
924 * Called on each result obtained for the DHT search.
926 * @param cls closure (search context)
927 * @param exp when will this value expire
928 * @param key key of the result
929 * @param get_path path of the get request (not used)
930 * @param get_path_length lenght of get_path (not used)
931 * @param put_path path of the put request (not used)
932 * @param put_path_length length of the put_path (not used)
933 * @param type type of the result
934 * @param size number of bytes in data
935 * @param data pointer to the result data
937 * TODO: re-issue the request after certain time? cancel after X results?
940 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
941 const struct GNUNET_HashCode * key,
942 const struct GNUNET_PeerIdentity *get_path,
943 unsigned int get_path_length,
944 const struct GNUNET_PeerIdentity *put_path,
945 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
946 size_t size, const void *data);
950 * Function to process DHT string to regex matching.
951 * Called on each result obtained for the DHT search.
953 * @param cls closure (search context)
954 * @param exp when will this value expire
955 * @param key key of the result
956 * @param get_path path of the get request (not used)
957 * @param get_path_length lenght of get_path (not used)
958 * @param put_path path of the put request (not used)
959 * @param put_path_length length of the put_path (not used)
960 * @param type type of the result
961 * @param size number of bytes in data
962 * @param data pointer to the result data
965 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
966 const struct GNUNET_HashCode * key,
967 const struct GNUNET_PeerIdentity *get_path,
968 unsigned int get_path_length,
969 const struct GNUNET_PeerIdentity *put_path,
970 unsigned int put_path_length,
971 enum GNUNET_BLOCK_Type type,
972 size_t size, const void *data);
976 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
977 * and insert it in the appropiate structures if the peer is not known yet.
979 * @param peer Short identity of the peer.
981 * @return Existing or newly created peer info.
983 static struct MeshPeerInfo *
984 peer_info_get_short (const GNUNET_PEER_Id peer);
988 * Try to establish a new connection to this peer.
989 * Use the best path for the given tunnel.
990 * If the peer doesn't have any path to it yet, try to get one.
991 * If the peer already has some path, send a CREATE PATH towards it.
993 * @param peer PeerInfo of the peer.
994 * @param t Tunnel for which to create the path, if possible.
997 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1001 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1002 * needed rescources.
1003 * If peer already exists, reevaluate shortest path and change if different.
1005 * @param t Tunnel we want to add a new peer to
1006 * @param peer PeerInfo of the peer being added
1010 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1014 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1015 * that are no longer needed, as well as nodes of no longer reachable peers.
1016 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1018 * @param t Tunnel from which to remove the path.
1019 * @param peer Short id of the peer which should be removed.
1022 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1026 * Search for a tunnel by global ID using full PeerIdentities.
1028 * @param oid owner of the tunnel.
1029 * @param tid global tunnel number.
1031 * @return tunnel handler, NULL if doesn't exist.
1033 static struct MeshTunnel *
1034 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1038 * Delete an active client from the tunnel.
1044 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1047 * Notify a tunnel that a connection has broken that affects at least
1048 * some of its peers.
1050 * @param t Tunnel affected.
1051 * @param p1 Peer that got disconnected from p2.
1052 * @param p2 Peer that got disconnected from p1.
1054 * @return Short ID of the peer disconnected (either p1 or p2).
1055 * 0 if the tunnel remained unaffected.
1057 static GNUNET_PEER_Id
1058 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1063 * Get the current ack value for a tunnel, for data going from root to leaves,
1064 * taking in account the tunnel mode and the status of all children and clients.
1068 * @return Maximum PID allowed.
1071 tunnel_get_fwd_ack (struct MeshTunnel *t);
1075 * Add a client to a tunnel, initializing all needed data structures.
1077 * @param t Tunnel to which add the client.
1078 * @param c Client which to add to the tunnel.
1081 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1085 * Iterator over edges in a regex block retrieved from the DHT.
1087 * @param cls Closure.
1088 * @param token Token that follows to next state.
1089 * @param len Lenght of token.
1090 * @param key Hash of next state.
1092 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1095 regex_edge_iterator (void *cls,
1098 const struct GNUNET_HashCode *key);
1102 * Find a path to a peer that offers a regex servcie compatible
1103 * with a given string.
1105 * @param key The key of the accepting state.
1106 * @param ctx Context containing info about the string, tunnel, etc.
1109 regex_find_path (const struct GNUNET_HashCode *key,
1110 struct MeshRegexSearchContext *ctx);
1114 * Queue and pass message to core when possible.
1116 * @param cls Closure (type dependant).
1117 * @param type Type of the message, 0 for a raw message.
1118 * @param size Size of the message.
1119 * @param dst Neighbor to send message to.
1120 * @param t Tunnel this message belongs to.
1123 queue_add (void *cls, uint16_t type, size_t size,
1124 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1127 * Free a transmission that was already queued with all resources
1128 * associated to the request.
1130 * @param queue Queue handler to cancel.
1131 * @param clear_cls Is it necessary to free associated cls?
1134 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1136 /******************************************************************************/
1137 /************************ ITERATORS ****************************/
1138 /******************************************************************************/
1141 * Iterator over found existing mesh regex blocks that match an ongoing search.
1143 * @param cls closure
1144 * @param key current key code
1145 * @param value value in the hash map
1146 * @return GNUNET_YES if we should continue to iterate,
1150 regex_result_iterator (void *cls,
1151 const struct GNUNET_HashCode * key,
1154 struct MeshRegexBlock *block = value;
1155 struct MeshRegexSearchContext *ctx = cls;
1157 if (GNUNET_YES == ntohl(block->accepting) &&
1158 ctx->position == strlen (ctx->info->description))
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1161 regex_find_path (key, ctx);
1162 return GNUNET_YES; // We found an accept state!
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1167 ctx->position, strlen(ctx->info->description),
1168 ntohl(block->accepting));
1171 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1172 ®ex_edge_iterator, ctx);
1179 * Iterator over edges in a regex block retrieved from the DHT.
1181 * @param cls Closure (context of the search).
1182 * @param token Token that follows to next state.
1183 * @param len Lenght of token.
1184 * @param key Hash of next state.
1186 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1189 regex_edge_iterator (void *cls,
1192 const struct GNUNET_HashCode *key)
1194 struct MeshRegexSearchContext *ctx = cls;
1195 struct MeshRegexSearchContext *new_ctx;
1196 struct MeshRegexSearchInfo *info = ctx->info;
1197 struct GNUNET_DHT_GetHandle *get_h;
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1204 current = &info->description[ctx->position];
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1206 current_len = strlen (info->description) - ctx->position;
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1211 if (len > current_len)
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1214 return GNUNET_YES; // Token too long, wont match
1216 if (0 != strncmp (current, token, len))
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1219 return GNUNET_YES; // Token doesn't match
1221 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1222 new_ctx->info = info;
1223 new_ctx->position = ctx->position + len;
1224 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1226 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1229 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1230 ®ex_result_iterator,
1232 return GNUNET_YES; // We are already looking for it
1234 /* Start search in DHT */
1236 GNUNET_DHT_get_start (dht_handle, /* handle */
1237 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1238 key, /* key to search */
1239 dht_replication_level, /* replication level */
1240 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1241 NULL, /* xquery */ // FIXME BLOOMFILTER
1242 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1243 &dht_get_string_handler, new_ctx);
1245 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1246 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1256 * Iterator over hash map entries to cancel DHT GET requests after a
1257 * successful connect_by_string.
1259 * @param cls Closure (unused).
1260 * @param key Current key code (unused).
1261 * @param value Value in the hash map (get handle).
1262 * @return GNUNET_YES if we should continue to iterate,
1266 regex_cancel_dht_get (void *cls,
1267 const struct GNUNET_HashCode * key,
1270 struct GNUNET_DHT_GetHandle *h = value;
1272 GNUNET_DHT_get_stop (h);
1278 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1279 * search for connect_by_string.
1281 * @param cls Closure (unused).
1282 * @param key Current key code (unused).
1283 * @param value MeshRegexBlock in the hash map.
1284 * @return GNUNET_YES if we should continue to iterate,
1288 regex_free_result (void *cls,
1289 const struct GNUNET_HashCode * key,
1293 GNUNET_free (value);
1299 * Regex callback iterator to store own service description in the DHT.
1301 * @param cls closure.
1302 * @param key hash for current state.
1303 * @param proof proof for current state.
1304 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1305 * @param num_edges number of edges leaving current state.
1306 * @param edges edges leaving current state.
1309 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1310 int accepting, unsigned int num_edges,
1311 const struct GNUNET_REGEX_Edge *edges)
1313 struct MeshRegexBlock *block;
1314 struct MeshRegexEdge *block_edge;
1315 enum GNUNET_DHT_RouteOption opt;
1319 unsigned int offset;
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 " regex dht put for state %s\n",
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1333 if (GNUNET_YES == accepting)
1335 struct MeshRegexAccept block;
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 " state %s is accepting, putting own id\n",
1340 size = sizeof (block);
1342 block.id = my_full_id;
1344 GNUNET_DHT_put(dht_handle, key,
1345 dht_replication_level,
1346 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1347 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1350 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1355 len = strlen(proof);
1356 size = sizeof (struct MeshRegexBlock) + len;
1357 block = GNUNET_malloc (size);
1360 block->n_proof = htonl (len);
1361 block->n_edges = htonl (num_edges);
1362 block->accepting = htonl (accepting);
1364 /* Store the proof at the end of the block. */
1365 aux = (char *) &block[1];
1366 memcpy (aux, proof, len);
1369 /* Store each edge in a variable length MeshEdge struct at the
1370 * very end of the MeshRegexBlock structure.
1372 for (i = 0; i < num_edges; i++)
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 " edge %s towards %s\n",
1377 GNUNET_h2s(&edges[i].destination));
1379 /* aux points at the end of the last block */
1380 len = strlen (edges[i].label);
1381 size += sizeof (struct MeshRegexEdge) + len;
1382 // Calculate offset FIXME is this ok? use size instead?
1383 offset = aux - (char *) block;
1384 block = GNUNET_realloc (block, size);
1385 aux = &((char *) block)[offset];
1386 block_edge = (struct MeshRegexEdge *) aux;
1387 block_edge->key = edges[i].destination;
1388 block_edge->n_token = htonl (len);
1389 aux = (char *) &block_edge[1];
1390 memcpy (aux, edges[i].label, len);
1394 GNUNET_DHT_put(dht_handle, key,
1395 dht_replication_level,
1397 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1399 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1403 GNUNET_free (block);
1408 * Store the regular expression describing a local service into the DHT.
1410 * @param regex The regular expresion.
1413 regex_put (const char *regex)
1415 struct GNUNET_REGEX_Automaton *dfa;
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1418 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1419 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1420 GNUNET_REGEX_automaton_destroy (dfa);
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1426 * Find a path to a peer that offers a regex servcie compatible
1427 * with a given string.
1429 * @param key The key of the accepting state.
1430 * @param ctx Context containing info about the string, tunnel, etc.
1433 regex_find_path (const struct GNUNET_HashCode *key,
1434 struct MeshRegexSearchContext *ctx)
1436 struct GNUNET_DHT_GetHandle *get_h;
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1439 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1440 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1441 key, /* key to search */
1442 dht_replication_level, /* replication level */
1443 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1444 GNUNET_DHT_RO_RECORD_ROUTE,
1445 NULL, /* xquery */ // FIXME BLOOMFILTER
1446 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1447 &dht_get_string_accept_handler, ctx);
1448 GNUNET_break (GNUNET_OK ==
1449 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1452 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1457 * Function called if the connect attempt to a peer found via
1458 * connect_by_string times out. Try to connect to another peer, if any.
1459 * Otherwise try to reconnect to the same peer.
1461 * @param cls Closure (info about regex search).
1462 * @param tc TaskContext.
1465 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1467 struct MeshRegexSearchInfo *info = cls;
1468 struct MeshPeerInfo *peer_info;
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1473 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1474 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1482 if (0 < info->n_peers)
1484 // Select next peer, put current in that spot.
1485 id = info->peers[info->i_peer];
1486 info->peers[info->i_peer] = info->peer;
1487 info->i_peer = (info->i_peer + 1) % info->n_peers;
1491 // Try to connect to same peer again.
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1496 peer_info = peer_info_get_short(id);
1497 tunnel_add_peer (info->t, peer_info);
1499 tunnel_delete_peer (info->t, old);
1500 peer_info_connect (peer_info, info->t);
1501 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1502 ®ex_connect_timeout,
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1509 * Cancel an ongoing regex search in the DHT and free all resources.
1511 * @param ctx The search context.
1514 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1516 struct MeshRegexSearchInfo *info = ctx->info;
1519 GNUNET_free (info->description);
1520 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1521 ®ex_cancel_dht_get, NULL);
1522 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1523 ®ex_free_result, NULL);
1524 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1525 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1526 info->t->regex_ctx = NULL;
1527 for (i = 0; i < info->n_contexts; i++)
1529 GNUNET_free (info->contexts[i]);
1531 if (0 < info->n_contexts)
1532 GNUNET_free (info->contexts);
1533 if (0 < info->n_peers)
1534 GNUNET_free (info->peers);
1535 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1537 GNUNET_SCHEDULER_cancel(info->timeout);
1543 /******************************************************************************/
1544 /************************ PERIODIC FUNCTIONS ****************************/
1545 /******************************************************************************/
1548 * Announce iterator over for each application provided by the peer
1550 * @param cls closure
1551 * @param key current key code
1552 * @param value value in the hash map
1553 * @return GNUNET_YES if we should continue to
1558 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1560 struct PBlock block;
1561 struct MeshClient *c;
1563 block.id = my_full_id;
1564 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1565 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1566 if (0 == block.type)
1571 block.type = htonl (block.type);
1573 GNUNET_break (NULL !=
1574 GNUNET_DHT_put (dht_handle, key,
1575 dht_replication_level,
1576 GNUNET_DHT_RO_RECORD_ROUTE |
1577 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1578 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1580 (const char *) &block,
1581 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1583 app_announce_time, NULL, NULL));
1589 * Periodically announce what applications are provided by local clients
1592 * @param cls closure
1593 * @param tc task context
1596 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1598 struct MeshClient *c = cls;
1601 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1603 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1607 DEBUG_DHT ("Starting PUT for regex\n");
1609 for (i = 0; i < c->n_regex; i++)
1611 regex_put (c->regexes[i]);
1613 c->regex_announce_task =
1614 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1615 DEBUG_DHT ("Finished PUT for regex\n");
1622 * Periodically announce what applications are provided by local clients
1625 * @param cls closure
1626 * @param tc task context
1629 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1631 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1633 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1637 DEBUG_DHT ("Starting PUT for apps\n");
1639 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1641 announce_applications_task =
1642 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1644 DEBUG_DHT ("Finished PUT for apps\n");
1651 * Periodically announce self id in the DHT
1653 * @param cls closure
1654 * @param tc task context
1657 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1659 struct PBlock block;
1661 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1663 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1667 * - Set data expiration in function of X
1668 * - Adapt X to churn
1670 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1672 block.id = my_full_id;
1673 block.type = htonl (0);
1674 GNUNET_DHT_put (dht_handle, /* DHT handle */
1675 &my_full_id.hashPubKey, /* Key to use */
1676 dht_replication_level, /* Replication level */
1677 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1678 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1679 sizeof (block), /* Size of the data */
1680 (const char *) &block, /* Data itself */
1681 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1682 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1683 NULL, /* Continuation */
1684 NULL); /* Continuation closure */
1686 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1690 /******************************************************************************/
1691 /****************** GENERAL HELPER FUNCTIONS ************************/
1692 /******************************************************************************/
1695 * Decrements the reference counter and frees all resources if needed
1697 * @param mesh_data Data Descriptor used in a multicast message.
1698 * Freed no longer needed (last message).
1701 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1703 /* Make sure it's a multicast packet */
1704 GNUNET_assert (NULL != mesh_data->reference_counter);
1706 if (0 == --(*(mesh_data->reference_counter)))
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1709 if (NULL != mesh_data->task)
1711 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1713 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1715 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1717 GNUNET_free (mesh_data->task);
1719 GNUNET_free (mesh_data->reference_counter);
1720 GNUNET_free (mesh_data->data);
1721 GNUNET_free (mesh_data);
1727 * Check if client has registered with the service and has not disconnected
1729 * @param client the client to check
1731 * @return non-NULL if client exists in the global DLL
1733 static struct MeshClient *
1734 client_get (struct GNUNET_SERVER_Client *client)
1736 struct MeshClient *c;
1741 if (c->handle == client)
1750 * Checks if a given client has subscribed to certain message type
1752 * @param message_type Type of message to check
1753 * @param c Client to check
1755 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1758 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1760 struct GNUNET_HashCode hc;
1762 if (NULL == c->types)
1764 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1765 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1770 * Allow a client to send more data after transmitting a multicast message
1771 * which some neighbor has not yet accepted altough a reasonable time has
1774 * @param cls Closure (DataDescriptor containing the task identifier)
1775 * @param tc Task Context
1777 * FIXME reference counter cshould be just int
1780 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1782 struct MeshData *mdata = cls;
1784 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1786 GNUNET_assert (NULL != mdata->reference_counter);
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1788 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1789 *(mdata->reference_counter));
1790 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1791 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1796 * Check whether client wants traffic from a tunnel.
1798 * @param c Client to check.
1799 * @param t Tunnel to be found.
1801 * @return GNUNET_YES if client knows tunnel.
1803 * TODO look in client hashmap
1806 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1810 for (i = 0; i < t->nclients; i++)
1811 if (t->clients[i] == c)
1818 * Check whether client has been informed about a tunnel.
1820 * @param c Client to check.
1821 * @param t Tunnel to be found.
1823 * @return GNUNET_YES if client knows tunnel.
1825 * TODO look in client hashmap
1828 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1832 for (i = 0; i < t->nignore; i++)
1833 if (t->ignore[i] == c)
1835 return client_wants_tunnel(c, t);
1840 * Marks a client as uninterested in traffic from the tunnel, updating both
1841 * client and tunnel to reflect this.
1843 * @param c Client that doesn't want traffic anymore.
1844 * @param t Tunnel which should be ignored.
1846 * FIXME when to delete an incoming tunnel?
1849 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1851 struct GNUNET_HashCode hash;
1853 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1854 GNUNET_break (GNUNET_YES ==
1855 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1857 GNUNET_break (GNUNET_YES ==
1858 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1859 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1860 tunnel_delete_active_client (t, c);
1861 GNUNET_array_append (t->ignore, t->nignore, c);
1866 * Deletes a tunnel from a client (either owner or destination). To be used on
1867 * tunnel destroy, otherwise, use client_ignore_tunnel.
1869 * @param c Client whose tunnel to delete.
1870 * @param t Tunnel which should be deleted.
1873 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1875 struct GNUNET_HashCode hash;
1879 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1880 GNUNET_assert (GNUNET_YES ==
1881 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1887 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1889 GNUNET_assert (GNUNET_YES ==
1890 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1894 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1902 * Send the message to all clients that have subscribed to its type
1904 * @param msg Pointer to the message itself
1905 * @param payload Pointer to the payload of the message.
1906 * @return number of clients this message was sent to
1909 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1910 const struct GNUNET_MessageHeader *payload)
1912 struct GNUNET_PeerIdentity *oid;
1913 struct MeshClient *c;
1914 struct MeshTunnel *t;
1915 MESH_TunnelNumber *tid;
1918 char cbuf[htons (msg->size)];
1920 type = ntohs (payload->type);
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1923 GNUNET_MESH_DEBUG_M2S (type));
1925 memcpy (cbuf, msg, sizeof (cbuf));
1926 switch (htons (msg->type))
1928 struct GNUNET_MESH_Unicast *uc;
1929 struct GNUNET_MESH_Multicast *mc;
1930 struct GNUNET_MESH_ToOrigin *to;
1932 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1933 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1937 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1938 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1942 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1943 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1951 t = tunnel_get (oid, ntohl (*tid));
1958 for (count = 0, c = clients; c != NULL; c = c->next)
1960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1961 if (client_is_subscribed (type, c))
1963 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1967 *tid = htonl (t->local_tid);
1971 if (GNUNET_NO == client_knows_tunnel (c, t))
1973 /* This client doesn't know the tunnel */
1974 struct GNUNET_MESH_TunnelNotification tmsg;
1975 struct GNUNET_HashCode hash;
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1978 tmsg.header.size = htons (sizeof (tmsg));
1979 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1980 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1981 tmsg.tunnel_id = htonl (t->local_tid_dest);
1982 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1983 &tmsg.header, GNUNET_NO);
1984 tunnel_add_client (t, c);
1985 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1987 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1988 c->incoming_tunnels, &hash, t,
1989 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1991 *tid = htonl (t->local_tid_dest);
1994 /* Check if the client wants to get traffic from the tunnel */
1995 if (GNUNET_NO == client_wants_tunnel(c, t))
1998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1999 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2000 (struct GNUNET_MessageHeader
2001 *) cbuf, GNUNET_NO);
2009 * Notify the client that owns the tunnel that a peer has connected to it
2010 * (the requested path to it has been confirmed).
2012 * @param t Tunnel whose owner to notify
2013 * @param id Short id of the peer that has connected
2016 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2018 struct GNUNET_MESH_PeerControl pc;
2020 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2021 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2022 pc.tunnel_id = htonl (t->local_tid);
2023 GNUNET_PEER_resolve (id, &pc.peer);
2024 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2030 * Notify all clients (not depending on registration status) that the incoming
2031 * tunnel is no longer valid.
2033 * @param t Tunnel that was destroyed.
2036 send_clients_tunnel_destroy (struct MeshTunnel *t)
2038 struct GNUNET_MESH_TunnelMessage msg;
2040 msg.header.size = htons (sizeof (msg));
2041 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2042 msg.tunnel_id = htonl (t->local_tid_dest);
2043 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2048 * Notify clients of tunnel disconnections, if needed.
2049 * In case the origin disconnects, the destination clients get a tunnel destroy
2050 * notification. If the last destination disconnects (only one remaining client
2051 * in tunnel), the origin gets a (local ID) peer disconnected.
2052 * Note that the function must be called BEFORE removing the client from
2055 * @param t Tunnel that was destroyed.
2056 * @param c Client that disconnected.
2059 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2065 struct GNUNET_MESH_TunnelMessage msg;
2067 msg.header.size = htons (sizeof (msg));
2068 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2069 msg.tunnel_id = htonl (t->local_tid_dest);
2070 for (i = 0; i < t->nclients; i++)
2071 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2072 &msg.header, GNUNET_NO);
2074 // FIXME when to disconnect an incoming tunnel?
2075 else if (1 == t->nclients && NULL != t->owner)
2077 struct GNUNET_MESH_PeerControl msg;
2079 msg.header.size = htons (sizeof (msg));
2080 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2081 msg.tunnel_id = htonl (t->local_tid);
2082 msg.peer = my_full_id;
2083 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2084 &msg.header, GNUNET_NO);
2090 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2091 * and insert it in the appropiate structures if the peer is not known yet.
2093 * @param peer Full identity of the peer.
2095 * @return Existing or newly created peer info.
2097 static struct MeshPeerInfo *
2098 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2100 struct MeshPeerInfo *peer_info;
2102 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2103 if (NULL == peer_info)
2106 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2107 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2108 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2109 peer_info->id = GNUNET_PEER_intern (peer);
2117 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2118 * and insert it in the appropiate structures if the peer is not known yet.
2120 * @param peer Short identity of the peer.
2122 * @return Existing or newly created peer info.
2124 static struct MeshPeerInfo *
2125 peer_info_get_short (const GNUNET_PEER_Id peer)
2127 struct GNUNET_PeerIdentity id;
2129 GNUNET_PEER_resolve (peer, &id);
2130 return peer_info_get (&id);
2135 * Iterator to remove the tunnel from the list of tunnels a peer participates
2138 * @param cls Closure (tunnel info)
2139 * @param key GNUNET_PeerIdentity of the peer (unused)
2140 * @param value PeerInfo of the peer
2142 * @return always GNUNET_YES, to keep iterating
2145 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2147 struct MeshTunnel *t = cls;
2148 struct MeshPeerInfo *peer = value;
2151 for (i = 0; i < peer->ntunnels; i++)
2154 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2157 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2158 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2167 * Core callback to write a pre-constructed data packet to core buffer
2169 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2170 * @param size Number of bytes available in buf.
2171 * @param buf Where the to write the message.
2173 * @return number of bytes written to buf
2176 send_core_data_raw (void *cls, size_t size, void *buf)
2178 struct MeshTransmissionDescriptor *info = cls;
2179 struct GNUNET_MessageHeader *msg;
2182 GNUNET_assert (NULL != info);
2183 GNUNET_assert (NULL != info->mesh_data);
2184 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2185 total_size = ntohs (msg->size);
2187 if (total_size > size)
2192 memcpy (buf, msg, total_size);
2193 data_descriptor_decrement_rc (info->mesh_data);
2200 * Sends an already built non-multicast message to a peer,
2201 * properly registrating all used resources.
2203 * @param message Message to send. Function makes a copy of it.
2204 * @param peer Short ID of the neighbor whom to send the message.
2205 * @param t Tunnel on which this message is transmitted.
2208 send_message (const struct GNUNET_MessageHeader *message,
2209 const struct GNUNET_PeerIdentity *peer,
2210 struct MeshTunnel *t)
2212 struct MeshTransmissionDescriptor *info;
2213 struct MeshPeerInfo *neighbor;
2214 struct MeshPeerPath *p;
2218 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2220 size = ntohs (message->size);
2221 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2222 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2223 info->mesh_data->data = GNUNET_malloc (size);
2224 memcpy (info->mesh_data->data, message, size);
2225 type = ntohs(message->type);
2226 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2228 struct GNUNET_MESH_Unicast *m;
2230 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2231 m->ttl = htonl (ntohl (m->ttl) - 1);
2233 info->mesh_data->data_len = size;
2234 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2235 *info->mesh_data->reference_counter = 1;
2236 neighbor = peer_info_get (peer);
2237 for (p = neighbor->path_head; NULL != p; p = p->next)
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2248 " %s IS NOT DIRECTLY CONNECTED\n",
2250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2253 for (p = neighbor->path_head; NULL != p; p = p->next)
2255 struct GNUNET_PeerIdentity debug_id;
2258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2259 " path with %u hops through:\n",
2261 for (i = 0; i < p->length; i++)
2263 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2266 i, GNUNET_i2s(&debug_id));
2270 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2271 GNUNET_free (info->mesh_data->data);
2272 GNUNET_free (info->mesh_data);
2276 info->peer = neighbor;
2277 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2280 * TODO: in this case we only need the service to retransmit
2281 * the message down the path. If we pass the real type to queue_add,
2282 * queue_send will try to build the message from scratch. This can
2283 * probably be done by some other way instead of deleteing the type
2297 * Sends a CREATE PATH message for a path to a peer, properly registrating
2298 * all used resources.
2300 * @param peer PeerInfo of the final peer for whom this path is being created.
2301 * @param p Path itself.
2302 * @param t Tunnel for which the path is created.
2305 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2306 struct MeshTunnel *t)
2308 struct GNUNET_PeerIdentity id;
2309 struct MeshPathInfo *path_info;
2310 struct MeshPeerInfo *neighbor;
2316 p = tree_get_path_to_peer (t->tree, peer->id);
2323 for (i = 0; i < p->length; i++)
2325 if (p->peers[i] == myid)
2328 if (i >= p->length - 1)
2334 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2336 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2337 path_info->path = p;
2339 neighbor = peer_info_get (&id);
2340 path_info->peer = neighbor;
2341 queue_add (path_info,
2342 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2343 sizeof (struct GNUNET_MESH_ManipulatePath) +
2344 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2351 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2353 * @param t Tunnel whose path to destroy.
2354 * @param destination Short ID of the peer to whom the path to destroy.
2357 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2359 struct MeshPeerPath *p;
2362 p = tree_get_path_to_peer (t->tree, destination);
2368 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2369 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2371 struct GNUNET_MESH_ManipulatePath *msg;
2372 struct GNUNET_PeerIdentity *pi;
2376 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2377 msg->header.size = htons (size);
2378 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2379 msg->tid = htonl (t->id.tid);
2380 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2381 for (i = 0; i < p->length; i++)
2383 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2385 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2392 * Try to establish a new connection to this peer.
2393 * Use the best path for the given tunnel.
2394 * If the peer doesn't have any path to it yet, try to get one.
2395 * If the peer already has some path, send a CREATE PATH towards it.
2397 * @param peer PeerInfo of the peer.
2398 * @param t Tunnel for which to create the path, if possible.
2401 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2403 struct MeshPeerPath *p;
2404 struct MeshPathInfo *path_info;
2406 if (NULL != peer->path_head)
2408 p = tree_get_path_to_peer (t->tree, peer->id);
2415 // FIXME always send create path to self
2418 send_create_path (peer, p, t);
2422 struct GNUNET_HashCode hash;
2425 send_client_peer_connected (t, myid);
2426 t->local_tid_dest = next_local_tid++;
2427 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2430 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2431 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2438 else if (NULL == peer->dhtget)
2440 struct GNUNET_PeerIdentity id;
2442 GNUNET_PEER_resolve (peer->id, &id);
2443 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2444 path_info->peer = peer;
2446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2447 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2448 peer->dhtgetcls = path_info;
2449 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2450 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2451 &id.hashPubKey, /* key to search */
2452 dht_replication_level, /* replication level */
2453 GNUNET_DHT_RO_RECORD_ROUTE |
2454 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2455 NULL, /* xquery */ // FIXME BLOOMFILTER
2456 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2457 &dht_get_id_handler, path_info);
2459 /* Otherwise, there is no path but the DHT get is already started. */
2464 * Task to delay the connection of a peer
2466 * @param cls Closure (path info with tunnel and peer to connect).
2467 * Will be free'd on exection.
2468 * @param tc TaskContext
2471 peer_info_connect_task (void *cls,
2472 const struct GNUNET_SCHEDULER_TaskContext *tc)
2474 struct MeshPathInfo *path_info = cls;
2476 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2481 peer_info_connect (path_info->peer, path_info->t);
2487 * Destroy the peer_info and free any allocated resources linked to it
2489 * @param pi The peer_info to destroy.
2491 * @return GNUNET_OK on success
2494 peer_info_destroy (struct MeshPeerInfo *pi)
2496 struct GNUNET_PeerIdentity id;
2497 struct MeshPeerPath *p;
2498 struct MeshPeerPath *nextp;
2500 GNUNET_PEER_resolve (pi->id, &id);
2501 GNUNET_PEER_change_rc (pi->id, -1);
2504 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2507 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2508 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2510 if (NULL != pi->dhtget)
2512 GNUNET_DHT_get_stop (pi->dhtget);
2513 GNUNET_free (pi->dhtgetcls);
2519 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2529 * Remove all paths that rely on a direct connection between p1 and p2
2530 * from the peer itself and notify all tunnels about it.
2532 * @param peer PeerInfo of affected peer.
2533 * @param p1 GNUNET_PEER_Id of one peer.
2534 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2537 * TODO: optimize (see below)
2540 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2543 struct MeshPeerPath *p;
2544 struct MeshPeerPath *aux;
2545 struct MeshPeerInfo *peer_d;
2547 unsigned int destroyed;
2552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2554 p = peer->path_head;
2558 for (i = 0; i < (p->length - 1); i++)
2560 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2561 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2563 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2574 for (i = 0; i < peer->ntunnels; i++)
2576 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2580 * Problem: one or more peers have been deleted from the tunnel tree.
2581 * We don't know who they are to try to add them again.
2582 * We need to try to find a new path for each of the disconnected peers.
2583 * Some of them might already have a path to reach them that does not
2584 * involve p1 and p2. Adding all anew might render in a better tree than
2585 * the trivial immediate fix.
2587 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2588 * its children will be reachable trough him.
2590 peer_d = peer_info_get_short (d);
2593 for (p = peer_d->path_head; NULL != p; p = p->next)
2595 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2603 /* No callback, as peer will be already disconnected and a connection
2604 * scheduled by tunnel_notify_connection_broken.
2606 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2610 peer_info_connect (peer_d, peer->tunnels[i]);
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2618 * Add the path to the peer and update the path used to reach it in case this
2621 * @param peer_info Destination peer to add the path to.
2622 * @param path New path to add. Last peer must be the peer in arg 1.
2623 * Path will be either used of freed if already known.
2624 * @param trusted Do we trust that this path is real?
2627 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2630 struct MeshPeerPath *aux;
2634 if ((NULL == peer_info) || (NULL == path))
2637 path_destroy (path);
2640 if (path->peers[path->length - 1] != peer_info->id)
2643 path_destroy (path);
2646 if (path->length <= 2 && GNUNET_NO == trusted)
2648 /* Only allow CORE to tell us about direct paths */
2649 path_destroy (path);
2652 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2653 for (l = 1; l < path->length; l++)
2655 if (path->peers[l] == myid)
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2658 for (l2 = 0; l2 < path->length - l; l2++)
2660 path->peers[l2] = path->peers[l + l2];
2665 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2670 struct GNUNET_PeerIdentity id;
2672 GNUNET_PEER_resolve (peer_info->id, &id);
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2674 path->length, GNUNET_i2s (&id));
2677 l = path_get_length (path);
2684 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2685 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2687 l2 = path_get_length (aux);
2690 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2691 peer_info->path_tail, aux, path);
2696 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2698 path_destroy (path);
2703 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2710 * Add the path to the origin peer and update the path used to reach it in case
2711 * this is the shortest.
2712 * The path is given in peer_info -> destination, therefore we turn the path
2713 * upside down first.
2715 * @param peer_info Peer to add the path to, being the origin of the path.
2716 * @param path New path to add after being inversed.
2717 * @param trusted Do we trust that this path is real?
2720 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2721 struct MeshPeerPath *path, int trusted)
2724 peer_info_add_path (peer_info, path, trusted);
2729 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2730 * to obtain a local peer -> destination path and interning the peer ids.
2732 * @return Newly allocated and created path
2734 static struct MeshPeerPath *
2735 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2736 unsigned int get_path_length,
2737 const struct GNUNET_PeerIdentity *put_path,
2738 unsigned int put_path_length)
2740 struct MeshPeerPath *p;
2746 GNUNET_PEER_change_rc (myid, 1);
2747 i = get_path_length;
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2749 for (i--; i >= 0; i--)
2751 id = GNUNET_PEER_intern (&get_path[i]);
2752 if (p->length > 0 && id == p->peers[p->length - 1])
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2755 GNUNET_PEER_change_rc (id, -1);
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2760 GNUNET_i2s (&get_path[i]));
2762 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2763 p->peers[p->length - 1] = id;
2766 i = put_path_length;
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2768 for (i--; i >= 0; i--)
2770 id = GNUNET_PEER_intern (&put_path[i]);
2773 /* PUT path went through us, so discard the path up until now and start
2774 * from here to get a much shorter (and loop-free) path.
2779 if (p->length > 0 && id == p->peers[p->length - 1])
2781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2782 GNUNET_PEER_change_rc (id, -1);
2786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2787 GNUNET_i2s (&put_path[i]));
2789 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2790 p->peers[p->length - 1] = id;
2794 if (get_path_length > 0)
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2796 GNUNET_i2s (&get_path[0]));
2797 if (put_path_length > 0)
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2799 GNUNET_i2s (&put_path[0]));
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2802 for (i = 0; i < p->length; i++)
2804 struct GNUNET_PeerIdentity peer_id;
2806 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2808 GNUNET_i2s (&peer_id));
2816 * Adds a path to the peer_infos of all the peers in the path
2818 * @param p Path to process.
2819 * @param confirmed Whether we know if the path works or not.
2822 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2826 /* TODO: invert and add */
2827 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2828 for (i++; i < p->length; i++)
2830 struct MeshPeerInfo *aux;
2831 struct MeshPeerPath *copy;
2833 aux = peer_info_get_short (p->peers[i]);
2834 copy = path_duplicate (p);
2835 copy->length = i + 1;
2836 peer_info_add_path (aux, copy, GNUNET_NO);
2842 * Send keepalive packets for a peer
2844 * @param cls Closure (tunnel for which to send the keepalive).
2845 * @param tc Notification context.
2847 * TODO: implement explicit multicast keepalive?
2850 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2854 * Search for a tunnel among the incoming tunnels
2856 * @param tid the local id of the tunnel
2858 * @return tunnel handler, NULL if doesn't exist
2860 static struct MeshTunnel *
2861 tunnel_get_incoming (MESH_TunnelNumber tid)
2863 struct GNUNET_HashCode hash;
2865 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2866 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2867 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2872 * Search for a tunnel among the tunnels for a client
2874 * @param c the client whose tunnels to search in
2875 * @param tid the local id of the tunnel
2877 * @return tunnel handler, NULL if doesn't exist
2879 static struct MeshTunnel *
2880 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2882 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2884 return tunnel_get_incoming (tid);
2888 struct GNUNET_HashCode hash;
2890 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2891 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2897 * Search for a tunnel by global ID using PEER_ID
2899 * @param pi owner of the tunnel
2900 * @param tid global tunnel number
2902 * @return tunnel handler, NULL if doesn't exist
2904 static struct MeshTunnel *
2905 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2907 struct MESH_TunnelID id;
2908 struct GNUNET_HashCode hash;
2913 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2914 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2919 * Search for a tunnel by global ID using full PeerIdentities
2921 * @param oid owner of the tunnel
2922 * @param tid global tunnel number
2924 * @return tunnel handler, NULL if doesn't exist
2926 static struct MeshTunnel *
2927 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2929 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2934 * Delete an active client from the tunnel.
2940 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2944 for (i = 0; i < t->nclients; i++)
2946 if (t->clients[i] == c)
2948 t->clients[i] = t->clients[t->nclients - 1];
2949 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2950 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2952 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2960 * Delete an ignored client from the tunnel.
2966 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2970 for (i = 0; i < t->nignore; i++)
2972 if (t->ignore[i] == c)
2974 t->ignore[i] = t->ignore[t->nignore - 1];
2975 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2983 * Delete a client from the tunnel. It should be only done on
2984 * client disconnection, otherwise use client_ignore_tunnel.
2990 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2992 tunnel_delete_ignored_client (t, c);
2993 tunnel_delete_active_client (t, c);
2998 * Iterator to free MeshTunnelChildInfo of tunnel children.
3000 * @param cls Closure (tunnel info).
3001 * @param key Hash of GNUNET_PEER_Id (unused).
3002 * @param value MeshTunnelChildInfo of the child.
3004 * @return always GNUNET_YES, to keep iterating
3007 tunnel_destroy_child (void *cls,
3008 const struct GNUNET_HashCode * key,
3011 GNUNET_free (value);
3017 * Callback used to notify a client owner of a tunnel that a peer has
3018 * disconnected, most likely because of a path change.
3020 * @param cls Closure (tunnel this notification is about).
3021 * @param peer_id Short ID of disconnected peer.
3024 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3026 struct MeshTunnel *t = cls;
3027 struct MeshPeerInfo *peer;
3028 struct MeshPathInfo *path_info;
3030 if (NULL != t->owner && NULL != nc)
3032 struct GNUNET_MESH_PeerControl msg;
3034 msg.header.size = htons (sizeof (msg));
3035 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3036 msg.tunnel_id = htonl (t->local_tid);
3037 GNUNET_PEER_resolve (peer_id, &msg.peer);
3038 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3039 &msg.header, GNUNET_NO);
3041 peer = peer_info_get_short (peer_id);
3042 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3043 path_info->peer = peer;
3045 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3050 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3051 * needed rescources.
3052 * If peer already exists, reevaluate shortest path and change if different.
3054 * @param t Tunnel we want to add a new peer to
3055 * @param peer PeerInfo of the peer being added
3059 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3061 struct GNUNET_PeerIdentity id;
3062 struct MeshPeerPath *best_p;
3063 struct MeshPeerPath *p;
3064 unsigned int best_cost;
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3068 GNUNET_PEER_resolve (peer->id, &id);
3070 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3073 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3074 GNUNET_assert (GNUNET_OK ==
3075 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3077 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3080 if (NULL != (p = peer->path_head))
3083 best_cost = tree_get_path_cost (t->tree, p);
3086 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3093 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3094 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3095 t->path_refresh_task =
3096 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3100 /* Start a DHT get */
3101 peer_info_connect (peer, t);
3103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3107 * Add a path to a tunnel which we don't own, just to remember the next hop.
3108 * If destination node was already in the tunnel, the first hop information
3109 * will be replaced with the new path.
3111 * @param t Tunnel we want to add a new peer to
3112 * @param p Path to add
3113 * @param own_pos Position of local node in path.
3117 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3118 unsigned int own_pos)
3120 struct GNUNET_PeerIdentity id;
3122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3123 GNUNET_assert (0 != own_pos);
3124 tree_add_path (t->tree, p, NULL, NULL);
3125 if (own_pos < p->length - 1)
3127 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3128 tree_update_first_hops (t->tree, myid, &id);
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3134 * Add a client to a tunnel, initializing all needed data structures.
3136 * @param t Tunnel to which add the client.
3137 * @param c Client which to add to the tunnel.
3140 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3142 struct MeshTunnelClientInfo clinfo;
3144 GNUNET_array_append (t->clients, t->nclients, c);
3145 clinfo.fwd_ack = t->fwd_pid + 1;
3146 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3147 clinfo.fwd_pid = t->fwd_pid;
3148 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3150 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3155 * Notifies a tunnel that a connection has broken that affects at least
3156 * some of its peers. Sends a notification towards the root of the tree.
3157 * In case the peer is the owner of the tree, notifies the client that owns
3158 * the tunnel and tries to reconnect.
3160 * @param t Tunnel affected.
3161 * @param p1 Peer that got disconnected from p2.
3162 * @param p2 Peer that got disconnected from p1.
3164 * @return Short ID of the peer disconnected (either p1 or p2).
3165 * 0 if the tunnel remained unaffected.
3167 static GNUNET_PEER_Id
3168 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3174 tree_notify_connection_broken (t->tree, p1, p2,
3175 &tunnel_notify_client_peer_disconnected,
3177 if (myid != p1 && myid != p2)
3183 if (tree_get_predecessor (t->tree) != 0)
3185 /* We are the peer still connected, notify owner of the disconnection. */
3186 struct GNUNET_MESH_PathBroken msg;
3187 struct GNUNET_PeerIdentity neighbor;
3189 msg.header.size = htons (sizeof (msg));
3190 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3191 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3192 msg.tid = htonl (t->id.tid);
3193 msg.peer1 = my_full_id;
3194 GNUNET_PEER_resolve (pid, &msg.peer2);
3195 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3196 send_message (&msg.header, &neighbor, t);
3204 * Send a multicast packet to a neighbor.
3206 * @param cls Closure (Info about the multicast packet)
3207 * @param neighbor_id Short ID of the neighbor to send the packet to.
3210 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3212 struct MeshData *mdata = cls;
3213 struct MeshTransmissionDescriptor *info;
3214 struct GNUNET_PeerIdentity neighbor;
3216 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3218 info->mesh_data = mdata;
3219 (*(mdata->reference_counter)) ++;
3220 info->destination = neighbor_id;
3221 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3223 GNUNET_i2s (&neighbor));
3224 info->peer = peer_info_get (&neighbor);
3225 GNUNET_assert (NULL != info->peer);
3227 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3228 info->mesh_data->data_len,
3235 * Send a message in a tunnel in multicast, sending a copy to each child node
3236 * down the local one in the tunnel tree.
3238 * @param t Tunnel in which to send the data.
3239 * @param msg Message to be sent.
3240 * @param internal Has the service generated this message?
3243 tunnel_send_multicast (struct MeshTunnel *t,
3244 const struct GNUNET_MessageHeader *msg,
3247 struct MeshData *mdata;
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 " sending a multicast packet...\n");
3251 mdata = GNUNET_malloc (sizeof (struct MeshData));
3252 mdata->data_len = ntohs (msg->size);
3253 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3255 mdata->data = GNUNET_malloc (mdata->data_len);
3256 memcpy (mdata->data, msg, mdata->data_len);
3257 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3259 struct GNUNET_MESH_Multicast *mcast;
3261 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3262 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3264 ntohl (mcast->ttl));
3268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3270 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3271 && GNUNET_NO == internal)
3273 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3275 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3281 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3282 if (*(mdata->reference_counter) == 0)
3284 GNUNET_free (mdata->data);
3285 GNUNET_free (mdata->reference_counter);
3286 if (NULL != mdata->task)
3288 GNUNET_SCHEDULER_cancel(*(mdata->task));
3289 GNUNET_free (mdata->task);
3290 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3292 // FIXME change order?
3293 GNUNET_free (mdata);
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3296 " sending a multicast packet done\n");
3302 * Increase the SKIP value of all peers that
3303 * have not received a unicast message.
3305 * @param cls Closure (ID of the peer that HAS received the message).
3306 * @param key ID of the neighbor.
3307 * @param value Information about the neighbor.
3309 * @return GNUNET_YES to keep iterating.
3312 tunnel_add_skip (void *cls,
3313 const struct GNUNET_HashCode * key,
3316 struct GNUNET_PeerIdentity *neighbor = cls;
3317 struct MeshTunnelChildInfo *cinfo = value;
3319 /* TODO compare only pointers? key == neighbor? */
3320 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3330 * @brief Get neighbor's Flow Control information.
3332 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3333 * descendant of the local node in a certain tunnel.
3334 * If the info is not yet there (recently created path), creates the data struct
3335 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3338 * @param t Tunnel related.
3339 * @param peer Neighbor whose Flow Control info is needed.
3341 * @return Neighbor's Flow Control info.
3343 static struct MeshTunnelChildInfo *
3344 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3345 const struct GNUNET_PeerIdentity *peer)
3347 struct MeshTunnelChildInfo *cinfo;
3348 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3354 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3355 cinfo->id = GNUNET_PEER_intern (peer);
3356 cinfo->skip = t->fwd_pid;
3358 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3359 cinfo->fwd_ack = t->fwd_pid + delta;
3360 cinfo->bck_ack = delta;
3362 GNUNET_assert (GNUNET_OK ==
3363 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3366 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3373 * Get the Flow Control info of a client.
3375 * @param t Tunnel on which to look.
3376 * @param c Client whose ACK to get.
3378 * @return ACK value.
3380 static struct MeshTunnelClientInfo *
3381 tunnel_get_client_fc (struct MeshTunnel *t,
3382 struct MeshClient *c)
3386 for (i = 0; i < t->nclients; i++)
3388 if (t->clients[i] != c)
3390 return &t->clients_fc[i];
3393 return NULL; // avoid compiler / coverity complaints
3398 * Iterator to get the appropiate ACK value from all children nodes.
3400 * @param cls Closue (tunnel).
3401 * @param id Id of the child node.
3404 tunnel_get_child_fwd_ack (void *cls,
3407 struct GNUNET_PeerIdentity peer_id;
3408 struct MeshTunnelChildInfo *cinfo;
3409 struct MeshTunnelChildIteratorContext *ctx = cls;
3410 struct MeshTunnel *t = ctx->t;
3413 GNUNET_PEER_resolve (id, &peer_id);
3414 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3415 ack = cinfo->fwd_ack;
3417 if (GNUNET_NO == ctx->init)
3419 ctx->max_child_ack = ack;
3420 ctx->init = GNUNET_YES;
3423 if (GNUNET_YES == t->speed_min)
3425 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3429 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3436 * Get the maximum PID allowed to transmit to any
3437 * tunnel child of the local peer, depending on the tunnel
3438 * buffering/speed settings.
3442 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3445 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3447 struct MeshTunnelChildIteratorContext ctx;
3449 ctx.max_child_ack = 0;
3451 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3453 if (0 == ctx.nchildren)
3456 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3457 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3459 return (int64_t) ctx.max_child_ack;
3464 * Set the FWD ACK value of a client in a particular tunnel.
3466 * @param t Tunnel affected.
3467 * @param c Client whose ACK to set.
3468 * @param ack ACK value.
3471 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3472 struct MeshClient *c,
3477 for (i = 0; i < t->nclients; i++)
3479 if (t->clients[i] != c)
3481 t->clients_fc[i].fwd_ack = ack;
3489 * Get the highest ACK value of all clients in a particular tunnel,
3490 * according to the buffering/speed settings.
3492 * @param t Tunnel on which to look.
3494 * @return Corresponding ACK value (max uint32_t).
3495 * If no clients are suscribed, -1.
3498 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3503 if (0 == t->nclients)
3506 for (ack = -1, i = 0; i < t->nclients; i++)
3509 (GNUNET_YES == t->speed_min &&
3510 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3511 (GNUNET_NO == t->speed_min &&
3512 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3514 ack = t->clients_fc[i].fwd_ack;
3518 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3519 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3521 return (uint32_t) ack;
3526 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3527 * mode and the status of all children nodes.
3531 * @return Maximum PID allowed.
3534 tunnel_get_fwd_ack (struct MeshTunnel *t)
3537 uint32_t buffer_free;
3542 count = t->fwd_pid - t->skip;
3543 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3544 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3545 child_ack = tunnel_get_children_fwd_ack (t);
3546 client_ack = tunnel_get_clients_fwd_ack (t);
3547 if (-1 == child_ack)
3549 // Node has no children, child_ack AND core buffer are irrelevant.
3550 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good! // FIXME fc
3551 return (uint32_t) client_ack;
3554 if (GNUNET_YES == t->speed_min)
3556 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3557 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3561 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3562 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3564 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3565 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3567 count, buffer_free, child_ack, client_ack, ack);
3573 * Build a local ACK message and send it to a local client.
3575 * @param t Tunnel on which to send the ACK.
3576 * @param c Client to whom send the ACK.
3577 * @param ack Value of the ACK.
3580 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3582 struct GNUNET_MESH_LocalAck msg;
3584 msg.header.size = htons (sizeof (msg));
3585 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3586 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3587 msg.max_pid = htonl (ack);
3588 GNUNET_SERVER_notification_context_unicast(nc,
3595 * Build an ACK message and queue it to send to the given peer.
3597 * @param t Tunnel on which to send the ACK.
3598 * @param peer Peer to whom send the ACK.
3599 * @param ack Value of the ACK.
3602 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3604 struct GNUNET_MESH_ACK msg;
3606 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3607 msg.header.size = htons (sizeof (msg));
3608 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3609 msg.pid = htonl (ack);
3610 msg.tid = htonl (t->id.tid);
3612 send_message (&msg.header, peer, t);
3617 * Notify a the owner of a tunnel about how many more
3618 * payload packages will we accept on a given tunnel.
3620 * @param t Tunnel on which to send the ACK.
3623 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3628 "Sending client FWD ACK on tunnel %X\n",
3631 ack = tunnel_get_fwd_ack (t);
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3634 if (t->last_fwd_ack == ack)
3637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3638 t->last_fwd_ack = ack;
3639 send_local_ack (t, t->owner, ack);
3644 * Send an ACK informing the predecessor about the available buffer space.
3645 * In case there is no predecessor, inform the owning client.
3646 * If buffering is off, send only on behalf of children or self if endpoint.
3647 * If buffering is on, send when sent to children and buffer space is free.
3648 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3649 * the ACK itself goes "back" (towards root).
3651 * @param t Tunnel on which to send the ACK.
3652 * @param type Type of message that triggered the ACK transmission.
3655 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3657 struct GNUNET_PeerIdentity id;
3660 if (NULL != t->owner)
3662 tunnel_send_client_fwd_ack (t);
3665 /* Is it after unicast / multicast retransmission? */
3668 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3669 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3671 "ACK due to FWD DATA retransmission\n");
3672 if (GNUNET_YES == t->nobuffer)
3674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3677 if (t->fwd_queue_max > t->fwd_queue_n * 2)
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3683 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3684 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3690 /* Ok, ACK might be necessary, what PID to ACK? */
3691 ack = tunnel_get_fwd_ack (t);
3693 /* If speed_min and not all children have ack'd, dont send yet */
3694 if (ack == t->last_fwd_ack)
3696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3700 t->last_fwd_ack = ack;
3701 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3702 send_ack (t, &id, ack);
3707 * Iterator to send a child node a BCK ACK to allow him to send more
3710 * @param cls Closure (tunnel).
3711 * @param id Id of the child node.
3714 tunnel_send_child_bck_ack (void *cls,
3717 struct MeshTunnel *t = cls;
3718 struct MeshTunnelChildInfo *cinfo;
3719 struct GNUNET_PeerIdentity peer;
3721 GNUNET_PEER_resolve (id, &peer);
3722 cinfo = tunnel_get_neighbor_fc (t, &peer);
3724 if (cinfo->bck_ack != cinfo->pid &&
3725 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3729 send_ack (t, &peer, cinfo->bck_ack);
3734 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3736 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3738 * @param t Tunnel on which to send the BCK ACKs.
3741 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3744 unsigned int tunnel_delta;
3746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3748 tunnel_delta = t->bck_ack - t->bck_pid;
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3751 /* Find client whom to allow to send to origin (with lowest buffer space) */
3752 for (i = 0; i < t->nclients; i++)
3754 struct MeshTunnelClientInfo *clinfo;
3757 clinfo = &t->clients_fc[i];
3758 delta = clinfo->bck_ack - clinfo->bck_pid;
3759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3760 t->clients[i]->id, delta);
3762 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3763 (GNUNET_YES == t->nobuffer && 0 == delta))
3767 ack = clinfo->bck_pid;
3768 ack += t->nobuffer ? 1 : tunnel_delta;
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3770 " sending ack to client %u: %u\n",
3771 t->clients[i]->id, ack);
3772 send_local_ack (t, t->clients[i], ack);
3773 clinfo->bck_ack = ack;
3780 * Send an ACK informing the children nodes and destination clients about
3781 * the available buffer space.
3782 * If buffering is off, send only on behalf of root (can be self).
3783 * If buffering is on, send when sent to predecessor and buffer space is free.
3784 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3785 * the ACK itself goes "forward" (towards children/clients).
3787 * @param t Tunnel on which to send the ACK.
3788 * @param type Type of message that triggered the ACK transmission.
3791 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3794 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3795 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3796 /* Is it after data to_origin retransmission? */
3799 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3800 if (GNUNET_YES == t->nobuffer)
3802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3803 " Not sending ACK, nobuffer\n");
3807 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3808 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3814 tunnel_send_clients_bck_ack (t);
3815 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, NULL);
3820 * Send a message to all peers in this tunnel that the tunnel is no longer
3823 * @param t The tunnel whose peers to notify.
3826 tunnel_send_destroy (struct MeshTunnel *t)
3828 struct GNUNET_MESH_TunnelDestroy msg;
3830 msg.header.size = htons (sizeof (msg));
3831 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3832 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3833 msg.tid = htonl (t->id.tid);
3834 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3839 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3841 * @param cls Closure (Tunnel which to cancel).
3842 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3845 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3847 struct MeshTunnel *t = cls;
3848 struct MeshPeerInfo *peer_info;
3849 struct MeshPeerQueue *pq;
3850 struct MeshPeerQueue *next;
3852 peer_info = peer_info_get_short (neighbor_id);
3853 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3856 if (pq->tunnel == t)
3858 queue_destroy (pq, GNUNET_YES);
3861 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3863 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3864 peer_info->core_transmit = NULL;
3869 * Destroy the tunnel and free any allocated resources linked to it.
3871 * @param t the tunnel to destroy
3873 * @return GNUNET_OK on success
3876 tunnel_destroy (struct MeshTunnel *t)
3878 struct MeshClient *c;
3879 struct GNUNET_HashCode hash;
3886 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3892 struct GNUNET_PeerIdentity id;
3894 GNUNET_PEER_resolve (t->id.oid, &id);
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3896 GNUNET_i2s (&id), t->id.tid);
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3902 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3903 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3908 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3911 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3915 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3916 for (i = 0; i < t->nclients; i++)
3920 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3925 for (i = 0; i < t->nignore; i++)
3929 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3934 if (t->nclients > 0)
3937 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3941 GNUNET_free (t->clients);
3943 if (NULL != t->peers)
3945 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3947 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3950 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3951 &tunnel_destroy_child,
3953 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3955 tree_destroy (t->tree);
3957 if (NULL != t->regex_ctx)
3958 regex_cancel_search (t->regex_ctx);
3959 if (NULL != t->dht_get_type)
3960 GNUNET_DHT_get_stop (t->dht_get_type);
3961 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3962 GNUNET_SCHEDULER_cancel (t->timeout_task);
3963 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3964 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3967 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3968 GNUNET_assert (0 <= n_tunnels);
3975 * Create a new tunnel
3977 * @param owner Who is the owner of the tunnel (short ID).
3978 * @param tid Tunnel Number of the tunnel.
3979 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3980 * @param local Tunnel Number for the tunnel, for the client point of view.
3982 * @return A new initialized tunnel. NULL on error.
3984 static struct MeshTunnel *
3985 tunnel_new (GNUNET_PEER_Id owner,
3986 MESH_TunnelNumber tid,
3987 struct MeshClient *client,
3988 MESH_TunnelNumber local)
3990 struct MeshTunnel *t;
3991 struct GNUNET_HashCode hash;
3993 if (n_tunnels >= max_tunnels && NULL == client)
3996 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3999 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4000 t->bck_queue_max = t->fwd_queue_max;
4001 t->tree = tree_new (owner);
4003 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4004 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4005 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4006 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4007 t->local_tid = local;
4008 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4010 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4012 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4014 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4015 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4022 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4029 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4031 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4032 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4036 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4046 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4047 * that are no longer needed, as well as nodes of no longer reachable peers.
4048 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4050 * @param t Tunnel from which to remove the path.
4051 * @param peer Short id of the peer which should be removed.
4054 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4056 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4062 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4063 * client when the client disconnects. If the client is not the owner, the
4064 * owner will get notified if no more clients are in the tunnel and the client
4065 * get removed from the tunnel's list.
4067 * @param cls closure (client that is disconnecting)
4068 * @param key the hash of the local tunnel id (used to access the hashmap)
4069 * @param value the value stored at the key (tunnel to destroy)
4071 * @return GNUNET_OK on success
4074 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4076 struct MeshTunnel *t = value;
4077 struct MeshClient *c = cls;
4080 send_client_tunnel_disconnect(t, c);
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4084 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4085 tunnel_delete_client(t, c);
4086 client_delete_tunnel(c, t);
4089 tunnel_send_destroy(t);
4090 r = tunnel_destroy (t);
4096 * Timeout function, destroys tunnel if called
4098 * @param cls Closure (tunnel to destroy).
4099 * @param tc TaskContext
4102 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4104 struct MeshTunnel *t = cls;
4106 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4108 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4113 * Resets the tunnel timeout. Starts it if no timeout was running.
4115 * @param t Tunnel whose timeout to reset.
4117 * TODO use heap to improve efficiency of scheduler.
4120 tunnel_reset_timeout (struct MeshTunnel *t)
4122 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4123 GNUNET_SCHEDULER_cancel (t->timeout_task);
4125 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4126 (refresh_path_time, 4), &tunnel_timeout, t);
4130 /******************************************************************************/
4131 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4132 /******************************************************************************/
4135 * Function to send a create path packet to a peer.
4137 * @param cls closure
4138 * @param size number of bytes available in buf
4139 * @param buf where the callee should write the message
4140 * @return number of bytes written to buf
4143 send_core_path_create (void *cls, size_t size, void *buf)
4145 struct MeshPathInfo *info = cls;
4146 struct GNUNET_MESH_ManipulatePath *msg;
4147 struct GNUNET_PeerIdentity *peer_ptr;
4148 struct MeshTunnel *t = info->t;
4149 struct MeshPeerPath *p = info->path;
4154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4156 sizeof (struct GNUNET_MESH_ManipulatePath) +
4157 p->length * sizeof (struct GNUNET_PeerIdentity);
4159 if (size < size_needed || NULL == buf)
4164 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4165 msg->header.size = htons (size_needed);
4166 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4167 msg->tid = ntohl (t->id.tid);
4169 if (GNUNET_YES == t->speed_min)
4170 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4171 if (GNUNET_YES == t->nobuffer)
4172 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4173 msg->opt = htonl(opt);
4175 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4176 for (i = 0; i < p->length; i++)
4178 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4185 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4191 * Fill the core buffer
4193 * @param cls closure (data itself)
4194 * @param size number of bytes available in buf
4195 * @param buf where the callee should write the message
4197 * @return number of bytes written to buf
4200 send_core_data_multicast (void *cls, size_t size, void *buf)
4202 struct MeshTransmissionDescriptor *info = cls;
4205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4206 GNUNET_assert (NULL != info);
4207 GNUNET_assert (NULL != info->peer);
4208 total_size = info->mesh_data->data_len;
4209 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4211 if (total_size > size)
4216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4217 memcpy (buf, info->mesh_data->data, total_size);
4220 struct GNUNET_MESH_Multicast *mc;
4221 struct GNUNET_MessageHeader *mh;
4224 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4226 mc = (struct GNUNET_MESH_Multicast *) mh;
4227 mh = (struct GNUNET_MessageHeader *) &mc[1];
4228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4229 " multicast, payload type %s\n",
4230 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4232 " multicast, payload size %u\n", ntohs (mh->size));
4236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4237 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4241 data_descriptor_decrement_rc (info->mesh_data);
4242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4250 * Creates a path ack message in buf and frees all unused resources.
4252 * @param cls closure (MeshTransmissionDescriptor)
4253 * @param size number of bytes available in buf
4254 * @param buf where the callee should write the message
4255 * @return number of bytes written to buf
4258 send_core_path_ack (void *cls, size_t size, void *buf)
4260 struct MeshTransmissionDescriptor *info = cls;
4261 struct GNUNET_MESH_PathACK *msg = buf;
4263 GNUNET_assert (NULL != info);
4264 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4269 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4270 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4271 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4272 msg->tid = htonl (info->origin->tid);
4273 msg->peer_id = my_full_id;
4276 /* TODO add signature */
4278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4279 return sizeof (struct GNUNET_MESH_PathACK);
4284 * Free a transmission that was already queued with all resources
4285 * associated to the request.
4287 * @param queue Queue handler to cancel.
4288 * @param clear_cls Is it necessary to free associated cls?
4291 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4293 struct MeshTransmissionDescriptor *dd;
4294 struct MeshPathInfo *path_info;
4296 if (GNUNET_YES == clear_cls)
4298 switch (queue->type)
4300 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4301 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4302 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4305 data_descriptor_decrement_rc (dd->mesh_data);
4307 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4309 path_info = queue->cls;
4310 path_destroy (path_info->path);
4314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4316 GNUNET_free_non_null (queue->cls);
4318 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4319 queue->peer->queue_tail,
4321 GNUNET_free (queue);
4326 * Core callback to write a queued packet to core buffer
4328 * @param cls Closure (peer info).
4329 * @param size Number of bytes available in buf.
4330 * @param buf Where the to write the message.
4332 * @return number of bytes written to buf
4335 queue_send (void *cls, size_t size, void *buf)
4337 struct MeshPeerInfo *peer = cls;
4338 struct GNUNET_MessageHeader *msg;
4339 struct MeshPeerQueue *queue;
4340 struct MeshTunnel *t;
4343 peer->core_transmit = NULL;
4344 queue = peer->queue_head;
4346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4348 /* If queue is empty, send should have been cancelled */
4354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4356 /* Check if buffer size is enough for the message */
4357 if (queue->size > size)
4359 struct GNUNET_PeerIdentity id;
4361 GNUNET_PEER_resolve (peer->id, &id);
4362 peer->core_transmit =
4363 GNUNET_CORE_notify_transmit_ready(core_handle,
4366 GNUNET_TIME_UNIT_FOREVER_REL,
4373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4376 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast: %u\n");
4381 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4388 switch (queue->type)
4391 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4392 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4393 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4395 "********* raw: %u\n",
4398 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4399 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4400 data_size = send_core_data_raw (queue->cls, size, buf);
4401 msg = (struct GNUNET_MessageHeader *) buf;
4402 switch (ntohs (msg->type)) // Type of preconstructed message
4404 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4405 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4407 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4408 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4414 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4416 data_size = send_core_data_multicast(queue->cls, size, buf);
4417 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4419 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4421 data_size = send_core_path_create(queue->cls, size, buf);
4423 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4425 data_size = send_core_path_ack(queue->cls, size, buf);
4429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4430 "********* type unknown: %u\n",
4435 /* Free queue, but cls was freed by send_core_* */
4436 queue_destroy (queue, GNUNET_NO);
4438 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4444 /* If more data in queue, send next */
4445 if (NULL != peer->queue_head)
4447 struct GNUNET_PeerIdentity id;
4449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4450 GNUNET_PEER_resolve (peer->id, &id);
4451 peer->core_transmit =
4452 GNUNET_CORE_notify_transmit_ready(core_handle,
4455 GNUNET_TIME_UNIT_FOREVER_REL,
4457 peer->queue_head->size,
4461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4467 * Queue and pass message to core when possible.
4469 * @param cls Closure (type dependant).
4470 * @param type Type of the message, 0 for a raw message.
4471 * @param size Size of the message.
4472 * @param dst Neighbor to send message to.
4473 * @param t Tunnel this message belongs to.
4476 queue_add (void *cls, uint16_t type, size_t size,
4477 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4479 struct MeshPeerQueue *queue;
4484 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4485 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4487 n = &t->fwd_queue_n;
4488 max = &t->fwd_queue_max;
4490 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4492 n = &t->bck_queue_n;
4493 max = &t->bck_queue_max;
4498 if (NULL == t->owner)
4499 GNUNET_break_op(0); // TODO: kill connection?
4502 return; // Drop message
4506 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4512 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4513 if (NULL == dst->core_transmit)
4515 struct GNUNET_PeerIdentity id;
4517 GNUNET_PEER_resolve (dst->id, &id);
4518 dst->core_transmit =
4519 GNUNET_CORE_notify_transmit_ready(core_handle,
4522 GNUNET_TIME_UNIT_FOREVER_REL,
4531 /******************************************************************************/
4532 /******************** MESH NETWORK HANDLERS **************************/
4533 /******************************************************************************/
4537 * Core handler for path creation
4539 * @param cls closure
4540 * @param message message
4541 * @param peer peer identity this notification is about
4542 * @param atsi performance data
4543 * @param atsi_count number of records in 'atsi'
4545 * @return GNUNET_OK to keep the connection open,
4546 * GNUNET_SYSERR to close it (signal serious error)
4549 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4550 const struct GNUNET_MessageHeader *message,
4551 const struct GNUNET_ATS_Information *atsi,
4552 unsigned int atsi_count)
4554 unsigned int own_pos;
4557 MESH_TunnelNumber tid;
4558 struct GNUNET_MESH_ManipulatePath *msg;
4559 struct GNUNET_PeerIdentity *pi;
4560 struct GNUNET_HashCode hash;
4561 struct MeshPeerPath *path;
4562 struct MeshPeerInfo *dest_peer_info;
4563 struct MeshPeerInfo *orig_peer_info;
4564 struct MeshTunnel *t;
4566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4567 "Received a path create msg [%s]\n",
4568 GNUNET_i2s (&my_full_id));
4569 size = ntohs (message->size);
4570 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4572 GNUNET_break_op (0);
4576 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4577 if (size % sizeof (struct GNUNET_PeerIdentity))
4579 GNUNET_break_op (0);
4582 size /= sizeof (struct GNUNET_PeerIdentity);
4585 GNUNET_break_op (0);
4588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4589 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4591 tid = ntohl (msg->tid);
4592 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4594 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4595 t = tunnel_get (pi, tid);
4596 if (NULL == t) // FIXME only for INCOMING tunnels?
4600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4601 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4604 // FIXME notify failure
4607 opt = ntohl (msg->opt);
4608 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4609 GNUNET_YES : GNUNET_NO;
4610 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4611 GNUNET_YES : GNUNET_NO;
4613 if (GNUNET_YES == t->nobuffer)
4615 t->bck_queue_max = 1;
4616 t->fwd_queue_max = 1;
4619 // FIXME only assign a local tid if a local client is interested (on demand)
4620 while (NULL != tunnel_get_incoming (next_local_tid))
4621 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4622 t->local_tid_dest = next_local_tid++;
4623 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4626 tunnel_reset_timeout (t);
4627 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4629 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4630 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4638 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4639 if (NULL == dest_peer_info)
4641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4642 " Creating PeerInfo for destination.\n");
4643 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4644 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4645 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4647 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4649 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4650 if (NULL == orig_peer_info)
4652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4653 " Creating PeerInfo for origin.\n");
4654 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4655 orig_peer_info->id = GNUNET_PEER_intern (pi);
4656 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4657 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4660 path = path_new (size);
4662 for (i = 0; i < size; i++)
4664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4665 GNUNET_i2s (&pi[i]));
4666 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4667 if (path->peers[i] == myid)
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4673 /* cannot be self, must be 'not found' */
4674 /* create path: self not found in path through self */
4675 GNUNET_break_op (0);
4676 path_destroy (path);
4680 path_add_to_peers (path, GNUNET_NO);
4681 tunnel_add_path (t, path, own_pos);
4682 if (own_pos == size - 1)
4684 /* It is for us! Send ack. */
4685 struct MeshTransmissionDescriptor *info;
4687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4688 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4689 if (NULL == t->peers)
4691 /* New tunnel! Notify clients on data. */
4692 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4694 GNUNET_break (GNUNET_SYSERR !=
4695 GNUNET_CONTAINER_multihashmap_put (t->peers,
4696 &my_full_id.hashPubKey,
4699 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4700 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4701 info->origin = &t->id;
4702 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4703 GNUNET_assert (NULL != info->peer);
4705 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4706 sizeof (struct GNUNET_MESH_PathACK),
4712 struct MeshPeerPath *path2;
4714 /* It's for somebody else! Retransmit. */
4715 path2 = path_duplicate (path);
4716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4717 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4718 path2 = path_duplicate (path);
4719 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4720 send_create_path (dest_peer_info, path, t);
4727 * Core handler for path destruction
4729 * @param cls closure
4730 * @param message message
4731 * @param peer peer identity this notification is about
4732 * @param atsi performance data
4733 * @param atsi_count number of records in 'atsi'
4735 * @return GNUNET_OK to keep the connection open,
4736 * GNUNET_SYSERR to close it (signal serious error)
4739 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4740 const struct GNUNET_MessageHeader *message,
4741 const struct GNUNET_ATS_Information *atsi,
4742 unsigned int atsi_count)
4744 struct GNUNET_MESH_ManipulatePath *msg;
4745 struct GNUNET_PeerIdentity *pi;
4746 struct MeshPeerPath *path;
4747 struct MeshTunnel *t;
4748 unsigned int own_pos;
4752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4753 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4754 size = ntohs (message->size);
4755 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4757 GNUNET_break_op (0);
4761 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4762 if (size % sizeof (struct GNUNET_PeerIdentity))
4764 GNUNET_break_op (0);
4767 size /= sizeof (struct GNUNET_PeerIdentity);
4770 GNUNET_break_op (0);
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4775 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4776 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4778 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4780 t = tunnel_get (pi, ntohl (msg->tid));
4783 /* TODO notify back: we don't know this tunnel */
4784 GNUNET_break_op (0);
4787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4788 path = path_new (size);
4790 for (i = 0; i < size; i++)
4792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4793 GNUNET_i2s (&pi[i]));
4794 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4795 if (path->peers[i] == myid)
4798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4799 if (own_pos < path->length - 1)
4800 send_message (message, &pi[own_pos + 1], t);
4802 send_client_tunnel_disconnect(t, NULL);
4804 tunnel_delete_peer (t, path->peers[path->length - 1]);
4805 path_destroy (path);
4811 * Core handler for notifications of broken paths
4813 * @param cls closure
4814 * @param message message
4815 * @param peer peer identity this notification is about
4816 * @param atsi performance data
4817 * @param atsi_count number of records in 'atsi'
4819 * @return GNUNET_OK to keep the connection open,
4820 * GNUNET_SYSERR to close it (signal serious error)
4823 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4824 const struct GNUNET_MessageHeader *message,
4825 const struct GNUNET_ATS_Information *atsi,
4826 unsigned int atsi_count)
4828 struct GNUNET_MESH_PathBroken *msg;
4829 struct MeshTunnel *t;
4831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4832 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4833 msg = (struct GNUNET_MESH_PathBroken *) message;
4834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4835 GNUNET_i2s (&msg->peer1));
4836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4837 GNUNET_i2s (&msg->peer2));
4838 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4841 GNUNET_break_op (0);
4844 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4845 GNUNET_PEER_search (&msg->peer2));
4852 * Core handler for tunnel destruction
4854 * @param cls closure
4855 * @param message message
4856 * @param peer peer identity this notification is about
4857 * @param atsi performance data
4858 * @param atsi_count number of records in 'atsi'
4860 * @return GNUNET_OK to keep the connection open,
4861 * GNUNET_SYSERR to close it (signal serious error)
4864 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4865 const struct GNUNET_MessageHeader *message,
4866 const struct GNUNET_ATS_Information *atsi,
4867 unsigned int atsi_count)
4869 struct GNUNET_MESH_TunnelDestroy *msg;
4870 struct MeshTunnel *t;
4872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4873 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4874 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4876 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4877 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4880 /* Probably already got the message from another path,
4881 * destroyed the tunnel and retransmitted to children.
4884 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4887 if (t->id.oid == myid)
4889 GNUNET_break_op (0);
4892 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4894 /* Tunnel was incoming, notify clients */
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4896 t->local_tid, t->local_tid_dest);
4897 send_clients_tunnel_destroy (t);
4899 tunnel_send_destroy (t);
4900 t->destroy = GNUNET_YES;
4901 // TODO: add timeout to destroy the tunnel anyway
4907 * Core handler for mesh network traffic going from the origin to a peer
4909 * @param cls closure
4910 * @param peer peer identity this notification is about
4911 * @param message message
4912 * @param atsi performance data
4913 * @param atsi_count number of records in 'atsi'
4914 * @return GNUNET_OK to keep the connection open,
4915 * GNUNET_SYSERR to close it (signal serious error)
4918 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4919 const struct GNUNET_MessageHeader *message,
4920 const struct GNUNET_ATS_Information *atsi,
4921 unsigned int atsi_count)
4923 struct GNUNET_MESH_Unicast *msg;
4924 struct GNUNET_PeerIdentity *neighbor;
4925 struct MeshTunnelChildInfo *cinfo;
4926 struct MeshTunnel *t;
4927 GNUNET_PEER_Id dest_id;
4932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4935 size = ntohs (message->size);
4937 sizeof (struct GNUNET_MESH_Unicast) +
4938 sizeof (struct GNUNET_MessageHeader))
4943 msg = (struct GNUNET_MESH_Unicast *) message;
4944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4945 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4947 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4950 /* TODO notify back: we don't know this tunnel */
4951 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4952 GNUNET_break_op (0);
4955 pid = ntohl (msg->pid);
4956 if (t->fwd_pid == pid)
4958 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4959 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4960 " Already seen pid %u, DROPPING!\n", pid);
4965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4966 " pid %u not seen yet, forwarding\n", pid);
4968 t->skip += (pid - t->fwd_pid) - 1;
4970 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4972 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4973 GNUNET_break_op (0);
4976 tunnel_reset_timeout (t);
4977 dest_id = GNUNET_PEER_search (&msg->destination);
4978 if (dest_id == myid)
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4981 " it's for us! sending to clients...\n");
4982 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4983 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4984 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4987 ttl = ntohl (msg->ttl);
4988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4991 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4992 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4993 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4997 " not for us, retransmitting...\n");
4999 neighbor = tree_get_first_hop (t->tree, dest_id);
5000 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5002 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5005 if (GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5007 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5008 GNUNET_break_op (0);
5011 send_message (message, neighbor, t);
5012 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5019 * Core handler for mesh network traffic going from the origin to all peers
5021 * @param cls closure
5022 * @param message message
5023 * @param peer peer identity this notification is about
5024 * @param atsi performance data
5025 * @param atsi_count number of records in 'atsi'
5026 * @return GNUNET_OK to keep the connection open,
5027 * GNUNET_SYSERR to close it (signal serious error)
5029 * TODO: Check who we got this from, to validate route.
5032 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5033 const struct GNUNET_MessageHeader *message,
5034 const struct GNUNET_ATS_Information *atsi,
5035 unsigned int atsi_count)
5037 struct GNUNET_MESH_Multicast *msg;
5038 struct MeshTunnel *t;
5042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5044 size = ntohs (message->size);
5045 if (sizeof (struct GNUNET_MESH_Multicast) +
5046 sizeof (struct GNUNET_MessageHeader) > size)
5048 GNUNET_break_op (0);
5051 msg = (struct GNUNET_MESH_Multicast *) message;
5052 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5056 /* TODO notify that we dont know that tunnel */
5057 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5058 GNUNET_break_op (0);
5061 pid = ntohl (msg->pid);
5062 if (t->fwd_pid == pid)
5064 /* already seen this packet, drop */
5065 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5067 " Already seen pid %u, DROPPING!\n", pid);
5068 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5074 " pid %u not seen yet, forwarding\n", pid);
5076 t->skip += (pid - t->fwd_pid) - 1;
5078 tunnel_reset_timeout (t);
5080 /* Transmit to locally interested clients */
5081 if (NULL != t->peers &&
5082 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5084 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5085 send_subscribed_clients (message, &msg[1].header);
5086 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5089 if (ntohl (msg->ttl) == 0)
5091 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5092 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5093 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5096 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5097 tunnel_send_multicast (t, message, GNUNET_NO);
5103 * Core handler for mesh network traffic toward the owner of a tunnel
5105 * @param cls closure
5106 * @param message message
5107 * @param peer peer identity this notification is about
5108 * @param atsi performance data
5109 * @param atsi_count number of records in 'atsi'
5111 * @return GNUNET_OK to keep the connection open,
5112 * GNUNET_SYSERR to close it (signal serious error)
5115 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5116 const struct GNUNET_MessageHeader *message,
5117 const struct GNUNET_ATS_Information *atsi,
5118 unsigned int atsi_count)
5120 struct GNUNET_MESH_ToOrigin *msg;
5121 struct GNUNET_PeerIdentity id;
5122 struct MeshPeerInfo *peer_info;
5123 struct MeshTunnel *t;
5126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5128 size = ntohs (message->size);
5129 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5130 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5132 GNUNET_break_op (0);
5135 msg = (struct GNUNET_MESH_ToOrigin *) message;
5136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5137 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5138 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5142 /* TODO notify that we dont know this tunnel (whom)? */
5143 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5144 GNUNET_break_op (0);
5148 if (NULL != t->owner)
5151 struct GNUNET_MESH_ToOrigin *copy;
5153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5154 " it's for us! sending to clients...\n");
5155 /* TODO signature verification */
5156 memcpy (cbuf, message, size);
5157 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5158 copy->tid = htonl (t->local_tid);
5159 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5160 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5161 ©->header, GNUNET_NO);
5162 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5166 " not for us, retransmitting...\n");
5168 peer_info = peer_info_get (&msg->oid);
5169 if (NULL == peer_info)
5171 /* unknown origin of tunnel */
5175 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5176 send_message (message, &id, t);
5177 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5184 * Core handler for mesh network traffic point-to-point acks.
5186 * @param cls closure
5187 * @param message message
5188 * @param peer peer identity this notification is about
5189 * @param atsi performance data
5190 * @param atsi_count number of records in 'atsi'
5192 * @return GNUNET_OK to keep the connection open,
5193 * GNUNET_SYSERR to close it (signal serious error)
5196 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5197 const struct GNUNET_MessageHeader *message,
5198 const struct GNUNET_ATS_Information *atsi,
5199 unsigned int atsi_count)
5201 struct GNUNET_MESH_ACK *msg;
5202 struct MeshTunnel *t;
5205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5207 msg = (struct GNUNET_MESH_ACK *) message;
5208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5209 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5210 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5214 /* TODO notify that we dont know this tunnel (whom)? */
5215 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5216 GNUNET_break_op (0);
5219 ack = ntohl (msg->pid);
5221 /* Is this a forward or backward ACK? */
5222 if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
5224 struct MeshTunnelChildInfo *cinfo;
5226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5227 cinfo = tunnel_get_neighbor_fc (t, peer);
5228 cinfo->fwd_ack = ack;
5229 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5235 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5237 // FIXME fc Unlock queues?
5243 * Core handler for path ACKs
5245 * @param cls closure
5246 * @param message message
5247 * @param peer peer identity this notification is about
5248 * @param atsi performance data
5249 * @param atsi_count number of records in 'atsi'
5251 * @return GNUNET_OK to keep the connection open,
5252 * GNUNET_SYSERR to close it (signal serious error)
5255 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5256 const struct GNUNET_MessageHeader *message,
5257 const struct GNUNET_ATS_Information *atsi,
5258 unsigned int atsi_count)
5260 struct GNUNET_MESH_PathACK *msg;
5261 struct GNUNET_PeerIdentity id;
5262 struct MeshPeerInfo *peer_info;
5263 struct MeshPeerPath *p;
5264 struct MeshTunnel *t;
5266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5267 GNUNET_i2s (&my_full_id));
5268 msg = (struct GNUNET_MESH_PathACK *) message;
5269 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5272 /* TODO notify that we don't know the tunnel */
5273 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5275 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5279 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5281 peer_info = peer_info_get (&msg->peer_id);
5282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5283 GNUNET_i2s (&msg->peer_id));
5284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5287 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5290 "connect_by_string completed, stopping search\n");
5291 regex_cancel_search (t->regex_ctx);
5292 t->regex_ctx = NULL;
5295 /* Add paths to peers? */
5296 p = tree_get_path_to_peer (t->tree, peer_info->id);
5299 path_add_to_peers (p, GNUNET_YES);
5307 /* Message for us? */
5308 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5311 if (NULL == t->owner)
5313 GNUNET_break_op (0);
5316 if (NULL != t->dht_get_type)
5318 GNUNET_DHT_get_stop (t->dht_get_type);
5319 t->dht_get_type = NULL;
5321 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5323 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5324 send_client_peer_connected (t, peer_info->id);
5329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5330 " not for us, retransmitting...\n");
5331 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5332 peer_info = peer_info_get (&msg->oid);
5333 if (NULL == peer_info)
5335 /* If we know the tunnel, we should DEFINITELY know the peer */
5339 send_message (message, &id, t);
5345 * Functions to handle messages from core
5347 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5348 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5349 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5350 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5351 sizeof (struct GNUNET_MESH_PathBroken)},
5352 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5353 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5354 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5355 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5356 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5357 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5358 sizeof (struct GNUNET_MESH_ACK)},
5359 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5360 sizeof (struct GNUNET_MESH_PathACK)},
5366 /******************************************************************************/
5367 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5368 /******************************************************************************/
5371 * deregister_app: iterator for removing each application registered by a client
5373 * @param cls closure
5374 * @param key the hash of the application id (used to access the hashmap)
5375 * @param value the value stored at the key (client)
5377 * @return GNUNET_OK on success
5380 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5382 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5383 GNUNET_break (GNUNET_YES ==
5384 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5390 * notify_client_connection_failure: notify a client that the connection to the
5391 * requested remote peer is not possible (for instance, no route found)
5392 * Function called when the socket is ready to queue more data. "buf" will be
5393 * NULL and "size" zero if the socket was closed for writing in the meantime.
5395 * @param cls closure
5396 * @param size number of bytes available in buf
5397 * @param buf where the callee should write the message
5398 * @return number of bytes written to buf
5401 notify_client_connection_failure (void *cls, size_t size, void *buf)
5404 struct MeshPeerInfo *peer_info;
5405 struct GNUNET_MESH_PeerControl *msg;
5406 struct GNUNET_PeerIdentity id;
5408 if (0 == size && NULL == buf)
5410 // TODO retry? cancel?
5414 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5415 peer_info = (struct MeshPeerInfo *) cls;
5416 msg = (struct GNUNET_MESH_PeerControl *) buf;
5417 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5418 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5419 // msg->tunnel_id = htonl(peer_info->t->tid);
5420 GNUNET_PEER_resolve (peer_info->id, &id);
5421 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5429 * Send keepalive packets for a peer
5431 * @param cls Closure (tunnel for which to send the keepalive).
5432 * @param tc Notification context.
5434 * TODO: implement explicit multicast keepalive?
5437 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5439 struct MeshTunnel *t = cls;
5440 struct GNUNET_MessageHeader *payload;
5441 struct GNUNET_MESH_Multicast *msg;
5443 sizeof (struct GNUNET_MESH_Multicast) +
5444 sizeof (struct GNUNET_MessageHeader);
5447 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5451 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5454 "sending keepalive for tunnel %d\n", t->id.tid);
5456 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5457 msg->header.size = htons (size);
5458 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5459 msg->oid = my_full_id;
5460 msg->tid = htonl (t->id.tid);
5461 msg->ttl = htonl (default_ttl);
5462 msg->pid = htonl (t->fwd_pid + 1);
5464 payload = (struct GNUNET_MessageHeader *) &msg[1];
5465 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5466 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5467 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5469 t->path_refresh_task =
5470 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5476 * Function to process paths received for a new peer addition. The recorded
5477 * paths form the initial tunnel, which can be optimized later.
5478 * Called on each result obtained for the DHT search.
5480 * @param cls closure
5481 * @param exp when will this value expire
5482 * @param key key of the result
5483 * @param get_path path of the get request
5484 * @param get_path_length lenght of get_path
5485 * @param put_path path of the put request
5486 * @param put_path_length length of the put_path
5487 * @param type type of the result
5488 * @param size number of bytes in data
5489 * @param data pointer to the result data
5491 * TODO: re-issue the request after certain time? cancel after X results?
5494 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5495 const struct GNUNET_HashCode * key,
5496 const struct GNUNET_PeerIdentity *get_path,
5497 unsigned int get_path_length,
5498 const struct GNUNET_PeerIdentity *put_path,
5499 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5500 size_t size, const void *data)
5502 struct MeshPathInfo *path_info = cls;
5503 struct MeshPeerPath *p;
5504 struct GNUNET_PeerIdentity pi;
5507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5508 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5511 p = path_build_from_dht (get_path, get_path_length, put_path,
5513 path_add_to_peers (p, GNUNET_NO);
5515 for (i = 0; i < path_info->peer->ntunnels; i++)
5517 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5518 peer_info_connect (path_info->peer, path_info->t);
5526 * Function to process paths received for a new peer addition. The recorded
5527 * paths form the initial tunnel, which can be optimized later.
5528 * Called on each result obtained for the DHT search.
5530 * @param cls closure
5531 * @param exp when will this value expire
5532 * @param key key of the result
5533 * @param get_path path of the get request
5534 * @param get_path_length lenght of get_path
5535 * @param put_path path of the put request
5536 * @param put_path_length length of the put_path
5537 * @param type type of the result
5538 * @param size number of bytes in data
5539 * @param data pointer to the result data
5542 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5543 const struct GNUNET_HashCode * key,
5544 const struct GNUNET_PeerIdentity *get_path,
5545 unsigned int get_path_length,
5546 const struct GNUNET_PeerIdentity *put_path,
5547 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5548 size_t size, const void *data)
5550 const struct PBlock *pb = data;
5551 const struct GNUNET_PeerIdentity *pi = &pb->id;
5552 struct MeshTunnel *t = cls;
5553 struct MeshPeerInfo *peer_info;
5554 struct MeshPeerPath *p;
5556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5557 if (size != sizeof (struct PBlock))
5559 GNUNET_break_op (0);
5562 if (ntohl(pb->type) != t->type)
5564 GNUNET_break_op (0);
5567 GNUNET_assert (NULL != t->owner);
5568 peer_info = peer_info_get (pi);
5569 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5571 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5573 p = path_build_from_dht (get_path, get_path_length, put_path,
5575 path_add_to_peers (p, GNUNET_NO);
5577 tunnel_add_peer (t, peer_info);
5578 peer_info_connect (peer_info, t);
5583 * Function to process DHT string to regex matching.
5584 * Called on each result obtained for the DHT search.
5586 * @param cls closure (search context)
5587 * @param exp when will this value expire
5588 * @param key key of the result
5589 * @param get_path path of the get request (not used)
5590 * @param get_path_length lenght of get_path (not used)
5591 * @param put_path path of the put request (not used)
5592 * @param put_path_length length of the put_path (not used)
5593 * @param type type of the result
5594 * @param size number of bytes in data
5595 * @param data pointer to the result data
5598 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5599 const struct GNUNET_HashCode * key,
5600 const struct GNUNET_PeerIdentity *get_path,
5601 unsigned int get_path_length,
5602 const struct GNUNET_PeerIdentity *put_path,
5603 unsigned int put_path_length,
5604 enum GNUNET_BLOCK_Type type,
5605 size_t size, const void *data)
5607 const struct MeshRegexAccept *block = data;
5608 struct MeshRegexSearchContext *ctx = cls;
5609 struct MeshRegexSearchInfo *info = ctx->info;
5610 struct MeshPeerPath *p;
5611 struct MeshPeerInfo *peer_info;
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5616 peer_info = peer_info_get(&block->id);
5617 p = path_build_from_dht (get_path, get_path_length, put_path,
5619 path_add_to_peers (p, GNUNET_NO);
5622 tunnel_add_peer (info->t, peer_info);
5623 peer_info_connect (peer_info, info->t);
5624 if (0 == info->peer)
5626 info->peer = peer_info->id;
5630 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5633 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5634 ®ex_connect_timeout,
5642 * Function to process DHT string to regex matching.
5643 * Called on each result obtained for the DHT search.
5645 * @param cls closure (search context)
5646 * @param exp when will this value expire
5647 * @param key key of the result
5648 * @param get_path path of the get request (not used)
5649 * @param get_path_length lenght of get_path (not used)
5650 * @param put_path path of the put request (not used)
5651 * @param put_path_length length of the put_path (not used)
5652 * @param type type of the result
5653 * @param size number of bytes in data
5654 * @param data pointer to the result data
5656 * TODO: re-issue the request after certain time? cancel after X results?
5659 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5660 const struct GNUNET_HashCode * key,
5661 const struct GNUNET_PeerIdentity *get_path,
5662 unsigned int get_path_length,
5663 const struct GNUNET_PeerIdentity *put_path,
5664 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5665 size_t size, const void *data)
5667 const struct MeshRegexBlock *block = data;
5668 struct MeshRegexSearchContext *ctx = cls;
5669 struct MeshRegexSearchInfo *info = ctx->info;
5673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5674 "DHT GET STRING RETURNED RESULTS\n");
5675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5676 " key: %s\n", GNUNET_h2s (key));
5678 copy = GNUNET_malloc (size);
5679 memcpy (copy, data, size);
5680 GNUNET_break (GNUNET_OK ==
5681 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5682 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5683 len = ntohl (block->n_proof);
5685 char proof[len + 1];
5687 memcpy (proof, &block[1], len);
5689 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5691 GNUNET_break_op (0);
5695 len = strlen (info->description);
5696 if (len == ctx->position) // String processed
5698 if (GNUNET_YES == ntohl (block->accepting))
5700 regex_find_path(key, ctx);
5704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5705 // FIXME REGEX this block not successful, wait for more? start timeout?
5709 GNUNET_break (GNUNET_OK ==
5710 GNUNET_MESH_regex_block_iterate (block, size,
5711 ®ex_edge_iterator, ctx));
5715 /******************************************************************************/
5716 /********************* MESH LOCAL HANDLES **************************/
5717 /******************************************************************************/
5721 * Handler for client disconnection
5723 * @param cls closure
5724 * @param client identification of the client; NULL
5725 * for the last call when the server is destroyed
5728 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5730 struct MeshClient *c;
5731 struct MeshClient *next;
5734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5743 if (c->handle != client)
5745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5751 GNUNET_SERVER_client_drop (c->handle);
5752 c->shutting_down = GNUNET_YES;
5753 GNUNET_assert (NULL != c->own_tunnels);
5754 GNUNET_assert (NULL != c->incoming_tunnels);
5755 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5756 &tunnel_destroy_iterator, c);
5757 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5758 &tunnel_destroy_iterator, c);
5759 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5760 &tunnel_destroy_iterator, c);
5761 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5762 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5763 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5765 /* deregister clients applications */
5766 if (NULL != c->apps)
5768 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5769 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5771 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5772 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5774 GNUNET_SCHEDULER_cancel (announce_applications_task);
5775 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5777 if (NULL != c->types)
5778 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5779 for (i = 0; i < c->n_regex; i++)
5781 GNUNET_free (c->regexes[i]);
5783 GNUNET_free_non_null (c->regexes);
5784 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5785 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5787 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5790 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5799 * Handler for new clients
5801 * @param cls closure
5802 * @param client identification of the client
5803 * @param message the actual message, which includes messages the client wants
5806 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5807 const struct GNUNET_MessageHeader *message)
5809 struct GNUNET_MESH_ClientConnect *cc_msg;
5810 struct MeshClient *c;
5811 GNUNET_MESH_ApplicationType *a;
5818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5819 /* Check data sanity */
5820 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5821 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5822 ntypes = ntohs (cc_msg->types);
5823 napps = ntohs (cc_msg->applications);
5825 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5832 /* Create new client structure */
5833 c = GNUNET_malloc (sizeof (struct MeshClient));
5834 c->id = next_client_id++;
5835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5837 GNUNET_SERVER_client_keep (client);
5838 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5841 GNUNET_MESH_ApplicationType at;
5842 struct GNUNET_HashCode hc;
5844 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5845 for (i = 0; i < napps; i++)
5848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5849 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5850 /* store in clients hashmap */
5851 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5852 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5853 /* store in global hashmap, for announcements */
5854 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5855 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5857 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5858 announce_applications_task =
5859 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5865 struct GNUNET_HashCode hc;
5867 t = (uint16_t *) & a[napps];
5868 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5869 for (i = 0; i < ntypes; i++)
5872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5873 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5875 /* store in clients hashmap */
5876 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5877 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5878 /* store in global hashmap */
5879 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5880 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5884 " client has %u+%u subscriptions\n", napps, ntypes);
5886 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5887 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5888 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5889 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5890 GNUNET_SERVER_notification_context_add (nc, client);
5891 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5893 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5899 * Handler for clients announcing available services by a regular expression.
5901 * @param cls closure
5902 * @param client identification of the client
5903 * @param message the actual message, which includes messages the client wants
5906 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5907 const struct GNUNET_MessageHeader *message)
5909 struct MeshClient *c;
5913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5915 /* Sanity check for client registration */
5916 if (NULL == (c = client_get (client)))
5919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5924 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5925 regex = GNUNET_malloc (len + 1);
5926 memcpy (regex, &message[1], len);
5928 GNUNET_array_append (c->regexes, c->n_regex, regex);
5929 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5931 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5937 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5943 * Handler for requests of new tunnels
5945 * @param cls closure
5946 * @param client identification of the client
5947 * @param message the actual message
5950 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5951 const struct GNUNET_MessageHeader *message)
5953 struct GNUNET_MESH_TunnelMessage *t_msg;
5954 struct MeshTunnel *t;
5955 struct MeshClient *c;
5956 MESH_TunnelNumber tid;
5958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5960 /* Sanity check for client registration */
5961 if (NULL == (c = client_get (client)))
5964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5969 /* Message sanity check */
5970 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5977 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5978 /* Sanity check for tunnel numbering */
5979 tid = ntohl (t_msg->tunnel_id);
5980 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5986 /* Sanity check for duplicate tunnel IDs */
5987 if (NULL != tunnel_get_by_local_id (c, tid))
5990 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5994 while (NULL != tunnel_get_by_pi (myid, next_tid))
5995 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5996 t = tunnel_new (myid, next_tid++, c, tid);
5999 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6004 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6006 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6007 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6010 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6016 * Handler for requests of deleting tunnels
6018 * @param cls closure
6019 * @param client identification of the client
6020 * @param message the actual message
6023 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6024 const struct GNUNET_MessageHeader *message)
6026 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6027 struct MeshClient *c;
6028 struct MeshTunnel *t;
6029 MESH_TunnelNumber tid;
6031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6032 "Got a DESTROY TUNNEL from client!\n");
6034 /* Sanity check for client registration */
6035 if (NULL == (c = client_get (client)))
6038 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6043 /* Message sanity check */
6044 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6047 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6051 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6053 /* Retrieve tunnel */
6054 tid = ntohl (tunnel_msg->tunnel_id);
6055 t = tunnel_get_by_local_id(c, tid);
6058 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6063 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6065 client_ignore_tunnel (c, t);
6067 // TODO: when to destroy incoming tunnel?
6068 if (t->nclients == 0)
6070 GNUNET_assert (GNUNET_YES ==
6071 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6073 GNUNET_assert (GNUNET_YES ==
6074 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6075 &my_full_id.hashPubKey,
6079 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6082 send_client_tunnel_disconnect(t, c);
6083 client_delete_tunnel(c, t);
6085 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6087 tunnel_send_destroy (t);
6088 t->destroy = GNUNET_YES;
6089 // The tunnel will be destroyed when the last message is transmitted.
6090 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6096 * Handler for requests of seeting tunnel's speed.
6098 * @param cls Closure (unused).
6099 * @param client Identification of the client.
6100 * @param message The actual message.
6103 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6104 const struct GNUNET_MessageHeader *message)
6106 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6107 struct MeshClient *c;
6108 struct MeshTunnel *t;
6109 MESH_TunnelNumber tid;
6111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6112 "Got a SPEED request from client!\n");
6114 /* Sanity check for client registration */
6115 if (NULL == (c = client_get (client)))
6118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6124 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6126 /* Retrieve tunnel */
6127 tid = ntohl (tunnel_msg->tunnel_id);
6128 t = tunnel_get_by_local_id(c, tid);
6131 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6133 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6137 switch (ntohs(message->type))
6139 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6140 t->speed_min = GNUNET_YES;
6142 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6143 t->speed_min = GNUNET_NO;
6148 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6153 * Handler for requests of seeting tunnel's buffering policy.
6155 * @param cls Closure (unused).
6156 * @param client Identification of the client.
6157 * @param message The actual message.
6160 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6161 const struct GNUNET_MessageHeader *message)
6163 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6164 struct MeshClient *c;
6165 struct MeshTunnel *t;
6166 MESH_TunnelNumber tid;
6168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6169 "Got a BUFFER request from client!\n");
6171 /* Sanity check for client registration */
6172 if (NULL == (c = client_get (client)))
6175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6180 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6182 /* Retrieve tunnel */
6183 tid = ntohl (tunnel_msg->tunnel_id);
6184 t = tunnel_get_by_local_id(c, tid);
6187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6189 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6193 switch (ntohs(message->type))
6195 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6196 t->nobuffer = GNUNET_NO;
6198 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6199 t->nobuffer = GNUNET_YES;
6205 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6210 * Handler for connection requests to new peers
6212 * @param cls closure
6213 * @param client identification of the client
6214 * @param message the actual message (PeerControl)
6217 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6218 const struct GNUNET_MessageHeader *message)
6220 struct GNUNET_MESH_PeerControl *peer_msg;
6221 struct MeshPeerInfo *peer_info;
6222 struct MeshClient *c;
6223 struct MeshTunnel *t;
6224 MESH_TunnelNumber tid;
6226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6227 /* Sanity check for client registration */
6228 if (NULL == (c = client_get (client)))
6231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6236 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6238 /* Sanity check for message size */
6239 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6246 /* Tunnel exists? */
6247 tid = ntohl (peer_msg->tunnel_id);
6248 t = tunnel_get_by_local_id (c, tid);
6252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6256 /* Does client own tunnel? */
6257 if (t->owner->handle != client)
6260 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6264 GNUNET_i2s (&peer_msg->peer));
6265 peer_info = peer_info_get (&peer_msg->peer);
6267 tunnel_add_peer (t, peer_info);
6268 peer_info_connect (peer_info, t);
6270 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6276 * Handler for disconnection requests of peers in a tunnel
6278 * @param cls closure
6279 * @param client identification of the client
6280 * @param message the actual message (PeerControl)
6283 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6284 const struct GNUNET_MessageHeader *message)
6286 struct GNUNET_MESH_PeerControl *peer_msg;
6287 struct MeshPeerInfo *peer_info;
6288 struct MeshClient *c;
6289 struct MeshTunnel *t;
6290 MESH_TunnelNumber tid;
6292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6293 /* Sanity check for client registration */
6294 if (NULL == (c = client_get (client)))
6297 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6302 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6304 /* Sanity check for message size */
6305 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6308 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6312 /* Tunnel exists? */
6313 tid = ntohl (peer_msg->tunnel_id);
6314 t = tunnel_get_by_local_id (c, tid);
6318 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6323 /* Does client own tunnel? */
6324 if (t->owner->handle != client)
6327 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6332 GNUNET_i2s (&peer_msg->peer));
6333 /* Is the peer in the tunnel? */
6335 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6336 if (NULL == peer_info)
6339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6343 /* Ok, delete peer from tunnel */
6344 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6345 &peer_msg->peer.hashPubKey);
6347 send_destroy_path (t, peer_info->id);
6348 tunnel_delete_peer (t, peer_info->id);
6349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6354 * Handler for blacklist requests of peers in a tunnel
6356 * @param cls closure
6357 * @param client identification of the client
6358 * @param message the actual message (PeerControl)
6360 * FIXME implement DHT block bloomfilter
6363 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6364 const struct GNUNET_MessageHeader *message)
6366 struct GNUNET_MESH_PeerControl *peer_msg;
6367 struct MeshClient *c;
6368 struct MeshTunnel *t;
6369 MESH_TunnelNumber tid;
6371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6372 /* Sanity check for client registration */
6373 if (NULL == (c = client_get (client)))
6376 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6381 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6383 /* Sanity check for message size */
6384 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6387 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6391 /* Tunnel exists? */
6392 tid = ntohl (peer_msg->tunnel_id);
6393 t = tunnel_get_by_local_id (c, tid);
6397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6402 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6403 GNUNET_PEER_intern(&peer_msg->peer));
6408 * Handler for unblacklist requests of peers in a tunnel
6410 * @param cls closure
6411 * @param client identification of the client
6412 * @param message the actual message (PeerControl)
6415 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6416 const struct GNUNET_MessageHeader *message)
6418 struct GNUNET_MESH_PeerControl *peer_msg;
6419 struct MeshClient *c;
6420 struct MeshTunnel *t;
6421 MESH_TunnelNumber tid;
6425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6426 /* Sanity check for client registration */
6427 if (NULL == (c = client_get (client)))
6430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6435 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6437 /* Sanity check for message size */
6438 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6445 /* Tunnel exists? */
6446 tid = ntohl (peer_msg->tunnel_id);
6447 t = tunnel_get_by_local_id (c, tid);
6451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6456 /* if peer is not known, complain */
6457 pid = GNUNET_PEER_search (&peer_msg->peer);
6464 /* search and remove from list */
6465 for (i = 0; i < t->nblacklisted; i++)
6467 if (t->blacklisted[i] == pid)
6469 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6470 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6475 /* if peer hasn't been blacklisted, complain */
6481 * Handler for connection requests to new peers by type
6483 * @param cls closure
6484 * @param client identification of the client
6485 * @param message the actual message (ConnectPeerByType)
6488 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6489 const struct GNUNET_MessageHeader *message)
6491 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6492 struct MeshClient *c;
6493 struct MeshTunnel *t;
6494 struct GNUNET_HashCode hash;
6495 MESH_TunnelNumber tid;
6497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6498 /* Sanity check for client registration */
6499 if (NULL == (c = client_get (client)))
6502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6507 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6509 /* Sanity check for message size */
6510 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6511 ntohs (connect_msg->header.size))
6514 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6518 /* Tunnel exists? */
6519 tid = ntohl (connect_msg->tunnel_id);
6520 t = tunnel_get_by_local_id (c, tid);
6524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6528 /* Does client own tunnel? */
6529 if (t->owner->handle != client)
6532 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6536 /* Do WE have the service? */
6537 t->type = ntohl (connect_msg->type);
6538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6539 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6540 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6543 /* Yes! Fast forward, add ourselves to the tunnel and send the
6544 * good news to the client, and alert the destination client of
6545 * an incoming tunnel.
6547 * FIXME send a path create to self, avoid code duplication
6549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6550 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6551 peer_info_get (&my_full_id),
6552 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6555 send_client_peer_connected (t, myid);
6556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6557 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6559 t->local_tid_dest = next_local_tid++;
6560 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6561 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6562 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6566 /* Ok, lets find a peer offering the service */
6567 if (NULL != t->dht_get_type)
6569 GNUNET_DHT_get_stop (t->dht_get_type);
6571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6572 GNUNET_h2s (&hash));
6574 GNUNET_DHT_get_start (dht_handle,
6575 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6577 dht_replication_level,
6578 GNUNET_DHT_RO_RECORD_ROUTE |
6579 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6581 &dht_get_type_handler, t);
6583 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6589 * Handler for connection requests to new peers by a string service description.
6591 * @param cls closure
6592 * @param client identification of the client
6593 * @param message the actual message, which includes messages the client wants
6596 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6597 const struct GNUNET_MessageHeader *message)
6599 struct GNUNET_MESH_ConnectPeerByString *msg;
6600 struct MeshRegexSearchContext *ctx;
6601 struct MeshRegexSearchInfo *info;
6602 struct GNUNET_DHT_GetHandle *get_h;
6603 struct GNUNET_HashCode key;
6604 struct MeshTunnel *t;
6605 struct MeshClient *c;
6606 MESH_TunnelNumber tid;
6610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6611 "Connect by string started\n");
6612 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6613 size = htons (message->size);
6615 /* Sanity check for client registration */
6616 if (NULL == (c = client_get (client)))
6619 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6624 /* Message size sanity check */
6625 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6628 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6632 /* Tunnel exists? */
6633 tid = ntohl (msg->tunnel_id);
6634 t = tunnel_get_by_local_id (c, tid);
6638 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6642 /* Does client own tunnel? */
6643 if (t->owner->handle != client)
6646 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6651 " on tunnel %s [%u]\n",
6652 GNUNET_i2s(&my_full_id),
6655 /* Only one connect_by_string allowed at the same time! */
6656 /* FIXME: allow more, return handle at api level to cancel, document */
6657 if (NULL != t->regex_ctx)
6660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6664 /* Find string itself */
6665 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6666 string = (const char *) &msg[1];
6668 /* Initialize context */
6669 size = GNUNET_REGEX_get_first_key(string, len, &key);
6670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6671 " consumed %u bits out of %u\n", size, len);
6672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6673 " looking for %s\n", GNUNET_h2s (&key));
6675 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6677 info->description = GNUNET_malloc (len + 1);
6678 memcpy (info->description, string, len);
6679 info->description[len] = '\0';
6680 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6681 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6684 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6685 ctx->position = size;
6689 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6691 /* Start search in DHT */
6692 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6693 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6694 &key, /* key to search */
6695 dht_replication_level, /* replication level */
6696 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6697 NULL, /* xquery */ // FIXME BLOOMFILTER
6698 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6699 &dht_get_string_handler, ctx);
6701 GNUNET_break (GNUNET_OK ==
6702 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6705 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6707 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6713 * Handler for client traffic directed to one peer
6715 * @param cls closure
6716 * @param client identification of the client
6717 * @param message the actual message
6720 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6721 const struct GNUNET_MessageHeader *message)
6723 struct MeshClient *c;
6724 struct MeshTunnel *t;
6725 struct MeshPeerInfo *pi;
6726 struct GNUNET_MESH_Unicast *data_msg;
6727 MESH_TunnelNumber tid;
6730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6731 "Got a unicast request from a client!\n");
6733 /* Sanity check for client registration */
6734 if (NULL == (c = client_get (client)))
6737 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6742 data_msg = (struct GNUNET_MESH_Unicast *) message;
6744 /* Sanity check for message size */
6745 size = ntohs (message->size);
6746 if (sizeof (struct GNUNET_MESH_Unicast) +
6747 sizeof (struct GNUNET_MessageHeader) > size)
6750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6754 /* Tunnel exists? */
6755 tid = ntohl (data_msg->tid);
6756 t = tunnel_get_by_local_id (c, tid);
6760 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6764 /* Is it a local tunnel? Then, does client own the tunnel? */
6765 if (t->owner->handle != client)
6768 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6772 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6773 &data_msg->destination.hashPubKey);
6774 /* Is the selected peer in the tunnel? */
6778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6782 /* PID should be as expected */
6783 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6786 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6787 "Unicast PID, expected %u, got %u\n",
6788 t->fwd_pid + 1, ntohl (data_msg->pid));
6789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6793 /* Ok, everything is correct, send the message
6794 * (pretend we got it from a mesh peer)
6797 /* Work around const limitation */
6798 char buf[ntohs (message->size)] GNUNET_ALIGN;
6799 struct GNUNET_MESH_Unicast *copy;
6801 copy = (struct GNUNET_MESH_Unicast *) buf;
6802 memcpy (buf, data_msg, size);
6803 copy->oid = my_full_id;
6804 copy->tid = htonl (t->id.tid);
6805 copy->ttl = htonl (default_ttl);
6806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6807 " calling generic handler...\n");
6808 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6811 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6817 * Handler for client traffic directed to the origin
6819 * @param cls closure
6820 * @param client identification of the client
6821 * @param message the actual message
6824 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6825 const struct GNUNET_MessageHeader *message)
6827 struct GNUNET_MESH_ToOrigin *data_msg;
6828 struct MeshTunnelClientInfo *clinfo;
6829 struct MeshClient *c;
6830 struct MeshTunnel *t;
6831 MESH_TunnelNumber tid;
6834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6835 "Got a ToOrigin request from a client!\n");
6836 /* Sanity check for client registration */
6837 if (NULL == (c = client_get (client)))
6840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6845 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6847 /* Sanity check for message size */
6848 size = ntohs (message->size);
6849 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6850 sizeof (struct GNUNET_MessageHeader) > size)
6853 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6857 /* Tunnel exists? */
6858 tid = ntohl (data_msg->tid);
6859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6860 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6863 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6866 t = tunnel_get_by_local_id (c, tid);
6870 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6874 /* It should be sent by someone who has this as incoming tunnel. */
6875 if (GNUNET_NO == client_knows_tunnel (c, t))
6878 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6882 /* PID should be as expected */
6883 clinfo = tunnel_get_client_fc (t, c);
6884 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
6887 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6888 "To Origin PID, expected %u, got %u\n",
6889 clinfo->bck_pid + 1,
6890 ntohl (data_msg->pid));
6891 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6895 /* Ok, everything is correct, send the message
6896 * (pretend we got it from a mesh peer)
6900 char buf[ntohs (message->size)] GNUNET_ALIGN;
6901 struct GNUNET_MESH_ToOrigin *copy;
6903 /* Work around const limitation */
6904 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6905 memcpy (buf, data_msg, size);
6906 GNUNET_PEER_resolve (t->id.oid, ©->oid);
6907 copy->tid = htonl (t->id.tid);
6908 copy->ttl = htonl (default_ttl);
6909 if (ntohl (copy->pid) != (t->bck_pid + 1))
6912 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6913 "To Origin PID, expected %u, got %u\n",
6919 copy->sender = my_full_id;
6920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6921 " calling generic handler...\n");
6922 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6924 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6930 * Handler for client traffic directed to all peers in a tunnel
6932 * @param cls closure
6933 * @param client identification of the client
6934 * @param message the actual message
6937 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6938 const struct GNUNET_MessageHeader *message)
6940 struct MeshClient *c;
6941 struct MeshTunnel *t;
6942 struct GNUNET_MESH_Multicast *data_msg;
6943 MESH_TunnelNumber tid;
6945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6946 "Got a multicast request from a client!\n");
6948 /* Sanity check for client registration */
6949 if (NULL == (c = client_get (client)))
6952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6957 data_msg = (struct GNUNET_MESH_Multicast *) message;
6959 /* Sanity check for message size */
6960 if (sizeof (struct GNUNET_MESH_Multicast) +
6961 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6968 /* Tunnel exists? */
6969 tid = ntohl (data_msg->tid);
6970 t = tunnel_get_by_local_id (c, tid);
6974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6978 /* Does client own tunnel? */
6979 if (t->owner->handle != client)
6982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6986 /* PID should be as expected */
6987 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6990 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6991 "Multicast PID, expected %u, got %u\n",
6992 t->fwd_pid + 1, ntohl (data_msg->pid));
6993 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6998 char buf[ntohs (message->size)] GNUNET_ALIGN;
6999 struct GNUNET_MESH_Multicast *copy;
7001 copy = (struct GNUNET_MESH_Multicast *) buf;
7002 memcpy (buf, message, ntohs (message->size));
7003 copy->oid = my_full_id;
7004 copy->tid = htonl (t->id.tid);
7005 copy->ttl = htonl (default_ttl);
7006 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7008 " calling generic handler...\n");
7009 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7012 /* receive done gets called when last copy is sent to a neighbor */
7018 * Handler for client's ACKs for payload traffic.
7020 * @param cls Closure (unused).
7021 * @param client Identification of the client.
7022 * @param message The actual message.
7025 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7026 const struct GNUNET_MessageHeader *message)
7028 struct GNUNET_MESH_LocalAck *msg;
7029 struct MeshTunnel *t;
7030 struct MeshClient *c;
7031 MESH_TunnelNumber tid;
7034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7035 /* Sanity check for client registration */
7036 if (NULL == (c = client_get (client)))
7039 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7044 msg = (struct GNUNET_MESH_LocalAck *) message;
7046 /* Tunnel exists? */
7047 tid = ntohl (msg->tunnel_id);
7048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7049 t = tunnel_get_by_local_id (c, tid);
7052 GNUNET_break (0); // FIXME fc
7053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7057 ack = ntohl (msg->max_pid);
7058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7060 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7061 if (NULL != t->owner && t->owner->handle == client)
7063 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7065 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7069 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7070 tunnel_set_client_fwd_ack (t, c, ack);
7071 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7074 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7080 * Functions to handle messages from clients
7082 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7083 {&handle_local_new_client, NULL,
7084 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7085 {&handle_local_announce_regex, NULL,
7086 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7087 {&handle_local_tunnel_create, NULL,
7088 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7089 sizeof (struct GNUNET_MESH_TunnelMessage)},
7090 {&handle_local_tunnel_destroy, NULL,
7091 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7092 sizeof (struct GNUNET_MESH_TunnelMessage)},
7093 {&handle_local_tunnel_speed, NULL,
7094 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7095 sizeof (struct GNUNET_MESH_TunnelMessage)},
7096 {&handle_local_tunnel_speed, NULL,
7097 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7098 sizeof (struct GNUNET_MESH_TunnelMessage)},
7099 {&handle_local_tunnel_buffer, NULL,
7100 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7101 sizeof (struct GNUNET_MESH_TunnelMessage)},
7102 {&handle_local_tunnel_buffer, NULL,
7103 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7104 sizeof (struct GNUNET_MESH_TunnelMessage)},
7105 {&handle_local_connect_add, NULL,
7106 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7107 sizeof (struct GNUNET_MESH_PeerControl)},
7108 {&handle_local_connect_del, NULL,
7109 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7110 sizeof (struct GNUNET_MESH_PeerControl)},
7111 {&handle_local_blacklist, NULL,
7112 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7113 sizeof (struct GNUNET_MESH_PeerControl)},
7114 {&handle_local_unblacklist, NULL,
7115 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7116 sizeof (struct GNUNET_MESH_PeerControl)},
7117 {&handle_local_connect_by_type, NULL,
7118 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7119 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7120 {&handle_local_connect_by_string, NULL,
7121 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7122 {&handle_local_unicast, NULL,
7123 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7124 {&handle_local_to_origin, NULL,
7125 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7126 {&handle_local_multicast, NULL,
7127 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7128 {&handle_local_ack, NULL,
7129 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7130 sizeof (struct GNUNET_MESH_LocalAck)},
7136 * To be called on core init/fail.
7138 * @param cls service closure
7139 * @param server handle to the server for this service
7140 * @param identity the public identity of this peer
7143 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7144 const struct GNUNET_PeerIdentity *identity)
7147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7148 core_handle = server;
7149 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7153 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7155 GNUNET_abort(); // Try harder
7162 * Method called whenever a given peer connects.
7164 * @param cls closure
7165 * @param peer peer identity this notification is about
7166 * @param atsi performance data for the connection
7167 * @param atsi_count number of records in 'atsi'
7170 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7171 const struct GNUNET_ATS_Information *atsi,
7172 unsigned int atsi_count)
7174 struct MeshPeerInfo *peer_info;
7175 struct MeshPeerPath *path;
7177 DEBUG_CONN ("Peer connected\n");
7178 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7179 peer_info = peer_info_get (peer);
7180 if (myid == peer_info->id)
7182 DEBUG_CONN (" (self)\n");
7187 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7189 path = path_new (2);
7190 path->peers[0] = myid;
7191 path->peers[1] = peer_info->id;
7192 GNUNET_PEER_change_rc (myid, 1);
7193 GNUNET_PEER_change_rc (peer_info->id, 1);
7194 peer_info_add_path (peer_info, path, GNUNET_YES);
7195 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7201 * Method called whenever a peer disconnects.
7203 * @param cls closure
7204 * @param peer peer identity this notification is about
7207 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7209 struct MeshPeerInfo *pi;
7210 struct MeshPeerQueue *q;
7211 struct MeshPeerQueue *n;
7213 DEBUG_CONN ("Peer disconnected\n");
7214 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7226 /* try to reroute this traffic instead */
7227 queue_destroy(q, GNUNET_YES);
7231 peer_info_remove_path (pi, pi->id, myid);
7234 DEBUG_CONN (" (self)\n");
7236 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7241 /******************************************************************************/
7242 /************************ MAIN FUNCTIONS ****************************/
7243 /******************************************************************************/
7246 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7248 * @param cls closure
7249 * @param key current key code
7250 * @param value value in the hash map
7251 * @return GNUNET_YES if we should continue to iterate,
7255 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7257 struct MeshTunnel *t = value;
7264 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7266 * @param cls closure
7267 * @param key current key code
7268 * @param value value in the hash map
7269 * @return GNUNET_YES if we should continue to iterate,
7273 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7275 struct MeshPeerInfo *p = value;
7276 struct MeshPeerQueue *q;
7277 struct MeshPeerQueue *n;
7285 queue_destroy(q, GNUNET_YES);
7289 peer_info_destroy (p);
7294 * Task run during shutdown.
7300 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7304 if (core_handle != NULL)
7306 GNUNET_CORE_disconnect (core_handle);
7309 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7310 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7311 if (dht_handle != NULL)
7313 GNUNET_DHT_disconnect (dht_handle);
7318 GNUNET_SERVER_notification_context_destroy (nc);
7321 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7323 GNUNET_SCHEDULER_cancel (announce_id_task);
7324 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7330 * Process mesh requests.
7332 * @param cls closure
7333 * @param server the initialized server
7334 * @param c configuration to use
7337 run (void *cls, struct GNUNET_SERVER_Handle *server,
7338 const struct GNUNET_CONFIGURATION_Handle *c)
7340 struct MeshPeerInfo *peer;
7341 struct MeshPeerPath *p;
7344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7345 server_handle = server;
7346 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7347 NULL, /* Closure passed to MESH functions */
7348 &core_init, /* Call core_init once connected */
7349 &core_connect, /* Handle connects */
7350 &core_disconnect, /* remove peers on disconnects */
7351 NULL, /* Don't notify about all incoming messages */
7352 GNUNET_NO, /* For header only in notification */
7353 NULL, /* Don't notify about all outbound messages */
7354 GNUNET_NO, /* For header-only out notification */
7355 core_handlers); /* Register these handlers */
7357 if (core_handle == NULL)
7360 GNUNET_SCHEDULER_shutdown ();
7365 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7370 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7372 GNUNET_SCHEDULER_shutdown ();
7377 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7378 &refresh_path_time))
7380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7382 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7383 "refresh path time");
7384 GNUNET_SCHEDULER_shutdown ();
7389 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7390 &app_announce_time))
7392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7394 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7395 "app announce time");
7396 GNUNET_SCHEDULER_shutdown ();
7401 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7406 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7407 "id announce time");
7408 GNUNET_SCHEDULER_shutdown ();
7413 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7414 &unacknowledged_wait_time))
7416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7418 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7419 "unacknowledged wait time");
7420 GNUNET_SCHEDULER_shutdown ();
7425 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7430 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7432 GNUNET_SCHEDULER_shutdown ();
7437 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7440 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7442 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7444 GNUNET_SCHEDULER_shutdown ();
7449 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7454 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7456 GNUNET_SCHEDULER_shutdown ();
7461 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7464 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7466 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7472 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7473 &dht_replication_level))
7475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7477 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7478 "dht replication level", 10);
7479 dht_replication_level = 10;
7483 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7484 GNUNET_free (keyfile);
7485 if (my_private_key == NULL)
7487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7488 _("Mesh service could not access hostkey. Exiting.\n"));
7489 GNUNET_SCHEDULER_shutdown ();
7492 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7493 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7494 &my_full_id.hashPubKey);
7495 myid = GNUNET_PEER_intern (&my_full_id);
7497 // transport_handle = GNUNET_TRANSPORT_connect(c,
7504 dht_handle = GNUNET_DHT_connect (c, 64);
7505 if (dht_handle == NULL)
7510 stats = GNUNET_STATISTICS_create ("mesh", c);
7514 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7516 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7517 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7518 peers = GNUNET_CONTAINER_multihashmap_create (32);
7519 applications = GNUNET_CONTAINER_multihashmap_create (32);
7520 types = GNUNET_CONTAINER_multihashmap_create (32);
7522 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7523 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7524 GNUNET_SERVER_disconnect_notify (server_handle,
7525 &handle_local_client_disconnect, NULL);
7529 clients_tail = NULL;
7532 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7533 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7535 /* Create a peer_info for the local peer */
7536 peer = peer_info_get (&my_full_id);
7539 GNUNET_PEER_change_rc (myid, 1);
7540 peer_info_add_path (peer, p, GNUNET_YES);
7542 /* Scheduled the task to clean up when shutdown is called */
7543 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7550 * The main function for the mesh service.
7552 * @param argc number of arguments from the command line
7553 * @param argv command line arguments
7554 * @return 0 ok, 1 on error
7557 main (int argc, char *const *argv)
7561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7564 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");