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->last_bck_ack = 1;
4014 t->last_fwd_ack = 1;
4015 t->local_tid = local;
4016 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4018 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4020 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4022 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4023 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4030 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4037 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4039 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4040 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4044 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4054 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4055 * that are no longer needed, as well as nodes of no longer reachable peers.
4056 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4058 * @param t Tunnel from which to remove the path.
4059 * @param peer Short id of the peer which should be removed.
4062 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4064 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4070 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4071 * client when the client disconnects. If the client is not the owner, the
4072 * owner will get notified if no more clients are in the tunnel and the client
4073 * get removed from the tunnel's list.
4075 * @param cls closure (client that is disconnecting)
4076 * @param key the hash of the local tunnel id (used to access the hashmap)
4077 * @param value the value stored at the key (tunnel to destroy)
4079 * @return GNUNET_OK on success
4082 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4084 struct MeshTunnel *t = value;
4085 struct MeshClient *c = cls;
4088 send_client_tunnel_disconnect(t, c);
4091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4092 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4093 tunnel_delete_client(t, c);
4094 client_delete_tunnel(c, t);
4097 tunnel_send_destroy(t);
4098 r = tunnel_destroy (t);
4104 * Timeout function, destroys tunnel if called
4106 * @param cls Closure (tunnel to destroy).
4107 * @param tc TaskContext
4110 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4112 struct MeshTunnel *t = cls;
4114 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4116 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4121 * Resets the tunnel timeout. Starts it if no timeout was running.
4123 * @param t Tunnel whose timeout to reset.
4125 * TODO use heap to improve efficiency of scheduler.
4128 tunnel_reset_timeout (struct MeshTunnel *t)
4130 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4131 GNUNET_SCHEDULER_cancel (t->timeout_task);
4133 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4134 (refresh_path_time, 4), &tunnel_timeout, t);
4138 /******************************************************************************/
4139 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4140 /******************************************************************************/
4143 * Function to send a create path packet to a peer.
4145 * @param cls closure
4146 * @param size number of bytes available in buf
4147 * @param buf where the callee should write the message
4148 * @return number of bytes written to buf
4151 send_core_path_create (void *cls, size_t size, void *buf)
4153 struct MeshPathInfo *info = cls;
4154 struct GNUNET_MESH_ManipulatePath *msg;
4155 struct GNUNET_PeerIdentity *peer_ptr;
4156 struct MeshTunnel *t = info->t;
4157 struct MeshPeerPath *p = info->path;
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4164 sizeof (struct GNUNET_MESH_ManipulatePath) +
4165 p->length * sizeof (struct GNUNET_PeerIdentity);
4167 if (size < size_needed || NULL == buf)
4172 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4173 msg->header.size = htons (size_needed);
4174 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4175 msg->tid = ntohl (t->id.tid);
4177 if (GNUNET_YES == t->speed_min)
4178 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4179 if (GNUNET_YES == t->nobuffer)
4180 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4181 msg->opt = htonl(opt);
4183 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4184 for (i = 0; i < p->length; i++)
4186 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4193 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4199 * Fill the core buffer
4201 * @param cls closure (data itself)
4202 * @param size number of bytes available in buf
4203 * @param buf where the callee should write the message
4205 * @return number of bytes written to buf
4208 send_core_data_multicast (void *cls, size_t size, void *buf)
4210 struct MeshTransmissionDescriptor *info = cls;
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4214 GNUNET_assert (NULL != info);
4215 GNUNET_assert (NULL != info->peer);
4216 total_size = info->mesh_data->data_len;
4217 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4219 if (total_size > size)
4224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4225 memcpy (buf, info->mesh_data->data, total_size);
4228 struct GNUNET_MESH_Multicast *mc;
4229 struct GNUNET_MessageHeader *mh;
4232 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4234 mc = (struct GNUNET_MESH_Multicast *) mh;
4235 mh = (struct GNUNET_MessageHeader *) &mc[1];
4236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4237 " multicast, payload type %u\n", ntohs (mh->type));
4238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4239 " multicast, payload size %u\n", ntohs (mh->size));
4243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
4248 data_descriptor_decrement_rc (info->mesh_data);
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4257 * Creates a path ack message in buf and frees all unused resources.
4259 * @param cls closure (MeshTransmissionDescriptor)
4260 * @param size number of bytes available in buf
4261 * @param buf where the callee should write the message
4262 * @return number of bytes written to buf
4265 send_core_path_ack (void *cls, size_t size, void *buf)
4267 struct MeshTransmissionDescriptor *info = cls;
4268 struct GNUNET_MESH_PathACK *msg = buf;
4270 GNUNET_assert (NULL != info);
4271 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4276 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4277 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4278 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4279 msg->tid = htonl (info->origin->tid);
4280 msg->peer_id = my_full_id;
4283 /* TODO add signature */
4285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4286 return sizeof (struct GNUNET_MESH_PathACK);
4291 * Free a transmission that was already queued with all resources
4292 * associated to the request.
4294 * @param queue Queue handler to cancel.
4295 * @param clear_cls Is it necessary to free associated cls?
4298 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4300 struct MeshTransmissionDescriptor *dd;
4301 struct MeshPathInfo *path_info;
4303 if (GNUNET_YES == clear_cls)
4305 switch (queue->type)
4307 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4308 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4309 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4312 data_descriptor_decrement_rc (dd->mesh_data);
4314 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4316 path_info = queue->cls;
4317 path_destroy (path_info->path);
4321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4323 GNUNET_free_non_null (queue->cls);
4325 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4326 queue->peer->queue_tail,
4328 GNUNET_free (queue);
4333 * Core callback to write a queued packet to core buffer
4335 * @param cls Closure (peer info).
4336 * @param size Number of bytes available in buf.
4337 * @param buf Where the to write the message.
4339 * @return number of bytes written to buf
4342 queue_send (void *cls, size_t size, void *buf)
4344 struct MeshPeerInfo *peer = cls;
4345 struct GNUNET_MessageHeader *msg;
4346 struct MeshPeerQueue *queue;
4347 struct MeshTunnel *t;
4350 peer->core_transmit = NULL;
4351 queue = peer->queue_head;
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4355 /* If queue is empty, send should have been cancelled */
4361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4363 /* Check if buffer size is enough for the message */
4364 if (queue->size > size)
4366 struct GNUNET_PeerIdentity id;
4368 GNUNET_PEER_resolve (peer->id, &id);
4369 peer->core_transmit =
4370 GNUNET_CORE_notify_transmit_ready(core_handle,
4373 GNUNET_TIME_UNIT_FOREVER_REL,
4380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4386 switch (queue->type)
4388 case 0: // RAW data (preconstructed message, retransmission, etc.)
4389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
4390 data_size = send_core_data_raw (queue->cls, size, buf);
4391 msg = (struct GNUNET_MessageHeader *) buf;
4392 switch (ntohs (msg->type)) // Type of preconstructed message
4394 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4395 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4397 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4398 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4404 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4406 data_size = send_core_data_multicast(queue->cls, size, buf);
4407 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4409 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4411 data_size = send_core_path_create(queue->cls, size, buf);
4413 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4415 data_size = send_core_path_ack(queue->cls, size, buf);
4419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
4423 /* Free queue, but cls was freed by send_core_* */
4424 queue_destroy (queue, GNUNET_NO);
4426 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4432 /* If more data in queue, send next */
4433 if (NULL != peer->queue_head)
4435 struct GNUNET_PeerIdentity id;
4437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4438 GNUNET_PEER_resolve (peer->id, &id);
4439 peer->core_transmit =
4440 GNUNET_CORE_notify_transmit_ready(core_handle,
4443 GNUNET_TIME_UNIT_FOREVER_REL,
4445 peer->queue_head->size,
4449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4455 * Queue and pass message to core when possible.
4457 * @param cls Closure (type dependant).
4458 * @param type Type of the message, 0 for a raw message.
4459 * @param size Size of the message.
4460 * @param dst Neighbor to send message to.
4461 * @param t Tunnel this message belongs to.
4464 queue_add (void *cls, uint16_t type, size_t size,
4465 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4467 struct MeshPeerQueue *queue;
4471 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4472 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4474 n = &t->fwd_queue_n;
4475 max = &t->fwd_queue_max;
4479 n = &t->bck_queue_n;
4480 max = &t->bck_queue_max;
4484 if (NULL == t->owner)
4485 GNUNET_break_op(0); // TODO: kill connection?
4488 return; // Drop message
4491 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4497 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4498 if (NULL == dst->core_transmit)
4500 struct GNUNET_PeerIdentity id;
4502 GNUNET_PEER_resolve (dst->id, &id);
4503 dst->core_transmit =
4504 GNUNET_CORE_notify_transmit_ready(core_handle,
4507 GNUNET_TIME_UNIT_FOREVER_REL,
4516 /******************************************************************************/
4517 /******************** MESH NETWORK HANDLERS **************************/
4518 /******************************************************************************/
4522 * Core handler for path creation
4524 * @param cls closure
4525 * @param message message
4526 * @param peer peer identity this notification is about
4527 * @param atsi performance data
4528 * @param atsi_count number of records in 'atsi'
4530 * @return GNUNET_OK to keep the connection open,
4531 * GNUNET_SYSERR to close it (signal serious error)
4534 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4535 const struct GNUNET_MessageHeader *message,
4536 const struct GNUNET_ATS_Information *atsi,
4537 unsigned int atsi_count)
4539 unsigned int own_pos;
4542 MESH_TunnelNumber tid;
4543 struct GNUNET_MESH_ManipulatePath *msg;
4544 struct GNUNET_PeerIdentity *pi;
4545 struct GNUNET_HashCode hash;
4546 struct MeshPeerPath *path;
4547 struct MeshPeerInfo *dest_peer_info;
4548 struct MeshPeerInfo *orig_peer_info;
4549 struct MeshTunnel *t;
4551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4552 "Received a path create msg [%s]\n",
4553 GNUNET_i2s (&my_full_id));
4554 size = ntohs (message->size);
4555 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4557 GNUNET_break_op (0);
4561 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4562 if (size % sizeof (struct GNUNET_PeerIdentity))
4564 GNUNET_break_op (0);
4567 size /= sizeof (struct GNUNET_PeerIdentity);
4570 GNUNET_break_op (0);
4573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4574 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4576 tid = ntohl (msg->tid);
4577 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4579 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4580 t = tunnel_get (pi, tid);
4581 if (NULL == t) // FIXME only for INCOMING tunnels?
4585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4586 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4589 // FIXME notify failure
4592 opt = ntohl (msg->opt);
4593 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4594 GNUNET_YES : GNUNET_NO;
4595 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4596 GNUNET_YES : GNUNET_NO;
4598 if (GNUNET_YES == t->nobuffer)
4600 t->bck_queue_max = 1;
4601 t->fwd_queue_max = 1;
4604 // FIXME only assign a local tid if a local client is interested (on demand)
4605 while (NULL != tunnel_get_incoming (next_local_tid))
4606 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4607 t->local_tid_dest = next_local_tid++;
4608 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4611 tunnel_reset_timeout (t);
4612 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4614 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4615 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4623 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4624 if (NULL == dest_peer_info)
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 " Creating PeerInfo for destination.\n");
4628 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4629 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4630 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4632 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4634 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4635 if (NULL == orig_peer_info)
4637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4638 " Creating PeerInfo for origin.\n");
4639 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4640 orig_peer_info->id = GNUNET_PEER_intern (pi);
4641 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4642 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4645 path = path_new (size);
4647 for (i = 0; i < size; i++)
4649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4650 GNUNET_i2s (&pi[i]));
4651 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4652 if (path->peers[i] == myid)
4655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4658 /* cannot be self, must be 'not found' */
4659 /* create path: self not found in path through self */
4660 GNUNET_break_op (0);
4661 path_destroy (path);
4665 path_add_to_peers (path, GNUNET_NO);
4666 tunnel_add_path (t, path, own_pos);
4667 if (own_pos == size - 1)
4669 /* It is for us! Send ack. */
4670 struct MeshTransmissionDescriptor *info;
4672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4673 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4674 if (NULL == t->peers)
4676 /* New tunnel! Notify clients on data. */
4677 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4679 GNUNET_break (GNUNET_SYSERR !=
4680 GNUNET_CONTAINER_multihashmap_put (t->peers,
4681 &my_full_id.hashPubKey,
4684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4685 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4686 info->origin = &t->id;
4687 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4688 GNUNET_assert (NULL != info->peer);
4690 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4691 sizeof (struct GNUNET_MESH_PathACK),
4697 struct MeshPeerPath *path2;
4699 /* It's for somebody else! Retransmit. */
4700 path2 = path_duplicate (path);
4701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4702 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4703 path2 = path_duplicate (path);
4704 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4705 send_create_path (dest_peer_info, path, t);
4712 * Core handler for path destruction
4714 * @param cls closure
4715 * @param message message
4716 * @param peer peer identity this notification is about
4717 * @param atsi performance data
4718 * @param atsi_count number of records in 'atsi'
4720 * @return GNUNET_OK to keep the connection open,
4721 * GNUNET_SYSERR to close it (signal serious error)
4724 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4725 const struct GNUNET_MessageHeader *message,
4726 const struct GNUNET_ATS_Information *atsi,
4727 unsigned int atsi_count)
4729 struct GNUNET_MESH_ManipulatePath *msg;
4730 struct GNUNET_PeerIdentity *pi;
4731 struct MeshPeerPath *path;
4732 struct MeshTunnel *t;
4733 unsigned int own_pos;
4737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4738 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4739 size = ntohs (message->size);
4740 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4742 GNUNET_break_op (0);
4746 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4747 if (size % sizeof (struct GNUNET_PeerIdentity))
4749 GNUNET_break_op (0);
4752 size /= sizeof (struct GNUNET_PeerIdentity);
4755 GNUNET_break_op (0);
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4760 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4761 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4763 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4765 t = tunnel_get (pi, ntohl (msg->tid));
4768 /* TODO notify back: we don't know this tunnel */
4769 GNUNET_break_op (0);
4772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4773 path = path_new (size);
4775 for (i = 0; i < size; i++)
4777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4778 GNUNET_i2s (&pi[i]));
4779 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4780 if (path->peers[i] == myid)
4783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4784 if (own_pos < path->length - 1)
4785 send_message (message, &pi[own_pos + 1], t);
4787 send_client_tunnel_disconnect(t, NULL);
4789 tunnel_delete_peer (t, path->peers[path->length - 1]);
4790 path_destroy (path);
4796 * Core handler for notifications of broken paths
4798 * @param cls closure
4799 * @param message message
4800 * @param peer peer identity this notification is about
4801 * @param atsi performance data
4802 * @param atsi_count number of records in 'atsi'
4804 * @return GNUNET_OK to keep the connection open,
4805 * GNUNET_SYSERR to close it (signal serious error)
4808 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4809 const struct GNUNET_MessageHeader *message,
4810 const struct GNUNET_ATS_Information *atsi,
4811 unsigned int atsi_count)
4813 struct GNUNET_MESH_PathBroken *msg;
4814 struct MeshTunnel *t;
4816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4817 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4818 msg = (struct GNUNET_MESH_PathBroken *) message;
4819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4820 GNUNET_i2s (&msg->peer1));
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4822 GNUNET_i2s (&msg->peer2));
4823 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4826 GNUNET_break_op (0);
4829 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4830 GNUNET_PEER_search (&msg->peer2));
4837 * Core handler for tunnel destruction
4839 * @param cls closure
4840 * @param message message
4841 * @param peer peer identity this notification is about
4842 * @param atsi performance data
4843 * @param atsi_count number of records in 'atsi'
4845 * @return GNUNET_OK to keep the connection open,
4846 * GNUNET_SYSERR to close it (signal serious error)
4849 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4850 const struct GNUNET_MessageHeader *message,
4851 const struct GNUNET_ATS_Information *atsi,
4852 unsigned int atsi_count)
4854 struct GNUNET_MESH_TunnelDestroy *msg;
4855 struct MeshTunnel *t;
4857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4858 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4859 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4861 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4862 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4865 /* Probably already got the message from another path,
4866 * destroyed the tunnel and retransmitted to children.
4869 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4872 if (t->id.oid == myid)
4874 GNUNET_break_op (0);
4877 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4879 /* Tunnel was incoming, notify clients */
4880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4881 t->local_tid, t->local_tid_dest);
4882 send_clients_tunnel_destroy (t);
4884 tunnel_send_destroy (t);
4885 t->destroy = GNUNET_YES;
4886 // TODO: add timeout to destroy the tunnel anyway
4892 * Core handler for mesh network traffic going from the origin to a peer
4894 * @param cls closure
4895 * @param peer peer identity this notification is about
4896 * @param message message
4897 * @param atsi performance data
4898 * @param atsi_count number of records in 'atsi'
4899 * @return GNUNET_OK to keep the connection open,
4900 * GNUNET_SYSERR to close it (signal serious error)
4903 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4904 const struct GNUNET_MessageHeader *message,
4905 const struct GNUNET_ATS_Information *atsi,
4906 unsigned int atsi_count)
4908 struct GNUNET_MESH_Unicast *msg;
4909 struct GNUNET_PeerIdentity *neighbor;
4910 struct MeshTunnelChildInfo *cinfo;
4911 struct MeshTunnel *t;
4912 GNUNET_PEER_Id dest_id;
4917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4920 size = ntohs (message->size);
4922 sizeof (struct GNUNET_MESH_Unicast) +
4923 sizeof (struct GNUNET_MessageHeader))
4928 msg = (struct GNUNET_MESH_Unicast *) message;
4929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4930 ntohs (msg[1].header.type));
4932 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4935 /* TODO notify back: we don't know this tunnel */
4936 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4937 GNUNET_break_op (0);
4940 pid = ntohl (msg->pid);
4941 if (t->fwd_pid == pid)
4943 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4944 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4945 " Already seen pid %u, DROPPING!\n", pid);
4950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4951 " pid %u not seen yet, forwarding\n", pid);
4953 t->skip += (pid - t->fwd_pid) - 1;
4955 if (is_pid_bigger (pid, t->last_fwd_ack))
4957 GNUNET_STATISTICS_update (stats, "# not allowed unicast", 1, GNUNET_NO);
4958 GNUNET_break_op (0);
4961 tunnel_reset_timeout (t);
4962 dest_id = GNUNET_PEER_search (&msg->destination);
4963 if (dest_id == myid)
4965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4966 " it's for us! sending to clients...\n");
4967 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4968 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4969 // ACK is generated by client (api part), service only retransmits.
4972 ttl = ntohl (msg->ttl);
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4976 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4977 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4981 " not for us, retransmitting...\n");
4982 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4984 neighbor = tree_get_first_hop (t->tree, dest_id);
4985 cinfo = tunnel_get_neighbor_fc (t, neighbor);
4987 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4990 if (is_pid_bigger(pid, cinfo->fwd_ack))
4992 GNUNET_break_op (0);
4995 send_message (message, neighbor, t);
5001 * Core handler for mesh network traffic going from the origin to all peers
5003 * @param cls closure
5004 * @param message message
5005 * @param peer peer identity this notification is about
5006 * @param atsi performance data
5007 * @param atsi_count number of records in 'atsi'
5008 * @return GNUNET_OK to keep the connection open,
5009 * GNUNET_SYSERR to close it (signal serious error)
5011 * TODO: Check who we got this from, to validate route.
5014 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5015 const struct GNUNET_MessageHeader *message,
5016 const struct GNUNET_ATS_Information *atsi,
5017 unsigned int atsi_count)
5019 struct GNUNET_MESH_Multicast *msg;
5020 struct MeshTunnel *t;
5024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5026 size = ntohs (message->size);
5027 if (sizeof (struct GNUNET_MESH_Multicast) +
5028 sizeof (struct GNUNET_MessageHeader) > size)
5030 GNUNET_break_op (0);
5033 msg = (struct GNUNET_MESH_Multicast *) message;
5034 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5038 /* TODO notify that we dont know that tunnel */
5039 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5040 GNUNET_break_op (0);
5043 pid = ntohl (msg->pid);
5044 if (t->fwd_pid == pid)
5046 /* already seen this packet, drop */
5047 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5049 " Already seen pid %u, DROPPING!\n", pid);
5054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5055 " pid %u not seen yet, forwarding\n", pid);
5057 t->skip += (pid - t->fwd_pid) - 1;
5059 tunnel_reset_timeout (t);
5061 /* Transmit to locally interested clients */
5062 if (NULL != t->peers &&
5063 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5065 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5066 send_subscribed_clients (message, &msg[1].header);
5068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5069 if (ntohl (msg->ttl) == 0)
5071 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5072 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5075 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5076 tunnel_send_multicast (t, message, GNUNET_NO);
5082 * Core handler for mesh network traffic toward the owner of a tunnel
5084 * @param cls closure
5085 * @param message message
5086 * @param peer peer identity this notification is about
5087 * @param atsi performance data
5088 * @param atsi_count number of records in 'atsi'
5090 * @return GNUNET_OK to keep the connection open,
5091 * GNUNET_SYSERR to close it (signal serious error)
5094 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5095 const struct GNUNET_MessageHeader *message,
5096 const struct GNUNET_ATS_Information *atsi,
5097 unsigned int atsi_count)
5099 struct GNUNET_MESH_ToOrigin *msg;
5100 struct GNUNET_PeerIdentity id;
5101 struct MeshPeerInfo *peer_info;
5102 struct MeshTunnel *t;
5105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5107 size = ntohs (message->size);
5108 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5109 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5111 GNUNET_break_op (0);
5114 msg = (struct GNUNET_MESH_ToOrigin *) message;
5115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5116 ntohs (msg[1].header.type));
5117 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5121 /* TODO notify that we dont know this tunnel (whom)? */
5122 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5123 GNUNET_break_op (0);
5127 if (t->id.oid == myid)
5130 struct GNUNET_MESH_ToOrigin *copy;
5132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5133 " it's for us! sending to clients...\n");
5134 if (NULL == t->owner)
5136 /* got data packet for ownerless tunnel */
5137 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
5140 GNUNET_break_op (0);
5143 /* TODO signature verification */
5144 memcpy (cbuf, message, size);
5145 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5146 copy->tid = htonl (t->local_tid);
5147 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5148 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5149 ©->header, GNUNET_NO);
5152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5153 " not for us, retransmitting...\n");
5155 peer_info = peer_info_get (&msg->oid);
5156 if (NULL == peer_info)
5158 /* unknown origin of tunnel */
5162 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5163 send_message (message, &id, t);
5164 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5171 * Core handler for mesh network traffic point-to-point acks.
5173 * @param cls closure
5174 * @param message message
5175 * @param peer peer identity this notification is about
5176 * @param atsi performance data
5177 * @param atsi_count number of records in 'atsi'
5179 * @return GNUNET_OK to keep the connection open,
5180 * GNUNET_SYSERR to close it (signal serious error)
5183 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5184 const struct GNUNET_MessageHeader *message,
5185 const struct GNUNET_ATS_Information *atsi,
5186 unsigned int atsi_count)
5188 struct GNUNET_MESH_ACK *msg;
5189 struct MeshTunnelChildInfo *cinfo;
5190 struct MeshTunnel *t;
5193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got an ACK packet from %s\n",
5195 msg = (struct GNUNET_MESH_ACK *) message;
5196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5197 ntohs (msg[1].header.type));
5198 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5202 /* TODO notify that we dont know this tunnel (whom)? */
5203 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5204 GNUNET_break_op (0);
5207 ack = ntohl (msg->pid);
5208 cinfo = tunnel_get_neighbor_fc (t, peer);
5209 cinfo->fwd_ack = ack;
5210 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5216 * Core handler for path ACKs
5218 * @param cls closure
5219 * @param message message
5220 * @param peer peer identity this notification is about
5221 * @param atsi performance data
5222 * @param atsi_count number of records in 'atsi'
5224 * @return GNUNET_OK to keep the connection open,
5225 * GNUNET_SYSERR to close it (signal serious error)
5228 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5229 const struct GNUNET_MessageHeader *message,
5230 const struct GNUNET_ATS_Information *atsi,
5231 unsigned int atsi_count)
5233 struct GNUNET_MESH_PathACK *msg;
5234 struct GNUNET_PeerIdentity id;
5235 struct MeshPeerInfo *peer_info;
5236 struct MeshPeerPath *p;
5237 struct MeshTunnel *t;
5239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5240 GNUNET_i2s (&my_full_id));
5241 msg = (struct GNUNET_MESH_PathACK *) message;
5242 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5245 /* TODO notify that we don't know the tunnel */
5246 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5248 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5252 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5254 peer_info = peer_info_get (&msg->peer_id);
5255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5256 GNUNET_i2s (&msg->peer_id));
5257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5260 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5263 "connect_by_string completed, stopping search\n");
5264 regex_cancel_search (t->regex_ctx);
5265 t->regex_ctx = NULL;
5268 /* Add paths to peers? */
5269 p = tree_get_path_to_peer (t->tree, peer_info->id);
5272 path_add_to_peers (p, GNUNET_YES);
5280 /* Message for us? */
5281 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5284 if (NULL == t->owner)
5286 GNUNET_break_op (0);
5289 if (NULL != t->dht_get_type)
5291 GNUNET_DHT_get_stop (t->dht_get_type);
5292 t->dht_get_type = NULL;
5294 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5296 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5297 send_client_peer_connected (t, peer_info->id);
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5303 " not for us, retransmitting...\n");
5304 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5305 peer_info = peer_info_get (&msg->oid);
5306 if (NULL == peer_info)
5308 /* If we know the tunnel, we should DEFINITELY know the peer */
5312 send_message (message, &id, t);
5318 * Functions to handle messages from core
5320 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5321 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5322 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5323 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5324 sizeof (struct GNUNET_MESH_PathBroken)},
5325 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5326 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5327 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5328 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5329 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5330 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5331 sizeof (struct GNUNET_MESH_ACK)},
5332 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5333 sizeof (struct GNUNET_MESH_PathACK)},
5339 /******************************************************************************/
5340 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5341 /******************************************************************************/
5344 * deregister_app: iterator for removing each application registered by a client
5346 * @param cls closure
5347 * @param key the hash of the application id (used to access the hashmap)
5348 * @param value the value stored at the key (client)
5350 * @return GNUNET_OK on success
5353 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5355 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5356 GNUNET_break (GNUNET_YES ==
5357 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5363 * notify_client_connection_failure: notify a client that the connection to the
5364 * requested remote peer is not possible (for instance, no route found)
5365 * Function called when the socket is ready to queue more data. "buf" will be
5366 * NULL and "size" zero if the socket was closed for writing in the meantime.
5368 * @param cls closure
5369 * @param size number of bytes available in buf
5370 * @param buf where the callee should write the message
5371 * @return number of bytes written to buf
5374 notify_client_connection_failure (void *cls, size_t size, void *buf)
5377 struct MeshPeerInfo *peer_info;
5378 struct GNUNET_MESH_PeerControl *msg;
5379 struct GNUNET_PeerIdentity id;
5381 if (0 == size && NULL == buf)
5383 // TODO retry? cancel?
5387 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5388 peer_info = (struct MeshPeerInfo *) cls;
5389 msg = (struct GNUNET_MESH_PeerControl *) buf;
5390 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5391 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5392 // msg->tunnel_id = htonl(peer_info->t->tid);
5393 GNUNET_PEER_resolve (peer_info->id, &id);
5394 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5402 * Send keepalive packets for a peer
5404 * @param cls Closure (tunnel for which to send the keepalive).
5405 * @param tc Notification context.
5407 * TODO: implement explicit multicast keepalive?
5410 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5412 struct MeshTunnel *t = cls;
5413 struct GNUNET_MessageHeader *payload;
5414 struct GNUNET_MESH_Multicast *msg;
5416 sizeof (struct GNUNET_MESH_Multicast) +
5417 sizeof (struct GNUNET_MessageHeader);
5420 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5424 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5427 "sending keepalive for tunnel %d\n", t->id.tid);
5429 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5430 msg->header.size = htons (size);
5431 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5432 msg->oid = my_full_id;
5433 msg->tid = htonl (t->id.tid);
5434 msg->ttl = htonl (default_ttl);
5435 msg->pid = htonl (t->fwd_pid + 1);
5437 payload = (struct GNUNET_MessageHeader *) &msg[1];
5438 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5439 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5440 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5442 t->path_refresh_task =
5443 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5449 * Function to process paths received for a new peer addition. The recorded
5450 * paths form the initial tunnel, which can be optimized later.
5451 * Called on each result obtained for the DHT search.
5453 * @param cls closure
5454 * @param exp when will this value expire
5455 * @param key key of the result
5456 * @param get_path path of the get request
5457 * @param get_path_length lenght of get_path
5458 * @param put_path path of the put request
5459 * @param put_path_length length of the put_path
5460 * @param type type of the result
5461 * @param size number of bytes in data
5462 * @param data pointer to the result data
5464 * TODO: re-issue the request after certain time? cancel after X results?
5467 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5468 const struct GNUNET_HashCode * key,
5469 const struct GNUNET_PeerIdentity *get_path,
5470 unsigned int get_path_length,
5471 const struct GNUNET_PeerIdentity *put_path,
5472 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5473 size_t size, const void *data)
5475 struct MeshPathInfo *path_info = cls;
5476 struct MeshPeerPath *p;
5477 struct GNUNET_PeerIdentity pi;
5480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5481 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5484 p = path_build_from_dht (get_path, get_path_length, put_path,
5486 path_add_to_peers (p, GNUNET_NO);
5488 for (i = 0; i < path_info->peer->ntunnels; i++)
5490 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5491 peer_info_connect (path_info->peer, path_info->t);
5499 * Function to process paths received for a new peer addition. The recorded
5500 * paths form the initial tunnel, which can be optimized later.
5501 * Called on each result obtained for the DHT search.
5503 * @param cls closure
5504 * @param exp when will this value expire
5505 * @param key key of the result
5506 * @param get_path path of the get request
5507 * @param get_path_length lenght of get_path
5508 * @param put_path path of the put request
5509 * @param put_path_length length of the put_path
5510 * @param type type of the result
5511 * @param size number of bytes in data
5512 * @param data pointer to the result data
5515 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5516 const struct GNUNET_HashCode * key,
5517 const struct GNUNET_PeerIdentity *get_path,
5518 unsigned int get_path_length,
5519 const struct GNUNET_PeerIdentity *put_path,
5520 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5521 size_t size, const void *data)
5523 const struct PBlock *pb = data;
5524 const struct GNUNET_PeerIdentity *pi = &pb->id;
5525 struct MeshTunnel *t = cls;
5526 struct MeshPeerInfo *peer_info;
5527 struct MeshPeerPath *p;
5529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5530 if (size != sizeof (struct PBlock))
5532 GNUNET_break_op (0);
5535 if (ntohl(pb->type) != t->type)
5537 GNUNET_break_op (0);
5540 GNUNET_assert (NULL != t->owner);
5541 peer_info = peer_info_get (pi);
5542 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5544 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5546 p = path_build_from_dht (get_path, get_path_length, put_path,
5548 path_add_to_peers (p, GNUNET_NO);
5550 tunnel_add_peer (t, peer_info);
5551 peer_info_connect (peer_info, t);
5556 * Function to process DHT string to regex matching.
5557 * Called on each result obtained for the DHT search.
5559 * @param cls closure (search context)
5560 * @param exp when will this value expire
5561 * @param key key of the result
5562 * @param get_path path of the get request (not used)
5563 * @param get_path_length lenght of get_path (not used)
5564 * @param put_path path of the put request (not used)
5565 * @param put_path_length length of the put_path (not used)
5566 * @param type type of the result
5567 * @param size number of bytes in data
5568 * @param data pointer to the result data
5571 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5572 const struct GNUNET_HashCode * key,
5573 const struct GNUNET_PeerIdentity *get_path,
5574 unsigned int get_path_length,
5575 const struct GNUNET_PeerIdentity *put_path,
5576 unsigned int put_path_length,
5577 enum GNUNET_BLOCK_Type type,
5578 size_t size, const void *data)
5580 const struct MeshRegexAccept *block = data;
5581 struct MeshRegexSearchContext *ctx = cls;
5582 struct MeshRegexSearchInfo *info = ctx->info;
5583 struct MeshPeerPath *p;
5584 struct MeshPeerInfo *peer_info;
5586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5589 peer_info = peer_info_get(&block->id);
5590 p = path_build_from_dht (get_path, get_path_length, put_path,
5592 path_add_to_peers (p, GNUNET_NO);
5595 tunnel_add_peer (info->t, peer_info);
5596 peer_info_connect (peer_info, info->t);
5597 if (0 == info->peer)
5599 info->peer = peer_info->id;
5603 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5606 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5607 ®ex_connect_timeout,
5615 * Function to process DHT string to regex matching.
5616 * Called on each result obtained for the DHT search.
5618 * @param cls closure (search context)
5619 * @param exp when will this value expire
5620 * @param key key of the result
5621 * @param get_path path of the get request (not used)
5622 * @param get_path_length lenght of get_path (not used)
5623 * @param put_path path of the put request (not used)
5624 * @param put_path_length length of the put_path (not used)
5625 * @param type type of the result
5626 * @param size number of bytes in data
5627 * @param data pointer to the result data
5629 * TODO: re-issue the request after certain time? cancel after X results?
5632 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5633 const struct GNUNET_HashCode * key,
5634 const struct GNUNET_PeerIdentity *get_path,
5635 unsigned int get_path_length,
5636 const struct GNUNET_PeerIdentity *put_path,
5637 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5638 size_t size, const void *data)
5640 const struct MeshRegexBlock *block = data;
5641 struct MeshRegexSearchContext *ctx = cls;
5642 struct MeshRegexSearchInfo *info = ctx->info;
5646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5647 "DHT GET STRING RETURNED RESULTS\n");
5648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5649 " key: %s\n", GNUNET_h2s (key));
5651 copy = GNUNET_malloc (size);
5652 memcpy (copy, data, size);
5653 GNUNET_break (GNUNET_OK ==
5654 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5655 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5656 len = ntohl (block->n_proof);
5658 char proof[len + 1];
5660 memcpy (proof, &block[1], len);
5662 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5664 GNUNET_break_op (0);
5668 len = strlen (info->description);
5669 if (len == ctx->position) // String processed
5671 if (GNUNET_YES == ntohl (block->accepting))
5673 regex_find_path(key, ctx);
5677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5678 // FIXME REGEX this block not successful, wait for more? start timeout?
5682 GNUNET_break (GNUNET_OK ==
5683 GNUNET_MESH_regex_block_iterate (block, size,
5684 ®ex_edge_iterator, ctx));
5688 /******************************************************************************/
5689 /********************* MESH LOCAL HANDLES **************************/
5690 /******************************************************************************/
5694 * Handler for client disconnection
5696 * @param cls closure
5697 * @param client identification of the client; NULL
5698 * for the last call when the server is destroyed
5701 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5703 struct MeshClient *c;
5704 struct MeshClient *next;
5707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5716 if (c->handle != client)
5718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5724 GNUNET_SERVER_client_drop (c->handle);
5725 c->shutting_down = GNUNET_YES;
5726 GNUNET_assert (NULL != c->own_tunnels);
5727 GNUNET_assert (NULL != c->incoming_tunnels);
5728 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5729 &tunnel_destroy_iterator, c);
5730 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5731 &tunnel_destroy_iterator, c);
5732 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5733 &tunnel_destroy_iterator, c);
5734 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5735 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5736 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5738 /* deregister clients applications */
5739 if (NULL != c->apps)
5741 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5742 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5744 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5745 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5747 GNUNET_SCHEDULER_cancel (announce_applications_task);
5748 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5750 if (NULL != c->types)
5751 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5752 for (i = 0; i < c->n_regex; i++)
5754 GNUNET_free (c->regexes[i]);
5756 GNUNET_free_non_null (c->regexes);
5757 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5758 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5760 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5763 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5772 * Handler for new clients
5774 * @param cls closure
5775 * @param client identification of the client
5776 * @param message the actual message, which includes messages the client wants
5779 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5780 const struct GNUNET_MessageHeader *message)
5782 struct GNUNET_MESH_ClientConnect *cc_msg;
5783 struct MeshClient *c;
5784 GNUNET_MESH_ApplicationType *a;
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5792 /* Check data sanity */
5793 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5794 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5795 ntypes = ntohs (cc_msg->types);
5796 napps = ntohs (cc_msg->applications);
5798 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5805 /* Create new client structure */
5806 c = GNUNET_malloc (sizeof (struct MeshClient));
5807 c->id = next_client_id++;
5808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5810 GNUNET_SERVER_client_keep (client);
5811 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5814 GNUNET_MESH_ApplicationType at;
5815 struct GNUNET_HashCode hc;
5817 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5818 for (i = 0; i < napps; i++)
5821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5822 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5823 /* store in clients hashmap */
5824 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5825 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5826 /* store in global hashmap, for announcements */
5827 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5828 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5830 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5831 announce_applications_task =
5832 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5838 struct GNUNET_HashCode hc;
5840 t = (uint16_t *) & a[napps];
5841 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5842 for (i = 0; i < ntypes; i++)
5845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5846 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5848 /* store in clients hashmap */
5849 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5850 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5851 /* store in global hashmap */
5852 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5853 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5857 " client has %u+%u subscriptions\n", napps, ntypes);
5859 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5860 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5861 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5862 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5863 GNUNET_SERVER_notification_context_add (nc, client);
5864 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5866 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5872 * Handler for clients announcing available services by a regular expression.
5874 * @param cls closure
5875 * @param client identification of the client
5876 * @param message the actual message, which includes messages the client wants
5879 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5880 const struct GNUNET_MessageHeader *message)
5882 struct MeshClient *c;
5886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5888 /* Sanity check for client registration */
5889 if (NULL == (c = client_get (client)))
5892 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5897 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5898 regex = GNUNET_malloc (len + 1);
5899 memcpy (regex, &message[1], len);
5901 GNUNET_array_append (c->regexes, c->n_regex, regex);
5902 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5904 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5910 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5916 * Handler for requests of new tunnels
5918 * @param cls closure
5919 * @param client identification of the client
5920 * @param message the actual message
5923 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5924 const struct GNUNET_MessageHeader *message)
5926 struct GNUNET_MESH_TunnelMessage *t_msg;
5927 struct MeshTunnel *t;
5928 struct MeshClient *c;
5929 MESH_TunnelNumber tid;
5931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5933 /* Sanity check for client registration */
5934 if (NULL == (c = client_get (client)))
5937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5942 /* Message sanity check */
5943 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5950 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5951 /* Sanity check for tunnel numbering */
5952 tid = ntohl (t_msg->tunnel_id);
5953 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5959 /* Sanity check for duplicate tunnel IDs */
5960 if (NULL != tunnel_get_by_local_id (c, tid))
5963 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5967 while (NULL != tunnel_get_by_pi (myid, next_tid))
5968 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5969 t = tunnel_new (myid, next_tid++, c, tid);
5972 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5977 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5979 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5980 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
5982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5983 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5989 * Handler for requests of deleting tunnels
5991 * @param cls closure
5992 * @param client identification of the client
5993 * @param message the actual message
5996 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5997 const struct GNUNET_MessageHeader *message)
5999 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6000 struct MeshClient *c;
6001 struct MeshTunnel *t;
6002 MESH_TunnelNumber tid;
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6005 "Got a DESTROY TUNNEL from client!\n");
6007 /* Sanity check for client registration */
6008 if (NULL == (c = client_get (client)))
6011 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6014 /* Message sanity check */
6015 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6022 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6024 /* Retrieve tunnel */
6025 tid = ntohl (tunnel_msg->tunnel_id);
6026 t = tunnel_get_by_local_id(c, tid);
6029 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6034 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6036 client_ignore_tunnel (c, t);
6038 // TODO: when to destroy incoming tunnel?
6039 if (t->nclients == 0)
6041 GNUNET_assert (GNUNET_YES ==
6042 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6044 GNUNET_assert (GNUNET_YES ==
6045 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6046 &my_full_id.hashPubKey,
6050 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6053 send_client_tunnel_disconnect(t, c);
6054 client_delete_tunnel(c, t);
6056 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6058 tunnel_send_destroy (t);
6059 t->destroy = GNUNET_YES;
6060 // The tunnel will be destroyed when the last message is transmitted.
6061 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6067 * Handler for requests of seeting tunnel's speed.
6069 * @param cls Closure (unused).
6070 * @param client Identification of the client.
6071 * @param message The actual message.
6074 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6075 const struct GNUNET_MessageHeader *message)
6077 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6078 struct MeshClient *c;
6079 struct MeshTunnel *t;
6080 MESH_TunnelNumber tid;
6082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6083 "Got a SPEED request from client!\n");
6085 /* Sanity check for client registration */
6086 if (NULL == (c = client_get (client)))
6089 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6094 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6096 /* Retrieve tunnel */
6097 tid = ntohl (tunnel_msg->tunnel_id);
6098 t = tunnel_get_by_local_id(c, tid);
6101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6103 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6107 switch (ntohs(message->type))
6109 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6110 t->speed_min = GNUNET_YES;
6112 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6113 t->speed_min = GNUNET_NO;
6118 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6123 * Handler for requests of seeting tunnel's buffering policy.
6125 * @param cls Closure (unused).
6126 * @param client Identification of the client.
6127 * @param message The actual message.
6130 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6131 const struct GNUNET_MessageHeader *message)
6133 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6134 struct MeshClient *c;
6135 struct MeshTunnel *t;
6136 MESH_TunnelNumber tid;
6138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6139 "Got a BUFFER request from client!\n");
6141 /* Sanity check for client registration */
6142 if (NULL == (c = client_get (client)))
6145 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6150 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6152 /* Retrieve tunnel */
6153 tid = ntohl (tunnel_msg->tunnel_id);
6154 t = tunnel_get_by_local_id(c, tid);
6157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6159 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6163 switch (ntohs(message->type))
6165 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6166 t->nobuffer = GNUNET_NO;
6168 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6169 t->nobuffer = GNUNET_YES;
6175 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6180 * Handler for connection requests to new peers
6182 * @param cls closure
6183 * @param client identification of the client
6184 * @param message the actual message (PeerControl)
6187 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6188 const struct GNUNET_MessageHeader *message)
6190 struct GNUNET_MESH_PeerControl *peer_msg;
6191 struct MeshPeerInfo *peer_info;
6192 struct MeshClient *c;
6193 struct MeshTunnel *t;
6194 MESH_TunnelNumber tid;
6196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6197 /* Sanity check for client registration */
6198 if (NULL == (c = client_get (client)))
6201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6205 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6206 /* Sanity check for message size */
6207 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6210 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6214 /* Tunnel exists? */
6215 tid = ntohl (peer_msg->tunnel_id);
6216 t = tunnel_get_by_local_id (c, tid);
6220 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6224 /* Does client own tunnel? */
6225 if (t->owner->handle != client)
6228 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6232 GNUNET_i2s (&peer_msg->peer));
6233 peer_info = peer_info_get (&peer_msg->peer);
6235 tunnel_add_peer (t, peer_info);
6236 peer_info_connect (peer_info, t);
6238 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6244 * Handler for disconnection requests of peers in a tunnel
6246 * @param cls closure
6247 * @param client identification of the client
6248 * @param message the actual message (PeerControl)
6251 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6252 const struct GNUNET_MessageHeader *message)
6254 struct GNUNET_MESH_PeerControl *peer_msg;
6255 struct MeshPeerInfo *peer_info;
6256 struct MeshClient *c;
6257 struct MeshTunnel *t;
6258 MESH_TunnelNumber tid;
6260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6261 /* Sanity check for client registration */
6262 if (NULL == (c = client_get (client)))
6265 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6268 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6269 /* Sanity check for message size */
6270 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6273 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6277 /* Tunnel exists? */
6278 tid = ntohl (peer_msg->tunnel_id);
6279 t = tunnel_get_by_local_id (c, tid);
6283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6288 /* Does client own tunnel? */
6289 if (t->owner->handle != client)
6292 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6297 GNUNET_i2s (&peer_msg->peer));
6298 /* Is the peer in the tunnel? */
6300 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6301 if (NULL == peer_info)
6304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6308 /* Ok, delete peer from tunnel */
6309 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6310 &peer_msg->peer.hashPubKey);
6312 send_destroy_path (t, peer_info->id);
6313 tunnel_delete_peer (t, peer_info->id);
6314 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6319 * Handler for blacklist requests of peers in a tunnel
6321 * @param cls closure
6322 * @param client identification of the client
6323 * @param message the actual message (PeerControl)
6326 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6327 const struct GNUNET_MessageHeader *message)
6329 struct GNUNET_MESH_PeerControl *peer_msg;
6330 struct MeshClient *c;
6331 struct MeshTunnel *t;
6332 MESH_TunnelNumber tid;
6334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6335 /* Sanity check for client registration */
6336 if (NULL == (c = client_get (client)))
6339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6342 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6344 /* Sanity check for message size */
6345 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6352 /* Tunnel exists? */
6353 tid = ntohl (peer_msg->tunnel_id);
6354 t = tunnel_get_by_local_id (c, tid);
6358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6363 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6364 GNUNET_PEER_intern(&peer_msg->peer));
6369 * Handler for unblacklist requests of peers in a tunnel
6371 * @param cls closure
6372 * @param client identification of the client
6373 * @param message the actual message (PeerControl)
6376 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6377 const struct GNUNET_MessageHeader *message)
6379 struct GNUNET_MESH_PeerControl *peer_msg;
6380 struct MeshClient *c;
6381 struct MeshTunnel *t;
6382 MESH_TunnelNumber tid;
6386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6387 /* Sanity check for client registration */
6388 if (NULL == (c = client_get (client)))
6391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6394 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6396 /* Sanity check for message size */
6397 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6400 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6404 /* Tunnel exists? */
6405 tid = ntohl (peer_msg->tunnel_id);
6406 t = tunnel_get_by_local_id (c, tid);
6410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6415 /* if peer is not known, complain */
6416 pid = GNUNET_PEER_search (&peer_msg->peer);
6423 /* search and remove from list */
6424 for (i = 0; i < t->nblacklisted; i++)
6426 if (t->blacklisted[i] == pid)
6428 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6429 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6434 /* if peer hasn't been blacklisted, complain */
6440 * Handler for connection requests to new peers by type
6442 * @param cls closure
6443 * @param client identification of the client
6444 * @param message the actual message (ConnectPeerByType)
6447 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6448 const struct GNUNET_MessageHeader *message)
6450 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6451 struct MeshClient *c;
6452 struct MeshTunnel *t;
6453 struct GNUNET_HashCode hash;
6454 MESH_TunnelNumber tid;
6456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6457 /* Sanity check for client registration */
6458 if (NULL == (c = client_get (client)))
6461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6465 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6466 /* Sanity check for message size */
6467 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6468 ntohs (connect_msg->header.size))
6471 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6475 /* Tunnel exists? */
6476 tid = ntohl (connect_msg->tunnel_id);
6477 t = tunnel_get_by_local_id (c, tid);
6481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6485 /* Does client own tunnel? */
6486 if (t->owner->handle != client)
6489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6493 /* Do WE have the service? */
6494 t->type = ntohl (connect_msg->type);
6495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6496 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6497 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6500 /* Yes! Fast forward, add ourselves to the tunnel and send the
6501 * good news to the client, and alert the destination client of
6502 * an incoming tunnel.
6504 * FIXME send a path create to self, avoid code duplication
6506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6507 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6508 peer_info_get (&my_full_id),
6509 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6512 send_client_peer_connected (t, myid);
6513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6514 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6516 t->local_tid_dest = next_local_tid++;
6517 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6518 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6519 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6523 /* Ok, lets find a peer offering the service */
6524 if (NULL != t->dht_get_type)
6526 GNUNET_DHT_get_stop (t->dht_get_type);
6528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6529 GNUNET_h2s (&hash));
6531 GNUNET_DHT_get_start (dht_handle,
6532 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6534 dht_replication_level,
6535 GNUNET_DHT_RO_RECORD_ROUTE |
6536 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6538 &dht_get_type_handler, t);
6540 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6546 * Handler for connection requests to new peers by a string service description.
6548 * @param cls closure
6549 * @param client identification of the client
6550 * @param message the actual message, which includes messages the client wants
6553 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6554 const struct GNUNET_MessageHeader *message)
6556 struct GNUNET_MESH_ConnectPeerByString *msg;
6557 struct MeshRegexSearchContext *ctx;
6558 struct MeshRegexSearchInfo *info;
6559 struct GNUNET_DHT_GetHandle *get_h;
6560 struct GNUNET_HashCode key;
6561 struct MeshTunnel *t;
6562 struct MeshClient *c;
6563 MESH_TunnelNumber tid;
6567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6568 "Connect by string started\n");
6569 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6570 size = htons (message->size);
6572 /* Sanity check for client registration */
6573 if (NULL == (c = client_get (client)))
6576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6580 /* Message size sanity check */
6581 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6584 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6588 /* Tunnel exists? */
6589 tid = ntohl (msg->tunnel_id);
6590 t = tunnel_get_by_local_id (c, tid);
6594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6598 /* Does client own tunnel? */
6599 if (t->owner->handle != client)
6602 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6607 " on tunnel %s [%u]\n",
6608 GNUNET_i2s(&my_full_id),
6611 /* Only one connect_by_string allowed at the same time! */
6612 /* FIXME: allow more, return handle at api level to cancel, document */
6613 if (NULL != t->regex_ctx)
6616 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6620 /* Find string itself */
6621 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6622 string = (const char *) &msg[1];
6624 /* Initialize context */
6625 size = GNUNET_REGEX_get_first_key(string, len, &key);
6626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6627 " consumed %u bits out of %u\n", size, len);
6628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6629 " looking for %s\n", GNUNET_h2s (&key));
6631 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6633 info->description = GNUNET_malloc (len + 1);
6634 memcpy (info->description, string, len);
6635 info->description[len] = '\0';
6636 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6637 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6640 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6641 ctx->position = size;
6645 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6647 /* Start search in DHT */
6648 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6649 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6650 &key, /* key to search */
6651 dht_replication_level, /* replication level */
6652 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6653 NULL, /* xquery */ // FIXME BLOOMFILTER
6654 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6655 &dht_get_string_handler, ctx);
6657 GNUNET_break (GNUNET_OK ==
6658 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6663 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6669 * Handler for client traffic directed to one peer
6671 * @param cls closure
6672 * @param client identification of the client
6673 * @param message the actual message
6676 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6677 const struct GNUNET_MessageHeader *message)
6679 struct MeshClient *c;
6680 struct MeshTunnel *t;
6681 struct MeshPeerInfo *pi;
6682 struct GNUNET_MESH_Unicast *data_msg;
6683 MESH_TunnelNumber tid;
6686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6687 "Got a unicast request from a client!\n");
6689 /* Sanity check for client registration */
6690 if (NULL == (c = client_get (client)))
6693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6696 data_msg = (struct GNUNET_MESH_Unicast *) message;
6697 /* Sanity check for message size */
6698 size = ntohs (message->size);
6699 if (sizeof (struct GNUNET_MESH_Unicast) +
6700 sizeof (struct GNUNET_MessageHeader) > size)
6703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6707 /* Tunnel exists? */
6708 tid = ntohl (data_msg->tid);
6709 t = tunnel_get_by_local_id (c, tid);
6713 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6717 /* Is it a local tunnel? Then, does client own the tunnel? */
6718 if (t->owner->handle != client)
6721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6725 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6726 &data_msg->destination.hashPubKey);
6727 /* Is the selected peer in the tunnel? */
6731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6735 /* Ok, everything is correct, send the message
6736 * (pretend we got it from a mesh peer)
6739 char buf[ntohs (message->size)] GNUNET_ALIGN;
6740 struct GNUNET_MESH_Unicast *copy;
6742 /* Work around const limitation */
6743 copy = (struct GNUNET_MESH_Unicast *) buf;
6744 memcpy (buf, data_msg, size);
6745 copy->oid = my_full_id;
6746 copy->tid = htonl (t->id.tid);
6747 copy->ttl = htonl (default_ttl);
6748 copy->pid = htonl (t->fwd_pid + 1);
6749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6750 " calling generic handler...\n");
6751 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6754 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6760 * Handler for client traffic directed to the origin
6762 * @param cls closure
6763 * @param client identification of the client
6764 * @param message the actual message
6767 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6768 const struct GNUNET_MessageHeader *message)
6770 struct GNUNET_MESH_ToOrigin *data_msg;
6771 struct GNUNET_PeerIdentity id;
6772 struct MeshClient *c;
6773 struct MeshTunnel *t;
6774 MESH_TunnelNumber tid;
6777 /* Sanity check for client registration */
6778 if (NULL == (c = client_get (client)))
6781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6784 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6785 /* Sanity check for message size */
6786 size = ntohs (message->size);
6787 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6788 sizeof (struct GNUNET_MessageHeader) > size)
6791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6795 /* Tunnel exists? */
6796 tid = ntohl (data_msg->tid);
6797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6798 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6799 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6805 t = tunnel_get_by_local_id (c, tid);
6809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6813 /* It should be sent by someone who has this as incoming tunnel. */
6814 if (-1 == client_knows_tunnel (c, t))
6817 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6820 GNUNET_PEER_resolve (t->id.oid, &id);
6822 /* Ok, everything is correct, send the message
6823 * (pretend we got it from a mesh peer)
6826 char buf[ntohs (message->size)] GNUNET_ALIGN;
6827 struct GNUNET_MESH_ToOrigin *copy;
6829 /* Work around const limitation */
6830 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6831 memcpy (buf, data_msg, size);
6833 copy->tid = htonl (t->id.tid);
6834 copy->sender = my_full_id;
6835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6836 " calling generic handler...\n");
6837 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6839 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6845 * Handler for client traffic directed to all peers in a tunnel
6847 * @param cls closure
6848 * @param client identification of the client
6849 * @param message the actual message
6852 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6853 const struct GNUNET_MessageHeader *message)
6855 struct MeshClient *c;
6856 struct MeshTunnel *t;
6857 struct GNUNET_MESH_Multicast *data_msg;
6858 MESH_TunnelNumber tid;
6860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6861 "Got a multicast request from a client!\n");
6863 /* Sanity check for client registration */
6864 if (NULL == (c = client_get (client)))
6867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6870 data_msg = (struct GNUNET_MESH_Multicast *) message;
6871 /* Sanity check for message size */
6872 if (sizeof (struct GNUNET_MESH_Multicast) +
6873 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6880 /* Tunnel exists? */
6881 tid = ntohl (data_msg->tid);
6882 t = tunnel_get_by_local_id (c, tid);
6886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6890 /* Does client own tunnel? */
6891 if (t->owner->handle != client)
6894 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6899 char buf[ntohs (message->size)] GNUNET_ALIGN;
6900 struct GNUNET_MESH_Multicast *copy;
6902 copy = (struct GNUNET_MESH_Multicast *) buf;
6903 memcpy (buf, message, ntohs (message->size));
6904 copy->oid = my_full_id;
6905 copy->tid = htonl (t->id.tid);
6906 copy->ttl = htonl (default_ttl);
6907 copy->pid = htonl (t->fwd_pid + 1);
6908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6909 " calling generic handler...\n");
6910 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
6913 /* receive done gets called when last copy is sent to a neighbor */
6919 * Handler for client's ACKs for payload traffic.
6921 * @param cls Closure (unused).
6922 * @param client Identification of the client.
6923 * @param message The actual message.
6926 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6927 const struct GNUNET_MessageHeader *message)
6929 struct GNUNET_MESH_LocalAck *msg;
6930 struct MeshTunnel *t;
6931 struct MeshClient *c;
6932 MESH_TunnelNumber tid;
6934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6935 /* Sanity check for client registration */
6936 if (NULL == (c = client_get (client)))
6939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6943 msg = (struct GNUNET_MESH_LocalAck *) message;
6944 /* Tunnel exists? */
6945 tid = ntohl (msg->tunnel_id);
6946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6947 t = tunnel_get_by_local_id (c, tid);
6951 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6955 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
6956 if (NULL != t->owner && t->owner->handle == client)
6958 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
6959 t->bck_ack = ntohl(msg->max_pid);
6960 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
6964 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
6965 tunnel_set_client_fwd_ack (t, c, ntohl (msg->max_pid));
6966 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
6969 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6975 * Functions to handle messages from clients
6977 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6978 {&handle_local_new_client, NULL,
6979 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6980 {&handle_local_announce_regex, NULL,
6981 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
6982 {&handle_local_tunnel_create, NULL,
6983 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6984 sizeof (struct GNUNET_MESH_TunnelMessage)},
6985 {&handle_local_tunnel_destroy, NULL,
6986 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6987 sizeof (struct GNUNET_MESH_TunnelMessage)},
6988 {&handle_local_tunnel_speed, NULL,
6989 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
6990 sizeof (struct GNUNET_MESH_TunnelMessage)},
6991 {&handle_local_tunnel_speed, NULL,
6992 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
6993 sizeof (struct GNUNET_MESH_TunnelMessage)},
6994 {&handle_local_tunnel_buffer, NULL,
6995 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6996 sizeof (struct GNUNET_MESH_TunnelMessage)},
6997 {&handle_local_tunnel_buffer, NULL,
6998 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6999 sizeof (struct GNUNET_MESH_TunnelMessage)},
7000 {&handle_local_connect_add, NULL,
7001 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7002 sizeof (struct GNUNET_MESH_PeerControl)},
7003 {&handle_local_connect_del, NULL,
7004 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7005 sizeof (struct GNUNET_MESH_PeerControl)},
7006 {&handle_local_blacklist, NULL,
7007 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7008 sizeof (struct GNUNET_MESH_PeerControl)},
7009 {&handle_local_unblacklist, NULL,
7010 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7011 sizeof (struct GNUNET_MESH_PeerControl)},
7012 {&handle_local_connect_by_type, NULL,
7013 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7014 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7015 {&handle_local_connect_by_string, NULL,
7016 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7017 {&handle_local_unicast, NULL,
7018 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7019 {&handle_local_to_origin, NULL,
7020 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7021 {&handle_local_multicast, NULL,
7022 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7023 {&handle_local_ack, NULL,
7024 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7025 sizeof (struct GNUNET_MESH_LocalAck)},
7031 * To be called on core init/fail.
7033 * @param cls service closure
7034 * @param server handle to the server for this service
7035 * @param identity the public identity of this peer
7038 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7039 const struct GNUNET_PeerIdentity *identity)
7041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7042 core_handle = server;
7043 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7046 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7047 GNUNET_SCHEDULER_shutdown ();
7054 * Method called whenever a given peer connects.
7056 * @param cls closure
7057 * @param peer peer identity this notification is about
7058 * @param atsi performance data for the connection
7059 * @param atsi_count number of records in 'atsi'
7062 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7063 const struct GNUNET_ATS_Information *atsi,
7064 unsigned int atsi_count)
7066 struct MeshPeerInfo *peer_info;
7067 struct MeshPeerPath *path;
7069 DEBUG_CONN ("Peer connected\n");
7070 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7071 peer_info = peer_info_get (peer);
7072 if (myid == peer_info->id)
7074 DEBUG_CONN (" (self)\n");
7079 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7081 path = path_new (2);
7082 path->peers[0] = myid;
7083 path->peers[1] = peer_info->id;
7084 GNUNET_PEER_change_rc (myid, 1);
7085 GNUNET_PEER_change_rc (peer_info->id, 1);
7086 peer_info_add_path (peer_info, path, GNUNET_YES);
7087 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7093 * Method called whenever a peer disconnects.
7095 * @param cls closure
7096 * @param peer peer identity this notification is about
7099 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7101 struct MeshPeerInfo *pi;
7102 struct MeshPeerQueue *q;
7103 struct MeshPeerQueue *n;
7105 DEBUG_CONN ("Peer disconnected\n");
7106 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7118 /* try to reroute this traffic instead */
7119 queue_destroy(q, GNUNET_YES);
7123 peer_info_remove_path (pi, pi->id, myid);
7126 DEBUG_CONN (" (self)\n");
7128 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7133 /******************************************************************************/
7134 /************************ MAIN FUNCTIONS ****************************/
7135 /******************************************************************************/
7138 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7140 * @param cls closure
7141 * @param key current key code
7142 * @param value value in the hash map
7143 * @return GNUNET_YES if we should continue to iterate,
7147 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7149 struct MeshTunnel *t = value;
7156 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7158 * @param cls closure
7159 * @param key current key code
7160 * @param value value in the hash map
7161 * @return GNUNET_YES if we should continue to iterate,
7165 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7167 struct MeshPeerInfo *p = value;
7168 struct MeshPeerQueue *q;
7169 struct MeshPeerQueue *n;
7177 queue_destroy(q, GNUNET_YES);
7181 peer_info_destroy (p);
7186 * Task run during shutdown.
7192 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7196 if (core_handle != NULL)
7198 GNUNET_CORE_disconnect (core_handle);
7201 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7202 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7203 if (dht_handle != NULL)
7205 GNUNET_DHT_disconnect (dht_handle);
7210 GNUNET_SERVER_notification_context_destroy (nc);
7213 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7215 GNUNET_SCHEDULER_cancel (announce_id_task);
7216 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7222 * Process mesh requests.
7224 * @param cls closure
7225 * @param server the initialized server
7226 * @param c configuration to use
7229 run (void *cls, struct GNUNET_SERVER_Handle *server,
7230 const struct GNUNET_CONFIGURATION_Handle *c)
7232 struct MeshPeerInfo *peer;
7233 struct MeshPeerPath *p;
7236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7237 server_handle = server;
7238 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7239 NULL, /* Closure passed to MESH functions */
7240 &core_init, /* Call core_init once connected */
7241 &core_connect, /* Handle connects */
7242 &core_disconnect, /* remove peers on disconnects */
7243 NULL, /* Don't notify about all incoming messages */
7244 GNUNET_NO, /* For header only in notification */
7245 NULL, /* Don't notify about all outbound messages */
7246 GNUNET_NO, /* For header-only out notification */
7247 core_handlers); /* Register these handlers */
7249 if (core_handle == NULL)
7252 GNUNET_SCHEDULER_shutdown ();
7257 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7262 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7264 GNUNET_SCHEDULER_shutdown ();
7269 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7270 &refresh_path_time))
7272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7274 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7275 "refresh path time");
7276 GNUNET_SCHEDULER_shutdown ();
7281 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7282 &app_announce_time))
7284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7286 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7287 "app announce time");
7288 GNUNET_SCHEDULER_shutdown ();
7293 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7298 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7299 "id announce time");
7300 GNUNET_SCHEDULER_shutdown ();
7305 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7306 &unacknowledged_wait_time))
7308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7310 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7311 "unacknowledged wait time");
7312 GNUNET_SCHEDULER_shutdown ();
7317 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7322 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7324 GNUNET_SCHEDULER_shutdown ();
7329 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7334 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7336 GNUNET_SCHEDULER_shutdown ();
7341 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7346 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7348 GNUNET_SCHEDULER_shutdown ();
7353 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7358 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7364 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7365 &dht_replication_level))
7367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7369 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7370 "dht replication level", 10);
7371 dht_replication_level = 10;
7375 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7376 GNUNET_free (keyfile);
7377 if (my_private_key == NULL)
7379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7380 _("Mesh service could not access hostkey. Exiting.\n"));
7381 GNUNET_SCHEDULER_shutdown ();
7384 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7385 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7386 &my_full_id.hashPubKey);
7387 myid = GNUNET_PEER_intern (&my_full_id);
7389 // transport_handle = GNUNET_TRANSPORT_connect(c,
7396 dht_handle = GNUNET_DHT_connect (c, 64);
7397 if (dht_handle == NULL)
7402 stats = GNUNET_STATISTICS_create ("mesh", c);
7406 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7408 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7409 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7410 peers = GNUNET_CONTAINER_multihashmap_create (32);
7411 applications = GNUNET_CONTAINER_multihashmap_create (32);
7412 types = GNUNET_CONTAINER_multihashmap_create (32);
7414 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7415 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7416 GNUNET_SERVER_disconnect_notify (server_handle,
7417 &handle_local_client_disconnect, NULL);
7421 clients_tail = NULL;
7424 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7425 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7427 /* Create a peer_info for the local peer */
7428 peer = peer_info_get (&my_full_id);
7431 GNUNET_PEER_change_rc (myid, 1);
7432 peer_info_add_path (peer, p, GNUNET_YES);
7434 /* Scheduled the task to clean up when shutdown is called */
7435 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7442 * The main function for the mesh service.
7444 * @param argc number of arguments from the command line
7445 * @param argv command line arguments
7446 * @return 0 ok, 1 on error
7449 main (int argc, char *const *argv)
7453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7456 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");