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 * Last ACK sent towards the next hop (for traffic towards root).
329 uint32_t last_bck_ack;
332 * BCK ACK value received from the hop towards the owner of the tunnel,
333 * (previous node / owner): up to what message PID can we sent back to him.
338 * How many messages are in the forward queue (towards leaves).
340 unsigned int fwd_queue_n;
343 * How many messages do we accept in the forward queue.
345 unsigned int fwd_queue_max;
348 * How many messages are in the backward queue (towards origin).
350 unsigned int bck_queue_n;
353 * How many messages do we accept in the backward queue.
355 unsigned int bck_queue_max;
358 * Last time the tunnel was used
360 struct GNUNET_TIME_Absolute timestamp;
363 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
364 * containing peers added by id or by type, not intermediate peers.
366 struct GNUNET_CONTAINER_MultiHashMap *peers;
369 * Number of peers that are connected and potentially ready to receive data
371 unsigned int peers_ready;
374 * Number of peers that have been added to the tunnel
376 unsigned int peers_total;
379 * Client owner of the tunnel, if any
381 struct MeshClient *owner;
384 * Clients that have been informed about and want to stay in the tunnel.
386 struct MeshClient **clients;
389 * Flow control info for each client.
391 struct MeshTunnelClientInfo *clients_fc;
394 * Number of elements in clients/clients_fc
396 unsigned int nclients;
399 * Clients that have been informed but requested to leave the tunnel.
401 struct MeshClient **ignore;
404 * Number of elements in clients
406 unsigned int nignore;
411 GNUNET_PEER_Id *blacklisted;
414 * Number of elements in blacklisted
416 unsigned int nblacklisted;
419 * Bloomfilter (for peer identities) to stop circular routes
421 char bloomfilter[MESH_BLOOM_SIZE];
426 struct MeshTunnelTree *tree;
429 * Application type we are looking for in this tunnel
431 GNUNET_MESH_ApplicationType type;
434 * Used to search peers offering a service
436 struct GNUNET_DHT_GetHandle *dht_get_type;
439 * Initial context of the regex search for a connect_by_string
441 struct MeshRegexSearchContext *regex_ctx;
444 * Task to keep the used paths alive
446 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
449 * Task to destroy the tunnel after timeout
451 * FIXME: merge the two? a tunnel will have either
452 * a path refresh OR a timeout, never both!
454 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
457 * Flag to signal the destruction of the tunnel.
458 * If this is set GNUNET_YES the tunnel will be destroyed
459 * when the queue is empty.
466 * Info about a child node in a tunnel, needed to perform flow control.
468 struct MeshTunnelChildInfo
471 * ID of the child node.
486 * Maximum PID allowed (FWD ACK received).
491 * Last ACK sent to that child (BCK ACK).
498 * Info about a leaf client of a tunnel, needed to perform flow control.
500 struct MeshTunnelClientInfo
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 * Maximum child ACK so far.
534 uint32_t max_child_ack;
537 * Number of children nodes
539 unsigned int nchildren;
544 * Info needed to work with tunnel paths and peers
551 struct MeshTunnel *t;
554 * Neighbouring peer to whom we send the packet to
556 struct MeshPeerInfo *peer;
561 struct MeshPeerPath *path;
566 * Struct containing information about a client of the service
573 struct MeshClient *next;
578 struct MeshClient *prev;
581 * Tunnels that belong to this client, indexed by local id
583 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
586 * Tunnels this client has accepted, indexed by incoming local id
588 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
591 * Tunnels this client has rejected, indexed by incoming local id
593 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
595 * Handle to communicate with the client
597 struct GNUNET_SERVER_Client *handle;
600 * Applications that this client has claimed to provide
602 struct GNUNET_CONTAINER_MultiHashMap *apps;
605 * Messages that this client has declared interest in
607 struct GNUNET_CONTAINER_MultiHashMap *types;
610 * Whether the client is active or shutting down (don't send confirmations
611 * to a client that is shutting down.
616 * ID of the client, mainly for debug messages
621 * Regular expressions describing the services offered by this client.
623 char **regexes; // FIXME add timeout? API to remove a regex?
626 * Number of regular expressions in regexes.
628 unsigned int n_regex;
631 * Task to refresh all regular expresions in the DHT.
633 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
639 * Struct to keep information of searches of services described by a regex
640 * using a user-provided string service description.
642 struct MeshRegexSearchInfo
645 * Which tunnel is this for
647 struct MeshTunnel *t;
650 * User provided description of the searched service.
655 * Part of the description already consumed by the search.
662 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
665 * Results from running DHT GETs.
667 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
670 * Contexts, for each running DHT GET. Free all on end of search.
672 struct MeshRegexSearchContext **contexts;
675 * Number of contexts (branches/steps in search).
677 unsigned int n_contexts;
680 * Peer that is connecting via connect_by_string. When connected, free ctx.
685 * Other peers that are found but not yet being connected to.
687 GNUNET_PEER_Id *peers;
690 * Number of elements in peers.
692 unsigned int n_peers;
695 * Next peer to try to connect to.
700 * Timeout for a connect attempt.
701 * When reached, try to connect to a different peer, if any. If not,
702 * try the same peer again.
704 GNUNET_SCHEDULER_TaskIdentifier timeout;
709 * Struct to keep state of running searches that have consumed a part of
712 struct MeshRegexSearchContext
715 * Part of the description already consumed by
716 * this particular search branch.
721 * Information about the search.
723 struct MeshRegexSearchInfo *info;
727 /******************************************************************************/
728 /************************ DEBUG FUNCTIONS ****************************/
729 /******************************************************************************/
733 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
734 * @param cls string to print
735 * @param success GNUNET_OK if the PUT was transmitted,
736 * GNUNET_NO on timeout,
737 * GNUNET_SYSERR on disconnect from service
738 * after the PUT message was transmitted
739 * (so we don't know if it was received or not)
744 mesh_debug (void *cls, int success)
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
754 /******************************************************************************/
755 /*********************** GLOBAL VARIABLES ****************************/
756 /******************************************************************************/
760 * Configuration parameters
762 static struct GNUNET_TIME_Relative refresh_path_time;
763 static struct GNUNET_TIME_Relative app_announce_time;
764 static struct GNUNET_TIME_Relative id_announce_time;
765 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
766 static struct GNUNET_TIME_Relative connect_timeout;
767 static long long unsigned int default_ttl;
768 static long long unsigned int dht_replication_level;
769 static long long unsigned int max_tunnels;
770 static long long unsigned int max_msgs_queue;
773 * DLL with all the clients, head.
775 static struct MeshClient *clients;
778 * DLL with all the clients, tail.
780 static struct MeshClient *clients_tail;
783 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
785 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
788 * Number of tunnels known.
790 static unsigned long long n_tunnels;
793 * Tunnels incoming, indexed by MESH_TunnelNumber
794 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
796 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
799 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
801 static struct GNUNET_CONTAINER_MultiHashMap *peers;
804 * Handle to communicate with transport
806 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
809 * Handle to communicate with core.
811 static struct GNUNET_CORE_Handle *core_handle;
816 static struct GNUNET_DHT_Handle *dht_handle;
821 static struct GNUNET_SERVER_Handle *server_handle;
824 * Handle to the statistics service.
826 static struct GNUNET_STATISTICS_Handle *stats;
829 * Notification context, to send messages to local clients.
831 static struct GNUNET_SERVER_NotificationContext *nc;
834 * Local peer own ID (memory efficient handle).
836 static GNUNET_PEER_Id myid;
839 * Local peer own ID (full value).
841 static struct GNUNET_PeerIdentity my_full_id;
846 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
851 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
854 * Tunnel ID for the next created tunnel (global tunnel number).
856 static MESH_TunnelNumber next_tid;
859 * Tunnel ID for the next incoming tunnel (local tunnel number).
861 static MESH_TunnelNumber next_local_tid;
864 * All application types provided by this peer.
866 static struct GNUNET_CONTAINER_MultiHashMap *applications;
869 * All message types clients of this peer are interested in.
871 static struct GNUNET_CONTAINER_MultiHashMap *types;
874 * Task to periodically announce provided applications.
876 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
879 * Task to periodically announce itself in the network.
881 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
884 * Next ID to assign to a client.
886 unsigned int next_client_id;
889 /******************************************************************************/
890 /*********************** DECLARATIONS **************************/
891 /******************************************************************************/
893 /* FIXME move declarations here */
896 * Function to process paths received for a new peer addition. The recorded
897 * paths form the initial tunnel, which can be optimized later.
898 * Called on each result obtained for the DHT search.
901 * @param exp when will this value expire
902 * @param key key of the result
903 * @param type type of the result
904 * @param size number of bytes in data
905 * @param data pointer to the result data
908 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
909 const struct GNUNET_HashCode * key,
910 const struct GNUNET_PeerIdentity *get_path,
911 unsigned int get_path_length,
912 const struct GNUNET_PeerIdentity *put_path,
913 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
914 size_t size, const void *data);
918 * Function to process DHT string to regex matching.
919 * Called on each result obtained for the DHT search.
921 * @param cls closure (search context)
922 * @param exp when will this value expire
923 * @param key key of the result
924 * @param get_path path of the get request (not used)
925 * @param get_path_length lenght of get_path (not used)
926 * @param put_path path of the put request (not used)
927 * @param put_path_length length of the put_path (not used)
928 * @param type type of the result
929 * @param size number of bytes in data
930 * @param data pointer to the result data
932 * TODO: re-issue the request after certain time? cancel after X results?
935 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
936 const struct GNUNET_HashCode * key,
937 const struct GNUNET_PeerIdentity *get_path,
938 unsigned int get_path_length,
939 const struct GNUNET_PeerIdentity *put_path,
940 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
941 size_t size, const void *data);
945 * Function to process DHT string to regex matching.
946 * Called on each result obtained for the DHT search.
948 * @param cls closure (search context)
949 * @param exp when will this value expire
950 * @param key key of the result
951 * @param get_path path of the get request (not used)
952 * @param get_path_length lenght of get_path (not used)
953 * @param put_path path of the put request (not used)
954 * @param put_path_length length of the put_path (not used)
955 * @param type type of the result
956 * @param size number of bytes in data
957 * @param data pointer to the result data
960 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
961 const struct GNUNET_HashCode * key,
962 const struct GNUNET_PeerIdentity *get_path,
963 unsigned int get_path_length,
964 const struct GNUNET_PeerIdentity *put_path,
965 unsigned int put_path_length,
966 enum GNUNET_BLOCK_Type type,
967 size_t size, const void *data);
971 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
972 * and insert it in the appropiate structures if the peer is not known yet.
974 * @param peer Short identity of the peer.
976 * @return Existing or newly created peer info.
978 static struct MeshPeerInfo *
979 peer_info_get_short (const GNUNET_PEER_Id peer);
983 * Try to establish a new connection to this peer.
984 * Use the best path for the given tunnel.
985 * If the peer doesn't have any path to it yet, try to get one.
986 * If the peer already has some path, send a CREATE PATH towards it.
988 * @param peer PeerInfo of the peer.
989 * @param t Tunnel for which to create the path, if possible.
992 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
996 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
998 * If peer already exists, reevaluate shortest path and change if different.
1000 * @param t Tunnel we want to add a new peer to
1001 * @param peer PeerInfo of the peer being added
1005 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1009 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1010 * that are no longer needed, as well as nodes of no longer reachable peers.
1011 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1013 * @param t Tunnel from which to remove the path.
1014 * @param peer Short id of the peer which should be removed.
1017 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1021 * Search for a tunnel by global ID using full PeerIdentities.
1023 * @param oid owner of the tunnel.
1024 * @param tid global tunnel number.
1026 * @return tunnel handler, NULL if doesn't exist.
1028 static struct MeshTunnel *
1029 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1033 * Delete an active client from the tunnel.
1039 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1042 * Notify a tunnel that a connection has broken that affects at least
1043 * some of its peers.
1045 * @param t Tunnel affected.
1046 * @param p1 Peer that got disconnected from p2.
1047 * @param p2 Peer that got disconnected from p1.
1049 * @return Short ID of the peer disconnected (either p1 or p2).
1050 * 0 if the tunnel remained unaffected.
1052 static GNUNET_PEER_Id
1053 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1058 * Get the current ack value for a tunnel, for data going from root to leaves,
1059 * taking in account the tunnel mode and the status of all children and clients.
1063 * @return Maximum PID allowed.
1066 tunnel_get_fwd_ack (struct MeshTunnel *t);
1069 * Get the current ack value for a tunnel, for data going from leaves to root,
1070 * taking in account the tunnel mode and the status of all children and clients.
1074 * @return Maximum PID allowed.
1077 tunnel_get_bck_ack (struct MeshTunnel *t);
1080 * Add a client to a tunnel, initializing all needed data structures.
1082 * @param t Tunnel to which add the client.
1083 * @param c Client which to add to the tunnel.
1086 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1090 * Iterator over edges in a regex block retrieved from the DHT.
1092 * @param cls Closure.
1093 * @param token Token that follows to next state.
1094 * @param len Lenght of token.
1095 * @param key Hash of next state.
1097 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1100 regex_edge_iterator (void *cls,
1103 const struct GNUNET_HashCode *key);
1107 * Find a path to a peer that offers a regex servcie compatible
1108 * with a given string.
1110 * @param key The key of the accepting state.
1111 * @param ctx Context containing info about the string, tunnel, etc.
1114 regex_find_path (const struct GNUNET_HashCode *key,
1115 struct MeshRegexSearchContext *ctx);
1119 * Queue and pass message to core when possible.
1121 * @param cls Closure (type dependant).
1122 * @param type Type of the message, 0 for a raw message.
1123 * @param size Size of the message.
1124 * @param dst Neighbor to send message to.
1125 * @param t Tunnel this message belongs to.
1128 queue_add (void *cls, uint16_t type, size_t size,
1129 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1132 * Free a transmission that was already queued with all resources
1133 * associated to the request.
1135 * @param queue Queue handler to cancel.
1136 * @param clear_cls Is it necessary to free associated cls?
1139 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1141 /******************************************************************************/
1142 /************************ ITERATORS ****************************/
1143 /******************************************************************************/
1146 * Iterator over found existing mesh regex blocks that match an ongoing search.
1148 * @param cls closure
1149 * @param key current key code
1150 * @param value value in the hash map
1151 * @return GNUNET_YES if we should continue to iterate,
1155 regex_result_iterator (void *cls,
1156 const struct GNUNET_HashCode * key,
1159 struct MeshRegexBlock *block = value;
1160 struct MeshRegexSearchContext *ctx = cls;
1162 if (GNUNET_YES == ntohl(block->accepting) &&
1163 ctx->position == strlen (ctx->info->description))
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1166 regex_find_path (key, ctx);
1167 return GNUNET_YES; // We found an accept state!
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1172 ctx->position, strlen(ctx->info->description),
1173 ntohl(block->accepting));
1176 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1177 ®ex_edge_iterator, ctx);
1184 * Iterator over edges in a regex block retrieved from the DHT.
1186 * @param cls Closure (context of the search).
1187 * @param token Token that follows to next state.
1188 * @param len Lenght of token.
1189 * @param key Hash of next state.
1191 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1194 regex_edge_iterator (void *cls,
1197 const struct GNUNET_HashCode *key)
1199 struct MeshRegexSearchContext *ctx = cls;
1200 struct MeshRegexSearchContext *new_ctx;
1201 struct MeshRegexSearchInfo *info = ctx->info;
1202 struct GNUNET_DHT_GetHandle *get_h;
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1209 current = &info->description[ctx->position];
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1211 current_len = strlen (info->description) - ctx->position;
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1216 if (len > current_len)
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1219 return GNUNET_YES; // Token too long, wont match
1221 if (0 != strncmp (current, token, len))
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1224 return GNUNET_YES; // Token doesn't match
1226 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1227 new_ctx->info = info;
1228 new_ctx->position = ctx->position + len;
1229 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1231 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1234 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1235 ®ex_result_iterator,
1237 return GNUNET_YES; // We are already looking for it
1239 /* Start search in DHT */
1241 GNUNET_DHT_get_start (dht_handle, /* handle */
1242 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1243 key, /* key to search */
1244 dht_replication_level, /* replication level */
1245 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1246 NULL, /* xquery */ // FIXME BLOOMFILTER
1247 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1248 &dht_get_string_handler, new_ctx);
1250 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1251 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1261 * Iterator over hash map entries to cancel DHT GET requests after a
1262 * successful connect_by_string.
1264 * @param cls Closure (unused).
1265 * @param key Current key code (unused).
1266 * @param value Value in the hash map (get handle).
1267 * @return GNUNET_YES if we should continue to iterate,
1271 regex_cancel_dht_get (void *cls,
1272 const struct GNUNET_HashCode * key,
1275 struct GNUNET_DHT_GetHandle *h = value;
1277 GNUNET_DHT_get_stop (h);
1283 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1284 * search for connect_by_string.
1286 * @param cls Closure (unused).
1287 * @param key Current key code (unused).
1288 * @param value MeshRegexBlock in the hash map.
1289 * @return GNUNET_YES if we should continue to iterate,
1293 regex_free_result (void *cls,
1294 const struct GNUNET_HashCode * key,
1298 GNUNET_free (value);
1304 * Regex callback iterator to store own service description in the DHT.
1306 * @param cls closure.
1307 * @param key hash for current state.
1308 * @param proof proof for current state.
1309 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1310 * @param num_edges number of edges leaving current state.
1311 * @param edges edges leaving current state.
1314 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1315 int accepting, unsigned int num_edges,
1316 const struct GNUNET_REGEX_Edge *edges)
1318 struct MeshRegexBlock *block;
1319 struct MeshRegexEdge *block_edge;
1320 enum GNUNET_DHT_RouteOption opt;
1324 unsigned int offset;
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1328 " regex dht put for state %s\n",
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1337 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1338 if (GNUNET_YES == accepting)
1340 struct MeshRegexAccept block;
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1343 " state %s is accepting, putting own id\n",
1345 size = sizeof (block);
1347 block.id = my_full_id;
1349 GNUNET_DHT_put(dht_handle, key,
1350 dht_replication_level,
1351 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1352 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1355 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1360 len = strlen(proof);
1361 size = sizeof (struct MeshRegexBlock) + len;
1362 block = GNUNET_malloc (size);
1365 block->n_proof = htonl (len);
1366 block->n_edges = htonl (num_edges);
1367 block->accepting = htonl (accepting);
1369 /* Store the proof at the end of the block. */
1370 aux = (char *) &block[1];
1371 memcpy (aux, proof, len);
1374 /* Store each edge in a variable length MeshEdge struct at the
1375 * very end of the MeshRegexBlock structure.
1377 for (i = 0; i < num_edges; i++)
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 " edge %s towards %s\n",
1382 GNUNET_h2s(&edges[i].destination));
1384 /* aux points at the end of the last block */
1385 len = strlen (edges[i].label);
1386 size += sizeof (struct MeshRegexEdge) + len;
1387 // Calculate offset FIXME is this ok? use size instead?
1388 offset = aux - (char *) block;
1389 block = GNUNET_realloc (block, size);
1390 aux = &((char *) block)[offset];
1391 block_edge = (struct MeshRegexEdge *) aux;
1392 block_edge->key = edges[i].destination;
1393 block_edge->n_token = htonl (len);
1394 aux = (char *) &block_edge[1];
1395 memcpy (aux, edges[i].label, len);
1399 GNUNET_DHT_put(dht_handle, key,
1400 dht_replication_level,
1402 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1404 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1408 GNUNET_free (block);
1413 * Store the regular expression describing a local service into the DHT.
1415 * @param regex The regular expresion.
1418 regex_put (const char *regex)
1420 struct GNUNET_REGEX_Automaton *dfa;
1422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1423 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1424 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1425 GNUNET_REGEX_automaton_destroy (dfa);
1426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1431 * Find a path to a peer that offers a regex servcie compatible
1432 * with a given string.
1434 * @param key The key of the accepting state.
1435 * @param ctx Context containing info about the string, tunnel, etc.
1438 regex_find_path (const struct GNUNET_HashCode *key,
1439 struct MeshRegexSearchContext *ctx)
1441 struct GNUNET_DHT_GetHandle *get_h;
1443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1444 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1445 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1446 key, /* key to search */
1447 dht_replication_level, /* replication level */
1448 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1449 GNUNET_DHT_RO_RECORD_ROUTE,
1450 NULL, /* xquery */ // FIXME BLOOMFILTER
1451 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1452 &dht_get_string_accept_handler, ctx);
1453 GNUNET_break (GNUNET_OK ==
1454 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1462 * Function called if the connect attempt to a peer found via
1463 * connect_by_string times out. Try to connect to another peer, if any.
1464 * Otherwise try to reconnect to the same peer.
1466 * @param cls Closure (info about regex search).
1467 * @param tc TaskContext.
1470 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1472 struct MeshRegexSearchInfo *info = cls;
1473 struct MeshPeerInfo *peer_info;
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1478 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1479 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1487 if (0 < info->n_peers)
1489 // Select next peer, put current in that spot.
1490 id = info->peers[info->i_peer];
1491 info->peers[info->i_peer] = info->peer;
1492 info->i_peer = (info->i_peer + 1) % info->n_peers;
1496 // Try to connect to same peer again.
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1501 peer_info = peer_info_get_short(id);
1502 tunnel_add_peer (info->t, peer_info);
1504 tunnel_delete_peer (info->t, old);
1505 peer_info_connect (peer_info, info->t);
1506 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1507 ®ex_connect_timeout,
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1514 * Cancel an ongoing regex search in the DHT and free all resources.
1516 * @param ctx The search context.
1519 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1521 struct MeshRegexSearchInfo *info = ctx->info;
1524 GNUNET_free (info->description);
1525 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1526 ®ex_cancel_dht_get, NULL);
1527 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1528 ®ex_free_result, NULL);
1529 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1530 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1531 info->t->regex_ctx = NULL;
1532 for (i = 0; i < info->n_contexts; i++)
1534 GNUNET_free (info->contexts[i]);
1536 if (0 < info->n_contexts)
1537 GNUNET_free (info->contexts);
1538 if (0 < info->n_peers)
1539 GNUNET_free (info->peers);
1540 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1542 GNUNET_SCHEDULER_cancel(info->timeout);
1548 /******************************************************************************/
1549 /************************ PERIODIC FUNCTIONS ****************************/
1550 /******************************************************************************/
1553 * Announce iterator over for each application provided by the peer
1555 * @param cls closure
1556 * @param key current key code
1557 * @param value value in the hash map
1558 * @return GNUNET_YES if we should continue to
1563 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1565 struct PBlock block;
1566 struct MeshClient *c;
1568 block.id = my_full_id;
1569 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1570 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1571 if (0 == block.type)
1576 block.type = htonl (block.type);
1578 GNUNET_break (NULL !=
1579 GNUNET_DHT_put (dht_handle, key,
1580 dht_replication_level,
1581 GNUNET_DHT_RO_RECORD_ROUTE |
1582 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1583 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1585 (const char *) &block,
1586 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1588 app_announce_time, NULL, NULL));
1594 * Periodically announce what applications are provided by local clients
1597 * @param cls closure
1598 * @param tc task context
1601 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1603 struct MeshClient *c = cls;
1606 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1608 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1612 DEBUG_DHT ("Starting PUT for regex\n");
1614 for (i = 0; i < c->n_regex; i++)
1616 regex_put (c->regexes[i]);
1618 c->regex_announce_task =
1619 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1620 DEBUG_DHT ("Finished PUT for regex\n");
1627 * Periodically announce what applications are provided by local clients
1630 * @param cls closure
1631 * @param tc task context
1634 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1636 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1638 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1642 DEBUG_DHT ("Starting PUT for apps\n");
1644 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1646 announce_applications_task =
1647 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1649 DEBUG_DHT ("Finished PUT for apps\n");
1656 * Periodically announce self id in the DHT
1658 * @param cls closure
1659 * @param tc task context
1662 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1664 struct PBlock block;
1666 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1668 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1672 * - Set data expiration in function of X
1673 * - Adapt X to churn
1675 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1677 block.id = my_full_id;
1678 block.type = htonl (0);
1679 GNUNET_DHT_put (dht_handle, /* DHT handle */
1680 &my_full_id.hashPubKey, /* Key to use */
1681 dht_replication_level, /* Replication level */
1682 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1683 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1684 sizeof (block), /* Size of the data */
1685 (const char *) &block, /* Data itself */
1686 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1687 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1688 NULL, /* Continuation */
1689 NULL); /* Continuation closure */
1691 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1695 /******************************************************************************/
1696 /****************** GENERAL HELPER FUNCTIONS ************************/
1697 /******************************************************************************/
1700 * Check if one pid is bigger than other, accounting for overflow.
1702 * @param bigger Argument that should be bigger.
1703 * @param smaller Argument that should be smaller.
1705 * @return True if bigger (arg1) has a higher value than smaller (arg 2).
1708 is_pid_bigger (uint32_t bigger, uint32_t smaller)
1710 return (GNUNET_YES == PID_OVERFLOW(smaller, bigger) ||
1711 (bigger > smaller && GNUNET_NO == PID_OVERFLOW(bigger, smaller)));
1715 * Get the higher ACK value out of two values, taking in account overflow.
1717 * @param a First ACK value.
1718 * @param b Second ACK value.
1720 * @return Highest ACK value from the two.
1723 max_pid (uint32_t a, uint32_t b)
1725 if (is_pid_bigger(a, b))
1732 * Get the lower ACK value out of two values, taking in account overflow.
1734 * @param a First ACK value.
1735 * @param b Second ACK value.
1737 * @return Lowest ACK value from the two.
1740 min_pid (uint32_t a, uint32_t b)
1742 if (is_pid_bigger(a, b))
1749 * Decrements the reference counter and frees all resources if needed
1751 * @param mesh_data Data Descriptor used in a multicast message.
1752 * Freed no longer needed (last message).
1755 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1757 /* Make sure it's a multicast packet */
1758 GNUNET_assert (NULL != mesh_data->reference_counter);
1760 if (0 == --(*(mesh_data->reference_counter)))
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1763 if (NULL != mesh_data->task)
1765 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1767 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1769 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1771 GNUNET_free (mesh_data->task);
1773 GNUNET_free (mesh_data->reference_counter);
1774 GNUNET_free (mesh_data->data);
1775 GNUNET_free (mesh_data);
1781 * Check if client has registered with the service and has not disconnected
1783 * @param client the client to check
1785 * @return non-NULL if client exists in the global DLL
1787 static struct MeshClient *
1788 client_get (struct GNUNET_SERVER_Client *client)
1790 struct MeshClient *c;
1795 if (c->handle == client)
1804 * Checks if a given client has subscribed to certain message type
1806 * @param message_type Type of message to check
1807 * @param c Client to check
1809 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1812 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1814 struct GNUNET_HashCode hc;
1816 if (NULL == c->types)
1818 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1819 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1824 * Allow a client to send more data after transmitting a multicast message
1825 * which some neighbor has not yet accepted altough a reasonable time has
1828 * @param cls Closure (DataDescriptor containing the task identifier)
1829 * @param tc Task Context
1831 * FIXME reference counter cshould be just int
1834 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1836 struct MeshData *mdata = cls;
1838 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1840 GNUNET_assert (NULL != mdata->reference_counter);
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1843 *(mdata->reference_counter));
1844 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1845 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1850 * Check whether client wants traffic from a tunnel.
1852 * @param c Client to check.
1853 * @param t Tunnel to be found.
1855 * @return GNUNET_YES if client knows tunnel.
1857 * TODO look in client hashmap
1860 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1864 for (i = 0; i < t->nclients; i++)
1865 if (t->clients[i] == c)
1872 * Check whether client has been informed about a tunnel.
1874 * @param c Client to check.
1875 * @param t Tunnel to be found.
1877 * @return GNUNET_YES if client knows tunnel.
1879 * TODO look in client hashmap
1882 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1886 for (i = 0; i < t->nignore; i++)
1887 if (t->ignore[i] == c)
1889 return client_wants_tunnel(c, t);
1894 * Marks a client as uninterested in traffic from the tunnel, updating both
1895 * client and tunnel to reflect this.
1897 * @param c Client that doesn't want traffic anymore.
1898 * @param t Tunnel which should be ignored.
1900 * FIXME when to delete an incoming tunnel?
1903 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1905 struct GNUNET_HashCode hash;
1907 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1908 GNUNET_break (GNUNET_YES ==
1909 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1911 GNUNET_break (GNUNET_YES ==
1912 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1913 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1914 tunnel_delete_active_client (t, c);
1915 GNUNET_array_append (t->ignore, t->nignore, c);
1920 * Deletes a tunnel from a client (either owner or destination). To be used on
1921 * tunnel destroy, otherwise, use client_ignore_tunnel.
1923 * @param c Client whose tunnel to delete.
1924 * @param t Tunnel which should be deleted.
1927 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1929 struct GNUNET_HashCode hash;
1933 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1934 GNUNET_assert (GNUNET_YES ==
1935 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1941 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1943 GNUNET_assert (GNUNET_YES ==
1944 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1948 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1956 * Send the message to all clients that have subscribed to its type
1958 * @param msg Pointer to the message itself
1959 * @param payload Pointer to the payload of the message.
1960 * @return number of clients this message was sent to
1963 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1964 const struct GNUNET_MessageHeader *payload)
1966 struct GNUNET_PeerIdentity *oid;
1967 struct MeshClient *c;
1968 struct MeshTunnel *t;
1969 MESH_TunnelNumber *tid;
1972 char cbuf[htons (msg->size)];
1974 type = ntohs (payload->type);
1975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1978 memcpy (cbuf, msg, sizeof (cbuf));
1979 switch (htons (msg->type))
1981 struct GNUNET_MESH_Unicast *uc;
1982 struct GNUNET_MESH_Multicast *mc;
1983 struct GNUNET_MESH_ToOrigin *to;
1985 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1986 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1990 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1991 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1995 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1996 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2004 t = tunnel_get (oid, ntohl (*tid));
2011 for (count = 0, c = clients; c != NULL; c = c->next)
2013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2014 if (client_is_subscribed (type, c))
2016 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2020 *tid = htonl (t->local_tid);
2024 if (GNUNET_NO == client_knows_tunnel (c, t))
2026 /* This client doesn't know the tunnel */
2027 struct GNUNET_MESH_TunnelNotification tmsg;
2028 struct GNUNET_HashCode hash;
2030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2031 tmsg.header.size = htons (sizeof (tmsg));
2032 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2033 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2034 tmsg.tunnel_id = htonl (t->local_tid_dest);
2035 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2036 &tmsg.header, GNUNET_NO);
2037 tunnel_add_client (t, c);
2038 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2040 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2041 c->incoming_tunnels, &hash, t,
2042 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2044 *tid = htonl (t->local_tid_dest);
2047 /* Check if the client wants to get traffic from the tunnel */
2048 if (GNUNET_NO == client_wants_tunnel(c, t))
2051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2052 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2053 (struct GNUNET_MessageHeader
2054 *) cbuf, GNUNET_NO);
2062 * Notify the client that owns the tunnel that a peer has connected to it
2063 * (the requested path to it has been confirmed).
2065 * @param t Tunnel whose owner to notify
2066 * @param id Short id of the peer that has connected
2069 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2071 struct GNUNET_MESH_PeerControl pc;
2073 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2074 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2075 pc.tunnel_id = htonl (t->local_tid);
2076 GNUNET_PEER_resolve (id, &pc.peer);
2077 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2083 * Notify a the client of a tunnel about how many more
2084 * payload packages will we accept on a given tunnel,
2085 * distinguishing between root and leaf clients.
2087 * @param c Client whom to send the ACK.
2088 * @param t Tunnel on which to send the ACK.
2091 send_client_tunnel_ack (struct MeshClient *c, struct MeshTunnel *t)
2093 struct GNUNET_MESH_LocalAck msg;
2094 MESH_TunnelNumber tid;
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2098 "Sending client ACK on tunnel %X\n",
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to client %u\n", c->id);
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (owner, FWD ACK)\n");
2108 ack = tunnel_get_fwd_ack (t);
2113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (leaf, BCK ACK)\n");
2114 ack = tunnel_get_bck_ack (t);
2115 tid = t->local_tid_dest;
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2119 if (t->last_fwd_ack == ack)
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2123 t->last_fwd_ack = ack;
2124 msg.header.size = htons (sizeof (msg));
2125 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2126 msg.tunnel_id = htonl (tid);
2127 msg.max_pid = htonl (ack);
2129 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2130 &msg.header, GNUNET_NO);
2135 * Notify all clients (not depending on registration status) that the incoming
2136 * tunnel is no longer valid.
2138 * @param t Tunnel that was destroyed.
2141 send_clients_tunnel_destroy (struct MeshTunnel *t)
2143 struct GNUNET_MESH_TunnelMessage msg;
2145 msg.header.size = htons (sizeof (msg));
2146 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2147 msg.tunnel_id = htonl (t->local_tid_dest);
2148 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2153 * Notify clients of tunnel disconnections, if needed.
2154 * In case the origin disconnects, the destination clients get a tunnel destroy
2155 * notification. If the last destination disconnects (only one remaining client
2156 * in tunnel), the origin gets a (local ID) peer disconnected.
2157 * Note that the function must be called BEFORE removing the client from
2160 * @param t Tunnel that was destroyed.
2161 * @param c Client that disconnected.
2164 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2170 struct GNUNET_MESH_TunnelMessage msg;
2172 msg.header.size = htons (sizeof (msg));
2173 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2174 msg.tunnel_id = htonl (t->local_tid_dest);
2175 for (i = 0; i < t->nclients; i++)
2176 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2177 &msg.header, GNUNET_NO);
2179 // FIXME when to disconnect an incoming tunnel?
2180 else if (1 == t->nclients && NULL != t->owner)
2182 struct GNUNET_MESH_PeerControl msg;
2184 msg.header.size = htons (sizeof (msg));
2185 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2186 msg.tunnel_id = htonl (t->local_tid);
2187 msg.peer = my_full_id;
2188 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2189 &msg.header, GNUNET_NO);
2195 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2196 * and insert it in the appropiate structures if the peer is not known yet.
2198 * @param peer Full identity of the peer.
2200 * @return Existing or newly created peer info.
2202 static struct MeshPeerInfo *
2203 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2205 struct MeshPeerInfo *peer_info;
2207 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2208 if (NULL == peer_info)
2211 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2212 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2213 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2214 peer_info->id = GNUNET_PEER_intern (peer);
2222 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2223 * and insert it in the appropiate structures if the peer is not known yet.
2225 * @param peer Short identity of the peer.
2227 * @return Existing or newly created peer info.
2229 static struct MeshPeerInfo *
2230 peer_info_get_short (const GNUNET_PEER_Id peer)
2232 struct GNUNET_PeerIdentity id;
2234 GNUNET_PEER_resolve (peer, &id);
2235 return peer_info_get (&id);
2240 * Iterator to remove the tunnel from the list of tunnels a peer participates
2243 * @param cls Closure (tunnel info)
2244 * @param key GNUNET_PeerIdentity of the peer (unused)
2245 * @param value PeerInfo of the peer
2247 * @return always GNUNET_YES, to keep iterating
2250 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2252 struct MeshTunnel *t = cls;
2253 struct MeshPeerInfo *peer = value;
2256 for (i = 0; i < peer->ntunnels; i++)
2259 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2262 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2263 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2272 * Core callback to write a pre-constructed data packet to core buffer
2274 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2275 * @param size Number of bytes available in buf.
2276 * @param buf Where the to write the message.
2278 * @return number of bytes written to buf
2281 send_core_data_raw (void *cls, size_t size, void *buf)
2283 struct MeshTransmissionDescriptor *info = cls;
2284 struct GNUNET_MessageHeader *msg;
2287 GNUNET_assert (NULL != info);
2288 GNUNET_assert (NULL != info->mesh_data);
2289 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2290 total_size = ntohs (msg->size);
2292 if (total_size > size)
2297 memcpy (buf, msg, total_size);
2298 data_descriptor_decrement_rc (info->mesh_data);
2305 * Sends an already built non-multicast message to a peer,
2306 * properly registrating all used resources.
2308 * @param message Message to send. Function makes a copy of it.
2309 * @param peer Short ID of the neighbor whom to send the message.
2310 * @param t Tunnel on which this message is transmitted.
2313 send_message (const struct GNUNET_MessageHeader *message,
2314 const struct GNUNET_PeerIdentity *peer,
2315 struct MeshTunnel *t)
2317 struct MeshTransmissionDescriptor *info;
2318 struct MeshPeerInfo *neighbor;
2319 struct MeshPeerPath *p;
2322 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2324 size = ntohs (message->size);
2325 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2326 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2327 info->mesh_data->data = GNUNET_malloc (size);
2328 memcpy (info->mesh_data->data, message, size);
2329 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
2331 struct GNUNET_MESH_Unicast *m;
2333 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2334 m->ttl = htonl (ntohl (m->ttl) - 1);
2336 info->mesh_data->data_len = size;
2337 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2338 *info->mesh_data->reference_counter = 1;
2339 neighbor = peer_info_get (peer);
2340 for (p = neighbor->path_head; NULL != p; p = p->next)
2349 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2350 GNUNET_free (info->mesh_data->data);
2351 GNUNET_free (info->mesh_data);
2355 info->peer = neighbor;
2365 * Sends a CREATE PATH message for a path to a peer, properly registrating
2366 * all used resources.
2368 * @param peer PeerInfo of the final peer for whom this path is being created.
2369 * @param p Path itself.
2370 * @param t Tunnel for which the path is created.
2373 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2374 struct MeshTunnel *t)
2376 struct GNUNET_PeerIdentity id;
2377 struct MeshPathInfo *path_info;
2378 struct MeshPeerInfo *neighbor;
2384 p = tree_get_path_to_peer (t->tree, peer->id);
2391 for (i = 0; i < p->length; i++)
2393 if (p->peers[i] == myid)
2396 if (i >= p->length - 1)
2402 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2404 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2405 path_info->path = p;
2407 neighbor = peer_info_get (&id);
2408 path_info->peer = neighbor;
2409 queue_add (path_info,
2410 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2411 sizeof (struct GNUNET_MESH_ManipulatePath) +
2412 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2419 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2421 * @param t Tunnel whose path to destroy.
2422 * @param destination Short ID of the peer to whom the path to destroy.
2425 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2427 struct MeshPeerPath *p;
2430 p = tree_get_path_to_peer (t->tree, destination);
2436 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2437 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2439 struct GNUNET_MESH_ManipulatePath *msg;
2440 struct GNUNET_PeerIdentity *pi;
2444 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2445 msg->header.size = htons (size);
2446 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2447 msg->tid = htonl (t->id.tid);
2448 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2449 for (i = 0; i < p->length; i++)
2451 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2453 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2460 * Try to establish a new connection to this peer.
2461 * Use the best path for the given tunnel.
2462 * If the peer doesn't have any path to it yet, try to get one.
2463 * If the peer already has some path, send a CREATE PATH towards it.
2465 * @param peer PeerInfo of the peer.
2466 * @param t Tunnel for which to create the path, if possible.
2469 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2471 struct MeshPeerPath *p;
2472 struct MeshPathInfo *path_info;
2474 if (NULL != peer->path_head)
2476 p = tree_get_path_to_peer (t->tree, peer->id);
2483 // FIXME always send create path to self
2486 send_create_path (peer, p, t);
2490 struct GNUNET_HashCode hash;
2493 send_client_peer_connected (t, myid);
2494 t->local_tid_dest = next_local_tid++;
2495 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2498 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2499 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2506 else if (NULL == peer->dhtget)
2508 struct GNUNET_PeerIdentity id;
2510 GNUNET_PEER_resolve (peer->id, &id);
2511 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2512 path_info->peer = peer;
2514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2515 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2516 peer->dhtgetcls = path_info;
2517 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2518 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2519 &id.hashPubKey, /* key to search */
2520 dht_replication_level, /* replication level */
2521 GNUNET_DHT_RO_RECORD_ROUTE |
2522 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2523 NULL, /* xquery */ // FIXME BLOOMFILTER
2524 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2525 &dht_get_id_handler, path_info);
2527 /* Otherwise, there is no path but the DHT get is already started. */
2532 * Task to delay the connection of a peer
2534 * @param cls Closure (path info with tunnel and peer to connect).
2535 * Will be free'd on exection.
2536 * @param tc TaskContext
2539 peer_info_connect_task (void *cls,
2540 const struct GNUNET_SCHEDULER_TaskContext *tc)
2542 struct MeshPathInfo *path_info = cls;
2544 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2549 peer_info_connect (path_info->peer, path_info->t);
2555 * Destroy the peer_info and free any allocated resources linked to it
2557 * @param pi The peer_info to destroy.
2559 * @return GNUNET_OK on success
2562 peer_info_destroy (struct MeshPeerInfo *pi)
2564 struct GNUNET_PeerIdentity id;
2565 struct MeshPeerPath *p;
2566 struct MeshPeerPath *nextp;
2568 GNUNET_PEER_resolve (pi->id, &id);
2569 GNUNET_PEER_change_rc (pi->id, -1);
2572 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2576 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2578 if (NULL != pi->dhtget)
2580 GNUNET_DHT_get_stop (pi->dhtget);
2581 GNUNET_free (pi->dhtgetcls);
2587 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2597 * Remove all paths that rely on a direct connection between p1 and p2
2598 * from the peer itself and notify all tunnels about it.
2600 * @param peer PeerInfo of affected peer.
2601 * @param p1 GNUNET_PEER_Id of one peer.
2602 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2605 * TODO: optimize (see below)
2608 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2611 struct MeshPeerPath *p;
2612 struct MeshPeerPath *aux;
2613 struct MeshPeerInfo *peer_d;
2615 unsigned int destroyed;
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2622 p = peer->path_head;
2626 for (i = 0; i < (p->length - 1); i++)
2628 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2629 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2631 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2642 for (i = 0; i < peer->ntunnels; i++)
2644 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2648 * Problem: one or more peers have been deleted from the tunnel tree.
2649 * We don't know who they are to try to add them again.
2650 * We need to try to find a new path for each of the disconnected peers.
2651 * Some of them might already have a path to reach them that does not
2652 * involve p1 and p2. Adding all anew might render in a better tree than
2653 * the trivial immediate fix.
2655 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2656 * its children will be reachable trough him.
2658 peer_d = peer_info_get_short (d);
2661 for (p = peer_d->path_head; NULL != p; p = p->next)
2663 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2671 /* No callback, as peer will be already disconnected and a connection
2672 * scheduled by tunnel_notify_connection_broken.
2674 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2678 peer_info_connect (peer_d, peer->tunnels[i]);
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2686 * Add the path to the peer and update the path used to reach it in case this
2689 * @param peer_info Destination peer to add the path to.
2690 * @param path New path to add. Last peer must be the peer in arg 1.
2691 * Path will be either used of freed if already known.
2692 * @param trusted Do we trust that this path is real?
2695 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2698 struct MeshPeerPath *aux;
2702 if ((NULL == peer_info) || (NULL == path))
2705 path_destroy (path);
2708 if (path->peers[path->length - 1] != peer_info->id)
2711 path_destroy (path);
2714 if (path->length <= 2 && GNUNET_NO == trusted)
2716 /* Only allow CORE to tell us about direct paths */
2717 path_destroy (path);
2720 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2721 for (l = 1; l < path->length; l++)
2723 if (path->peers[l] == myid)
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2726 for (l2 = 0; l2 < path->length - l; l2++)
2728 path->peers[l2] = path->peers[l + l2];
2733 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2738 struct GNUNET_PeerIdentity id;
2740 GNUNET_PEER_resolve (peer_info->id, &id);
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2742 path->length, GNUNET_i2s (&id));
2745 l = path_get_length (path);
2752 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2753 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2755 l2 = path_get_length (aux);
2758 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2759 peer_info->path_tail, aux, path);
2764 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2766 path_destroy (path);
2771 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2778 * Add the path to the origin peer and update the path used to reach it in case
2779 * this is the shortest.
2780 * The path is given in peer_info -> destination, therefore we turn the path
2781 * upside down first.
2783 * @param peer_info Peer to add the path to, being the origin of the path.
2784 * @param path New path to add after being inversed.
2785 * @param trusted Do we trust that this path is real?
2788 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2789 struct MeshPeerPath *path, int trusted)
2792 peer_info_add_path (peer_info, path, trusted);
2797 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2798 * to obtain a local peer -> destination path and interning the peer ids.
2800 * @return Newly allocated and created path
2802 static struct MeshPeerPath *
2803 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2804 unsigned int get_path_length,
2805 const struct GNUNET_PeerIdentity *put_path,
2806 unsigned int put_path_length)
2808 struct MeshPeerPath *p;
2814 GNUNET_PEER_change_rc (myid, 1);
2815 i = get_path_length;
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2817 for (i--; i >= 0; i--)
2819 id = GNUNET_PEER_intern (&get_path[i]);
2820 if (p->length > 0 && id == p->peers[p->length - 1])
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2823 GNUNET_PEER_change_rc (id, -1);
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2828 GNUNET_i2s (&get_path[i]));
2830 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2831 p->peers[p->length - 1] = id;
2834 i = put_path_length;
2835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2836 for (i--; i >= 0; i--)
2838 id = GNUNET_PEER_intern (&put_path[i]);
2841 /* PUT path went through us, so discard the path up until now and start
2842 * from here to get a much shorter (and loop-free) path.
2847 if (p->length > 0 && id == p->peers[p->length - 1])
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2850 GNUNET_PEER_change_rc (id, -1);
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2855 GNUNET_i2s (&put_path[i]));
2857 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2858 p->peers[p->length - 1] = id;
2862 if (get_path_length > 0)
2863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2864 GNUNET_i2s (&get_path[0]));
2865 if (put_path_length > 0)
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2867 GNUNET_i2s (&put_path[0]));
2868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2870 for (i = 0; i < p->length; i++)
2872 struct GNUNET_PeerIdentity peer_id;
2874 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2876 GNUNET_i2s (&peer_id));
2884 * Adds a path to the peer_infos of all the peers in the path
2886 * @param p Path to process.
2887 * @param confirmed Whether we know if the path works or not.
2890 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2894 /* TODO: invert and add */
2895 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2896 for (i++; i < p->length; i++)
2898 struct MeshPeerInfo *aux;
2899 struct MeshPeerPath *copy;
2901 aux = peer_info_get_short (p->peers[i]);
2902 copy = path_duplicate (p);
2903 copy->length = i + 1;
2904 peer_info_add_path (aux, copy, GNUNET_NO);
2910 * Send keepalive packets for a peer
2912 * @param cls Closure (tunnel for which to send the keepalive).
2913 * @param tc Notification context.
2915 * TODO: implement explicit multicast keepalive?
2918 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2922 * Search for a tunnel among the incoming tunnels
2924 * @param tid the local id of the tunnel
2926 * @return tunnel handler, NULL if doesn't exist
2928 static struct MeshTunnel *
2929 tunnel_get_incoming (MESH_TunnelNumber tid)
2931 struct GNUNET_HashCode hash;
2933 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2934 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2935 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2940 * Search for a tunnel among the tunnels for a client
2942 * @param c the client whose tunnels to search in
2943 * @param tid the local id of the tunnel
2945 * @return tunnel handler, NULL if doesn't exist
2947 static struct MeshTunnel *
2948 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2950 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2952 return tunnel_get_incoming (tid);
2956 struct GNUNET_HashCode hash;
2958 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2959 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2965 * Search for a tunnel by global ID using PEER_ID
2967 * @param pi owner of the tunnel
2968 * @param tid global tunnel number
2970 * @return tunnel handler, NULL if doesn't exist
2972 static struct MeshTunnel *
2973 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2975 struct MESH_TunnelID id;
2976 struct GNUNET_HashCode hash;
2981 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2982 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2987 * Search for a tunnel by global ID using full PeerIdentities
2989 * @param oid owner of the tunnel
2990 * @param tid global tunnel number
2992 * @return tunnel handler, NULL if doesn't exist
2994 static struct MeshTunnel *
2995 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2997 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3002 * Delete an active client from the tunnel.
3008 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3012 for (i = 0; i < t->nclients; i++)
3014 if (t->clients[i] == c)
3016 t->clients[i] = t->clients[t->nclients - 1];
3017 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3018 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3020 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3028 * Delete an ignored client from the tunnel.
3034 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3038 for (i = 0; i < t->nignore; i++)
3040 if (t->ignore[i] == c)
3042 t->ignore[i] = t->ignore[t->nignore - 1];
3043 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3051 * Delete a client from the tunnel. It should be only done on
3052 * client disconnection, otherwise use client_ignore_tunnel.
3058 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3060 tunnel_delete_ignored_client (t, c);
3061 tunnel_delete_active_client (t, c);
3066 * Iterator to free MeshTunnelChildInfo of tunnel children.
3068 * @param cls Closure (tunnel info).
3069 * @param key Hash of GNUNET_PEER_Id (unused).
3070 * @param value MeshTunnelChildInfo of the child.
3072 * @return always GNUNET_YES, to keep iterating
3075 tunnel_destroy_child (void *cls,
3076 const struct GNUNET_HashCode * key,
3079 GNUNET_free (value);
3085 * Callback used to notify a client owner of a tunnel that a peer has
3086 * disconnected, most likely because of a path change.
3088 * @param cls Closure (tunnel this notification is about).
3089 * @param peer_id Short ID of disconnected peer.
3092 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3094 struct MeshTunnel *t = cls;
3095 struct MeshPeerInfo *peer;
3096 struct MeshPathInfo *path_info;
3098 if (NULL != t->owner && NULL != nc)
3100 struct GNUNET_MESH_PeerControl msg;
3102 msg.header.size = htons (sizeof (msg));
3103 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3104 msg.tunnel_id = htonl (t->local_tid);
3105 GNUNET_PEER_resolve (peer_id, &msg.peer);
3106 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3107 &msg.header, GNUNET_NO);
3109 peer = peer_info_get_short (peer_id);
3110 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3111 path_info->peer = peer;
3113 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3118 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3119 * needed rescources.
3120 * If peer already exists, reevaluate shortest path and change if different.
3122 * @param t Tunnel we want to add a new peer to
3123 * @param peer PeerInfo of the peer being added
3127 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3129 struct GNUNET_PeerIdentity id;
3130 struct MeshPeerPath *best_p;
3131 struct MeshPeerPath *p;
3132 unsigned int best_cost;
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3136 GNUNET_PEER_resolve (peer->id, &id);
3138 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3141 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3142 GNUNET_assert (GNUNET_OK ==
3143 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3145 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3148 if (NULL != (p = peer->path_head))
3151 best_cost = tree_get_path_cost (t->tree, p);
3154 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3161 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3162 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3163 t->path_refresh_task =
3164 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3168 /* Start a DHT get */
3169 peer_info_connect (peer, t);
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3175 * Add a path to a tunnel which we don't own, just to remember the next hop.
3176 * If destination node was already in the tunnel, the first hop information
3177 * will be replaced with the new path.
3179 * @param t Tunnel we want to add a new peer to
3180 * @param p Path to add
3181 * @param own_pos Position of local node in path.
3185 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3186 unsigned int own_pos)
3188 struct GNUNET_PeerIdentity id;
3190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3191 GNUNET_assert (0 != own_pos);
3192 tree_add_path (t->tree, p, NULL, NULL);
3193 if (own_pos < p->length - 1)
3195 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3196 tree_update_first_hops (t->tree, myid, &id);
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3203 * Notifies a tunnel that a connection has broken that affects at least
3204 * some of its peers. Sends a notification towards the root of the tree.
3205 * In case the peer is the owner of the tree, notifies the client that owns
3206 * the tunnel and tries to reconnect.
3208 * @param t Tunnel affected.
3209 * @param p1 Peer that got disconnected from p2.
3210 * @param p2 Peer that got disconnected from p1.
3212 * @return Short ID of the peer disconnected (either p1 or p2).
3213 * 0 if the tunnel remained unaffected.
3215 static GNUNET_PEER_Id
3216 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3222 tree_notify_connection_broken (t->tree, p1, p2,
3223 &tunnel_notify_client_peer_disconnected,
3225 if (myid != p1 && myid != p2)
3231 if (tree_get_predecessor (t->tree) != 0)
3233 /* We are the peer still connected, notify owner of the disconnection. */
3234 struct GNUNET_MESH_PathBroken msg;
3235 struct GNUNET_PeerIdentity neighbor;
3237 msg.header.size = htons (sizeof (msg));
3238 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3239 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3240 msg.tid = htonl (t->id.tid);
3241 msg.peer1 = my_full_id;
3242 GNUNET_PEER_resolve (pid, &msg.peer2);
3243 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3244 send_message (&msg.header, &neighbor, t);
3252 * Send a multicast packet to a neighbor.
3254 * @param cls Closure (Info about the multicast packet)
3255 * @param neighbor_id Short ID of the neighbor to send the packet to.
3258 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3260 struct MeshData *mdata = cls;
3261 struct MeshTransmissionDescriptor *info;
3262 struct GNUNET_PeerIdentity neighbor;
3264 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3266 info->mesh_data = mdata;
3267 (*(mdata->reference_counter)) ++;
3268 info->destination = neighbor_id;
3269 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3271 GNUNET_i2s (&neighbor));
3272 info->peer = peer_info_get (&neighbor);
3273 GNUNET_assert (NULL != info->peer);
3275 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3276 info->mesh_data->data_len,
3283 * Send a message in a tunnel in multicast, sending a copy to each child node
3284 * down the local one in the tunnel tree.
3286 * @param t Tunnel in which to send the data.
3287 * @param msg Message to be sent.
3288 * @param internal Has the service generated this message?
3291 tunnel_send_multicast (struct MeshTunnel *t,
3292 const struct GNUNET_MessageHeader *msg,
3295 struct MeshData *mdata;
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3298 " sending a multicast packet...\n");
3299 mdata = GNUNET_malloc (sizeof (struct MeshData));
3300 mdata->data_len = ntohs (msg->size);
3301 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3303 mdata->data = GNUNET_malloc (mdata->data_len);
3304 memcpy (mdata->data, msg, mdata->data_len);
3305 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3307 struct GNUNET_MESH_Multicast *mcast;
3309 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3310 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3312 ntohl (mcast->ttl));
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3318 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3319 && GNUNET_NO == internal)
3321 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3323 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3329 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3330 if (*(mdata->reference_counter) == 0)
3332 GNUNET_free (mdata->data);
3333 GNUNET_free (mdata->reference_counter);
3334 if (NULL != mdata->task)
3336 GNUNET_SCHEDULER_cancel(*(mdata->task));
3337 GNUNET_free (mdata->task);
3338 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3340 // FIXME change order?
3341 GNUNET_free (mdata);
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3344 " sending a multicast packet done\n");
3350 * Increase the SKIP value of all peers that
3351 * have not received a unicast message.
3353 * @param cls Closure (ID of the peer that HAS received the message).
3354 * @param key ID of the neighbor.
3355 * @param value Information about the neighbor.
3357 * @return GNUNET_YES to keep iterating.
3360 tunnel_add_skip (void *cls,
3361 const struct GNUNET_HashCode * key,
3364 struct GNUNET_PeerIdentity *neighbor = cls;
3365 struct MeshTunnelChildInfo *cinfo = value;
3367 /* TODO compare only pointers? key == neighbor? */
3368 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3378 * @brief Get neighbor's Flow Control information.
3380 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3381 * descendant of the local node in a certain tunnel.
3382 * If the info is not yet there (recently created path), creates the data struct
3383 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3386 * @param t Tunnel related.
3387 * @param peer Neighbor whose Flow Control info is needed.
3389 * @return Neighbor's Flow Control info.
3391 static struct MeshTunnelChildInfo *
3392 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3393 const struct GNUNET_PeerIdentity *peer)
3395 struct MeshTunnelChildInfo *cinfo;
3396 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3400 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3401 cinfo->id = GNUNET_PEER_intern (peer);
3402 cinfo->skip = t->fwd_pid;
3403 cinfo->fwd_ack = t->fwd_pid + t->fwd_queue_max - t->fwd_queue_n; // FIXME review
3405 GNUNET_assert (GNUNET_OK ==
3406 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3409 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3416 * Iterator to get the appropiate ACK value from all children nodes.
3418 * @param cls Closue (tunnel).
3419 * @param id Id of the child node.
3422 tunnel_get_child_ack (void *cls,
3425 struct GNUNET_PeerIdentity peer_id;
3426 struct MeshTunnelChildInfo *cinfo;
3427 struct MeshTunnelChildIteratorContext *ctx = cls;
3428 struct MeshTunnel *t = ctx->t;
3431 GNUNET_PEER_resolve (id, &peer_id);
3432 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3433 ack = cinfo->fwd_ack;
3435 if (0 == ctx->max_child_ack)
3436 ctx->max_child_ack = ack;
3438 if (GNUNET_YES == t->speed_min)
3440 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3444 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3451 * Get the maximum PID allowed to transmit to any
3452 * tunnel child of the local peer, depending on the tunnel
3453 * buffering/speed settings.
3457 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3460 tunnel_get_children_ack (struct MeshTunnel *t)
3462 struct MeshTunnelChildIteratorContext ctx;
3464 ctx.max_child_ack = 0;
3466 tree_iterate_children (t->tree, tunnel_get_child_ack, &ctx);
3468 if (0 == ctx.nchildren)
3471 return (int64_t) ctx.max_child_ack;
3476 * Add a client to a tunnel, initializing all needed data structures.
3478 * @param t Tunnel to which add the client.
3479 * @param c Client which to add to the tunnel.
3482 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3484 struct MeshTunnelClientInfo clinfo;
3486 GNUNET_array_append (t->clients, t->nclients, c);
3488 clinfo.fwd_ack = t->fwd_pid + 1;
3489 clinfo.bck_ack = t->bck_ack + 1; // FIXME fc review
3490 clinfo.pid = t->fwd_pid;
3491 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3496 * Set the ACK value of a client in a particular tunnel.
3498 * @param t Tunnel affected.
3499 * @param c Client whose ACK to set.
3500 * @param ack ACK value.
3503 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3504 struct MeshClient *c,
3509 for (i = 0; i < t->nclients; i++)
3511 if (t->clients[i] != c)
3513 t->clients_fc[i].fwd_ack = ack;
3521 * Get the ACK value of a client in a particular tunnel.
3523 * @param t Tunnel on which to look.
3524 * @param c Client whose ACK to get.
3526 * @return ACK value.
3528 uint32_t // FIXME static when used!!
3529 tunnel_get_client_ack (struct MeshTunnel *t,
3530 struct MeshClient *c)
3534 for (i = 0; i < t->nclients; i++)
3536 if (t->clients[i] != c)
3538 return t->clients_fc[i].fwd_ack;
3546 * Get the highest ACK value of all clients in a particular tunnel,
3547 * according to the buffering/speed settings.
3549 * @param t Tunnel on which to look.
3551 * @return Corresponding ACK value (max uint32_t).
3552 * If no clients are suscribed, -1.
3555 tunnel_get_clients_ack (struct MeshTunnel *t)
3560 if (0 == t->nclients)
3563 for (ack = -1, i = 0; i < t->nclients; i++)
3566 (GNUNET_YES == t->speed_min &&
3567 GNUNET_YES == is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3568 (GNUNET_NO == t->speed_min &&
3569 GNUNET_YES == is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3571 ack = t->clients_fc[i].fwd_ack;
3575 if (GNUNET_YES == t->nobuffer && is_pid_bigger(ack, t->fwd_pid))
3576 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3578 return (uint32_t) ack;
3583 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3584 * mode and the status of all children nodes.
3588 * @return Maximum PID allowed.
3591 tunnel_get_fwd_ack (struct MeshTunnel *t)
3594 uint32_t buffer_free;
3599 count = t->fwd_pid - t->skip;
3600 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3601 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3602 child_ack = tunnel_get_children_ack (t);
3603 client_ack = tunnel_get_clients_ack (t);
3604 if (-1 == child_ack)
3606 // Node has no children, child_ack AND core buffer are irrelevant.
3607 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good!
3608 return (uint32_t) client_ack;
3611 if (GNUNET_YES == t->speed_min)
3613 ack = min_pid ((uint32_t) child_ack, ack);
3614 ack = min_pid ((uint32_t) client_ack, ack);
3618 ack = max_pid ((uint32_t) child_ack, ack);
3619 ack = max_pid ((uint32_t) client_ack, ack);
3621 if (GNUNET_YES == t->nobuffer && is_pid_bigger(ack, t->fwd_pid))
3622 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3624 count, buffer_free, child_ack, client_ack, ack);
3629 * Get the current bck ack value for a tunnel, taking in account the tunnel
3630 * mode and the status of all children nodes.
3634 * @return Maximum PID allowed.
3637 tunnel_get_bck_ack (struct MeshTunnel *t)
3641 if (GNUNET_YES == t->nobuffer)
3643 if (t->bck_ack > t->bck_pid)
3645 return t->bck_pid + 1;
3652 ack = t->bck_pid + t->bck_queue_max - t->bck_queue_n;
3660 * Send an ACK informing the predecessor about the available buffer space.
3661 * In case there is no predecessor, inform the owning client.
3662 * If buffering is off, send only on behalf of children or self if endpoint.
3663 * If buffering is on, send when sent to children and buffer space is free.
3664 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3665 * the ACK itself goes "back" (towards root).
3667 * @param t Tunnel on which to send the ACK.
3668 * @param type Type of message that triggered the ACK transmission.
3671 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3673 struct GNUNET_MESH_ACK msg;
3674 struct GNUNET_PeerIdentity id;
3677 if (NULL != t->owner)
3679 send_client_tunnel_ack (t->owner, t);
3682 /* Is it after unicast / multicast retransmission? */
3685 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3686 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3688 "ACK due to FWD DATA retransmission\n");
3689 if (GNUNET_YES == t->nobuffer)
3691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3694 if (t->fwd_queue_max > t->fwd_queue_n * 2)
3696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3700 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3701 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3707 /* Ok, ACK might be necessary, what PID to ACK? */
3708 ack = tunnel_get_fwd_ack (t);
3710 /* If speed_min and not all children have ack'd, dont send yet */
3711 if (ack == t->last_fwd_ack)
3713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3717 t->last_fwd_ack = ack;
3718 msg.pid = htonl (ack);
3720 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3722 msg.header.size = htons (sizeof (msg));
3723 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3724 msg.tid = htonl (t->id.tid);
3725 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3726 send_message (&msg.header, &id, t);
3731 * Send an ACK informing the children nodes about the available buffer space.
3732 * In case there is no child node, inform the destination clients.
3733 * If buffering is off, send only on behalf of root (can be self).
3734 * If buffering is on, send when sent to predecessor and buffer space is free.
3735 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3736 * the ACK itself goes "forward" (towards children/clients).
3738 * @param t Tunnel on which to send the ACK.
3739 * @param type Type of message that triggered the ACK transmission.
3742 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3744 struct GNUNET_MESH_ACK msg;
3745 struct GNUNET_PeerIdentity id;
3751 if (NULL != t->owner)
3753 send_client_tunnel_ack (t->owner, t);
3756 /* Is it after data to_origin retransmission? */
3759 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3761 "ACK due to BCK DATA retransmission\n");
3762 if (GNUNET_YES == t->nobuffer)
3764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3767 if (t->bck_queue_max > t->bck_queue_n * 2)
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3773 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3774 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3780 /* Ok, ACK might be necessary, what PID to ACK? */
3781 ack = tunnel_get_bck_ack (t);
3783 /* If speed_min and not all children have ack'd, dont send yet */
3784 if (ack == t->last_bck_ack)
3786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending BCK ACK, not ready\n");
3790 /* Unlock local clients. */
3791 if (0 < t->nclients)
3793 struct GNUNET_MESH_LocalAck msg;
3795 /* Find client who to allow to send to origin (with lowest buffer space) */
3796 /* FIXME fc Round robin? Priority? FIFO? */
3797 for (i = 0; i < t->nclients; i++)
3801 d = t->clients_fc[i].bck_ack - t->clients_fc[i].pid;
3802 if (0 == i || d < min_d)
3808 msg.header.size = htons (sizeof (msg));
3809 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3810 msg.tunnel_id = htonl (t->local_tid_dest);
3811 msg.max_pid = t->bck_pid + 1; // FIXME fc
3812 GNUNET_SERVER_notification_context_unicast(nc,
3813 t->clients[min_i]->handle,
3818 t->last_bck_ack = ack;
3819 msg.pid = htonl (ack);
3820 msg.header.size = htons (sizeof (msg));
3821 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3822 msg.tid = htonl (t->id.tid);
3823 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3824 send_message (&msg.header, &id, t);
3829 * Send a message to all peers in this tunnel that the tunnel is no longer
3832 * @param t The tunnel whose peers to notify.
3835 tunnel_send_destroy (struct MeshTunnel *t)
3837 struct GNUNET_MESH_TunnelDestroy msg;
3839 msg.header.size = htons (sizeof (msg));
3840 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3841 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3842 msg.tid = htonl (t->id.tid);
3843 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3848 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3850 * @param cls Closure (Tunnel which to cancel).
3851 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3854 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3856 struct MeshTunnel *t = cls;
3857 struct MeshPeerInfo *peer_info;
3858 struct MeshPeerQueue *pq;
3859 struct MeshPeerQueue *next;
3861 peer_info = peer_info_get_short (neighbor_id);
3862 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3865 if (pq->tunnel == t)
3867 queue_destroy (pq, GNUNET_YES);
3870 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3872 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3873 peer_info->core_transmit = NULL;
3878 * Destroy the tunnel and free any allocated resources linked to it.
3880 * @param t the tunnel to destroy
3882 * @return GNUNET_OK on success
3885 tunnel_destroy (struct MeshTunnel *t)
3887 struct MeshClient *c;
3888 struct GNUNET_HashCode hash;
3895 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3901 struct GNUNET_PeerIdentity id;
3903 GNUNET_PEER_resolve (t->id.oid, &id);
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3905 GNUNET_i2s (&id), t->id.tid);
3907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3911 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3912 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3917 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3920 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3924 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3925 for (i = 0; i < t->nclients; i++)
3929 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3934 for (i = 0; i < t->nignore; i++)
3938 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3943 if (t->nclients > 0)
3946 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3950 GNUNET_free (t->clients);
3952 if (NULL != t->peers)
3954 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3956 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3959 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3960 &tunnel_destroy_child,
3962 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3964 tree_destroy (t->tree);
3966 if (NULL != t->regex_ctx)
3967 regex_cancel_search (t->regex_ctx);
3968 if (NULL != t->dht_get_type)
3969 GNUNET_DHT_get_stop (t->dht_get_type);
3970 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3971 GNUNET_SCHEDULER_cancel (t->timeout_task);
3972 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3973 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3976 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3977 GNUNET_assert (0 <= n_tunnels);
3984 * Create a new tunnel
3986 * @param owner Who is the owner of the tunnel (short ID).
3987 * @param tid Tunnel Number of the tunnel.
3988 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3989 * @param local Tunnel Number for the tunnel, for the client point of view.
3991 * @return A new initialized tunnel. NULL on error.
3993 static struct MeshTunnel *
3994 tunnel_new (GNUNET_PEER_Id owner,
3995 MESH_TunnelNumber tid,
3996 struct MeshClient *client,
3997 MESH_TunnelNumber local)
3999 struct MeshTunnel *t;
4000 struct GNUNET_HashCode hash;
4002 if (n_tunnels >= max_tunnels && NULL == client)
4005 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4008 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4009 t->bck_queue_max = t->fwd_queue_max;
4010 t->tree = tree_new (owner);
4013 t->local_tid = local;
4014 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4016 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4018 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4020 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4021 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4028 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4035 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4037 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4038 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4042 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4052 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4053 * that are no longer needed, as well as nodes of no longer reachable peers.
4054 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4056 * @param t Tunnel from which to remove the path.
4057 * @param peer Short id of the peer which should be removed.
4060 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4062 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4068 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4069 * client when the client disconnects. If the client is not the owner, the
4070 * owner will get notified if no more clients are in the tunnel and the client
4071 * get removed from the tunnel's list.
4073 * @param cls closure (client that is disconnecting)
4074 * @param key the hash of the local tunnel id (used to access the hashmap)
4075 * @param value the value stored at the key (tunnel to destroy)
4077 * @return GNUNET_OK on success
4080 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4082 struct MeshTunnel *t = value;
4083 struct MeshClient *c = cls;
4086 send_client_tunnel_disconnect(t, c);
4089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4090 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4091 tunnel_delete_client(t, c);
4092 client_delete_tunnel(c, t);
4095 tunnel_send_destroy(t);
4096 r = tunnel_destroy (t);
4102 * Timeout function, destroys tunnel if called
4104 * @param cls Closure (tunnel to destroy).
4105 * @param tc TaskContext
4108 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4110 struct MeshTunnel *t = cls;
4112 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4114 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4119 * Resets the tunnel timeout. Starts it if no timeout was running.
4121 * @param t Tunnel whose timeout to reset.
4123 * TODO use heap to improve efficiency of scheduler.
4126 tunnel_reset_timeout (struct MeshTunnel *t)
4128 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4129 GNUNET_SCHEDULER_cancel (t->timeout_task);
4131 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4132 (refresh_path_time, 4), &tunnel_timeout, t);
4136 /******************************************************************************/
4137 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4138 /******************************************************************************/
4141 * Function to send a create path packet to a peer.
4143 * @param cls closure
4144 * @param size number of bytes available in buf
4145 * @param buf where the callee should write the message
4146 * @return number of bytes written to buf
4149 send_core_path_create (void *cls, size_t size, void *buf)
4151 struct MeshPathInfo *info = cls;
4152 struct GNUNET_MESH_ManipulatePath *msg;
4153 struct GNUNET_PeerIdentity *peer_ptr;
4154 struct MeshTunnel *t = info->t;
4155 struct MeshPeerPath *p = info->path;
4160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4162 sizeof (struct GNUNET_MESH_ManipulatePath) +
4163 p->length * sizeof (struct GNUNET_PeerIdentity);
4165 if (size < size_needed || NULL == buf)
4170 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4171 msg->header.size = htons (size_needed);
4172 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4173 msg->tid = ntohl (t->id.tid);
4175 if (GNUNET_YES == t->speed_min)
4176 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4177 if (GNUNET_YES == t->nobuffer)
4178 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4179 msg->opt = htonl(opt);
4181 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4182 for (i = 0; i < p->length; i++)
4184 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4191 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4197 * Fill the core buffer
4199 * @param cls closure (data itself)
4200 * @param size number of bytes available in buf
4201 * @param buf where the callee should write the message
4203 * @return number of bytes written to buf
4206 send_core_data_multicast (void *cls, size_t size, void *buf)
4208 struct MeshTransmissionDescriptor *info = cls;
4211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4212 GNUNET_assert (NULL != info);
4213 GNUNET_assert (NULL != info->peer);
4214 total_size = info->mesh_data->data_len;
4215 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4217 if (total_size > size)
4222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4223 memcpy (buf, info->mesh_data->data, total_size);
4226 struct GNUNET_MESH_Multicast *mc;
4227 struct GNUNET_MessageHeader *mh;
4230 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4232 mc = (struct GNUNET_MESH_Multicast *) mh;
4233 mh = (struct GNUNET_MessageHeader *) &mc[1];
4234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4235 " multicast, payload type %u\n", ntohs (mh->type));
4236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4237 " multicast, payload size %u\n", ntohs (mh->size));
4241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
4246 data_descriptor_decrement_rc (info->mesh_data);
4247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4255 * Creates a path ack message in buf and frees all unused resources.
4257 * @param cls closure (MeshTransmissionDescriptor)
4258 * @param size number of bytes available in buf
4259 * @param buf where the callee should write the message
4260 * @return number of bytes written to buf
4263 send_core_path_ack (void *cls, size_t size, void *buf)
4265 struct MeshTransmissionDescriptor *info = cls;
4266 struct GNUNET_MESH_PathACK *msg = buf;
4268 GNUNET_assert (NULL != info);
4269 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4274 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4275 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4276 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4277 msg->tid = htonl (info->origin->tid);
4278 msg->peer_id = my_full_id;
4281 /* TODO add signature */
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4284 return sizeof (struct GNUNET_MESH_PathACK);
4289 * Free a transmission that was already queued with all resources
4290 * associated to the request.
4292 * @param queue Queue handler to cancel.
4293 * @param clear_cls Is it necessary to free associated cls?
4296 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4298 struct MeshTransmissionDescriptor *dd;
4299 struct MeshPathInfo *path_info;
4301 if (GNUNET_YES == clear_cls)
4303 switch (queue->type)
4305 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4306 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4307 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4310 data_descriptor_decrement_rc (dd->mesh_data);
4312 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4314 path_info = queue->cls;
4315 path_destroy (path_info->path);
4319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4321 GNUNET_free_non_null (queue->cls);
4323 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4324 queue->peer->queue_tail,
4326 GNUNET_free (queue);
4331 * Core callback to write a queued packet to core buffer
4333 * @param cls Closure (peer info).
4334 * @param size Number of bytes available in buf.
4335 * @param buf Where the to write the message.
4337 * @return number of bytes written to buf
4340 queue_send (void *cls, size_t size, void *buf)
4342 struct MeshPeerInfo *peer = cls;
4343 struct GNUNET_MessageHeader *msg;
4344 struct MeshPeerQueue *queue;
4345 struct MeshTunnel *t;
4348 peer->core_transmit = NULL;
4349 queue = peer->queue_head;
4351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4353 /* If queue is empty, send should have been cancelled */
4359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4361 /* Check if buffer size is enough for the message */
4362 if (queue->size > size)
4364 struct GNUNET_PeerIdentity id;
4366 GNUNET_PEER_resolve (peer->id, &id);
4367 peer->core_transmit =
4368 GNUNET_CORE_notify_transmit_ready(core_handle,
4371 GNUNET_TIME_UNIT_FOREVER_REL,
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4384 switch (queue->type)
4386 case 0: // RAW data (preconstructed message, retransmission, etc.)
4387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
4388 data_size = send_core_data_raw (queue->cls, size, buf);
4389 msg = (struct GNUNET_MessageHeader *) buf;
4390 switch (ntohs (msg->type)) // Type of preconstructed message
4392 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4393 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4395 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4396 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4402 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4404 data_size = send_core_data_multicast(queue->cls, size, buf);
4405 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4407 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4409 data_size = send_core_path_create(queue->cls, size, buf);
4411 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4413 data_size = send_core_path_ack(queue->cls, size, buf);
4417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
4421 /* Free queue, but cls was freed by send_core_* */
4422 queue_destroy (queue, GNUNET_NO);
4424 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4430 /* If more data in queue, send next */
4431 if (NULL != peer->queue_head)
4433 struct GNUNET_PeerIdentity id;
4435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4436 GNUNET_PEER_resolve (peer->id, &id);
4437 peer->core_transmit =
4438 GNUNET_CORE_notify_transmit_ready(core_handle,
4441 GNUNET_TIME_UNIT_FOREVER_REL,
4443 peer->queue_head->size,
4447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4453 * Queue and pass message to core when possible.
4455 * @param cls Closure (type dependant).
4456 * @param type Type of the message, 0 for a raw message.
4457 * @param size Size of the message.
4458 * @param dst Neighbor to send message to.
4459 * @param t Tunnel this message belongs to.
4462 queue_add (void *cls, uint16_t type, size_t size,
4463 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4465 struct MeshPeerQueue *queue;
4469 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4470 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4472 n = &t->fwd_queue_n;
4473 max = &t->fwd_queue_max;
4477 n = &t->bck_queue_n;
4478 max = &t->bck_queue_max;
4482 if (NULL == t->owner)
4483 GNUNET_break_op(0); // TODO: kill connection?
4486 return; // Drop message
4489 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4495 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4496 if (NULL == dst->core_transmit)
4498 struct GNUNET_PeerIdentity id;
4500 GNUNET_PEER_resolve (dst->id, &id);
4501 dst->core_transmit =
4502 GNUNET_CORE_notify_transmit_ready(core_handle,
4505 GNUNET_TIME_UNIT_FOREVER_REL,
4514 /******************************************************************************/
4515 /******************** MESH NETWORK HANDLERS **************************/
4516 /******************************************************************************/
4520 * Core handler for path creation
4522 * @param cls closure
4523 * @param message message
4524 * @param peer peer identity this notification is about
4525 * @param atsi performance data
4526 * @param atsi_count number of records in 'atsi'
4528 * @return GNUNET_OK to keep the connection open,
4529 * GNUNET_SYSERR to close it (signal serious error)
4532 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4533 const struct GNUNET_MessageHeader *message,
4534 const struct GNUNET_ATS_Information *atsi,
4535 unsigned int atsi_count)
4537 unsigned int own_pos;
4540 MESH_TunnelNumber tid;
4541 struct GNUNET_MESH_ManipulatePath *msg;
4542 struct GNUNET_PeerIdentity *pi;
4543 struct GNUNET_HashCode hash;
4544 struct MeshPeerPath *path;
4545 struct MeshPeerInfo *dest_peer_info;
4546 struct MeshPeerInfo *orig_peer_info;
4547 struct MeshTunnel *t;
4549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4550 "Received a path create msg [%s]\n",
4551 GNUNET_i2s (&my_full_id));
4552 size = ntohs (message->size);
4553 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4555 GNUNET_break_op (0);
4559 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4560 if (size % sizeof (struct GNUNET_PeerIdentity))
4562 GNUNET_break_op (0);
4565 size /= sizeof (struct GNUNET_PeerIdentity);
4568 GNUNET_break_op (0);
4571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4572 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4574 tid = ntohl (msg->tid);
4575 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4577 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4578 t = tunnel_get (pi, tid);
4579 if (NULL == t) // FIXME only for INCOMING tunnels?
4583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4584 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4587 // FIXME notify failure
4590 opt = ntohl (msg->opt);
4591 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4592 GNUNET_YES : GNUNET_NO;
4593 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4594 GNUNET_YES : GNUNET_NO;
4596 if (GNUNET_YES == t->nobuffer)
4598 t->bck_queue_max = 1;
4599 t->fwd_queue_max = 1;
4602 // FIXME only assign a local tid if a local client is interested (on demand)
4603 while (NULL != tunnel_get_incoming (next_local_tid))
4604 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4605 t->local_tid_dest = next_local_tid++;
4606 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4609 tunnel_reset_timeout (t);
4610 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4612 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4613 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4621 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4622 if (NULL == dest_peer_info)
4624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4625 " Creating PeerInfo for destination.\n");
4626 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4627 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4628 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4630 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4632 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4633 if (NULL == orig_peer_info)
4635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4636 " Creating PeerInfo for origin.\n");
4637 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4638 orig_peer_info->id = GNUNET_PEER_intern (pi);
4639 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4640 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4643 path = path_new (size);
4645 for (i = 0; i < size; i++)
4647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4648 GNUNET_i2s (&pi[i]));
4649 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4650 if (path->peers[i] == myid)
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4656 /* cannot be self, must be 'not found' */
4657 /* create path: self not found in path through self */
4658 GNUNET_break_op (0);
4659 path_destroy (path);
4663 path_add_to_peers (path, GNUNET_NO);
4664 tunnel_add_path (t, path, own_pos);
4665 if (own_pos == size - 1)
4667 /* It is for us! Send ack. */
4668 struct MeshTransmissionDescriptor *info;
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4671 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4672 if (NULL == t->peers)
4674 /* New tunnel! Notify clients on data. */
4675 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4677 GNUNET_break (GNUNET_SYSERR !=
4678 GNUNET_CONTAINER_multihashmap_put (t->peers,
4679 &my_full_id.hashPubKey,
4682 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4683 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4684 info->origin = &t->id;
4685 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4686 GNUNET_assert (NULL != info->peer);
4688 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4689 sizeof (struct GNUNET_MESH_PathACK),
4695 struct MeshPeerPath *path2;
4697 /* It's for somebody else! Retransmit. */
4698 path2 = path_duplicate (path);
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4700 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4701 path2 = path_duplicate (path);
4702 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4703 send_create_path (dest_peer_info, path, t);
4710 * Core handler for path destruction
4712 * @param cls closure
4713 * @param message message
4714 * @param peer peer identity this notification is about
4715 * @param atsi performance data
4716 * @param atsi_count number of records in 'atsi'
4718 * @return GNUNET_OK to keep the connection open,
4719 * GNUNET_SYSERR to close it (signal serious error)
4722 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4723 const struct GNUNET_MessageHeader *message,
4724 const struct GNUNET_ATS_Information *atsi,
4725 unsigned int atsi_count)
4727 struct GNUNET_MESH_ManipulatePath *msg;
4728 struct GNUNET_PeerIdentity *pi;
4729 struct MeshPeerPath *path;
4730 struct MeshTunnel *t;
4731 unsigned int own_pos;
4735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4736 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4737 size = ntohs (message->size);
4738 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4740 GNUNET_break_op (0);
4744 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4745 if (size % sizeof (struct GNUNET_PeerIdentity))
4747 GNUNET_break_op (0);
4750 size /= sizeof (struct GNUNET_PeerIdentity);
4753 GNUNET_break_op (0);
4756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4758 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4759 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4761 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4763 t = tunnel_get (pi, ntohl (msg->tid));
4766 /* TODO notify back: we don't know this tunnel */
4767 GNUNET_break_op (0);
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4771 path = path_new (size);
4773 for (i = 0; i < size; i++)
4775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4776 GNUNET_i2s (&pi[i]));
4777 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4778 if (path->peers[i] == myid)
4781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4782 if (own_pos < path->length - 1)
4783 send_message (message, &pi[own_pos + 1], t);
4785 send_client_tunnel_disconnect(t, NULL);
4787 tunnel_delete_peer (t, path->peers[path->length - 1]);
4788 path_destroy (path);
4794 * Core handler for notifications of broken paths
4796 * @param cls closure
4797 * @param message message
4798 * @param peer peer identity this notification is about
4799 * @param atsi performance data
4800 * @param atsi_count number of records in 'atsi'
4802 * @return GNUNET_OK to keep the connection open,
4803 * GNUNET_SYSERR to close it (signal serious error)
4806 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4807 const struct GNUNET_MessageHeader *message,
4808 const struct GNUNET_ATS_Information *atsi,
4809 unsigned int atsi_count)
4811 struct GNUNET_MESH_PathBroken *msg;
4812 struct MeshTunnel *t;
4814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4815 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4816 msg = (struct GNUNET_MESH_PathBroken *) message;
4817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4818 GNUNET_i2s (&msg->peer1));
4819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4820 GNUNET_i2s (&msg->peer2));
4821 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4824 GNUNET_break_op (0);
4827 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4828 GNUNET_PEER_search (&msg->peer2));
4835 * Core handler for tunnel destruction
4837 * @param cls closure
4838 * @param message message
4839 * @param peer peer identity this notification is about
4840 * @param atsi performance data
4841 * @param atsi_count number of records in 'atsi'
4843 * @return GNUNET_OK to keep the connection open,
4844 * GNUNET_SYSERR to close it (signal serious error)
4847 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4848 const struct GNUNET_MessageHeader *message,
4849 const struct GNUNET_ATS_Information *atsi,
4850 unsigned int atsi_count)
4852 struct GNUNET_MESH_TunnelDestroy *msg;
4853 struct MeshTunnel *t;
4855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4856 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4857 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4859 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4860 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4863 /* Probably already got the message from another path,
4864 * destroyed the tunnel and retransmitted to children.
4867 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4870 if (t->id.oid == myid)
4872 GNUNET_break_op (0);
4875 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4877 /* Tunnel was incoming, notify clients */
4878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4879 t->local_tid, t->local_tid_dest);
4880 send_clients_tunnel_destroy (t);
4882 tunnel_send_destroy (t);
4883 t->destroy = GNUNET_YES;
4884 // TODO: add timeout to destroy the tunnel anyway
4890 * Core handler for mesh network traffic going from the origin to a peer
4892 * @param cls closure
4893 * @param peer peer identity this notification is about
4894 * @param message message
4895 * @param atsi performance data
4896 * @param atsi_count number of records in 'atsi'
4897 * @return GNUNET_OK to keep the connection open,
4898 * GNUNET_SYSERR to close it (signal serious error)
4901 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4902 const struct GNUNET_MessageHeader *message,
4903 const struct GNUNET_ATS_Information *atsi,
4904 unsigned int atsi_count)
4906 struct GNUNET_MESH_Unicast *msg;
4907 struct GNUNET_PeerIdentity *neighbor;
4908 struct MeshTunnelChildInfo *cinfo;
4909 struct MeshTunnel *t;
4910 GNUNET_PEER_Id dest_id;
4915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4918 size = ntohs (message->size);
4920 sizeof (struct GNUNET_MESH_Unicast) +
4921 sizeof (struct GNUNET_MessageHeader))
4926 msg = (struct GNUNET_MESH_Unicast *) message;
4927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4928 ntohs (msg[1].header.type));
4930 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4933 /* TODO notify back: we don't know this tunnel */
4934 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4935 GNUNET_break_op (0);
4938 pid = ntohl (msg->pid);
4939 if (t->fwd_pid == pid)
4941 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4942 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4943 " Already seen pid %u, DROPPING!\n", pid);
4948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4949 " pid %u not seen yet, forwarding\n", pid);
4951 t->skip += (pid - t->fwd_pid) - 1;
4953 if (is_pid_bigger (pid, t->last_fwd_ack))
4955 GNUNET_STATISTICS_update (stats, "# not allowed unicast", 1, GNUNET_NO);
4956 GNUNET_break_op (0);
4959 tunnel_reset_timeout (t);
4960 dest_id = GNUNET_PEER_search (&msg->destination);
4961 if (dest_id == myid)
4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4964 " it's for us! sending to clients...\n");
4965 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4966 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4967 // ACK is generated by client (api part), service only retransmits.
4970 ttl = ntohl (msg->ttl);
4971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4974 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4975 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4979 " not for us, retransmitting...\n");
4980 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4982 neighbor = tree_get_first_hop (t->tree, dest_id);
4983 cinfo = tunnel_get_neighbor_fc (t, neighbor);
4985 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4988 if (is_pid_bigger(pid, cinfo->fwd_ack))
4990 GNUNET_break_op (0);
4993 send_message (message, neighbor, t);
4999 * Core handler for mesh network traffic going from the origin to all peers
5001 * @param cls closure
5002 * @param message message
5003 * @param peer peer identity this notification is about
5004 * @param atsi performance data
5005 * @param atsi_count number of records in 'atsi'
5006 * @return GNUNET_OK to keep the connection open,
5007 * GNUNET_SYSERR to close it (signal serious error)
5009 * TODO: Check who we got this from, to validate route.
5012 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5013 const struct GNUNET_MessageHeader *message,
5014 const struct GNUNET_ATS_Information *atsi,
5015 unsigned int atsi_count)
5017 struct GNUNET_MESH_Multicast *msg;
5018 struct MeshTunnel *t;
5022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5024 size = ntohs (message->size);
5025 if (sizeof (struct GNUNET_MESH_Multicast) +
5026 sizeof (struct GNUNET_MessageHeader) > size)
5028 GNUNET_break_op (0);
5031 msg = (struct GNUNET_MESH_Multicast *) message;
5032 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5036 /* TODO notify that we dont know that tunnel */
5037 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5038 GNUNET_break_op (0);
5041 pid = ntohl (msg->pid);
5042 if (t->fwd_pid == pid)
5044 /* already seen this packet, drop */
5045 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5047 " Already seen pid %u, DROPPING!\n", pid);
5052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5053 " pid %u not seen yet, forwarding\n", pid);
5055 t->skip += (pid - t->fwd_pid) - 1;
5057 tunnel_reset_timeout (t);
5059 /* Transmit to locally interested clients */
5060 if (NULL != t->peers &&
5061 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5063 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5064 send_subscribed_clients (message, &msg[1].header);
5066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5067 if (ntohl (msg->ttl) == 0)
5069 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5070 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5073 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5074 tunnel_send_multicast (t, message, GNUNET_NO);
5080 * Core handler for mesh network traffic toward the owner of a tunnel
5082 * @param cls closure
5083 * @param message message
5084 * @param peer peer identity this notification is about
5085 * @param atsi performance data
5086 * @param atsi_count number of records in 'atsi'
5088 * @return GNUNET_OK to keep the connection open,
5089 * GNUNET_SYSERR to close it (signal serious error)
5092 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5093 const struct GNUNET_MessageHeader *message,
5094 const struct GNUNET_ATS_Information *atsi,
5095 unsigned int atsi_count)
5097 struct GNUNET_MESH_ToOrigin *msg;
5098 struct GNUNET_PeerIdentity id;
5099 struct MeshPeerInfo *peer_info;
5100 struct MeshTunnel *t;
5103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5105 size = ntohs (message->size);
5106 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5107 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5109 GNUNET_break_op (0);
5112 msg = (struct GNUNET_MESH_ToOrigin *) message;
5113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5114 ntohs (msg[1].header.type));
5115 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5119 /* TODO notify that we dont know this tunnel (whom)? */
5120 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5121 GNUNET_break_op (0);
5125 if (t->id.oid == myid)
5128 struct GNUNET_MESH_ToOrigin *copy;
5130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5131 " it's for us! sending to clients...\n");
5132 if (NULL == t->owner)
5134 /* got data packet for ownerless tunnel */
5135 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
5137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
5138 GNUNET_break_op (0);
5141 /* TODO signature verification */
5142 memcpy (cbuf, message, size);
5143 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5144 copy->tid = htonl (t->local_tid);
5145 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5146 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5147 ©->header, GNUNET_NO);
5150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5151 " not for us, retransmitting...\n");
5153 peer_info = peer_info_get (&msg->oid);
5154 if (NULL == peer_info)
5156 /* unknown origin of tunnel */
5160 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5161 send_message (message, &id, t);
5162 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5169 * Core handler for mesh network traffic point-to-point acks.
5171 * @param cls closure
5172 * @param message message
5173 * @param peer peer identity this notification is about
5174 * @param atsi performance data
5175 * @param atsi_count number of records in 'atsi'
5177 * @return GNUNET_OK to keep the connection open,
5178 * GNUNET_SYSERR to close it (signal serious error)
5181 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5182 const struct GNUNET_MessageHeader *message,
5183 const struct GNUNET_ATS_Information *atsi,
5184 unsigned int atsi_count)
5186 struct GNUNET_MESH_ACK *msg;
5187 struct MeshTunnelChildInfo *cinfo;
5188 struct MeshTunnel *t;
5191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got an ACK packet from %s\n",
5193 msg = (struct GNUNET_MESH_ACK *) message;
5194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5195 ntohs (msg[1].header.type));
5196 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5200 /* TODO notify that we dont know this tunnel (whom)? */
5201 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5202 GNUNET_break_op (0);
5205 ack = ntohl (msg->pid);
5206 cinfo = tunnel_get_neighbor_fc (t, peer);
5207 cinfo->fwd_ack = ack;
5208 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5214 * Core handler for path ACKs
5216 * @param cls closure
5217 * @param message message
5218 * @param peer peer identity this notification is about
5219 * @param atsi performance data
5220 * @param atsi_count number of records in 'atsi'
5222 * @return GNUNET_OK to keep the connection open,
5223 * GNUNET_SYSERR to close it (signal serious error)
5226 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5227 const struct GNUNET_MessageHeader *message,
5228 const struct GNUNET_ATS_Information *atsi,
5229 unsigned int atsi_count)
5231 struct GNUNET_MESH_PathACK *msg;
5232 struct GNUNET_PeerIdentity id;
5233 struct MeshPeerInfo *peer_info;
5234 struct MeshPeerPath *p;
5235 struct MeshTunnel *t;
5237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5238 GNUNET_i2s (&my_full_id));
5239 msg = (struct GNUNET_MESH_PathACK *) message;
5240 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5243 /* TODO notify that we don't know the tunnel */
5244 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5246 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5250 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5252 peer_info = peer_info_get (&msg->peer_id);
5253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5254 GNUNET_i2s (&msg->peer_id));
5255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5258 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5261 "connect_by_string completed, stopping search\n");
5262 regex_cancel_search (t->regex_ctx);
5263 t->regex_ctx = NULL;
5266 /* Add paths to peers? */
5267 p = tree_get_path_to_peer (t->tree, peer_info->id);
5270 path_add_to_peers (p, GNUNET_YES);
5278 /* Message for us? */
5279 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5282 if (NULL == t->owner)
5284 GNUNET_break_op (0);
5287 if (NULL != t->dht_get_type)
5289 GNUNET_DHT_get_stop (t->dht_get_type);
5290 t->dht_get_type = NULL;
5292 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5294 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5295 send_client_peer_connected (t, peer_info->id);
5300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5301 " not for us, retransmitting...\n");
5302 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5303 peer_info = peer_info_get (&msg->oid);
5304 if (NULL == peer_info)
5306 /* If we know the tunnel, we should DEFINITELY know the peer */
5310 send_message (message, &id, t);
5316 * Functions to handle messages from core
5318 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5319 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5320 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5321 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5322 sizeof (struct GNUNET_MESH_PathBroken)},
5323 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5324 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5325 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5326 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5327 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5328 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5329 sizeof (struct GNUNET_MESH_ACK)},
5330 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5331 sizeof (struct GNUNET_MESH_PathACK)},
5337 /******************************************************************************/
5338 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5339 /******************************************************************************/
5342 * deregister_app: iterator for removing each application registered by a client
5344 * @param cls closure
5345 * @param key the hash of the application id (used to access the hashmap)
5346 * @param value the value stored at the key (client)
5348 * @return GNUNET_OK on success
5351 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5353 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5354 GNUNET_break (GNUNET_YES ==
5355 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5361 * notify_client_connection_failure: notify a client that the connection to the
5362 * requested remote peer is not possible (for instance, no route found)
5363 * Function called when the socket is ready to queue more data. "buf" will be
5364 * NULL and "size" zero if the socket was closed for writing in the meantime.
5366 * @param cls closure
5367 * @param size number of bytes available in buf
5368 * @param buf where the callee should write the message
5369 * @return number of bytes written to buf
5372 notify_client_connection_failure (void *cls, size_t size, void *buf)
5375 struct MeshPeerInfo *peer_info;
5376 struct GNUNET_MESH_PeerControl *msg;
5377 struct GNUNET_PeerIdentity id;
5379 if (0 == size && NULL == buf)
5381 // TODO retry? cancel?
5385 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5386 peer_info = (struct MeshPeerInfo *) cls;
5387 msg = (struct GNUNET_MESH_PeerControl *) buf;
5388 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5389 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5390 // msg->tunnel_id = htonl(peer_info->t->tid);
5391 GNUNET_PEER_resolve (peer_info->id, &id);
5392 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5400 * Send keepalive packets for a peer
5402 * @param cls Closure (tunnel for which to send the keepalive).
5403 * @param tc Notification context.
5405 * TODO: implement explicit multicast keepalive?
5408 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5410 struct MeshTunnel *t = cls;
5411 struct GNUNET_MessageHeader *payload;
5412 struct GNUNET_MESH_Multicast *msg;
5414 sizeof (struct GNUNET_MESH_Multicast) +
5415 sizeof (struct GNUNET_MessageHeader);
5418 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5422 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5425 "sending keepalive for tunnel %d\n", t->id.tid);
5427 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5428 msg->header.size = htons (size);
5429 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5430 msg->oid = my_full_id;
5431 msg->tid = htonl (t->id.tid);
5432 msg->ttl = htonl (default_ttl);
5433 msg->pid = htonl (t->fwd_pid + 1);
5435 payload = (struct GNUNET_MessageHeader *) &msg[1];
5436 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5437 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5438 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5440 t->path_refresh_task =
5441 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5447 * Function to process paths received for a new peer addition. The recorded
5448 * paths form the initial tunnel, which can be optimized later.
5449 * Called on each result obtained for the DHT search.
5451 * @param cls closure
5452 * @param exp when will this value expire
5453 * @param key key of the result
5454 * @param get_path path of the get request
5455 * @param get_path_length lenght of get_path
5456 * @param put_path path of the put request
5457 * @param put_path_length length of the put_path
5458 * @param type type of the result
5459 * @param size number of bytes in data
5460 * @param data pointer to the result data
5462 * TODO: re-issue the request after certain time? cancel after X results?
5465 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5466 const struct GNUNET_HashCode * key,
5467 const struct GNUNET_PeerIdentity *get_path,
5468 unsigned int get_path_length,
5469 const struct GNUNET_PeerIdentity *put_path,
5470 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5471 size_t size, const void *data)
5473 struct MeshPathInfo *path_info = cls;
5474 struct MeshPeerPath *p;
5475 struct GNUNET_PeerIdentity pi;
5478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5479 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5482 p = path_build_from_dht (get_path, get_path_length, put_path,
5484 path_add_to_peers (p, GNUNET_NO);
5486 for (i = 0; i < path_info->peer->ntunnels; i++)
5488 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5489 peer_info_connect (path_info->peer, path_info->t);
5497 * Function to process paths received for a new peer addition. The recorded
5498 * paths form the initial tunnel, which can be optimized later.
5499 * Called on each result obtained for the DHT search.
5501 * @param cls closure
5502 * @param exp when will this value expire
5503 * @param key key of the result
5504 * @param get_path path of the get request
5505 * @param get_path_length lenght of get_path
5506 * @param put_path path of the put request
5507 * @param put_path_length length of the put_path
5508 * @param type type of the result
5509 * @param size number of bytes in data
5510 * @param data pointer to the result data
5513 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5514 const struct GNUNET_HashCode * key,
5515 const struct GNUNET_PeerIdentity *get_path,
5516 unsigned int get_path_length,
5517 const struct GNUNET_PeerIdentity *put_path,
5518 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5519 size_t size, const void *data)
5521 const struct PBlock *pb = data;
5522 const struct GNUNET_PeerIdentity *pi = &pb->id;
5523 struct MeshTunnel *t = cls;
5524 struct MeshPeerInfo *peer_info;
5525 struct MeshPeerPath *p;
5527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5528 if (size != sizeof (struct PBlock))
5530 GNUNET_break_op (0);
5533 if (ntohl(pb->type) != t->type)
5535 GNUNET_break_op (0);
5538 GNUNET_assert (NULL != t->owner);
5539 peer_info = peer_info_get (pi);
5540 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5542 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5544 p = path_build_from_dht (get_path, get_path_length, put_path,
5546 path_add_to_peers (p, GNUNET_NO);
5548 tunnel_add_peer (t, peer_info);
5549 peer_info_connect (peer_info, t);
5554 * Function to process DHT string to regex matching.
5555 * Called on each result obtained for the DHT search.
5557 * @param cls closure (search context)
5558 * @param exp when will this value expire
5559 * @param key key of the result
5560 * @param get_path path of the get request (not used)
5561 * @param get_path_length lenght of get_path (not used)
5562 * @param put_path path of the put request (not used)
5563 * @param put_path_length length of the put_path (not used)
5564 * @param type type of the result
5565 * @param size number of bytes in data
5566 * @param data pointer to the result data
5569 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5570 const struct GNUNET_HashCode * key,
5571 const struct GNUNET_PeerIdentity *get_path,
5572 unsigned int get_path_length,
5573 const struct GNUNET_PeerIdentity *put_path,
5574 unsigned int put_path_length,
5575 enum GNUNET_BLOCK_Type type,
5576 size_t size, const void *data)
5578 const struct MeshRegexAccept *block = data;
5579 struct MeshRegexSearchContext *ctx = cls;
5580 struct MeshRegexSearchInfo *info = ctx->info;
5581 struct MeshPeerPath *p;
5582 struct MeshPeerInfo *peer_info;
5584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5587 peer_info = peer_info_get(&block->id);
5588 p = path_build_from_dht (get_path, get_path_length, put_path,
5590 path_add_to_peers (p, GNUNET_NO);
5593 tunnel_add_peer (info->t, peer_info);
5594 peer_info_connect (peer_info, info->t);
5595 if (0 == info->peer)
5597 info->peer = peer_info->id;
5601 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5604 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5605 ®ex_connect_timeout,
5613 * Function to process DHT string to regex matching.
5614 * Called on each result obtained for the DHT search.
5616 * @param cls closure (search context)
5617 * @param exp when will this value expire
5618 * @param key key of the result
5619 * @param get_path path of the get request (not used)
5620 * @param get_path_length lenght of get_path (not used)
5621 * @param put_path path of the put request (not used)
5622 * @param put_path_length length of the put_path (not used)
5623 * @param type type of the result
5624 * @param size number of bytes in data
5625 * @param data pointer to the result data
5627 * TODO: re-issue the request after certain time? cancel after X results?
5630 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5631 const struct GNUNET_HashCode * key,
5632 const struct GNUNET_PeerIdentity *get_path,
5633 unsigned int get_path_length,
5634 const struct GNUNET_PeerIdentity *put_path,
5635 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5636 size_t size, const void *data)
5638 const struct MeshRegexBlock *block = data;
5639 struct MeshRegexSearchContext *ctx = cls;
5640 struct MeshRegexSearchInfo *info = ctx->info;
5644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5645 "DHT GET STRING RETURNED RESULTS\n");
5646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5647 " key: %s\n", GNUNET_h2s (key));
5649 copy = GNUNET_malloc (size);
5650 memcpy (copy, data, size);
5651 GNUNET_break (GNUNET_OK ==
5652 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5653 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5654 len = ntohl (block->n_proof);
5656 char proof[len + 1];
5658 memcpy (proof, &block[1], len);
5660 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5662 GNUNET_break_op (0);
5666 len = strlen (info->description);
5667 if (len == ctx->position) // String processed
5669 if (GNUNET_YES == ntohl (block->accepting))
5671 regex_find_path(key, ctx);
5675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5676 // FIXME REGEX this block not successful, wait for more? start timeout?
5680 GNUNET_break (GNUNET_OK ==
5681 GNUNET_MESH_regex_block_iterate (block, size,
5682 ®ex_edge_iterator, ctx));
5686 /******************************************************************************/
5687 /********************* MESH LOCAL HANDLES **************************/
5688 /******************************************************************************/
5692 * Handler for client disconnection
5694 * @param cls closure
5695 * @param client identification of the client; NULL
5696 * for the last call when the server is destroyed
5699 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5701 struct MeshClient *c;
5702 struct MeshClient *next;
5705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5714 if (c->handle != client)
5716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5722 GNUNET_SERVER_client_drop (c->handle);
5723 c->shutting_down = GNUNET_YES;
5724 GNUNET_assert (NULL != c->own_tunnels);
5725 GNUNET_assert (NULL != c->incoming_tunnels);
5726 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5727 &tunnel_destroy_iterator, c);
5728 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5729 &tunnel_destroy_iterator, c);
5730 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5731 &tunnel_destroy_iterator, c);
5732 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5733 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5734 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5736 /* deregister clients applications */
5737 if (NULL != c->apps)
5739 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5740 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5742 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5743 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5745 GNUNET_SCHEDULER_cancel (announce_applications_task);
5746 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5748 if (NULL != c->types)
5749 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5750 for (i = 0; i < c->n_regex; i++)
5752 GNUNET_free (c->regexes[i]);
5754 GNUNET_free_non_null (c->regexes);
5755 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5756 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5758 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5761 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5770 * Handler for new clients
5772 * @param cls closure
5773 * @param client identification of the client
5774 * @param message the actual message, which includes messages the client wants
5777 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5778 const struct GNUNET_MessageHeader *message)
5780 struct GNUNET_MESH_ClientConnect *cc_msg;
5781 struct MeshClient *c;
5782 GNUNET_MESH_ApplicationType *a;
5789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5790 /* Check data sanity */
5791 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5792 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5793 ntypes = ntohs (cc_msg->types);
5794 napps = ntohs (cc_msg->applications);
5796 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5799 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5803 /* Create new client structure */
5804 c = GNUNET_malloc (sizeof (struct MeshClient));
5805 c->id = next_client_id++;
5806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5808 GNUNET_SERVER_client_keep (client);
5809 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5812 GNUNET_MESH_ApplicationType at;
5813 struct GNUNET_HashCode hc;
5815 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5816 for (i = 0; i < napps; i++)
5819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5820 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5821 /* store in clients hashmap */
5822 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5823 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5824 /* store in global hashmap, for announcements */
5825 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5826 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5828 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5829 announce_applications_task =
5830 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5836 struct GNUNET_HashCode hc;
5838 t = (uint16_t *) & a[napps];
5839 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5840 for (i = 0; i < ntypes; i++)
5843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5844 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5846 /* store in clients hashmap */
5847 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5848 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5849 /* store in global hashmap */
5850 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5851 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5855 " client has %u+%u subscriptions\n", napps, ntypes);
5857 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5858 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5859 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5860 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5861 GNUNET_SERVER_notification_context_add (nc, client);
5862 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5864 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5870 * Handler for clients announcing available services by a regular expression.
5872 * @param cls closure
5873 * @param client identification of the client
5874 * @param message the actual message, which includes messages the client wants
5877 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5878 const struct GNUNET_MessageHeader *message)
5880 struct MeshClient *c;
5884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5886 /* Sanity check for client registration */
5887 if (NULL == (c = client_get (client)))
5890 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5895 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5896 regex = GNUNET_malloc (len + 1);
5897 memcpy (regex, &message[1], len);
5899 GNUNET_array_append (c->regexes, c->n_regex, regex);
5900 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5902 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5908 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5914 * Handler for requests of new tunnels
5916 * @param cls closure
5917 * @param client identification of the client
5918 * @param message the actual message
5921 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5922 const struct GNUNET_MessageHeader *message)
5924 struct GNUNET_MESH_TunnelMessage *t_msg;
5925 struct MeshTunnel *t;
5926 struct MeshClient *c;
5927 MESH_TunnelNumber tid;
5929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5931 /* Sanity check for client registration */
5932 if (NULL == (c = client_get (client)))
5935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5940 /* Message sanity check */
5941 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5948 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5949 /* Sanity check for tunnel numbering */
5950 tid = ntohl (t_msg->tunnel_id);
5951 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5954 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5957 /* Sanity check for duplicate tunnel IDs */
5958 if (NULL != tunnel_get_by_local_id (c, tid))
5961 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5965 while (NULL != tunnel_get_by_pi (myid, next_tid))
5966 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5967 t = tunnel_new (myid, next_tid++, c, tid);
5970 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5972 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5975 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5977 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5978 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
5980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5981 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5987 * Handler for requests of deleting tunnels
5989 * @param cls closure
5990 * @param client identification of the client
5991 * @param message the actual message
5994 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5995 const struct GNUNET_MessageHeader *message)
5997 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5998 struct MeshClient *c;
5999 struct MeshTunnel *t;
6000 MESH_TunnelNumber tid;
6002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6003 "Got a DESTROY TUNNEL from client!\n");
6005 /* Sanity check for client registration */
6006 if (NULL == (c = client_get (client)))
6009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6012 /* Message sanity check */
6013 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6016 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6020 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6022 /* Retrieve tunnel */
6023 tid = ntohl (tunnel_msg->tunnel_id);
6024 t = tunnel_get_by_local_id(c, tid);
6027 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6032 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6034 client_ignore_tunnel (c, t);
6036 // TODO: when to destroy incoming tunnel?
6037 if (t->nclients == 0)
6039 GNUNET_assert (GNUNET_YES ==
6040 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6042 GNUNET_assert (GNUNET_YES ==
6043 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6044 &my_full_id.hashPubKey,
6048 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6051 send_client_tunnel_disconnect(t, c);
6052 client_delete_tunnel(c, t);
6054 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6056 tunnel_send_destroy (t);
6057 t->destroy = GNUNET_YES;
6058 // The tunnel will be destroyed when the last message is transmitted.
6059 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6065 * Handler for requests of seeting tunnel's speed.
6067 * @param cls Closure (unused).
6068 * @param client Identification of the client.
6069 * @param message The actual message.
6072 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6073 const struct GNUNET_MessageHeader *message)
6075 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6076 struct MeshClient *c;
6077 struct MeshTunnel *t;
6078 MESH_TunnelNumber tid;
6080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6081 "Got a SPEED request from client!\n");
6083 /* Sanity check for client registration */
6084 if (NULL == (c = client_get (client)))
6087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6092 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6094 /* Retrieve tunnel */
6095 tid = ntohl (tunnel_msg->tunnel_id);
6096 t = tunnel_get_by_local_id(c, tid);
6099 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6101 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6105 switch (ntohs(message->type))
6107 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6108 t->speed_min = GNUNET_YES;
6110 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6111 t->speed_min = GNUNET_NO;
6116 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6121 * Handler for requests of seeting tunnel's buffering policy.
6123 * @param cls Closure (unused).
6124 * @param client Identification of the client.
6125 * @param message The actual message.
6128 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6129 const struct GNUNET_MessageHeader *message)
6131 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6132 struct MeshClient *c;
6133 struct MeshTunnel *t;
6134 MESH_TunnelNumber tid;
6136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6137 "Got a BUFFER request from client!\n");
6139 /* Sanity check for client registration */
6140 if (NULL == (c = client_get (client)))
6143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6148 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6150 /* Retrieve tunnel */
6151 tid = ntohl (tunnel_msg->tunnel_id);
6152 t = tunnel_get_by_local_id(c, tid);
6155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6161 switch (ntohs(message->type))
6163 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6164 t->nobuffer = GNUNET_NO;
6166 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6167 t->nobuffer = GNUNET_YES;
6173 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6178 * Handler for connection requests to new peers
6180 * @param cls closure
6181 * @param client identification of the client
6182 * @param message the actual message (PeerControl)
6185 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6186 const struct GNUNET_MessageHeader *message)
6188 struct GNUNET_MESH_PeerControl *peer_msg;
6189 struct MeshPeerInfo *peer_info;
6190 struct MeshClient *c;
6191 struct MeshTunnel *t;
6192 MESH_TunnelNumber tid;
6194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6195 /* Sanity check for client registration */
6196 if (NULL == (c = client_get (client)))
6199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6203 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6204 /* Sanity check for message size */
6205 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6212 /* Tunnel exists? */
6213 tid = ntohl (peer_msg->tunnel_id);
6214 t = tunnel_get_by_local_id (c, tid);
6218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6222 /* Does client own tunnel? */
6223 if (t->owner->handle != client)
6226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6230 GNUNET_i2s (&peer_msg->peer));
6231 peer_info = peer_info_get (&peer_msg->peer);
6233 tunnel_add_peer (t, peer_info);
6234 peer_info_connect (peer_info, t);
6236 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6242 * Handler for disconnection requests of peers in a tunnel
6244 * @param cls closure
6245 * @param client identification of the client
6246 * @param message the actual message (PeerControl)
6249 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6250 const struct GNUNET_MessageHeader *message)
6252 struct GNUNET_MESH_PeerControl *peer_msg;
6253 struct MeshPeerInfo *peer_info;
6254 struct MeshClient *c;
6255 struct MeshTunnel *t;
6256 MESH_TunnelNumber tid;
6258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6259 /* Sanity check for client registration */
6260 if (NULL == (c = client_get (client)))
6263 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6266 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6267 /* Sanity check for message size */
6268 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6271 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6275 /* Tunnel exists? */
6276 tid = ntohl (peer_msg->tunnel_id);
6277 t = tunnel_get_by_local_id (c, tid);
6281 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6286 /* Does client own tunnel? */
6287 if (t->owner->handle != client)
6290 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6295 GNUNET_i2s (&peer_msg->peer));
6296 /* Is the peer in the tunnel? */
6298 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6299 if (NULL == peer_info)
6302 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6306 /* Ok, delete peer from tunnel */
6307 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6308 &peer_msg->peer.hashPubKey);
6310 send_destroy_path (t, peer_info->id);
6311 tunnel_delete_peer (t, peer_info->id);
6312 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6317 * Handler for blacklist requests of peers in a tunnel
6319 * @param cls closure
6320 * @param client identification of the client
6321 * @param message the actual message (PeerControl)
6324 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6325 const struct GNUNET_MessageHeader *message)
6327 struct GNUNET_MESH_PeerControl *peer_msg;
6328 struct MeshClient *c;
6329 struct MeshTunnel *t;
6330 MESH_TunnelNumber tid;
6332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6333 /* Sanity check for client registration */
6334 if (NULL == (c = client_get (client)))
6337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6340 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6342 /* Sanity check for message size */
6343 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6350 /* Tunnel exists? */
6351 tid = ntohl (peer_msg->tunnel_id);
6352 t = tunnel_get_by_local_id (c, tid);
6356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6361 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6362 GNUNET_PEER_intern(&peer_msg->peer));
6367 * Handler for unblacklist requests of peers in a tunnel
6369 * @param cls closure
6370 * @param client identification of the client
6371 * @param message the actual message (PeerControl)
6374 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6375 const struct GNUNET_MessageHeader *message)
6377 struct GNUNET_MESH_PeerControl *peer_msg;
6378 struct MeshClient *c;
6379 struct MeshTunnel *t;
6380 MESH_TunnelNumber tid;
6384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6385 /* Sanity check for client registration */
6386 if (NULL == (c = client_get (client)))
6389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6392 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6394 /* Sanity check for message size */
6395 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6402 /* Tunnel exists? */
6403 tid = ntohl (peer_msg->tunnel_id);
6404 t = tunnel_get_by_local_id (c, tid);
6408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6413 /* if peer is not known, complain */
6414 pid = GNUNET_PEER_search (&peer_msg->peer);
6421 /* search and remove from list */
6422 for (i = 0; i < t->nblacklisted; i++)
6424 if (t->blacklisted[i] == pid)
6426 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6427 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6432 /* if peer hasn't been blacklisted, complain */
6438 * Handler for connection requests to new peers by type
6440 * @param cls closure
6441 * @param client identification of the client
6442 * @param message the actual message (ConnectPeerByType)
6445 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6446 const struct GNUNET_MessageHeader *message)
6448 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6449 struct MeshClient *c;
6450 struct MeshTunnel *t;
6451 struct GNUNET_HashCode hash;
6452 MESH_TunnelNumber tid;
6454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6455 /* Sanity check for client registration */
6456 if (NULL == (c = client_get (client)))
6459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6463 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6464 /* Sanity check for message size */
6465 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6466 ntohs (connect_msg->header.size))
6469 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6473 /* Tunnel exists? */
6474 tid = ntohl (connect_msg->tunnel_id);
6475 t = tunnel_get_by_local_id (c, tid);
6479 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6483 /* Does client own tunnel? */
6484 if (t->owner->handle != client)
6487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6491 /* Do WE have the service? */
6492 t->type = ntohl (connect_msg->type);
6493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6494 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6495 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6498 /* Yes! Fast forward, add ourselves to the tunnel and send the
6499 * good news to the client, and alert the destination client of
6500 * an incoming tunnel.
6502 * FIXME send a path create to self, avoid code duplication
6504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6505 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6506 peer_info_get (&my_full_id),
6507 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6510 send_client_peer_connected (t, myid);
6511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6512 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6514 t->local_tid_dest = next_local_tid++;
6515 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6516 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6517 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6521 /* Ok, lets find a peer offering the service */
6522 if (NULL != t->dht_get_type)
6524 GNUNET_DHT_get_stop (t->dht_get_type);
6526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6527 GNUNET_h2s (&hash));
6529 GNUNET_DHT_get_start (dht_handle,
6530 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6532 dht_replication_level,
6533 GNUNET_DHT_RO_RECORD_ROUTE |
6534 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6536 &dht_get_type_handler, t);
6538 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6544 * Handler for connection requests to new peers by a string service description.
6546 * @param cls closure
6547 * @param client identification of the client
6548 * @param message the actual message, which includes messages the client wants
6551 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6552 const struct GNUNET_MessageHeader *message)
6554 struct GNUNET_MESH_ConnectPeerByString *msg;
6555 struct MeshRegexSearchContext *ctx;
6556 struct MeshRegexSearchInfo *info;
6557 struct GNUNET_DHT_GetHandle *get_h;
6558 struct GNUNET_HashCode key;
6559 struct MeshTunnel *t;
6560 struct MeshClient *c;
6561 MESH_TunnelNumber tid;
6565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6566 "Connect by string started\n");
6567 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6568 size = htons (message->size);
6570 /* Sanity check for client registration */
6571 if (NULL == (c = client_get (client)))
6574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6578 /* Message size sanity check */
6579 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6586 /* Tunnel exists? */
6587 tid = ntohl (msg->tunnel_id);
6588 t = tunnel_get_by_local_id (c, tid);
6592 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6596 /* Does client own tunnel? */
6597 if (t->owner->handle != client)
6600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6605 " on tunnel %s [%u]\n",
6606 GNUNET_i2s(&my_full_id),
6609 /* Only one connect_by_string allowed at the same time! */
6610 /* FIXME: allow more, return handle at api level to cancel, document */
6611 if (NULL != t->regex_ctx)
6614 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6618 /* Find string itself */
6619 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6620 string = (const char *) &msg[1];
6622 /* Initialize context */
6623 size = GNUNET_REGEX_get_first_key(string, len, &key);
6624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6625 " consumed %u bits out of %u\n", size, len);
6626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6627 " looking for %s\n", GNUNET_h2s (&key));
6629 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6631 info->description = GNUNET_malloc (len + 1);
6632 memcpy (info->description, string, len);
6633 info->description[len] = '\0';
6634 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6635 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6638 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6639 ctx->position = size;
6643 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6645 /* Start search in DHT */
6646 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6647 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6648 &key, /* key to search */
6649 dht_replication_level, /* replication level */
6650 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6651 NULL, /* xquery */ // FIXME BLOOMFILTER
6652 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6653 &dht_get_string_handler, ctx);
6655 GNUNET_break (GNUNET_OK ==
6656 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6659 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6661 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6667 * Handler for client traffic directed to one peer
6669 * @param cls closure
6670 * @param client identification of the client
6671 * @param message the actual message
6674 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6675 const struct GNUNET_MessageHeader *message)
6677 struct MeshClient *c;
6678 struct MeshTunnel *t;
6679 struct MeshPeerInfo *pi;
6680 struct GNUNET_MESH_Unicast *data_msg;
6681 MESH_TunnelNumber tid;
6684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6685 "Got a unicast request from a client!\n");
6687 /* Sanity check for client registration */
6688 if (NULL == (c = client_get (client)))
6691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6694 data_msg = (struct GNUNET_MESH_Unicast *) message;
6695 /* Sanity check for message size */
6696 size = ntohs (message->size);
6697 if (sizeof (struct GNUNET_MESH_Unicast) +
6698 sizeof (struct GNUNET_MessageHeader) > size)
6701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6705 /* Tunnel exists? */
6706 tid = ntohl (data_msg->tid);
6707 t = tunnel_get_by_local_id (c, tid);
6711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6715 /* Is it a local tunnel? Then, does client own the tunnel? */
6716 if (t->owner->handle != client)
6719 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6723 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6724 &data_msg->destination.hashPubKey);
6725 /* Is the selected peer in the tunnel? */
6729 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6733 /* Ok, everything is correct, send the message
6734 * (pretend we got it from a mesh peer)
6737 char buf[ntohs (message->size)] GNUNET_ALIGN;
6738 struct GNUNET_MESH_Unicast *copy;
6740 /* Work around const limitation */
6741 copy = (struct GNUNET_MESH_Unicast *) buf;
6742 memcpy (buf, data_msg, size);
6743 copy->oid = my_full_id;
6744 copy->tid = htonl (t->id.tid);
6745 copy->ttl = htonl (default_ttl);
6746 copy->pid = htonl (t->fwd_pid + 1);
6747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6748 " calling generic handler...\n");
6749 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6752 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6758 * Handler for client traffic directed to the origin
6760 * @param cls closure
6761 * @param client identification of the client
6762 * @param message the actual message
6765 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6766 const struct GNUNET_MessageHeader *message)
6768 struct GNUNET_MESH_ToOrigin *data_msg;
6769 struct GNUNET_PeerIdentity id;
6770 struct MeshClient *c;
6771 struct MeshTunnel *t;
6772 MESH_TunnelNumber tid;
6775 /* Sanity check for client registration */
6776 if (NULL == (c = client_get (client)))
6779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6782 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6783 /* Sanity check for message size */
6784 size = ntohs (message->size);
6785 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6786 sizeof (struct GNUNET_MessageHeader) > size)
6789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6793 /* Tunnel exists? */
6794 tid = ntohl (data_msg->tid);
6795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6796 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6797 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6803 t = tunnel_get_by_local_id (c, tid);
6807 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6811 /* It should be sent by someone who has this as incoming tunnel. */
6812 if (-1 == client_knows_tunnel (c, t))
6815 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6818 GNUNET_PEER_resolve (t->id.oid, &id);
6820 /* Ok, everything is correct, send the message
6821 * (pretend we got it from a mesh peer)
6824 char buf[ntohs (message->size)] GNUNET_ALIGN;
6825 struct GNUNET_MESH_ToOrigin *copy;
6827 /* Work around const limitation */
6828 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6829 memcpy (buf, data_msg, size);
6831 copy->tid = htonl (t->id.tid);
6832 copy->sender = my_full_id;
6833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6834 " calling generic handler...\n");
6835 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6837 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6843 * Handler for client traffic directed to all peers in a tunnel
6845 * @param cls closure
6846 * @param client identification of the client
6847 * @param message the actual message
6850 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6851 const struct GNUNET_MessageHeader *message)
6853 struct MeshClient *c;
6854 struct MeshTunnel *t;
6855 struct GNUNET_MESH_Multicast *data_msg;
6856 MESH_TunnelNumber tid;
6858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6859 "Got a multicast request from a client!\n");
6861 /* Sanity check for client registration */
6862 if (NULL == (c = client_get (client)))
6865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6868 data_msg = (struct GNUNET_MESH_Multicast *) message;
6869 /* Sanity check for message size */
6870 if (sizeof (struct GNUNET_MESH_Multicast) +
6871 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6874 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6878 /* Tunnel exists? */
6879 tid = ntohl (data_msg->tid);
6880 t = tunnel_get_by_local_id (c, tid);
6884 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6888 /* Does client own tunnel? */
6889 if (t->owner->handle != client)
6892 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6897 char buf[ntohs (message->size)] GNUNET_ALIGN;
6898 struct GNUNET_MESH_Multicast *copy;
6900 copy = (struct GNUNET_MESH_Multicast *) buf;
6901 memcpy (buf, message, ntohs (message->size));
6902 copy->oid = my_full_id;
6903 copy->tid = htonl (t->id.tid);
6904 copy->ttl = htonl (default_ttl);
6905 copy->pid = htonl (t->fwd_pid + 1);
6906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6907 " calling generic handler...\n");
6908 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
6911 /* receive done gets called when last copy is sent to a neighbor */
6917 * Handler for client's ACKs for payload traffic.
6919 * @param cls Closure (unused).
6920 * @param client Identification of the client.
6921 * @param message The actual message.
6924 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6925 const struct GNUNET_MessageHeader *message)
6927 struct GNUNET_MESH_LocalAck *msg;
6928 struct MeshTunnel *t;
6929 struct MeshClient *c;
6930 MESH_TunnelNumber tid;
6932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6933 /* Sanity check for client registration */
6934 if (NULL == (c = client_get (client)))
6937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6941 msg = (struct GNUNET_MESH_LocalAck *) message;
6942 /* Tunnel exists? */
6943 tid = ntohl (msg->tunnel_id);
6944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6945 t = tunnel_get_by_local_id (c, tid);
6949 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6953 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
6954 if (NULL != t->owner && t->owner->handle == client)
6956 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
6957 t->bck_ack = ntohl(msg->max_pid);
6958 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
6962 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
6963 tunnel_set_client_fwd_ack (t, c, ntohl (msg->max_pid));
6964 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
6967 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6973 * Functions to handle messages from clients
6975 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6976 {&handle_local_new_client, NULL,
6977 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6978 {&handle_local_announce_regex, NULL,
6979 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
6980 {&handle_local_tunnel_create, NULL,
6981 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6982 sizeof (struct GNUNET_MESH_TunnelMessage)},
6983 {&handle_local_tunnel_destroy, NULL,
6984 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6985 sizeof (struct GNUNET_MESH_TunnelMessage)},
6986 {&handle_local_tunnel_speed, NULL,
6987 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
6988 sizeof (struct GNUNET_MESH_TunnelMessage)},
6989 {&handle_local_tunnel_speed, NULL,
6990 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
6991 sizeof (struct GNUNET_MESH_TunnelMessage)},
6992 {&handle_local_tunnel_buffer, NULL,
6993 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6994 sizeof (struct GNUNET_MESH_TunnelMessage)},
6995 {&handle_local_tunnel_buffer, NULL,
6996 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6997 sizeof (struct GNUNET_MESH_TunnelMessage)},
6998 {&handle_local_connect_add, NULL,
6999 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7000 sizeof (struct GNUNET_MESH_PeerControl)},
7001 {&handle_local_connect_del, NULL,
7002 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7003 sizeof (struct GNUNET_MESH_PeerControl)},
7004 {&handle_local_blacklist, NULL,
7005 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7006 sizeof (struct GNUNET_MESH_PeerControl)},
7007 {&handle_local_unblacklist, NULL,
7008 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7009 sizeof (struct GNUNET_MESH_PeerControl)},
7010 {&handle_local_connect_by_type, NULL,
7011 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7012 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7013 {&handle_local_connect_by_string, NULL,
7014 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7015 {&handle_local_unicast, NULL,
7016 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7017 {&handle_local_to_origin, NULL,
7018 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7019 {&handle_local_multicast, NULL,
7020 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7021 {&handle_local_ack, NULL,
7022 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7023 sizeof (struct GNUNET_MESH_LocalAck)},
7029 * To be called on core init/fail.
7031 * @param cls service closure
7032 * @param server handle to the server for this service
7033 * @param identity the public identity of this peer
7036 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7037 const struct GNUNET_PeerIdentity *identity)
7039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7040 core_handle = server;
7041 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7044 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7045 GNUNET_SCHEDULER_shutdown ();
7052 * Method called whenever a given peer connects.
7054 * @param cls closure
7055 * @param peer peer identity this notification is about
7056 * @param atsi performance data for the connection
7057 * @param atsi_count number of records in 'atsi'
7060 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7061 const struct GNUNET_ATS_Information *atsi,
7062 unsigned int atsi_count)
7064 struct MeshPeerInfo *peer_info;
7065 struct MeshPeerPath *path;
7067 DEBUG_CONN ("Peer connected\n");
7068 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7069 peer_info = peer_info_get (peer);
7070 if (myid == peer_info->id)
7072 DEBUG_CONN (" (self)\n");
7077 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7079 path = path_new (2);
7080 path->peers[0] = myid;
7081 path->peers[1] = peer_info->id;
7082 GNUNET_PEER_change_rc (myid, 1);
7083 GNUNET_PEER_change_rc (peer_info->id, 1);
7084 peer_info_add_path (peer_info, path, GNUNET_YES);
7085 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7091 * Method called whenever a peer disconnects.
7093 * @param cls closure
7094 * @param peer peer identity this notification is about
7097 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7099 struct MeshPeerInfo *pi;
7100 struct MeshPeerQueue *q;
7101 struct MeshPeerQueue *n;
7103 DEBUG_CONN ("Peer disconnected\n");
7104 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7116 /* try to reroute this traffic instead */
7117 queue_destroy(q, GNUNET_YES);
7121 peer_info_remove_path (pi, pi->id, myid);
7124 DEBUG_CONN (" (self)\n");
7126 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7131 /******************************************************************************/
7132 /************************ MAIN FUNCTIONS ****************************/
7133 /******************************************************************************/
7136 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7138 * @param cls closure
7139 * @param key current key code
7140 * @param value value in the hash map
7141 * @return GNUNET_YES if we should continue to iterate,
7145 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7147 struct MeshTunnel *t = value;
7154 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7156 * @param cls closure
7157 * @param key current key code
7158 * @param value value in the hash map
7159 * @return GNUNET_YES if we should continue to iterate,
7163 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7165 struct MeshPeerInfo *p = value;
7166 struct MeshPeerQueue *q;
7167 struct MeshPeerQueue *n;
7175 queue_destroy(q, GNUNET_YES);
7179 peer_info_destroy (p);
7184 * Task run during shutdown.
7190 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7194 if (core_handle != NULL)
7196 GNUNET_CORE_disconnect (core_handle);
7199 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7200 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7201 if (dht_handle != NULL)
7203 GNUNET_DHT_disconnect (dht_handle);
7208 GNUNET_SERVER_notification_context_destroy (nc);
7211 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7213 GNUNET_SCHEDULER_cancel (announce_id_task);
7214 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7220 * Process mesh requests.
7222 * @param cls closure
7223 * @param server the initialized server
7224 * @param c configuration to use
7227 run (void *cls, struct GNUNET_SERVER_Handle *server,
7228 const struct GNUNET_CONFIGURATION_Handle *c)
7230 struct MeshPeerInfo *peer;
7231 struct MeshPeerPath *p;
7234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7235 server_handle = server;
7236 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7237 NULL, /* Closure passed to MESH functions */
7238 &core_init, /* Call core_init once connected */
7239 &core_connect, /* Handle connects */
7240 &core_disconnect, /* remove peers on disconnects */
7241 NULL, /* Don't notify about all incoming messages */
7242 GNUNET_NO, /* For header only in notification */
7243 NULL, /* Don't notify about all outbound messages */
7244 GNUNET_NO, /* For header-only out notification */
7245 core_handlers); /* Register these handlers */
7247 if (core_handle == NULL)
7250 GNUNET_SCHEDULER_shutdown ();
7255 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7260 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7262 GNUNET_SCHEDULER_shutdown ();
7267 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7268 &refresh_path_time))
7270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7272 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7273 "refresh path time");
7274 GNUNET_SCHEDULER_shutdown ();
7279 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7280 &app_announce_time))
7282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7284 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7285 "app announce time");
7286 GNUNET_SCHEDULER_shutdown ();
7291 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7296 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7297 "id announce time");
7298 GNUNET_SCHEDULER_shutdown ();
7303 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7304 &unacknowledged_wait_time))
7306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7308 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7309 "unacknowledged wait time");
7310 GNUNET_SCHEDULER_shutdown ();
7315 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7320 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7322 GNUNET_SCHEDULER_shutdown ();
7327 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7332 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7334 GNUNET_SCHEDULER_shutdown ();
7339 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7344 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7346 GNUNET_SCHEDULER_shutdown ();
7351 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7356 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7362 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7363 &dht_replication_level))
7365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7367 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7368 "dht replication level", 10);
7369 dht_replication_level = 10;
7373 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7374 GNUNET_free (keyfile);
7375 if (my_private_key == NULL)
7377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7378 _("Mesh service could not access hostkey. Exiting.\n"));
7379 GNUNET_SCHEDULER_shutdown ();
7382 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7383 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7384 &my_full_id.hashPubKey);
7385 myid = GNUNET_PEER_intern (&my_full_id);
7387 // transport_handle = GNUNET_TRANSPORT_connect(c,
7394 dht_handle = GNUNET_DHT_connect (c, 64);
7395 if (dht_handle == NULL)
7400 stats = GNUNET_STATISTICS_create ("mesh", c);
7404 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7406 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7407 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7408 peers = GNUNET_CONTAINER_multihashmap_create (32);
7409 applications = GNUNET_CONTAINER_multihashmap_create (32);
7410 types = GNUNET_CONTAINER_multihashmap_create (32);
7412 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7413 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7414 GNUNET_SERVER_disconnect_notify (server_handle,
7415 &handle_local_client_disconnect, NULL);
7419 clients_tail = NULL;
7422 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7423 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7425 /* Create a peer_info for the local peer */
7426 peer = peer_info_get (&my_full_id);
7429 GNUNET_PEER_change_rc (myid, 1);
7430 peer_info_add_path (peer, p, GNUNET_YES);
7432 /* Scheduled the task to clean up when shutdown is called */
7433 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7440 * The main function for the mesh service.
7442 * @param argc number of arguments from the command line
7443 * @param argv command line arguments
7444 * @return 0 ok, 1 on error
7447 main (int argc, char *const *argv)
7451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7454 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");