2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
44 * - set ttl relative to tree depth
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_YES
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #if MESH_DEBUG_CONNECTION
64 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
66 #define DEBUG_CONN(...)
70 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
72 #define DEBUG_DHT(...)
75 /******************************************************************************/
76 /************************ DATA STRUCTURES ****************************/
77 /******************************************************************************/
79 /** FWD declaration */
84 * Struct representing a piece of data being sent to other peers
88 /** Tunnel it belongs to. */
91 /** In case of a multicast, task to allow a client to send more data if
92 * some neighbor is too slow. */
93 GNUNET_SCHEDULER_TaskIdentifier *task;
95 /** How many remaining neighbors we need to send this to. */
96 unsigned int *reference_counter;
98 /** Size of the data. */
107 * Struct containing info about a queued transmission to this peer
114 struct MeshPeerQueue *next;
119 struct MeshPeerQueue *prev;
122 * Peer this transmission is directed to.
124 struct MeshPeerInfo *peer;
127 * Tunnel this message belongs to.
129 struct MeshTunnel *tunnel;
132 * Pointer to info stucture used as cls.
142 * Size of the message
149 * Struct containing all info possibly needed to build a package when called
152 struct MeshTransmissionDescriptor
154 /** ID of the tunnel this packet travels in */
155 struct MESH_TunnelID *origin;
157 /** Who was this message being sent to */
158 struct MeshPeerInfo *peer;
160 /** Ultimate destination of the packet */
161 GNUNET_PEER_Id destination;
163 /** Data descriptor */
164 struct MeshData* mesh_data;
169 * Struct containing all information regarding a given peer
179 * Last time we heard from this peer
181 struct GNUNET_TIME_Absolute last_contact;
184 * Number of attempts to reconnect so far
186 int n_reconnect_attempts;
189 * Paths to reach the peer, ordered by ascending hop count
191 struct MeshPeerPath *path_head;
194 * Paths to reach the peer, ordered by ascending hop count
196 struct MeshPeerPath *path_tail;
199 * Handle to stop the DHT search for a path to this peer
201 struct GNUNET_DHT_GetHandle *dhtget;
204 * Closure given to the DHT GET
206 struct MeshPathInfo *dhtgetcls;
209 * Array of tunnels this peer participates in
210 * (most probably a small amount, therefore not a hashmap)
211 * When the path to the peer changes, notify these tunnels to let them
212 * re-adjust their path trees.
214 struct MeshTunnel **tunnels;
217 * Number of tunnels this peers participates in
219 unsigned int ntunnels;
222 * Transmission queue to core DLL head
224 struct MeshPeerQueue *queue_head;
227 * Transmission queue to core DLL tail
229 struct MeshPeerQueue *queue_tail;
232 * How many messages are in the queue to this peer.
234 unsigned int queue_n;
237 * Handle to for queued transmissions
239 struct GNUNET_CORE_TransmitHandle *core_transmit;
244 * Globally unique tunnel identification (owner + number)
245 * DO NOT USE OVER THE NETWORK
250 * Node that owns the tunnel
255 * Tunnel number to differentiate all the tunnels owned by the node oid
256 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
258 MESH_TunnelNumber tid;
262 struct MeshClient; /* FWD declaration */
265 * Struct containing all information regarding a tunnel
266 * For an intermediate node the improtant info used will be:
267 * - id Tunnel unique identification
268 * - paths[0] To know where to send it next
269 * - metainfo: ready, speeds, accounting
276 struct MESH_TunnelID id;
279 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
281 MESH_TunnelNumber local_tid;
284 * Local tunnel number for local destination clients (incoming number)
285 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
288 MESH_TunnelNumber local_tid_dest;
291 * Local count ID of the last packet seen/sent.
296 * SKIP value for this tunnel.
301 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
303 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
313 uint32_t max_child_ack;
316 * How many messages are in the queue.
318 unsigned int queue_n;
321 * How many messages do we accept in the queue.
323 unsigned int queue_max;
326 * Is the speed on the tunnel limited to the slowest peer?
331 * Is the tunnel bufferless (minimum latency)?
336 * Flag to signal the destruction of the tunnel.
337 * If this is set GNUNET_YES the tunnel will be destroyed
338 * when the queue is empty.
343 * Last time the tunnel was used
345 struct GNUNET_TIME_Absolute timestamp;
348 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
349 * containing peers added by id or by type, not intermediate peers.
351 struct GNUNET_CONTAINER_MultiHashMap *peers;
354 * Number of peers that are connected and potentially ready to receive data
356 unsigned int peers_ready;
359 * Number of peers that have been added to the tunnel
361 unsigned int peers_total;
364 * Client owner of the tunnel, if any
366 struct MeshClient *owner;
369 * Clients that have been informed about the tunnel, if any
371 struct MeshClient **clients;
374 * Number of elements in clients
376 unsigned int nclients;
379 * Clients that have requested to leave the tunnel
381 struct MeshClient **ignore;
384 * Number of elements in clients
386 unsigned int nignore;
391 GNUNET_PEER_Id *blacklisted;
394 * Number of elements in blacklisted
396 unsigned int nblacklisted;
399 * Bloomfilter (for peer identities) to stop circular routes
401 char bloomfilter[MESH_BLOOM_SIZE];
406 struct MeshTunnelTree *tree;
409 * Application type we are looking for in this tunnel
411 GNUNET_MESH_ApplicationType type;
414 * Used to search peers offering a service
416 struct GNUNET_DHT_GetHandle *dht_get_type;
419 * Initial context of the regex search for a connect_by_string
421 struct MeshRegexSearchContext *regex_ctx;
424 * Task to keep the used paths alive
426 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
429 * Task to destroy the tunnel after timeout
431 * FIXME: merge the two? a tunnel will have either
432 * a path refresh OR a timeout, never both!
434 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
439 * Info about a child node in a tunnel, needed to perform flow control.
441 struct MeshTunnelChildInfo
444 * ID of the child node.
459 * Maximum PID allowed.
465 * Info needed to work with tunnel paths and peers
472 struct MeshTunnel *t;
475 * Neighbouring peer to whom we send the packet to
477 struct MeshPeerInfo *peer;
482 struct MeshPeerPath *path;
487 * Struct containing information about a client of the service
494 struct MeshClient *next;
499 struct MeshClient *prev;
502 * Tunnels that belong to this client, indexed by local id
504 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
507 * Tunnels this client has accepted, indexed by incoming local id
509 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
512 * Tunnels this client has rejected, indexed by incoming local id
514 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
516 * Handle to communicate with the client
518 struct GNUNET_SERVER_Client *handle;
521 * Applications that this client has claimed to provide
523 struct GNUNET_CONTAINER_MultiHashMap *apps;
526 * Messages that this client has declared interest in
528 struct GNUNET_CONTAINER_MultiHashMap *types;
531 * Whether the client is active or shutting down (don't send confirmations
532 * to a client that is shutting down.
537 * ID of the client, mainly for debug messages
542 * Regular expressions describing the services offered by this client.
544 char **regexes; // FIXME add timeout? API to remove a regex?
547 * Number of regular expressions in regexes.
549 unsigned int n_regex;
552 * Task to refresh all regular expresions in the DHT.
554 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
560 * Struct to keep information of searches of services described by a regex
561 * using a user-provided string service description.
563 struct MeshRegexSearchInfo
566 * Which tunnel is this for
568 struct MeshTunnel *t;
571 * User provided description of the searched service.
576 * Part of the description already consumed by the search.
583 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
586 * Results from running DHT GETs.
588 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
591 * Contexts, for each running DHT GET. Free all on end of search.
593 struct MeshRegexSearchContext **contexts;
596 * Number of contexts (branches/steps in search).
598 unsigned int n_contexts;
601 * Peer that is connecting via connect_by_string. When connected, free ctx.
606 * Other peers that are found but not yet being connected to.
608 GNUNET_PEER_Id *peers;
611 * Number of elements in peers.
613 unsigned int n_peers;
616 * Next peer to try to connect to.
621 * Timeout for a connect attempt.
622 * When reached, try to connect to a different peer, if any. If not,
623 * try the same peer again.
625 GNUNET_SCHEDULER_TaskIdentifier timeout;
630 * Struct to keep state of running searches that have consumed a part of
633 struct MeshRegexSearchContext
636 * Part of the description already consumed by
637 * this particular search branch.
642 * Information about the search.
644 struct MeshRegexSearchInfo *info;
648 /******************************************************************************/
649 /************************ DEBUG FUNCTIONS ****************************/
650 /******************************************************************************/
654 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
655 * @param cls string to print
656 * @param success GNUNET_OK if the PUT was transmitted,
657 * GNUNET_NO on timeout,
658 * GNUNET_SYSERR on disconnect from service
659 * after the PUT message was transmitted
660 * (so we don't know if it was received or not)
665 mesh_debug (void *cls, int success)
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
675 /******************************************************************************/
676 /*********************** GLOBAL VARIABLES ****************************/
677 /******************************************************************************/
681 * Configuration parameters
683 static struct GNUNET_TIME_Relative refresh_path_time;
684 static struct GNUNET_TIME_Relative app_announce_time;
685 static struct GNUNET_TIME_Relative id_announce_time;
686 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
687 static struct GNUNET_TIME_Relative connect_timeout;
688 static long long unsigned int default_ttl;
689 static long long unsigned int dht_replication_level;
690 static long long unsigned int max_tunnels;
691 static long long unsigned int max_msgs_queue;
694 * DLL with all the clients, head.
696 static struct MeshClient *clients;
699 * DLL with all the clients, tail.
701 static struct MeshClient *clients_tail;
704 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
706 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
709 * Number of tunnels known.
711 static unsigned long long n_tunnels;
714 * Tunnels incoming, indexed by MESH_TunnelNumber
715 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
717 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
720 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
722 static struct GNUNET_CONTAINER_MultiHashMap *peers;
725 * Handle to communicate with transport
727 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
730 * Handle to communicate with core.
732 static struct GNUNET_CORE_Handle *core_handle;
737 static struct GNUNET_DHT_Handle *dht_handle;
742 static struct GNUNET_SERVER_Handle *server_handle;
745 * Handle to the statistics service.
747 static struct GNUNET_STATISTICS_Handle *stats;
750 * Notification context, to send messages to local clients.
752 static struct GNUNET_SERVER_NotificationContext *nc;
755 * Local peer own ID (memory efficient handle).
757 static GNUNET_PEER_Id myid;
760 * Local peer own ID (full value).
762 static struct GNUNET_PeerIdentity my_full_id;
767 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
772 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
775 * Tunnel ID for the next created tunnel (global tunnel number).
777 static MESH_TunnelNumber next_tid;
780 * Tunnel ID for the next incoming tunnel (local tunnel number).
782 static MESH_TunnelNumber next_local_tid;
785 * All application types provided by this peer.
787 static struct GNUNET_CONTAINER_MultiHashMap *applications;
790 * All message types clients of this peer are interested in.
792 static struct GNUNET_CONTAINER_MultiHashMap *types;
795 * Task to periodically announce provided applications.
797 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
800 * Task to periodically announce itself in the network.
802 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
805 * Next ID to assign to a client.
807 unsigned int next_client_id;
810 /******************************************************************************/
811 /*********************** DECLARATIONS **************************/
812 /******************************************************************************/
814 /* FIXME move declarations here */
817 * Function to process paths received for a new peer addition. The recorded
818 * paths form the initial tunnel, which can be optimized later.
819 * Called on each result obtained for the DHT search.
822 * @param exp when will this value expire
823 * @param key key of the result
824 * @param type type of the result
825 * @param size number of bytes in data
826 * @param data pointer to the result data
829 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
830 const struct GNUNET_HashCode * key,
831 const struct GNUNET_PeerIdentity *get_path,
832 unsigned int get_path_length,
833 const struct GNUNET_PeerIdentity *put_path,
834 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
835 size_t size, const void *data);
839 * Function to process DHT string to regex matching.
840 * Called on each result obtained for the DHT search.
842 * @param cls closure (search context)
843 * @param exp when will this value expire
844 * @param key key of the result
845 * @param get_path path of the get request (not used)
846 * @param get_path_length lenght of get_path (not used)
847 * @param put_path path of the put request (not used)
848 * @param put_path_length length of the put_path (not used)
849 * @param type type of the result
850 * @param size number of bytes in data
851 * @param data pointer to the result data
853 * TODO: re-issue the request after certain time? cancel after X results?
856 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
857 const struct GNUNET_HashCode * key,
858 const struct GNUNET_PeerIdentity *get_path,
859 unsigned int get_path_length,
860 const struct GNUNET_PeerIdentity *put_path,
861 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
862 size_t size, const void *data);
866 * Function to process DHT string to regex matching.
867 * Called on each result obtained for the DHT search.
869 * @param cls closure (search context)
870 * @param exp when will this value expire
871 * @param key key of the result
872 * @param get_path path of the get request (not used)
873 * @param get_path_length lenght of get_path (not used)
874 * @param put_path path of the put request (not used)
875 * @param put_path_length length of the put_path (not used)
876 * @param type type of the result
877 * @param size number of bytes in data
878 * @param data pointer to the result data
881 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
882 const struct GNUNET_HashCode * key,
883 const struct GNUNET_PeerIdentity *get_path,
884 unsigned int get_path_length,
885 const struct GNUNET_PeerIdentity *put_path,
886 unsigned int put_path_length,
887 enum GNUNET_BLOCK_Type type,
888 size_t size, const void *data);
892 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
893 * and insert it in the appropiate structures if the peer is not known yet.
895 * @param peer Short identity of the peer.
897 * @return Existing or newly created peer info.
899 static struct MeshPeerInfo *
900 peer_info_get_short (const GNUNET_PEER_Id peer);
904 * Try to establish a new connection to this peer.
905 * Use the best path for the given tunnel.
906 * If the peer doesn't have any path to it yet, try to get one.
907 * If the peer already has some path, send a CREATE PATH towards it.
909 * @param peer PeerInfo of the peer.
910 * @param t Tunnel for which to create the path, if possible.
913 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
917 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
919 * If peer already exists, reevaluate shortest path and change if different.
921 * @param t Tunnel we want to add a new peer to
922 * @param peer PeerInfo of the peer being added
926 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
930 * Removes an explicit path from a tunnel, freeing all intermediate nodes
931 * that are no longer needed, as well as nodes of no longer reachable peers.
932 * The tunnel itself is also destoyed if results in a remote empty tunnel.
934 * @param t Tunnel from which to remove the path.
935 * @param peer Short id of the peer which should be removed.
938 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
942 * Search for a tunnel by global ID using full PeerIdentities.
944 * @param oid owner of the tunnel.
945 * @param tid global tunnel number.
947 * @return tunnel handler, NULL if doesn't exist.
949 static struct MeshTunnel *
950 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
954 * Delete an active client from the tunnel.
960 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
963 * Notify a tunnel that a connection has broken that affects at least
966 * @param t Tunnel affected.
967 * @param p1 Peer that got disconnected from p2.
968 * @param p2 Peer that got disconnected from p1.
970 * @return Short ID of the peer disconnected (either p1 or p2).
971 * 0 if the tunnel remained unaffected.
973 static GNUNET_PEER_Id
974 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
979 * Get the current ack value for a tunnel, taking in account the tunnel
980 * mode and the status of all children nodes.
984 * @return Maximum PID allowed.
987 tunnel_get_ack (struct MeshTunnel *t);
991 * Iterator over edges in a regex block retrieved from the DHT.
993 * @param cls Closure.
994 * @param token Token that follows to next state.
995 * @param len Lenght of token.
996 * @param key Hash of next state.
998 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1001 regex_edge_iterator (void *cls,
1004 const struct GNUNET_HashCode *key);
1008 * Find a path to a peer that offers a regex servcie compatible
1009 * with a given string.
1011 * @param key The key of the accepting state.
1012 * @param ctx Context containing info about the string, tunnel, etc.
1015 regex_find_path (const struct GNUNET_HashCode *key,
1016 struct MeshRegexSearchContext *ctx);
1020 * Queue and pass message to core when possible.
1022 * @param cls Closure (type dependant).
1023 * @param type Type of the message, 0 for a raw message.
1024 * @param size Size of the message.
1025 * @param dst Neighbor to send message to.
1026 * @param t Tunnel this message belongs to.
1029 queue_add (void *cls, uint16_t type, size_t size,
1030 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1033 * Free a transmission that was already queued with all resources
1034 * associated to the request.
1036 * @param queue Queue handler to cancel.
1037 * @param clear_cls Is it necessary to free associated cls?
1040 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1042 /******************************************************************************/
1043 /************************ ITERATORS ****************************/
1044 /******************************************************************************/
1047 * Iterator over found existing mesh regex blocks that match an ongoing search.
1049 * @param cls closure
1050 * @param key current key code
1051 * @param value value in the hash map
1052 * @return GNUNET_YES if we should continue to iterate,
1056 regex_result_iterator (void *cls,
1057 const struct GNUNET_HashCode * key,
1060 struct MeshRegexBlock *block = value;
1061 struct MeshRegexSearchContext *ctx = cls;
1063 if (GNUNET_YES == ntohl(block->accepting) &&
1064 ctx->position == strlen (ctx->info->description))
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1067 regex_find_path (key, ctx);
1068 return GNUNET_YES; // We found an accept state!
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1073 ctx->position, strlen(ctx->info->description),
1074 ntohl(block->accepting));
1077 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1078 ®ex_edge_iterator, ctx);
1085 * Iterator over edges in a regex block retrieved from the DHT.
1087 * @param cls Closure (context of the search).
1088 * @param token Token that follows to next state.
1089 * @param len Lenght of token.
1090 * @param key Hash of next state.
1092 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1095 regex_edge_iterator (void *cls,
1098 const struct GNUNET_HashCode *key)
1100 struct MeshRegexSearchContext *ctx = cls;
1101 struct MeshRegexSearchContext *new_ctx;
1102 struct MeshRegexSearchInfo *info = ctx->info;
1103 struct GNUNET_DHT_GetHandle *get_h;
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1110 current = &info->description[ctx->position];
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1112 current_len = strlen (info->description) - ctx->position;
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1117 if (len > current_len)
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1120 return GNUNET_YES; // Token too long, wont match
1122 if (0 != strncmp (current, token, len))
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1125 return GNUNET_YES; // Token doesn't match
1127 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1128 new_ctx->info = info;
1129 new_ctx->position = ctx->position + len;
1130 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1132 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1135 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1136 ®ex_result_iterator,
1138 return GNUNET_YES; // We are already looking for it
1140 /* Start search in DHT */
1142 GNUNET_DHT_get_start (dht_handle, /* handle */
1143 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1144 key, /* key to search */
1145 dht_replication_level, /* replication level */
1146 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1147 NULL, /* xquery */ // FIXME BLOOMFILTER
1148 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1149 &dht_get_string_handler, new_ctx);
1151 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1152 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1162 * Iterator over hash map entries to cancel DHT GET requests after a
1163 * successful connect_by_string.
1165 * @param cls Closure (unused).
1166 * @param key Current key code (unused).
1167 * @param value Value in the hash map (get handle).
1168 * @return GNUNET_YES if we should continue to iterate,
1172 regex_cancel_dht_get (void *cls,
1173 const struct GNUNET_HashCode * key,
1176 struct GNUNET_DHT_GetHandle *h = value;
1178 GNUNET_DHT_get_stop (h);
1184 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1185 * search for connect_by_string.
1187 * @param cls Closure (unused).
1188 * @param key Current key code (unused).
1189 * @param value MeshRegexBlock in the hash map.
1190 * @return GNUNET_YES if we should continue to iterate,
1194 regex_free_result (void *cls,
1195 const struct GNUNET_HashCode * key,
1199 GNUNET_free (value);
1205 * Regex callback iterator to store own service description in the DHT.
1207 * @param cls closure.
1208 * @param key hash for current state.
1209 * @param proof proof for current state.
1210 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1211 * @param num_edges number of edges leaving current state.
1212 * @param edges edges leaving current state.
1215 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1216 int accepting, unsigned int num_edges,
1217 const struct GNUNET_REGEX_Edge *edges)
1219 struct MeshRegexBlock *block;
1220 struct MeshRegexEdge *block_edge;
1221 enum GNUNET_DHT_RouteOption opt;
1225 unsigned int offset;
1228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1229 " regex dht put for state %s\n",
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1239 if (GNUNET_YES == accepting)
1241 struct MeshRegexAccept block;
1243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1244 " state %s is accepting, putting own id\n",
1246 size = sizeof (block);
1248 block.id = my_full_id;
1250 GNUNET_DHT_put(dht_handle, key,
1251 dht_replication_level,
1252 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1253 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1256 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1261 len = strlen(proof);
1262 size = sizeof (struct MeshRegexBlock) + len;
1263 block = GNUNET_malloc (size);
1266 block->n_proof = htonl (len);
1267 block->n_edges = htonl (num_edges);
1268 block->accepting = htonl (accepting);
1270 /* Store the proof at the end of the block. */
1271 aux = (char *) &block[1];
1272 memcpy (aux, proof, len);
1275 /* Store each edge in a variable length MeshEdge struct at the
1276 * very end of the MeshRegexBlock structure.
1278 for (i = 0; i < num_edges; i++)
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 " edge %s towards %s\n",
1283 GNUNET_h2s(&edges[i].destination));
1285 /* aux points at the end of the last block */
1286 len = strlen (edges[i].label);
1287 size += sizeof (struct MeshRegexEdge) + len;
1288 // Calculate offset FIXME is this ok? use size instead?
1289 offset = aux - (char *) block;
1290 block = GNUNET_realloc (block, size);
1291 aux = &((char *) block)[offset];
1292 block_edge = (struct MeshRegexEdge *) aux;
1293 block_edge->key = edges[i].destination;
1294 block_edge->n_token = htonl (len);
1295 aux = (char *) &block_edge[1];
1296 memcpy (aux, edges[i].label, len);
1300 GNUNET_DHT_put(dht_handle, key,
1301 dht_replication_level,
1303 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1305 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1309 GNUNET_free (block);
1314 * Store the regular expression describing a local service into the DHT.
1316 * @param regex The regular expresion.
1319 regex_put (const char *regex)
1321 struct GNUNET_REGEX_Automaton *dfa;
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1324 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1325 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1326 GNUNET_REGEX_automaton_destroy (dfa);
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1332 * Find a path to a peer that offers a regex servcie compatible
1333 * with a given string.
1335 * @param key The key of the accepting state.
1336 * @param ctx Context containing info about the string, tunnel, etc.
1339 regex_find_path (const struct GNUNET_HashCode *key,
1340 struct MeshRegexSearchContext *ctx)
1342 struct GNUNET_DHT_GetHandle *get_h;
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1345 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1346 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1347 key, /* key to search */
1348 dht_replication_level, /* replication level */
1349 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1350 GNUNET_DHT_RO_RECORD_ROUTE,
1351 NULL, /* xquery */ // FIXME BLOOMFILTER
1352 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1353 &dht_get_string_accept_handler, ctx);
1354 GNUNET_break (GNUNET_OK ==
1355 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1358 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1363 * Function called if the connect attempt to a peer found via
1364 * connect_by_string times out. Try to connect to another peer, if any.
1365 * Otherwise try to reconnect to the same peer.
1367 * @param cls Closure (info about regex search).
1368 * @param tc TaskContext.
1371 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1373 struct MeshRegexSearchInfo *info = cls;
1374 struct MeshPeerInfo *peer_info;
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1379 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1380 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1388 if (0 < info->n_peers)
1390 // Select next peer, put current in that spot.
1391 id = info->peers[info->i_peer];
1392 info->peers[info->i_peer] = info->peer;
1393 info->i_peer = (info->i_peer + 1) % info->n_peers;
1397 // Try to connect to same peer again.
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1402 peer_info = peer_info_get_short(id);
1403 tunnel_add_peer (info->t, peer_info);
1405 tunnel_delete_peer (info->t, old);
1406 peer_info_connect (peer_info, info->t);
1407 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1408 ®ex_connect_timeout,
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1415 * Cancel an ongoing regex search in the DHT and free all resources.
1417 * @param ctx The search context.
1420 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1422 struct MeshRegexSearchInfo *info = ctx->info;
1425 GNUNET_free (info->description);
1426 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1427 ®ex_cancel_dht_get, NULL);
1428 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1429 ®ex_free_result, NULL);
1430 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1431 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1432 info->t->regex_ctx = NULL;
1433 for (i = 0; i < info->n_contexts; i++)
1435 GNUNET_free (info->contexts[i]);
1437 if (0 < info->n_contexts)
1438 GNUNET_free (info->contexts);
1439 if (0 < info->n_peers)
1440 GNUNET_free (info->peers);
1441 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1443 GNUNET_SCHEDULER_cancel(info->timeout);
1449 /******************************************************************************/
1450 /************************ PERIODIC FUNCTIONS ****************************/
1451 /******************************************************************************/
1454 * Announce iterator over for each application provided by the peer
1456 * @param cls closure
1457 * @param key current key code
1458 * @param value value in the hash map
1459 * @return GNUNET_YES if we should continue to
1464 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1466 struct PBlock block;
1467 struct MeshClient *c;
1469 block.id = my_full_id;
1470 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1471 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1472 if (0 == block.type)
1477 block.type = htonl (block.type);
1479 GNUNET_break (NULL !=
1480 GNUNET_DHT_put (dht_handle, key,
1481 dht_replication_level,
1482 GNUNET_DHT_RO_RECORD_ROUTE |
1483 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1484 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1486 (const char *) &block,
1487 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1489 app_announce_time, NULL, NULL));
1495 * Periodically announce what applications are provided by local clients
1498 * @param cls closure
1499 * @param tc task context
1502 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1504 struct MeshClient *c = cls;
1507 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1509 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1513 DEBUG_DHT ("Starting PUT for regex\n");
1515 for (i = 0; i < c->n_regex; i++)
1517 regex_put (c->regexes[i]);
1519 c->regex_announce_task =
1520 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1521 DEBUG_DHT ("Finished PUT for regex\n");
1528 * Periodically announce what applications are provided by local clients
1531 * @param cls closure
1532 * @param tc task context
1535 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1537 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1539 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1543 DEBUG_DHT ("Starting PUT for apps\n");
1545 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1547 announce_applications_task =
1548 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1550 DEBUG_DHT ("Finished PUT for apps\n");
1557 * Periodically announce self id in the DHT
1559 * @param cls closure
1560 * @param tc task context
1563 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1565 struct PBlock block;
1567 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1569 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1573 * - Set data expiration in function of X
1574 * - Adapt X to churn
1576 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1578 block.id = my_full_id;
1579 block.type = htonl (0);
1580 GNUNET_DHT_put (dht_handle, /* DHT handle */
1581 &my_full_id.hashPubKey, /* Key to use */
1582 dht_replication_level, /* Replication level */
1583 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1584 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1585 sizeof (block), /* Size of the data */
1586 (const char *) &block, /* Data itself */
1587 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1588 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1589 NULL, /* Continuation */
1590 NULL); /* Continuation closure */
1592 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1596 /******************************************************************************/
1597 /****************** GENERAL HELPER FUNCTIONS ************************/
1598 /******************************************************************************/
1602 * Decrements the reference counter and frees all resources if needed
1604 * @param mesh_data Data Descriptor used in a multicast message.
1605 * Freed no longer needed (last message).
1608 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1610 /* Make sure it's a multicast packet */
1611 GNUNET_assert (NULL != mesh_data->reference_counter);
1613 if (0 == --(*(mesh_data->reference_counter)))
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1616 if (NULL != mesh_data->task)
1618 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1620 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1622 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1624 GNUNET_free (mesh_data->task);
1626 GNUNET_free (mesh_data->reference_counter);
1627 GNUNET_free (mesh_data->data);
1628 GNUNET_free (mesh_data);
1634 * Check if client has registered with the service and has not disconnected
1636 * @param client the client to check
1638 * @return non-NULL if client exists in the global DLL
1640 static struct MeshClient *
1641 client_get (struct GNUNET_SERVER_Client *client)
1643 struct MeshClient *c;
1648 if (c->handle == client)
1657 * Checks if a given client has subscribed to certain message type
1659 * @param message_type Type of message to check
1660 * @param c Client to check
1662 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1665 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1667 struct GNUNET_HashCode hc;
1669 if (NULL == c->types)
1671 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1672 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1677 * Allow a client to send more data after transmitting a multicast message
1678 * which some neighbor has not yet accepted altough a reasonable time has
1681 * @param cls Closure (DataDescriptor containing the task identifier)
1682 * @param tc Task Context
1684 * FIXME reference counter cshould be just int
1687 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1689 struct MeshData *mdata = cls;
1691 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1693 GNUNET_assert (NULL != mdata->reference_counter);
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1696 *(mdata->reference_counter));
1697 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1698 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1703 * Check whether client wants traffic from a tunnel.
1705 * @param c Client to check.
1706 * @param t Tunnel to be found.
1708 * @return GNUNET_YES if client knows tunnel.
1710 * TODO look in client hashmap
1713 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1717 for (i = 0; i < t->nclients; i++)
1718 if (t->clients[i] == c)
1725 * Check whether client has been informed about a tunnel.
1727 * @param c Client to check.
1728 * @param t Tunnel to be found.
1730 * @return GNUNET_YES if client knows tunnel.
1732 * TODO look in client hashmap
1735 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1739 for (i = 0; i < t->nignore; i++)
1740 if (t->ignore[i] == c)
1742 return client_wants_tunnel(c, t);
1747 * Marks a client as uninterested in traffic from the tunnel, updating both
1748 * client and tunnel to reflect this.
1750 * @param c Client that doesn't want traffic anymore.
1751 * @param t Tunnel which should be ignored.
1753 * FIXME when to delete an incoming tunnel?
1756 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1758 struct GNUNET_HashCode hash;
1760 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1761 GNUNET_break (GNUNET_YES ==
1762 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1764 GNUNET_break (GNUNET_YES ==
1765 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1766 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1767 tunnel_delete_active_client (t, c);
1768 GNUNET_array_append (t->ignore, t->nignore, c);
1773 * Deletes a tunnel from a client (either owner or destination). To be used on
1774 * tunnel destroy, otherwise, use client_ignore_tunnel.
1776 * @param c Client whose tunnel to delete.
1777 * @param t Tunnel which should be deleted.
1780 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1782 struct GNUNET_HashCode hash;
1786 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1787 GNUNET_assert (GNUNET_YES ==
1788 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1794 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1796 GNUNET_assert (GNUNET_YES ==
1797 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1801 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1810 * Send the message to all clients that have subscribed to its type
1812 * @param msg Pointer to the message itself
1813 * @param payload Pointer to the payload of the message.
1814 * @return number of clients this message was sent to
1817 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1818 const struct GNUNET_MessageHeader *payload)
1820 struct GNUNET_PeerIdentity *oid;
1821 struct MeshClient *c;
1822 struct MeshTunnel *t;
1823 MESH_TunnelNumber *tid;
1826 char cbuf[htons (msg->size)];
1828 type = ntohs (payload->type);
1829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1832 memcpy (cbuf, msg, sizeof (cbuf));
1833 switch (htons (msg->type))
1835 struct GNUNET_MESH_Unicast *uc;
1836 struct GNUNET_MESH_Multicast *mc;
1837 struct GNUNET_MESH_ToOrigin *to;
1839 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1840 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1844 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1845 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1849 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1850 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1858 t = tunnel_get (oid, ntohl (*tid));
1865 for (count = 0, c = clients; c != NULL; c = c->next)
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1868 if (client_is_subscribed (type, c))
1870 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1874 *tid = htonl (t->local_tid);
1878 if (GNUNET_NO == client_knows_tunnel (c, t))
1880 /* This client doesn't know the tunnel */
1881 struct GNUNET_MESH_TunnelNotification tmsg;
1882 struct GNUNET_HashCode hash;
1884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1885 tmsg.header.size = htons (sizeof (tmsg));
1886 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1887 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1888 tmsg.tunnel_id = htonl (t->local_tid_dest);
1889 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1890 &tmsg.header, GNUNET_NO);
1891 GNUNET_array_append (t->clients, t->nclients, c);
1892 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1894 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1895 c->incoming_tunnels, &hash, t,
1896 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1898 *tid = htonl (t->local_tid_dest);
1901 /* Check if the client wants to get traffic from the tunnel */
1902 if (GNUNET_NO == client_wants_tunnel(c, t))
1905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1906 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1907 (struct GNUNET_MessageHeader
1908 *) cbuf, GNUNET_YES);
1916 * Notify the client that owns the tunnel that a peer has connected to it
1917 * (the requested path to it has been confirmed).
1919 * @param t Tunnel whose owner to notify
1920 * @param id Short id of the peer that has connected
1923 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1925 struct GNUNET_MESH_PeerControl pc;
1927 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1928 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1929 pc.tunnel_id = htonl (t->local_tid);
1930 GNUNET_PEER_resolve (id, &pc.peer);
1931 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1937 * Notify a client about how many more payload packages will we accept
1938 * on a given tunnel.
1944 send_client_tunnel_ack (struct MeshClient *c, struct MeshTunnel *t)
1946 struct GNUNET_MESH_LocalAck msg;
1949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1950 "Sending client ACK on tunnel %X\n",
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to client %u\n", c->id);
1957 ack = tunnel_get_ack (t);
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
1960 if (t->last_ack == ack)
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
1965 msg.header.size = htons (sizeof (msg));
1966 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1967 msg.tunnel_id = htonl (t->local_tid);
1968 msg.max_pid = htonl (ack);
1970 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1971 &msg.header, GNUNET_NO);
1976 * Notify all clients (not depending on registration status) that the incoming
1977 * tunnel is no longer valid.
1979 * @param t Tunnel that was destroyed.
1982 send_clients_tunnel_destroy (struct MeshTunnel *t)
1984 struct GNUNET_MESH_TunnelMessage msg;
1986 msg.header.size = htons (sizeof (msg));
1987 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1988 msg.tunnel_id = htonl (t->local_tid_dest);
1989 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1994 * Notify clients of tunnel disconnections, if needed.
1995 * In case the origin disconnects, the destination clients get a tunnel destroy
1996 * notification. If the last destination disconnects (only one remaining client
1997 * in tunnel), the origin gets a (local ID) peer disconnected.
1998 * Note that the function must be called BEFORE removing the client from
2001 * @param t Tunnel that was destroyed.
2002 * @param c Client that disconnected.
2005 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2011 struct GNUNET_MESH_TunnelMessage msg;
2013 msg.header.size = htons (sizeof (msg));
2014 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2015 msg.tunnel_id = htonl (t->local_tid_dest);
2016 for (i = 0; i < t->nclients; i++)
2017 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2018 &msg.header, GNUNET_NO);
2020 // FIXME when to disconnect an incoming tunnel?
2021 else if (1 == t->nclients && NULL != t->owner)
2023 struct GNUNET_MESH_PeerControl msg;
2025 msg.header.size = htons (sizeof (msg));
2026 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2027 msg.tunnel_id = htonl (t->local_tid);
2028 msg.peer = my_full_id;
2029 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2030 &msg.header, GNUNET_NO);
2036 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2037 * and insert it in the appropiate structures if the peer is not known yet.
2039 * @param peer Full identity of the peer.
2041 * @return Existing or newly created peer info.
2043 static struct MeshPeerInfo *
2044 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2046 struct MeshPeerInfo *peer_info;
2048 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2049 if (NULL == peer_info)
2052 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2053 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2054 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2055 peer_info->id = GNUNET_PEER_intern (peer);
2063 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2064 * and insert it in the appropiate structures if the peer is not known yet.
2066 * @param peer Short identity of the peer.
2068 * @return Existing or newly created peer info.
2070 static struct MeshPeerInfo *
2071 peer_info_get_short (const GNUNET_PEER_Id peer)
2073 struct GNUNET_PeerIdentity id;
2075 GNUNET_PEER_resolve (peer, &id);
2076 return peer_info_get (&id);
2081 * Iterator to remove the tunnel from the list of tunnels a peer participates
2084 * @param cls Closure (tunnel info)
2085 * @param key GNUNET_PeerIdentity of the peer (unused)
2086 * @param value PeerInfo of the peer
2088 * @return always GNUNET_YES, to keep iterating
2091 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2093 struct MeshTunnel *t = cls;
2094 struct MeshPeerInfo *peer = value;
2097 for (i = 0; i < peer->ntunnels; i++)
2100 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2103 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2104 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2113 * Core callback to write a pre-constructed data packet to core buffer
2115 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2116 * @param size Number of bytes available in buf.
2117 * @param buf Where the to write the message.
2119 * @return number of bytes written to buf
2122 send_core_data_raw (void *cls, size_t size, void *buf)
2124 struct MeshTransmissionDescriptor *info = cls;
2125 struct GNUNET_MessageHeader *msg;
2128 GNUNET_assert (NULL != info);
2129 GNUNET_assert (NULL != info->mesh_data);
2130 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2131 total_size = ntohs (msg->size);
2133 if (total_size > size)
2138 memcpy (buf, msg, total_size);
2139 data_descriptor_decrement_rc (info->mesh_data);
2146 * Sends an already built non-multicast message to a peer,
2147 * properly registrating all used resources.
2149 * @param message Message to send. Function makes a copy of it.
2150 * @param peer Short ID of the neighbor whom to send the message.
2151 * @param t Tunnel on which this message is transmitted.
2154 send_message (const struct GNUNET_MessageHeader *message,
2155 const struct GNUNET_PeerIdentity *peer,
2156 struct MeshTunnel *t)
2158 struct MeshTransmissionDescriptor *info;
2159 struct MeshPeerInfo *neighbor;
2160 struct MeshPeerPath *p;
2163 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2165 size = ntohs (message->size);
2166 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2167 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2168 info->mesh_data->data = GNUNET_malloc (size);
2169 memcpy (info->mesh_data->data, message, size);
2170 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
2172 struct GNUNET_MESH_Unicast *m;
2174 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2175 m->ttl = htonl (ntohl (m->ttl) - 1);
2177 info->mesh_data->data_len = size;
2178 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2179 *info->mesh_data->reference_counter = 1;
2180 neighbor = peer_info_get (peer);
2181 for (p = neighbor->path_head; NULL != p; p = p->next)
2190 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2191 GNUNET_free (info->mesh_data->data);
2192 GNUNET_free (info->mesh_data);
2196 info->peer = neighbor;
2206 * Sends a CREATE PATH message for a path to a peer, properly registrating
2207 * all used resources.
2209 * @param peer PeerInfo of the final peer for whom this path is being created.
2210 * @param p Path itself.
2211 * @param t Tunnel for which the path is created.
2214 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2215 struct MeshTunnel *t)
2217 struct GNUNET_PeerIdentity id;
2218 struct MeshPathInfo *path_info;
2219 struct MeshPeerInfo *neighbor;
2225 p = tree_get_path_to_peer (t->tree, peer->id);
2232 for (i = 0; i < p->length; i++)
2234 if (p->peers[i] == myid)
2237 if (i >= p->length - 1)
2243 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2245 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2246 path_info->path = p;
2248 neighbor = peer_info_get (&id);
2249 path_info->peer = neighbor;
2250 queue_add (path_info,
2251 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2252 sizeof (struct GNUNET_MESH_ManipulatePath) +
2253 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2260 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2262 * @param t Tunnel whose path to destroy.
2263 * @param destination Short ID of the peer to whom the path to destroy.
2266 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2268 struct MeshPeerPath *p;
2271 p = tree_get_path_to_peer (t->tree, destination);
2277 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2278 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2280 struct GNUNET_MESH_ManipulatePath *msg;
2281 struct GNUNET_PeerIdentity *pi;
2285 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2286 msg->header.size = htons (size);
2287 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2288 msg->tid = htonl (t->id.tid);
2289 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2290 for (i = 0; i < p->length; i++)
2292 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2294 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2301 * Try to establish a new connection to this peer.
2302 * Use the best path for the given tunnel.
2303 * If the peer doesn't have any path to it yet, try to get one.
2304 * If the peer already has some path, send a CREATE PATH towards it.
2306 * @param peer PeerInfo of the peer.
2307 * @param t Tunnel for which to create the path, if possible.
2310 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2312 struct MeshPeerPath *p;
2313 struct MeshPathInfo *path_info;
2315 if (NULL != peer->path_head)
2317 p = tree_get_path_to_peer (t->tree, peer->id);
2324 // FIXME always send create path to self
2327 send_create_path (peer, p, t);
2331 struct GNUNET_HashCode hash;
2334 send_client_peer_connected (t, myid);
2335 t->local_tid_dest = next_local_tid++;
2336 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2339 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2340 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2347 else if (NULL == peer->dhtget)
2349 struct GNUNET_PeerIdentity id;
2351 GNUNET_PEER_resolve (peer->id, &id);
2352 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2353 path_info->peer = peer;
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2356 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2357 peer->dhtgetcls = path_info;
2358 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2359 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2360 &id.hashPubKey, /* key to search */
2361 dht_replication_level, /* replication level */
2362 GNUNET_DHT_RO_RECORD_ROUTE |
2363 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2364 NULL, /* xquery */ // FIXME BLOOMFILTER
2365 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2366 &dht_get_id_handler, path_info);
2368 /* Otherwise, there is no path but the DHT get is already started. */
2373 * Task to delay the connection of a peer
2375 * @param cls Closure (path info with tunnel and peer to connect).
2376 * Will be free'd on exection.
2377 * @param tc TaskContext
2380 peer_info_connect_task (void *cls,
2381 const struct GNUNET_SCHEDULER_TaskContext *tc)
2383 struct MeshPathInfo *path_info = cls;
2385 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2390 peer_info_connect (path_info->peer, path_info->t);
2396 * Destroy the peer_info and free any allocated resources linked to it
2398 * @param pi The peer_info to destroy.
2400 * @return GNUNET_OK on success
2403 peer_info_destroy (struct MeshPeerInfo *pi)
2405 struct GNUNET_PeerIdentity id;
2406 struct MeshPeerPath *p;
2407 struct MeshPeerPath *nextp;
2409 GNUNET_PEER_resolve (pi->id, &id);
2410 GNUNET_PEER_change_rc (pi->id, -1);
2413 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2417 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2419 if (NULL != pi->dhtget)
2421 GNUNET_DHT_get_stop (pi->dhtget);
2422 GNUNET_free (pi->dhtgetcls);
2428 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2438 * Remove all paths that rely on a direct connection between p1 and p2
2439 * from the peer itself and notify all tunnels about it.
2441 * @param peer PeerInfo of affected peer.
2442 * @param p1 GNUNET_PEER_Id of one peer.
2443 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2446 * TODO: optimize (see below)
2449 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2452 struct MeshPeerPath *p;
2453 struct MeshPeerPath *aux;
2454 struct MeshPeerInfo *peer_d;
2456 unsigned int destroyed;
2461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2463 p = peer->path_head;
2467 for (i = 0; i < (p->length - 1); i++)
2469 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2470 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2472 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2483 for (i = 0; i < peer->ntunnels; i++)
2485 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2489 * Problem: one or more peers have been deleted from the tunnel tree.
2490 * We don't know who they are to try to add them again.
2491 * We need to try to find a new path for each of the disconnected peers.
2492 * Some of them might already have a path to reach them that does not
2493 * involve p1 and p2. Adding all anew might render in a better tree than
2494 * the trivial immediate fix.
2496 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2497 * its children will be reachable trough him.
2499 peer_d = peer_info_get_short (d);
2502 for (p = peer_d->path_head; NULL != p; p = p->next)
2504 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2512 /* No callback, as peer will be already disconnected and a connection
2513 * scheduled by tunnel_notify_connection_broken.
2515 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2519 peer_info_connect (peer_d, peer->tunnels[i]);
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2527 * Add the path to the peer and update the path used to reach it in case this
2530 * @param peer_info Destination peer to add the path to.
2531 * @param path New path to add. Last peer must be the peer in arg 1.
2532 * Path will be either used of freed if already known.
2533 * @param trusted Do we trust that this path is real?
2536 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2539 struct MeshPeerPath *aux;
2543 if ((NULL == peer_info) || (NULL == path))
2546 path_destroy (path);
2549 if (path->peers[path->length - 1] != peer_info->id)
2552 path_destroy (path);
2555 if (path->length <= 2 && GNUNET_NO == trusted)
2557 /* Only allow CORE to tell us about direct paths */
2558 path_destroy (path);
2561 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2562 for (l = 1; l < path->length; l++)
2564 if (path->peers[l] == myid)
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2567 for (l2 = 0; l2 < path->length - l; l2++)
2569 path->peers[l2] = path->peers[l + l2];
2574 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2579 struct GNUNET_PeerIdentity id;
2581 GNUNET_PEER_resolve (peer_info->id, &id);
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2583 path->length, GNUNET_i2s (&id));
2586 l = path_get_length (path);
2593 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2594 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2596 l2 = path_get_length (aux);
2599 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2600 peer_info->path_tail, aux, path);
2605 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2607 path_destroy (path);
2612 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2619 * Add the path to the origin peer and update the path used to reach it in case
2620 * this is the shortest.
2621 * The path is given in peer_info -> destination, therefore we turn the path
2622 * upside down first.
2624 * @param peer_info Peer to add the path to, being the origin of the path.
2625 * @param path New path to add after being inversed.
2626 * @param trusted Do we trust that this path is real?
2629 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2630 struct MeshPeerPath *path, int trusted)
2633 peer_info_add_path (peer_info, path, trusted);
2638 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2639 * to obtain a local peer -> destination path and interning the peer ids.
2641 * @return Newly allocated and created path
2643 static struct MeshPeerPath *
2644 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2645 unsigned int get_path_length,
2646 const struct GNUNET_PeerIdentity *put_path,
2647 unsigned int put_path_length)
2649 struct MeshPeerPath *p;
2655 GNUNET_PEER_change_rc (myid, 1);
2656 i = get_path_length;
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2658 for (i--; i >= 0; i--)
2660 id = GNUNET_PEER_intern (&get_path[i]);
2661 if (p->length > 0 && id == p->peers[p->length - 1])
2663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2664 GNUNET_PEER_change_rc (id, -1);
2668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2669 GNUNET_i2s (&get_path[i]));
2671 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2672 p->peers[p->length - 1] = id;
2675 i = put_path_length;
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2677 for (i--; i >= 0; i--)
2679 id = GNUNET_PEER_intern (&put_path[i]);
2682 /* PUT path went through us, so discard the path up until now and start
2683 * from here to get a much shorter (and loop-free) path.
2688 if (p->length > 0 && id == p->peers[p->length - 1])
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2691 GNUNET_PEER_change_rc (id, -1);
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2696 GNUNET_i2s (&put_path[i]));
2698 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2699 p->peers[p->length - 1] = id;
2703 if (get_path_length > 0)
2704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2705 GNUNET_i2s (&get_path[0]));
2706 if (put_path_length > 0)
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2708 GNUNET_i2s (&put_path[0]));
2709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2711 for (i = 0; i < p->length; i++)
2713 struct GNUNET_PeerIdentity peer_id;
2715 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2717 GNUNET_i2s (&peer_id));
2725 * Adds a path to the peer_infos of all the peers in the path
2727 * @param p Path to process.
2728 * @param confirmed Whether we know if the path works or not.
2731 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2735 /* TODO: invert and add */
2736 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2737 for (i++; i < p->length; i++)
2739 struct MeshPeerInfo *aux;
2740 struct MeshPeerPath *copy;
2742 aux = peer_info_get_short (p->peers[i]);
2743 copy = path_duplicate (p);
2744 copy->length = i + 1;
2745 peer_info_add_path (aux, copy, GNUNET_NO);
2751 * Send keepalive packets for a peer
2753 * @param cls Closure (tunnel for which to send the keepalive).
2754 * @param tc Notification context.
2756 * TODO: implement explicit multicast keepalive?
2759 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2763 * Search for a tunnel among the incoming tunnels
2765 * @param tid the local id of the tunnel
2767 * @return tunnel handler, NULL if doesn't exist
2769 static struct MeshTunnel *
2770 tunnel_get_incoming (MESH_TunnelNumber tid)
2772 struct GNUNET_HashCode hash;
2774 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2775 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2776 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2781 * Search for a tunnel among the tunnels for a client
2783 * @param c the client whose tunnels to search in
2784 * @param tid the local id of the tunnel
2786 * @return tunnel handler, NULL if doesn't exist
2788 static struct MeshTunnel *
2789 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2791 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2793 return tunnel_get_incoming (tid);
2797 struct GNUNET_HashCode hash;
2799 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2800 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2806 * Search for a tunnel by global ID using PEER_ID
2808 * @param pi owner of the tunnel
2809 * @param tid global tunnel number
2811 * @return tunnel handler, NULL if doesn't exist
2813 static struct MeshTunnel *
2814 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2816 struct MESH_TunnelID id;
2817 struct GNUNET_HashCode hash;
2822 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2823 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2828 * Search for a tunnel by global ID using full PeerIdentities
2830 * @param oid owner of the tunnel
2831 * @param tid global tunnel number
2833 * @return tunnel handler, NULL if doesn't exist
2835 static struct MeshTunnel *
2836 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2838 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2843 * Delete an active client from the tunnel.
2849 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2853 for (i = 0; i < t->nclients; i++)
2855 if (t->clients[i] == c)
2857 t->clients[i] = t->clients[t->nclients - 1];
2858 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2866 * Delete an ignored client from the tunnel.
2872 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2876 for (i = 0; i < t->nignore; i++)
2878 if (t->ignore[i] == c)
2880 t->ignore[i] = t->ignore[t->nignore - 1];
2881 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2889 * Delete a client from the tunnel. It should be only done on
2890 * client disconnection, otherwise use client_ignore_tunnel.
2896 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2898 tunnel_delete_ignored_client (t, c);
2899 tunnel_delete_active_client (t, c);
2904 * Iterator to free MeshTunnelChildInfo of tunnel children.
2906 * @param cls Closure (tunnel info).
2907 * @param key Hash of GNUNET_PEER_Id (unused).
2908 * @param value MeshTunnelChildInfo of the child.
2910 * @return always GNUNET_YES, to keep iterating
2913 tunnel_destroy_child (void *cls,
2914 const struct GNUNET_HashCode * key,
2917 GNUNET_free (value);
2923 * Callback used to notify a client owner of a tunnel that a peer has
2924 * disconnected, most likely because of a path change.
2926 * @param cls Closure (tunnel this notification is about).
2927 * @param peer_id Short ID of disconnected peer.
2930 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2932 struct MeshTunnel *t = cls;
2933 struct MeshPeerInfo *peer;
2934 struct MeshPathInfo *path_info;
2936 if (NULL != t->owner && NULL != nc)
2938 struct GNUNET_MESH_PeerControl msg;
2940 msg.header.size = htons (sizeof (msg));
2941 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2942 msg.tunnel_id = htonl (t->local_tid);
2943 GNUNET_PEER_resolve (peer_id, &msg.peer);
2944 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2945 &msg.header, GNUNET_NO);
2947 peer = peer_info_get_short (peer_id);
2948 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2949 path_info->peer = peer;
2951 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2956 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2957 * needed rescources.
2958 * If peer already exists, reevaluate shortest path and change if different.
2960 * @param t Tunnel we want to add a new peer to
2961 * @param peer PeerInfo of the peer being added
2965 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2967 struct GNUNET_PeerIdentity id;
2968 struct MeshPeerPath *best_p;
2969 struct MeshPeerPath *p;
2970 unsigned int best_cost;
2973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2974 GNUNET_PEER_resolve (peer->id, &id);
2976 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2979 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2980 GNUNET_assert (GNUNET_OK ==
2981 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2983 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2986 if (NULL != (p = peer->path_head))
2989 best_cost = tree_get_path_cost (t->tree, p);
2992 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2999 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3000 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3001 t->path_refresh_task =
3002 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3006 /* Start a DHT get */
3007 peer_info_connect (peer, t);
3009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3013 * Add a path to a tunnel which we don't own, just to remember the next hop.
3014 * If destination node was already in the tunnel, the first hop information
3015 * will be replaced with the new path.
3017 * @param t Tunnel we want to add a new peer to
3018 * @param p Path to add
3019 * @param own_pos Position of local node in path.
3023 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3024 unsigned int own_pos)
3026 struct GNUNET_PeerIdentity id;
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3029 GNUNET_assert (0 != own_pos);
3030 tree_add_path (t->tree, p, NULL, NULL);
3031 if (own_pos < p->length - 1)
3033 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3034 tree_update_first_hops (t->tree, myid, &id);
3036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3041 * Notifies a tunnel that a connection has broken that affects at least
3042 * some of its peers. Sends a notification towards the root of the tree.
3043 * In case the peer is the owner of the tree, notifies the client that owns
3044 * the tunnel and tries to reconnect.
3046 * @param t Tunnel affected.
3047 * @param p1 Peer that got disconnected from p2.
3048 * @param p2 Peer that got disconnected from p1.
3050 * @return Short ID of the peer disconnected (either p1 or p2).
3051 * 0 if the tunnel remained unaffected.
3053 static GNUNET_PEER_Id
3054 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3060 tree_notify_connection_broken (t->tree, p1, p2,
3061 &tunnel_notify_client_peer_disconnected,
3063 if (myid != p1 && myid != p2)
3069 if (tree_get_predecessor (t->tree) != 0)
3071 /* We are the peer still connected, notify owner of the disconnection. */
3072 struct GNUNET_MESH_PathBroken msg;
3073 struct GNUNET_PeerIdentity neighbor;
3075 msg.header.size = htons (sizeof (msg));
3076 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3077 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3078 msg.tid = htonl (t->id.tid);
3079 msg.peer1 = my_full_id;
3080 GNUNET_PEER_resolve (pid, &msg.peer2);
3081 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3082 send_message (&msg.header, &neighbor, t);
3090 * Send a multicast packet to a neighbor.
3092 * @param cls Closure (Info about the multicast packet)
3093 * @param neighbor_id Short ID of the neighbor to send the packet to.
3096 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3098 struct MeshData *mdata = cls;
3099 struct MeshTransmissionDescriptor *info;
3100 struct GNUNET_PeerIdentity neighbor;
3102 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3104 info->mesh_data = mdata;
3105 (*(mdata->reference_counter)) ++;
3106 info->destination = neighbor_id;
3107 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3109 GNUNET_i2s (&neighbor));
3110 info->peer = peer_info_get (&neighbor);
3111 GNUNET_assert (NULL != info->peer);
3113 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3114 info->mesh_data->data_len,
3121 * Send a message in a tunnel in multicast, sending a copy to each child node
3122 * down the local one in the tunnel tree.
3124 * @param t Tunnel in which to send the data.
3125 * @param msg Message to be sent.
3126 * @param internal Has the service generated this message?
3129 tunnel_send_multicast (struct MeshTunnel *t,
3130 const struct GNUNET_MessageHeader *msg,
3133 struct MeshData *mdata;
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3136 " sending a multicast packet...\n");
3137 mdata = GNUNET_malloc (sizeof (struct MeshData));
3138 mdata->data_len = ntohs (msg->size);
3139 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3141 mdata->data = GNUNET_malloc (mdata->data_len);
3142 memcpy (mdata->data, msg, mdata->data_len);
3143 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3145 struct GNUNET_MESH_Multicast *mcast;
3147 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3148 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3150 ntohl (mcast->ttl));
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3156 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3157 && GNUNET_NO == internal)
3159 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3161 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3167 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3168 if (*(mdata->reference_counter) == 0)
3170 GNUNET_free (mdata->data);
3171 GNUNET_free (mdata->reference_counter);
3172 if (NULL != mdata->task)
3174 GNUNET_SCHEDULER_cancel(*(mdata->task));
3175 GNUNET_free (mdata->task);
3176 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
3178 // FIXME change order?
3179 GNUNET_free (mdata);
3181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3182 " sending a multicast packet done\n");
3188 * Increase the SKIP value of all peers that
3189 * have not received a unicast message.
3191 * @param cls Closure (ID of the peer that HAS received the message).
3192 * @param key ID of the neighbor.
3193 * @param value Information about the neighbor.
3195 * @return GNUNET_YES to keep iterating.
3198 tunnel_add_skip (void *cls,
3199 const struct GNUNET_HashCode * key,
3202 struct GNUNET_PeerIdentity *neighbor = cls;
3203 struct MeshTunnelChildInfo *cinfo = value;
3205 /* TODO compare only pointers? key == neighbor? */
3206 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3217 * Iterator to get the appropiate ACK value from all children nodes.
3219 * @param cls Closue (tunnel).
3220 * @param id Id of the child node.
3223 tunnel_get_child_ack (void *cls,
3226 struct GNUNET_PeerIdentity peer_id;
3227 struct MeshTunnelChildInfo *cinfo;
3228 struct MeshTunnel *t = cls;
3231 GNUNET_PEER_resolve (id, &peer_id);
3232 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3233 &peer_id.hashPubKey);
3236 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3238 cinfo->pid = t->pid;
3239 cinfo->skip = t->pid;
3240 cinfo->max_pid = ack = t->pid + 1;
3241 GNUNET_assert (GNUNET_OK ==
3242 GNUNET_CONTAINER_multihashmap_put(t->children_fc,
3243 &peer_id.hashPubKey,
3245 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3249 ack = cinfo->max_pid;
3252 if (0 == t->max_child_ack)
3253 t->max_child_ack = ack;
3255 if (GNUNET_YES == t->speed_min)
3257 t->max_child_ack = t->max_child_ack > ack ? ack : t->max_child_ack;
3261 t->max_child_ack = t->max_child_ack > ack ? t->max_child_ack : ack;
3268 * Get the maximum PID allowed to transmit to any
3269 * tunnel child of the local peer.
3273 * @return Maximum PID allowed.
3276 tunnel_get_children_ack (struct MeshTunnel *t)
3278 t->max_child_ack = 0;
3279 tree_iterate_children (t->tree, tunnel_get_child_ack, t);
3280 return t->max_child_ack;
3285 * Get the current ack value for a tunnel, taking in account the tunnel
3286 * mode and the status of all children nodes.
3290 * @return Maximum PID allowed.
3293 tunnel_get_ack (struct MeshTunnel *t)
3296 uint32_t buffer_free;
3300 count = t->pid - t->skip;
3301 buffer_free = t->queue_max - t->queue_n;
3302 ack = count + buffer_free;
3303 child_ack = tunnel_get_children_ack (t);
3305 if (GNUNET_YES == t->speed_min)
3307 ack = child_ack > ack ? ack : child_ack;
3311 ack = child_ack > ack ? child_ack : ack;
3313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u\n", count, buffer_free, child_ack);
3319 * Send an ACK informing the predecessor about the available buffer space.
3320 * If buffering is off, send only on behalf of children or self if endpoint.
3321 * If buffering is on, send when sent to children and buffer space is free.
3323 * @param t Tunnel on which to send the ACK.
3326 tunnel_send_ack (struct MeshTunnel *t, uint16_t type)
3328 struct GNUNET_MESH_ACK msg;
3329 struct GNUNET_PeerIdentity id;
3332 /* Is it after unicast / multicast retransmission? */
3333 if (GNUNET_MESSAGE_TYPE_MESH_ACK != type)
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ACK via DATA retransmission\n");
3336 if (GNUNET_YES == t->nobuffer)
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3341 if (t->queue_max > t->queue_n * 2)
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3348 /* Ok, ACK might be necessary, what PID to ACK? */
3349 ack = tunnel_get_ack (t);
3351 /* If speed_min and not all children have ack'd, dont send yet */
3352 if (ack == t->last_ack)
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, not ready\n");
3359 msg.pid = htonl (ack);
3361 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3363 msg.header.size = htons (sizeof (msg));
3364 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3365 msg.tid = htonl (t->id.tid);
3366 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3367 send_message (&msg.header, &id, t);
3372 * Send a message to all peers in this tunnel that the tunnel is no longer
3375 * @param t The tunnel whose peers to notify.
3378 tunnel_send_destroy (struct MeshTunnel *t)
3380 struct GNUNET_MESH_TunnelDestroy msg;
3382 msg.header.size = htons (sizeof (msg));
3383 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3384 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3385 msg.tid = htonl (t->id.tid);
3386 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3391 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3393 * @param cls Closure (Tunnel which to cancel).
3394 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3397 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3399 struct MeshTunnel *t = cls;
3400 struct MeshPeerInfo *peer_info;
3401 struct MeshPeerQueue *pq;
3402 struct MeshPeerQueue *next;
3404 peer_info = peer_info_get_short (neighbor_id);
3405 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3408 if (pq->tunnel == t)
3410 queue_destroy (pq, GNUNET_YES);
3413 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3415 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3416 peer_info->core_transmit = NULL;
3421 * Destroy the tunnel and free any allocated resources linked to it.
3423 * @param t the tunnel to destroy
3425 * @return GNUNET_OK on success
3428 tunnel_destroy (struct MeshTunnel *t)
3430 struct MeshClient *c;
3431 struct GNUNET_HashCode hash;
3438 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3444 struct GNUNET_PeerIdentity id;
3446 GNUNET_PEER_resolve (t->id.oid, &id);
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3448 GNUNET_i2s (&id), t->id.tid);
3450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3454 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3455 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3460 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3463 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3467 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3468 for (i = 0; i < t->nclients; i++)
3472 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3477 for (i = 0; i < t->nignore; i++)
3481 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3486 if (t->nclients > 0)
3489 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3493 GNUNET_free (t->clients);
3495 if (NULL != t->peers)
3497 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3499 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3502 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3503 &tunnel_destroy_child,
3505 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3507 tree_destroy (t->tree);
3509 if (NULL != t->regex_ctx)
3510 regex_cancel_search (t->regex_ctx);
3511 if (NULL != t->dht_get_type)
3512 GNUNET_DHT_get_stop (t->dht_get_type);
3513 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3514 GNUNET_SCHEDULER_cancel (t->timeout_task);
3515 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3516 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3519 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3520 GNUNET_assert (0 <= n_tunnels);
3527 * Create a new tunnel
3529 * @param owner Who is the owner of the tunnel (short ID).
3530 * @param tid Tunnel Number of the tunnel.
3531 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3532 * @param local Tunnel Number for the tunnel, for the client point of view.
3534 * @return A new initialized tunnel. NULL on error.
3536 static struct MeshTunnel *
3537 tunnel_new (GNUNET_PEER_Id owner,
3538 MESH_TunnelNumber tid,
3539 struct MeshClient *client,
3540 MESH_TunnelNumber local)
3542 struct MeshTunnel *t;
3543 struct GNUNET_HashCode hash;
3545 if (n_tunnels >= max_tunnels && NULL == client)
3548 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3551 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
3552 t->tree = tree_new (owner);
3554 t->local_tid = local;
3555 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
3557 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3559 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3561 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3562 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3567 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3573 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3575 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3576 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3580 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3590 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3591 * that are no longer needed, as well as nodes of no longer reachable peers.
3592 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3594 * @param t Tunnel from which to remove the path.
3595 * @param peer Short id of the peer which should be removed.
3598 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3600 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3606 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3607 * client when the client disconnects. If the client is not the owner, the
3608 * owner will get notified if no more clients are in the tunnel and the client
3609 * get removed from the tunnel's list.
3611 * @param cls closure (client that is disconnecting)
3612 * @param key the hash of the local tunnel id (used to access the hashmap)
3613 * @param value the value stored at the key (tunnel to destroy)
3615 * @return GNUNET_OK on success
3618 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3620 struct MeshTunnel *t = value;
3621 struct MeshClient *c = cls;
3624 send_client_tunnel_disconnect(t, c);
3627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3628 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3629 tunnel_delete_client(t, c);
3630 client_delete_tunnel(c, t);
3633 tunnel_send_destroy(t);
3634 r = tunnel_destroy (t);
3640 * Timeout function, destroys tunnel if called
3642 * @param cls Closure (tunnel to destroy).
3643 * @param tc TaskContext
3646 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3648 struct MeshTunnel *t = cls;
3650 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3652 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3657 * Resets the tunnel timeout. Starts it if no timeout was running.
3659 * @param t Tunnel whose timeout to reset.
3662 tunnel_reset_timeout (struct MeshTunnel *t)
3664 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3665 GNUNET_SCHEDULER_cancel (t->timeout_task);
3667 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3668 (refresh_path_time, 4), &tunnel_timeout, t);
3672 /******************************************************************************/
3673 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3674 /******************************************************************************/
3677 * Function to send a create path packet to a peer.
3679 * @param cls closure
3680 * @param size number of bytes available in buf
3681 * @param buf where the callee should write the message
3682 * @return number of bytes written to buf
3685 send_core_path_create (void *cls, size_t size, void *buf)
3687 struct MeshPathInfo *info = cls;
3688 struct GNUNET_MESH_ManipulatePath *msg;
3689 struct GNUNET_PeerIdentity *peer_ptr;
3690 struct MeshTunnel *t = info->t;
3691 struct MeshPeerPath *p = info->path;
3696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3698 sizeof (struct GNUNET_MESH_ManipulatePath) +
3699 p->length * sizeof (struct GNUNET_PeerIdentity);
3701 if (size < size_needed || NULL == buf)
3706 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3707 msg->header.size = htons (size_needed);
3708 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3709 msg->tid = ntohl (t->id.tid);
3711 if (GNUNET_YES == t->speed_min)
3712 opt = MESH_TUNNEL_OPT_SPEED_MIN;
3713 if (GNUNET_YES == t->nobuffer)
3714 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3715 msg->opt = htonl(opt);
3717 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3718 for (i = 0; i < p->length; i++)
3720 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3727 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3733 * Fill the core buffer
3735 * @param cls closure (data itself)
3736 * @param size number of bytes available in buf
3737 * @param buf where the callee should write the message
3739 * @return number of bytes written to buf
3742 send_core_data_multicast (void *cls, size_t size, void *buf)
3744 struct MeshTransmissionDescriptor *info = cls;
3747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3748 GNUNET_assert (NULL != info);
3749 GNUNET_assert (NULL != info->peer);
3750 total_size = info->mesh_data->data_len;
3751 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3753 if (total_size > size)
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3759 memcpy (buf, info->mesh_data->data, total_size);
3762 struct GNUNET_MESH_Multicast *mc;
3763 struct GNUNET_MessageHeader *mh;
3766 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3768 mc = (struct GNUNET_MESH_Multicast *) mh;
3769 mh = (struct GNUNET_MessageHeader *) &mc[1];
3770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3771 " multicast, payload type %u\n", ntohs (mh->type));
3772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3773 " multicast, payload size %u\n", ntohs (mh->size));
3777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3782 data_descriptor_decrement_rc (info->mesh_data);
3783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3791 * Creates a path ack message in buf and frees all unused resources.
3793 * @param cls closure (MeshTransmissionDescriptor)
3794 * @param size number of bytes available in buf
3795 * @param buf where the callee should write the message
3796 * @return number of bytes written to buf
3799 send_core_path_ack (void *cls, size_t size, void *buf)
3801 struct MeshTransmissionDescriptor *info = cls;
3802 struct GNUNET_MESH_PathACK *msg = buf;
3804 GNUNET_assert (NULL != info);
3805 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3810 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3811 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3812 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3813 msg->tid = htonl (info->origin->tid);
3814 msg->peer_id = my_full_id;
3817 /* TODO add signature */
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3820 return sizeof (struct GNUNET_MESH_PathACK);
3825 * Free a transmission that was already queued with all resources
3826 * associated to the request.
3828 * @param queue Queue handler to cancel.
3829 * @param clear_cls Is it necessary to free associated cls?
3832 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3834 struct MeshTransmissionDescriptor *dd;
3835 struct MeshPathInfo *path_info;
3837 if (GNUNET_YES == clear_cls)
3839 switch (queue->type)
3841 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3842 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3843 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
3846 data_descriptor_decrement_rc (dd->mesh_data);
3848 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3850 path_info = queue->cls;
3851 path_destroy (path_info->path);
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
3857 GNUNET_free_non_null (queue->cls);
3859 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3860 queue->peer->queue_tail,
3862 GNUNET_free (queue);
3867 * Core callback to write a queued packet to core buffer
3869 * @param cls Closure (peer info).
3870 * @param size Number of bytes available in buf.
3871 * @param buf Where the to write the message.
3873 * @return number of bytes written to buf
3876 queue_send (void *cls, size_t size, void *buf)
3878 struct MeshPeerInfo *peer = cls;
3879 struct GNUNET_MessageHeader *msg;
3880 struct MeshPeerQueue *queue;
3881 struct MeshTunnel *t;
3884 peer->core_transmit = NULL;
3885 queue = peer->queue_head;
3887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3889 /* If queue is empty, send should have been cancelled */
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3897 /* Check if buffer size is enough for the message */
3898 if (queue->size > size)
3900 struct GNUNET_PeerIdentity id;
3902 GNUNET_PEER_resolve (peer->id, &id);
3903 peer->core_transmit =
3904 GNUNET_CORE_notify_transmit_ready(core_handle,
3907 GNUNET_TIME_UNIT_FOREVER_REL,
3914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3920 switch (queue->type)
3923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
3924 data_size = send_core_data_raw (queue->cls, size, buf);
3925 msg = (struct GNUNET_MessageHeader *) buf;
3926 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
3927 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3929 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
3931 data_size = send_core_data_multicast(queue->cls, size, buf);
3932 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3934 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3936 data_size = send_core_path_create(queue->cls, size, buf);
3938 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3940 data_size = send_core_path_ack(queue->cls, size, buf);
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
3948 /* Free queue, but cls was freed by send_core_* */
3949 queue_destroy (queue, GNUNET_NO);
3951 if (GNUNET_YES == t->destroy && 0 == t->queue_n)
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3957 /* If more data in queue, send next */
3958 if (NULL != peer->queue_head)
3960 struct GNUNET_PeerIdentity id;
3962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3963 GNUNET_PEER_resolve (peer->id, &id);
3964 peer->core_transmit =
3965 GNUNET_CORE_notify_transmit_ready(core_handle,
3968 GNUNET_TIME_UNIT_FOREVER_REL,
3970 peer->queue_head->size,
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3980 * Queue and pass message to core when possible.
3982 * @param cls Closure (type dependant).
3983 * @param type Type of the message, 0 for a raw message.
3984 * @param size Size of the message.
3985 * @param dst Neighbor to send message to.
3986 * @param t Tunnel this message belongs to.
3989 queue_add (void *cls, uint16_t type, size_t size,
3990 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3992 struct MeshPeerQueue *queue;
3994 if (t->queue_n >= t->queue_max)
3996 if (NULL == t->owner)
3997 GNUNET_break_op(0); // TODO: kill connection?
4000 return; // Drop message
4003 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4009 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4010 if (NULL == dst->core_transmit)
4012 struct GNUNET_PeerIdentity id;
4014 GNUNET_PEER_resolve (dst->id, &id);
4015 dst->core_transmit =
4016 GNUNET_CORE_notify_transmit_ready(core_handle,
4019 GNUNET_TIME_UNIT_FOREVER_REL,
4028 /******************************************************************************/
4029 /******************** MESH NETWORK HANDLERS **************************/
4030 /******************************************************************************/
4034 * Core handler for path creation
4036 * @param cls closure
4037 * @param message message
4038 * @param peer peer identity this notification is about
4039 * @param atsi performance data
4040 * @param atsi_count number of records in 'atsi'
4042 * @return GNUNET_OK to keep the connection open,
4043 * GNUNET_SYSERR to close it (signal serious error)
4046 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4047 const struct GNUNET_MessageHeader *message,
4048 const struct GNUNET_ATS_Information *atsi,
4049 unsigned int atsi_count)
4051 unsigned int own_pos;
4054 MESH_TunnelNumber tid;
4055 struct GNUNET_MESH_ManipulatePath *msg;
4056 struct GNUNET_PeerIdentity *pi;
4057 struct GNUNET_HashCode hash;
4058 struct MeshPeerPath *path;
4059 struct MeshPeerInfo *dest_peer_info;
4060 struct MeshPeerInfo *orig_peer_info;
4061 struct MeshTunnel *t;
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4064 "Received a path create msg [%s]\n",
4065 GNUNET_i2s (&my_full_id));
4066 size = ntohs (message->size);
4067 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4069 GNUNET_break_op (0);
4073 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4074 if (size % sizeof (struct GNUNET_PeerIdentity))
4076 GNUNET_break_op (0);
4079 size /= sizeof (struct GNUNET_PeerIdentity);
4082 GNUNET_break_op (0);
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4086 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4088 tid = ntohl (msg->tid);
4089 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4091 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4092 t = tunnel_get (pi, tid);
4093 if (NULL == t) // FIXME only for INCOMING tunnels?
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4098 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4101 // FIXME notify failure
4104 opt = ntohl (msg->opt);
4105 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4106 GNUNET_YES : GNUNET_NO;
4107 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4108 GNUNET_YES : GNUNET_NO;
4110 if (GNUNET_YES == t->nobuffer)
4113 while (NULL != tunnel_get_incoming (next_local_tid))
4114 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4115 t->local_tid_dest = next_local_tid++;
4116 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4118 tunnel_reset_timeout (t);
4119 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4121 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4122 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4130 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4131 if (NULL == dest_peer_info)
4133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4134 " Creating PeerInfo for destination.\n");
4135 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4136 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4137 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4139 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4141 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4142 if (NULL == orig_peer_info)
4144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4145 " Creating PeerInfo for origin.\n");
4146 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4147 orig_peer_info->id = GNUNET_PEER_intern (pi);
4148 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4149 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4152 path = path_new (size);
4154 for (i = 0; i < size; i++)
4156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4157 GNUNET_i2s (&pi[i]));
4158 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4159 if (path->peers[i] == myid)
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4165 /* cannot be self, must be 'not found' */
4166 /* create path: self not found in path through self */
4167 GNUNET_break_op (0);
4168 path_destroy (path);
4169 /* FIXME error. destroy tunnel? leave for timeout? */
4172 path_add_to_peers (path, GNUNET_NO);
4173 tunnel_add_path (t, path, own_pos);
4174 if (own_pos == size - 1)
4176 /* It is for us! Send ack. */
4177 struct MeshTransmissionDescriptor *info;
4179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4180 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4181 if (NULL == t->peers)
4183 /* New tunnel! Notify clients on data. */
4184 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4186 GNUNET_break (GNUNET_SYSERR !=
4187 GNUNET_CONTAINER_multihashmap_put (t->peers,
4188 &my_full_id.hashPubKey,
4191 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4192 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4193 info->origin = &t->id;
4194 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4195 GNUNET_assert (NULL != info->peer);
4197 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4198 sizeof (struct GNUNET_MESH_PathACK),
4204 struct MeshPeerPath *path2;
4206 /* It's for somebody else! Retransmit. */
4207 path2 = path_duplicate (path);
4208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4209 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4210 path2 = path_duplicate (path);
4211 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4212 send_create_path (dest_peer_info, path, t);
4219 * Core handler for path destruction
4221 * @param cls closure
4222 * @param message message
4223 * @param peer peer identity this notification is about
4224 * @param atsi performance data
4225 * @param atsi_count number of records in 'atsi'
4227 * @return GNUNET_OK to keep the connection open,
4228 * GNUNET_SYSERR to close it (signal serious error)
4231 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4232 const struct GNUNET_MessageHeader *message,
4233 const struct GNUNET_ATS_Information *atsi,
4234 unsigned int atsi_count)
4236 struct GNUNET_MESH_ManipulatePath *msg;
4237 struct GNUNET_PeerIdentity *pi;
4238 struct MeshPeerPath *path;
4239 struct MeshTunnel *t;
4240 unsigned int own_pos;
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4245 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4246 size = ntohs (message->size);
4247 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4249 GNUNET_break_op (0);
4253 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4254 if (size % sizeof (struct GNUNET_PeerIdentity))
4256 GNUNET_break_op (0);
4259 size /= sizeof (struct GNUNET_PeerIdentity);
4262 GNUNET_break_op (0);
4265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4267 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4268 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4270 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4272 t = tunnel_get (pi, ntohl (msg->tid));
4275 /* TODO notify back: we don't know this tunnel */
4276 GNUNET_break_op (0);
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4280 path = path_new (size);
4282 for (i = 0; i < size; i++)
4284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4285 GNUNET_i2s (&pi[i]));
4286 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4287 if (path->peers[i] == myid)
4290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4291 if (own_pos < path->length - 1)
4292 send_message (message, &pi[own_pos + 1], t);
4294 send_client_tunnel_disconnect(t, NULL);
4296 tunnel_delete_peer (t, path->peers[path->length - 1]);
4297 path_destroy (path);
4303 * Core handler for notifications of broken paths
4305 * @param cls closure
4306 * @param message message
4307 * @param peer peer identity this notification is about
4308 * @param atsi performance data
4309 * @param atsi_count number of records in 'atsi'
4311 * @return GNUNET_OK to keep the connection open,
4312 * GNUNET_SYSERR to close it (signal serious error)
4315 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4316 const struct GNUNET_MessageHeader *message,
4317 const struct GNUNET_ATS_Information *atsi,
4318 unsigned int atsi_count)
4320 struct GNUNET_MESH_PathBroken *msg;
4321 struct MeshTunnel *t;
4323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4324 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4325 msg = (struct GNUNET_MESH_PathBroken *) message;
4326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4327 GNUNET_i2s (&msg->peer1));
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4329 GNUNET_i2s (&msg->peer2));
4330 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4333 GNUNET_break_op (0);
4336 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4337 GNUNET_PEER_search (&msg->peer2));
4344 * Core handler for tunnel destruction
4346 * @param cls closure
4347 * @param message message
4348 * @param peer peer identity this notification is about
4349 * @param atsi performance data
4350 * @param atsi_count number of records in 'atsi'
4352 * @return GNUNET_OK to keep the connection open,
4353 * GNUNET_SYSERR to close it (signal serious error)
4356 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4357 const struct GNUNET_MessageHeader *message,
4358 const struct GNUNET_ATS_Information *atsi,
4359 unsigned int atsi_count)
4361 struct GNUNET_MESH_TunnelDestroy *msg;
4362 struct MeshTunnel *t;
4364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4365 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4366 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4368 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4369 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4372 /* Probably already got the message from another path,
4373 * destroyed the tunnel and retransmitted to children.
4376 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4379 if (t->id.oid == myid)
4381 GNUNET_break_op (0);
4384 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4386 /* Tunnel was incoming, notify clients */
4387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4388 t->local_tid, t->local_tid_dest);
4389 send_clients_tunnel_destroy (t);
4391 tunnel_send_destroy (t);
4392 t->destroy = GNUNET_YES;
4393 // TODO: add timeout to destroy the tunnel anyway
4399 * Core handler for mesh network traffic going from the origin to a peer
4401 * @param cls closure
4402 * @param peer peer identity this notification is about
4403 * @param message message
4404 * @param atsi performance data
4405 * @param atsi_count number of records in 'atsi'
4406 * @return GNUNET_OK to keep the connection open,
4407 * GNUNET_SYSERR to close it (signal serious error)
4410 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4411 const struct GNUNET_MessageHeader *message,
4412 const struct GNUNET_ATS_Information *atsi,
4413 unsigned int atsi_count)
4415 struct GNUNET_MESH_Unicast *msg;
4416 struct GNUNET_PeerIdentity *neighbor;
4417 struct MeshTunnelChildInfo *cinfo;
4418 struct MeshTunnel *t;
4419 GNUNET_PEER_Id dest_id;
4424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4426 size = ntohs (message->size);
4428 sizeof (struct GNUNET_MESH_Unicast) +
4429 sizeof (struct GNUNET_MessageHeader))
4434 msg = (struct GNUNET_MESH_Unicast *) message;
4435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4436 ntohs (msg[1].header.type));
4437 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4440 /* TODO notify back: we don't know this tunnel */
4441 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4442 GNUNET_break_op (0);
4445 pid = ntohl (msg->pid);
4448 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4450 " Already seen pid %u, DROPPING!\n", pid);
4455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4456 " pid %u not seen yet, forwarding\n", pid);
4458 t->skip += (pid - t->pid) - 1;
4460 tunnel_reset_timeout (t);
4461 dest_id = GNUNET_PEER_search (&msg->destination);
4462 if (dest_id == myid)
4464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4465 " it's for us! sending to clients...\n");
4466 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4467 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4468 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK); // FIXME send after client processes the packet
4471 ttl = ntohl (msg->ttl);
4472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4475 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4476 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4480 " not for us, retransmitting...\n");
4481 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4483 neighbor = tree_get_first_hop (t->tree, dest_id);
4484 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4485 &neighbor->hashPubKey);
4488 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
4489 cinfo->id = GNUNET_PEER_intern (neighbor);
4491 cinfo->max_pid = pid + t->queue_max - t->queue_n; // FIXME review
4493 GNUNET_assert (GNUNET_OK ==
4494 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
4495 &neighbor->hashPubKey,
4497 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
4500 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4503 send_message (message, neighbor, t);
4509 * Core handler for mesh network traffic going from the origin to all peers
4511 * @param cls closure
4512 * @param message message
4513 * @param peer peer identity this notification is about
4514 * @param atsi performance data
4515 * @param atsi_count number of records in 'atsi'
4516 * @return GNUNET_OK to keep the connection open,
4517 * GNUNET_SYSERR to close it (signal serious error)
4519 * TODO: Check who we got this from, to validate route.
4522 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4523 const struct GNUNET_MessageHeader *message,
4524 const struct GNUNET_ATS_Information *atsi,
4525 unsigned int atsi_count)
4527 struct GNUNET_MESH_Multicast *msg;
4528 struct MeshTunnel *t;
4532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
4534 size = ntohs (message->size);
4535 if (sizeof (struct GNUNET_MESH_Multicast) +
4536 sizeof (struct GNUNET_MessageHeader) > size)
4538 GNUNET_break_op (0);
4541 msg = (struct GNUNET_MESH_Multicast *) message;
4542 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4546 /* TODO notify that we dont know that tunnel */
4547 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4548 GNUNET_break_op (0);
4551 pid = ntohl (msg->pid);
4554 /* already seen this packet, drop */
4555 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4556 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4557 " Already seen pid %u, DROPPING!\n", pid);
4562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4563 " pid %u not seen yet, forwarding\n", pid);
4565 t->skip += (pid - t->pid) - 1;
4567 tunnel_reset_timeout (t);
4569 /* Transmit to locally interested clients */
4570 if (NULL != t->peers &&
4571 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
4573 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
4574 send_subscribed_clients (message, &msg[1].header);
4576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
4577 if (ntohl (msg->ttl) == 0)
4579 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4580 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4583 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
4584 tunnel_send_multicast (t, message, GNUNET_NO);
4590 * Core handler for mesh network traffic toward the owner of a tunnel
4592 * @param cls closure
4593 * @param message message
4594 * @param peer peer identity this notification is about
4595 * @param atsi performance data
4596 * @param atsi_count number of records in 'atsi'
4598 * @return GNUNET_OK to keep the connection open,
4599 * GNUNET_SYSERR to close it (signal serious error)
4602 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4603 const struct GNUNET_MessageHeader *message,
4604 const struct GNUNET_ATS_Information *atsi,
4605 unsigned int atsi_count)
4607 struct GNUNET_MESH_ToOrigin *msg;
4608 struct GNUNET_PeerIdentity id;
4609 struct MeshPeerInfo *peer_info;
4610 struct MeshTunnel *t;
4613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4615 size = ntohs (message->size);
4616 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4617 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4619 GNUNET_break_op (0);
4622 msg = (struct GNUNET_MESH_ToOrigin *) message;
4623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4624 ntohs (msg[1].header.type));
4625 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4629 /* TODO notify that we dont know this tunnel (whom)? */
4630 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4631 GNUNET_break_op (0);
4635 if (t->id.oid == myid)
4638 struct GNUNET_MESH_ToOrigin *copy;
4640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4641 " it's for us! sending to clients...\n");
4642 if (NULL == t->owner)
4644 /* got data packet for ownerless tunnel */
4645 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
4647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4648 GNUNET_break_op (0);
4651 /* TODO signature verification */
4652 memcpy (cbuf, message, size);
4653 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4654 copy->tid = htonl (t->local_tid);
4655 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4656 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4657 ©->header, GNUNET_YES);
4660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4661 " not for us, retransmitting...\n");
4663 peer_info = peer_info_get (&msg->oid);
4664 if (NULL == peer_info)
4666 /* unknown origin of tunnel */
4670 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4671 send_message (message, &id, t);
4672 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4679 * Core handler for mesh network traffic point-to-point acks.
4681 * @param cls closure
4682 * @param message message
4683 * @param peer peer identity this notification is about
4684 * @param atsi performance data
4685 * @param atsi_count number of records in 'atsi'
4687 * @return GNUNET_OK to keep the connection open,
4688 * GNUNET_SYSERR to close it (signal serious error)
4691 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4692 const struct GNUNET_MessageHeader *message,
4693 const struct GNUNET_ATS_Information *atsi,
4694 unsigned int atsi_count)
4696 struct GNUNET_MESH_ACK *msg;
4697 struct MeshTunnelChildInfo *cinfo;
4698 struct MeshTunnel *t;
4701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got an ACK packet from %s\n",
4703 msg = (struct GNUNET_MESH_ACK *) message;
4704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4705 ntohs (msg[1].header.type));
4706 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4710 /* TODO notify that we dont know this tunnel (whom)? */
4711 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4712 GNUNET_break_op (0);
4715 ack = ntohl (msg->pid);
4716 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4720 GNUNET_break_op (0);
4723 cinfo->max_pid = ack;
4724 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4730 * Core handler for path ACKs
4732 * @param cls closure
4733 * @param message message
4734 * @param peer peer identity this notification is about
4735 * @param atsi performance data
4736 * @param atsi_count number of records in 'atsi'
4738 * @return GNUNET_OK to keep the connection open,
4739 * GNUNET_SYSERR to close it (signal serious error)
4742 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4743 const struct GNUNET_MessageHeader *message,
4744 const struct GNUNET_ATS_Information *atsi,
4745 unsigned int atsi_count)
4747 struct GNUNET_MESH_PathACK *msg;
4748 struct GNUNET_PeerIdentity id;
4749 struct MeshPeerInfo *peer_info;
4750 struct MeshPeerPath *p;
4751 struct MeshTunnel *t;
4753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4754 GNUNET_i2s (&my_full_id));
4755 msg = (struct GNUNET_MESH_PathACK *) message;
4756 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4759 /* TODO notify that we don't know the tunnel */
4760 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4762 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4766 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4768 peer_info = peer_info_get (&msg->peer_id);
4769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4770 GNUNET_i2s (&msg->peer_id));
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4774 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
4776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4777 "connect_by_string completed, stopping search\n");
4778 regex_cancel_search (t->regex_ctx);
4779 t->regex_ctx = NULL;
4782 /* Add paths to peers? */
4783 p = tree_get_path_to_peer (t->tree, peer_info->id);
4786 path_add_to_peers (p, GNUNET_YES);
4794 /* Message for us? */
4795 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4798 if (NULL == t->owner)
4800 GNUNET_break_op (0);
4803 if (NULL != t->dht_get_type)
4805 GNUNET_DHT_get_stop (t->dht_get_type);
4806 t->dht_get_type = NULL;
4808 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4810 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4811 send_client_peer_connected (t, peer_info->id);
4816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4817 " not for us, retransmitting...\n");
4818 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4819 peer_info = peer_info_get (&msg->oid);
4820 if (NULL == peer_info)
4822 /* If we know the tunnel, we should DEFINITELY know the peer */
4826 send_message (message, &id, t);
4832 * Functions to handle messages from core
4834 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4835 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4836 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4837 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4838 sizeof (struct GNUNET_MESH_PathBroken)},
4839 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4840 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4841 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4842 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4843 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4844 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4845 sizeof (struct GNUNET_MESH_ACK)},
4846 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4847 sizeof (struct GNUNET_MESH_PathACK)},
4853 /******************************************************************************/
4854 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4855 /******************************************************************************/
4858 * deregister_app: iterator for removing each application registered by a client
4860 * @param cls closure
4861 * @param key the hash of the application id (used to access the hashmap)
4862 * @param value the value stored at the key (client)
4864 * @return GNUNET_OK on success
4867 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
4869 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
4870 GNUNET_break (GNUNET_YES ==
4871 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
4877 * notify_client_connection_failure: notify a client that the connection to the
4878 * requested remote peer is not possible (for instance, no route found)
4879 * Function called when the socket is ready to queue more data. "buf" will be
4880 * NULL and "size" zero if the socket was closed for writing in the meantime.
4882 * @param cls closure
4883 * @param size number of bytes available in buf
4884 * @param buf where the callee should write the message
4885 * @return number of bytes written to buf
4888 notify_client_connection_failure (void *cls, size_t size, void *buf)
4891 struct MeshPeerInfo *peer_info;
4892 struct GNUNET_MESH_PeerControl *msg;
4893 struct GNUNET_PeerIdentity id;
4895 if (0 == size && NULL == buf)
4897 // TODO retry? cancel?
4901 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4902 peer_info = (struct MeshPeerInfo *) cls;
4903 msg = (struct GNUNET_MESH_PeerControl *) buf;
4904 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4905 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4906 // msg->tunnel_id = htonl(peer_info->t->tid);
4907 GNUNET_PEER_resolve (peer_info->id, &id);
4908 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4916 * Send keepalive packets for a peer
4918 * @param cls Closure (tunnel for which to send the keepalive).
4919 * @param tc Notification context.
4921 * TODO: implement explicit multicast keepalive?
4924 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4926 struct MeshTunnel *t = cls;
4927 struct GNUNET_MessageHeader *payload;
4928 struct GNUNET_MESH_Multicast *msg;
4930 sizeof (struct GNUNET_MESH_Multicast) +
4931 sizeof (struct GNUNET_MessageHeader);
4934 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4938 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4941 "sending keepalive for tunnel %d\n", t->id.tid);
4943 msg = (struct GNUNET_MESH_Multicast *) cbuf;
4944 msg->header.size = htons (size);
4945 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4946 msg->oid = my_full_id;
4947 msg->tid = htonl (t->id.tid);
4948 msg->ttl = htonl (default_ttl);
4949 msg->pid = htonl (t->pid + 1);
4951 payload = (struct GNUNET_MessageHeader *) &msg[1];
4952 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
4953 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4954 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
4956 t->path_refresh_task =
4957 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4963 * Function to process paths received for a new peer addition. The recorded
4964 * paths form the initial tunnel, which can be optimized later.
4965 * Called on each result obtained for the DHT search.
4967 * @param cls closure
4968 * @param exp when will this value expire
4969 * @param key key of the result
4970 * @param get_path path of the get request
4971 * @param get_path_length lenght of get_path
4972 * @param put_path path of the put request
4973 * @param put_path_length length of the put_path
4974 * @param type type of the result
4975 * @param size number of bytes in data
4976 * @param data pointer to the result data
4978 * TODO: re-issue the request after certain time? cancel after X results?
4981 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4982 const struct GNUNET_HashCode * key,
4983 const struct GNUNET_PeerIdentity *get_path,
4984 unsigned int get_path_length,
4985 const struct GNUNET_PeerIdentity *put_path,
4986 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4987 size_t size, const void *data)
4989 struct MeshPathInfo *path_info = cls;
4990 struct MeshPeerPath *p;
4991 struct GNUNET_PeerIdentity pi;
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4995 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4998 p = path_build_from_dht (get_path, get_path_length, put_path,
5000 path_add_to_peers (p, GNUNET_NO);
5002 for (i = 0; i < path_info->peer->ntunnels; i++)
5004 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5005 peer_info_connect (path_info->peer, path_info->t);
5013 * Function to process paths received for a new peer addition. The recorded
5014 * paths form the initial tunnel, which can be optimized later.
5015 * Called on each result obtained for the DHT search.
5017 * @param cls closure
5018 * @param exp when will this value expire
5019 * @param key key of the result
5020 * @param get_path path of the get request
5021 * @param get_path_length lenght of get_path
5022 * @param put_path path of the put request
5023 * @param put_path_length length of the put_path
5024 * @param type type of the result
5025 * @param size number of bytes in data
5026 * @param data pointer to the result data
5029 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5030 const struct GNUNET_HashCode * key,
5031 const struct GNUNET_PeerIdentity *get_path,
5032 unsigned int get_path_length,
5033 const struct GNUNET_PeerIdentity *put_path,
5034 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5035 size_t size, const void *data)
5037 const struct PBlock *pb = data;
5038 const struct GNUNET_PeerIdentity *pi = &pb->id;
5039 struct MeshTunnel *t = cls;
5040 struct MeshPeerInfo *peer_info;
5041 struct MeshPeerPath *p;
5043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5044 if (size != sizeof (struct PBlock))
5046 GNUNET_break_op (0);
5049 if (ntohl(pb->type) != t->type)
5051 GNUNET_break_op (0);
5054 GNUNET_assert (NULL != t->owner);
5055 peer_info = peer_info_get (pi);
5056 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5058 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5060 p = path_build_from_dht (get_path, get_path_length, put_path,
5062 path_add_to_peers (p, GNUNET_NO);
5064 tunnel_add_peer (t, peer_info);
5065 peer_info_connect (peer_info, t);
5070 * Function to process DHT string to regex matching.
5071 * Called on each result obtained for the DHT search.
5073 * @param cls closure (search context)
5074 * @param exp when will this value expire
5075 * @param key key of the result
5076 * @param get_path path of the get request (not used)
5077 * @param get_path_length lenght of get_path (not used)
5078 * @param put_path path of the put request (not used)
5079 * @param put_path_length length of the put_path (not used)
5080 * @param type type of the result
5081 * @param size number of bytes in data
5082 * @param data pointer to the result data
5085 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5086 const struct GNUNET_HashCode * key,
5087 const struct GNUNET_PeerIdentity *get_path,
5088 unsigned int get_path_length,
5089 const struct GNUNET_PeerIdentity *put_path,
5090 unsigned int put_path_length,
5091 enum GNUNET_BLOCK_Type type,
5092 size_t size, const void *data)
5094 const struct MeshRegexAccept *block = data;
5095 struct MeshRegexSearchContext *ctx = cls;
5096 struct MeshRegexSearchInfo *info = ctx->info;
5097 struct MeshPeerPath *p;
5098 struct MeshPeerInfo *peer_info;
5100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5103 peer_info = peer_info_get(&block->id);
5104 p = path_build_from_dht (get_path, get_path_length, put_path,
5106 path_add_to_peers (p, GNUNET_NO);
5109 tunnel_add_peer (info->t, peer_info);
5110 peer_info_connect (peer_info, info->t);
5111 if (0 == info->peer)
5113 info->peer = peer_info->id;
5117 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5120 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5121 ®ex_connect_timeout,
5129 * Function to process DHT string to regex matching.
5130 * Called on each result obtained for the DHT search.
5132 * @param cls closure (search context)
5133 * @param exp when will this value expire
5134 * @param key key of the result
5135 * @param get_path path of the get request (not used)
5136 * @param get_path_length lenght of get_path (not used)
5137 * @param put_path path of the put request (not used)
5138 * @param put_path_length length of the put_path (not used)
5139 * @param type type of the result
5140 * @param size number of bytes in data
5141 * @param data pointer to the result data
5143 * TODO: re-issue the request after certain time? cancel after X results?
5146 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5147 const struct GNUNET_HashCode * key,
5148 const struct GNUNET_PeerIdentity *get_path,
5149 unsigned int get_path_length,
5150 const struct GNUNET_PeerIdentity *put_path,
5151 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5152 size_t size, const void *data)
5154 const struct MeshRegexBlock *block = data;
5155 struct MeshRegexSearchContext *ctx = cls;
5156 struct MeshRegexSearchInfo *info = ctx->info;
5160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5161 "DHT GET STRING RETURNED RESULTS\n");
5162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5163 " key: %s\n", GNUNET_h2s (key));
5165 copy = GNUNET_malloc (size);
5166 memcpy (copy, data, size);
5167 GNUNET_break (GNUNET_OK ==
5168 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5169 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5170 len = ntohl (block->n_proof);
5172 char proof[len + 1];
5174 memcpy (proof, &block[1], len);
5176 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5178 GNUNET_break_op (0);
5182 len = strlen (info->description);
5183 if (len == ctx->position) // String processed
5185 if (GNUNET_YES == ntohl (block->accepting))
5187 regex_find_path(key, ctx);
5191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5192 // FIXME REGEX this block not successful, wait for more? start timeout?
5196 GNUNET_break (GNUNET_OK ==
5197 GNUNET_MESH_regex_block_iterate (block, size,
5198 ®ex_edge_iterator, ctx));
5202 /******************************************************************************/
5203 /********************* MESH LOCAL HANDLES **************************/
5204 /******************************************************************************/
5208 * Handler for client disconnection
5210 * @param cls closure
5211 * @param client identification of the client; NULL
5212 * for the last call when the server is destroyed
5215 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5217 struct MeshClient *c;
5218 struct MeshClient *next;
5221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5230 if (c->handle != client)
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5238 GNUNET_SERVER_client_drop (c->handle);
5239 c->shutting_down = GNUNET_YES;
5240 GNUNET_assert (NULL != c->own_tunnels);
5241 GNUNET_assert (NULL != c->incoming_tunnels);
5242 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5243 &tunnel_destroy_iterator, c);
5244 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5245 &tunnel_destroy_iterator, c);
5246 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5247 &tunnel_destroy_iterator, c);
5248 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5249 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5250 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5252 /* deregister clients applications */
5253 if (NULL != c->apps)
5255 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5256 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5258 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5259 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5261 GNUNET_SCHEDULER_cancel (announce_applications_task);
5262 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5264 if (NULL != c->types)
5265 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5266 for (i = 0; i < c->n_regex; i++)
5268 GNUNET_free (c->regexes[i]);
5270 GNUNET_free_non_null (c->regexes);
5271 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5272 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5274 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5277 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5286 * Handler for new clients
5288 * @param cls closure
5289 * @param client identification of the client
5290 * @param message the actual message, which includes messages the client wants
5293 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5294 const struct GNUNET_MessageHeader *message)
5296 struct GNUNET_MESH_ClientConnect *cc_msg;
5297 struct MeshClient *c;
5298 GNUNET_MESH_ApplicationType *a;
5305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5306 /* Check data sanity */
5307 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5308 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5309 ntypes = ntohs (cc_msg->types);
5310 napps = ntohs (cc_msg->applications);
5312 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5319 /* Create new client structure */
5320 c = GNUNET_malloc (sizeof (struct MeshClient));
5321 c->id = next_client_id++;
5322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5324 GNUNET_SERVER_client_keep (client);
5325 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5328 GNUNET_MESH_ApplicationType at;
5329 struct GNUNET_HashCode hc;
5331 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5332 for (i = 0; i < napps; i++)
5335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5336 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5337 /* store in clients hashmap */
5338 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5340 /* store in global hashmap, for announcements */
5341 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5342 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5344 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5345 announce_applications_task =
5346 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5352 struct GNUNET_HashCode hc;
5354 t = (uint16_t *) & a[napps];
5355 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5356 for (i = 0; i < ntypes; i++)
5359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5360 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5362 /* store in clients hashmap */
5363 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5364 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5365 /* store in global hashmap */
5366 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5367 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5371 " client has %u+%u subscriptions\n", napps, ntypes);
5373 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5374 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5375 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5376 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5377 GNUNET_SERVER_notification_context_add (nc, client);
5378 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5380 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5386 * Handler for clients announcing available services by a regular expression.
5388 * @param cls closure
5389 * @param client identification of the client
5390 * @param message the actual message, which includes messages the client wants
5393 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5394 const struct GNUNET_MessageHeader *message)
5396 struct MeshClient *c;
5400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5402 /* Sanity check for client registration */
5403 if (NULL == (c = client_get (client)))
5406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5411 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5412 regex = GNUNET_malloc (len + 1);
5413 memcpy (regex, &message[1], len);
5415 GNUNET_array_append (c->regexes, c->n_regex, regex);
5416 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5418 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5424 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5430 * Handler for requests of new tunnels
5432 * @param cls closure
5433 * @param client identification of the client
5434 * @param message the actual message
5437 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5438 const struct GNUNET_MessageHeader *message)
5440 struct GNUNET_MESH_TunnelMessage *t_msg;
5441 struct MeshTunnel *t;
5442 struct MeshClient *c;
5444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5446 /* Sanity check for client registration */
5447 if (NULL == (c = client_get (client)))
5450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5455 /* Message sanity check */
5456 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5463 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5464 /* Sanity check for tunnel numbering */
5465 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5468 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5471 /* Sanity check for duplicate tunnel IDs */
5472 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
5475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5479 while (NULL != tunnel_get_by_pi (myid, next_tid))
5480 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5481 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
5484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5485 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5488 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5490 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5491 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
5493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5494 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5500 * Handler for requests of deleting tunnels
5502 * @param cls closure
5503 * @param client identification of the client
5504 * @param message the actual message
5507 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5508 const struct GNUNET_MessageHeader *message)
5510 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5511 struct MeshClient *c;
5512 struct MeshTunnel *t;
5513 MESH_TunnelNumber tid;
5515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5516 "Got a DESTROY TUNNEL from client!\n");
5518 /* Sanity check for client registration */
5519 if (NULL == (c = client_get (client)))
5522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5525 /* Message sanity check */
5526 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5533 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5535 /* Retrieve tunnel */
5536 tid = ntohl (tunnel_msg->tunnel_id);
5537 t = tunnel_get_by_local_id(c, tid);
5541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5545 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5547 client_ignore_tunnel (c, t);
5549 // TODO: when to destroy incoming tunnel?
5550 if (t->nclients == 0)
5552 GNUNET_assert (GNUNET_YES ==
5553 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
5555 GNUNET_assert (GNUNET_YES ==
5556 GNUNET_CONTAINER_multihashmap_remove (t->peers,
5557 &my_full_id.hashPubKey,
5561 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5564 send_client_tunnel_disconnect(t, c);
5565 client_delete_tunnel(c, t);
5567 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5569 tunnel_send_destroy (t);
5570 t->destroy = GNUNET_YES;
5571 // The tunnel will be destroyed when the last message is transmitted.
5572 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5578 * Handler for requests of seeting tunnel's speed.
5580 * @param cls Closure (unused).
5581 * @param client Identification of the client.
5582 * @param message The actual message.
5585 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
5586 const struct GNUNET_MessageHeader *message)
5588 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5589 struct MeshClient *c;
5590 struct MeshTunnel *t;
5591 MESH_TunnelNumber tid;
5593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5594 "Got a SPEED request from client!\n");
5596 /* Sanity check for client registration */
5597 if (NULL == (c = client_get (client)))
5600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5605 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5607 /* Retrieve tunnel */
5608 tid = ntohl (tunnel_msg->tunnel_id);
5609 t = tunnel_get_by_local_id(c, tid);
5613 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5614 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5618 switch (ntohs(message->type))
5620 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
5621 t->speed_min = GNUNET_YES;
5623 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
5624 t->speed_min = GNUNET_NO;
5629 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5634 * Handler for requests of seeting tunnel's buffering policy.
5636 * @param cls Closure (unused).
5637 * @param client Identification of the client.
5638 * @param message The actual message.
5641 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5642 const struct GNUNET_MessageHeader *message)
5644 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5645 struct MeshClient *c;
5646 struct MeshTunnel *t;
5647 MESH_TunnelNumber tid;
5649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5650 "Got a BUFFER request from client!\n");
5652 /* Sanity check for client registration */
5653 if (NULL == (c = client_get (client)))
5656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5661 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5663 /* Retrieve tunnel */
5664 tid = ntohl (tunnel_msg->tunnel_id);
5665 t = tunnel_get_by_local_id(c, tid);
5669 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5674 switch (ntohs(message->type))
5676 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5677 t->nobuffer = GNUNET_NO;
5679 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5680 t->nobuffer = GNUNET_YES;
5686 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5691 * Handler for connection requests to new peers
5693 * @param cls closure
5694 * @param client identification of the client
5695 * @param message the actual message (PeerControl)
5698 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
5699 const struct GNUNET_MessageHeader *message)
5701 struct GNUNET_MESH_PeerControl *peer_msg;
5702 struct MeshPeerInfo *peer_info;
5703 struct MeshClient *c;
5704 struct MeshTunnel *t;
5705 MESH_TunnelNumber tid;
5707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
5708 /* Sanity check for client registration */
5709 if (NULL == (c = client_get (client)))
5712 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5716 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5717 /* Sanity check for message size */
5718 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5725 /* Tunnel exists? */
5726 tid = ntohl (peer_msg->tunnel_id);
5727 t = tunnel_get_by_local_id (c, tid);
5731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5735 /* Does client own tunnel? */
5736 if (t->owner->handle != client)
5739 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
5743 GNUNET_i2s (&peer_msg->peer));
5744 peer_info = peer_info_get (&peer_msg->peer);
5746 tunnel_add_peer (t, peer_info);
5747 peer_info_connect (peer_info, t);
5749 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5755 * Handler for disconnection requests of peers in a tunnel
5757 * @param cls closure
5758 * @param client identification of the client
5759 * @param message the actual message (PeerControl)
5762 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
5763 const struct GNUNET_MessageHeader *message)
5765 struct GNUNET_MESH_PeerControl *peer_msg;
5766 struct MeshPeerInfo *peer_info;
5767 struct MeshClient *c;
5768 struct MeshTunnel *t;
5769 MESH_TunnelNumber tid;
5771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
5772 /* Sanity check for client registration */
5773 if (NULL == (c = client_get (client)))
5776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5779 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5780 /* Sanity check for message size */
5781 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5784 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5788 /* Tunnel exists? */
5789 tid = ntohl (peer_msg->tunnel_id);
5790 t = tunnel_get_by_local_id (c, tid);
5794 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5799 /* Does client own tunnel? */
5800 if (t->owner->handle != client)
5803 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
5808 GNUNET_i2s (&peer_msg->peer));
5809 /* Is the peer in the tunnel? */
5811 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
5812 if (NULL == peer_info)
5815 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5819 /* Ok, delete peer from tunnel */
5820 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
5821 &peer_msg->peer.hashPubKey);
5823 send_destroy_path (t, peer_info->id);
5824 tunnel_delete_peer (t, peer_info->id);
5825 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5830 * Handler for blacklist requests of peers in a tunnel
5832 * @param cls closure
5833 * @param client identification of the client
5834 * @param message the actual message (PeerControl)
5837 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
5838 const struct GNUNET_MessageHeader *message)
5840 struct GNUNET_MESH_PeerControl *peer_msg;
5841 struct MeshClient *c;
5842 struct MeshTunnel *t;
5843 MESH_TunnelNumber tid;
5845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
5846 /* Sanity check for client registration */
5847 if (NULL == (c = client_get (client)))
5850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5853 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5855 /* Sanity check for message size */
5856 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5859 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5863 /* Tunnel exists? */
5864 tid = ntohl (peer_msg->tunnel_id);
5865 t = tunnel_get_by_local_id (c, tid);
5869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5874 GNUNET_array_append(t->blacklisted, t->nblacklisted,
5875 GNUNET_PEER_intern(&peer_msg->peer));
5880 * Handler for unblacklist requests of peers in a tunnel
5882 * @param cls closure
5883 * @param client identification of the client
5884 * @param message the actual message (PeerControl)
5887 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
5888 const struct GNUNET_MessageHeader *message)
5890 struct GNUNET_MESH_PeerControl *peer_msg;
5891 struct MeshClient *c;
5892 struct MeshTunnel *t;
5893 MESH_TunnelNumber tid;
5897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
5898 /* Sanity check for client registration */
5899 if (NULL == (c = client_get (client)))
5902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5905 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5907 /* Sanity check for message size */
5908 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5911 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5915 /* Tunnel exists? */
5916 tid = ntohl (peer_msg->tunnel_id);
5917 t = tunnel_get_by_local_id (c, tid);
5921 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5926 /* if peer is not known, complain */
5927 pid = GNUNET_PEER_search (&peer_msg->peer);
5934 /* search and remove from list */
5935 for (i = 0; i < t->nblacklisted; i++)
5937 if (t->blacklisted[i] == pid)
5939 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
5940 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
5945 /* if peer hasn't been blacklisted, complain */
5951 * Handler for connection requests to new peers by type
5953 * @param cls closure
5954 * @param client identification of the client
5955 * @param message the actual message (ConnectPeerByType)
5958 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
5959 const struct GNUNET_MessageHeader *message)
5961 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
5962 struct MeshClient *c;
5963 struct MeshTunnel *t;
5964 struct GNUNET_HashCode hash;
5965 MESH_TunnelNumber tid;
5967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
5968 /* Sanity check for client registration */
5969 if (NULL == (c = client_get (client)))
5972 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5976 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
5977 /* Sanity check for message size */
5978 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
5979 ntohs (connect_msg->header.size))
5982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5986 /* Tunnel exists? */
5987 tid = ntohl (connect_msg->tunnel_id);
5988 t = tunnel_get_by_local_id (c, tid);
5992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5996 /* Does client own tunnel? */
5997 if (t->owner->handle != client)
6000 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6004 /* Do WE have the service? */
6005 t->type = ntohl (connect_msg->type);
6006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6007 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6008 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6011 /* Yes! Fast forward, add ourselves to the tunnel and send the
6012 * good news to the client, and alert the destination client of
6013 * an incoming tunnel.
6015 * FIXME send a path create to self, avoid code duplication
6017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6018 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6019 peer_info_get (&my_full_id),
6020 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6023 send_client_peer_connected (t, myid);
6024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6025 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6027 t->local_tid_dest = next_local_tid++;
6028 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6029 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6030 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6034 /* Ok, lets find a peer offering the service */
6035 if (NULL != t->dht_get_type)
6037 GNUNET_DHT_get_stop (t->dht_get_type);
6039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6040 GNUNET_h2s (&hash));
6042 GNUNET_DHT_get_start (dht_handle,
6043 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6045 dht_replication_level,
6046 GNUNET_DHT_RO_RECORD_ROUTE |
6047 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6049 &dht_get_type_handler, t);
6051 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6057 * Handler for connection requests to new peers by a string service description.
6059 * @param cls closure
6060 * @param client identification of the client
6061 * @param message the actual message, which includes messages the client wants
6064 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6065 const struct GNUNET_MessageHeader *message)
6067 struct GNUNET_MESH_ConnectPeerByString *msg;
6068 struct MeshRegexSearchContext *ctx;
6069 struct MeshRegexSearchInfo *info;
6070 struct GNUNET_DHT_GetHandle *get_h;
6071 struct GNUNET_HashCode key;
6072 struct MeshTunnel *t;
6073 struct MeshClient *c;
6074 MESH_TunnelNumber tid;
6078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6079 "Connect by string started\n");
6080 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6081 size = htons (message->size);
6083 /* Sanity check for client registration */
6084 if (NULL == (c = client_get (client)))
6087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6091 /* Message size sanity check */
6092 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6095 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6099 /* Tunnel exists? */
6100 tid = ntohl (msg->tunnel_id);
6101 t = tunnel_get_by_local_id (c, tid);
6105 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6109 /* Does client own tunnel? */
6110 if (t->owner->handle != client)
6113 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6118 " on tunnel %s [%u]\n",
6119 GNUNET_i2s(&my_full_id),
6122 /* Only one connect_by_string allowed at the same time! */
6123 /* FIXME: allow more, return handle at api level to cancel, document */
6124 if (NULL != t->regex_ctx)
6127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6131 /* Find string itself */
6132 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6133 string = (const char *) &msg[1];
6135 /* Initialize context */
6136 size = GNUNET_REGEX_get_first_key(string, len, &key);
6137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6138 " consumed %u bits out of %u\n", size, len);
6139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6140 " looking for %s\n", GNUNET_h2s (&key));
6142 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6144 info->description = GNUNET_malloc (len + 1);
6145 memcpy (info->description, string, len);
6146 info->description[len] = '\0';
6147 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6148 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6151 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6152 ctx->position = size;
6156 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6158 /* Start search in DHT */
6159 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6160 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6161 &key, /* key to search */
6162 dht_replication_level, /* replication level */
6163 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6164 NULL, /* xquery */ // FIXME BLOOMFILTER
6165 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6166 &dht_get_string_handler, ctx);
6168 GNUNET_break (GNUNET_OK ==
6169 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6172 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6174 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6180 * Handler for client traffic directed to one peer
6182 * @param cls closure
6183 * @param client identification of the client
6184 * @param message the actual message
6187 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6188 const struct GNUNET_MessageHeader *message)
6190 struct MeshClient *c;
6191 struct MeshTunnel *t;
6192 struct MeshPeerInfo *pi;
6193 struct GNUNET_MESH_Unicast *data_msg;
6194 MESH_TunnelNumber tid;
6197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6198 "Got a unicast request from a client!\n");
6200 /* Sanity check for client registration */
6201 if (NULL == (c = client_get (client)))
6204 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6207 data_msg = (struct GNUNET_MESH_Unicast *) message;
6208 /* Sanity check for message size */
6209 size = ntohs (message->size);
6210 if (sizeof (struct GNUNET_MESH_Unicast) +
6211 sizeof (struct GNUNET_MessageHeader) > size)
6214 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6218 /* Tunnel exists? */
6219 tid = ntohl (data_msg->tid);
6220 t = tunnel_get_by_local_id (c, tid);
6224 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6228 /* Is it a local tunnel? Then, does client own the tunnel? */
6229 if (t->owner->handle != client)
6232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6236 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6237 &data_msg->destination.hashPubKey);
6238 /* Is the selected peer in the tunnel? */
6242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6246 /* Ok, everything is correct, send the message
6247 * (pretend we got it from a mesh peer)
6250 char buf[ntohs (message->size)] GNUNET_ALIGN;
6251 struct GNUNET_MESH_Unicast *copy;
6253 /* Work around const limitation */
6254 copy = (struct GNUNET_MESH_Unicast *) buf;
6255 memcpy (buf, data_msg, size);
6256 copy->oid = my_full_id;
6257 copy->tid = htonl (t->id.tid);
6258 copy->ttl = htonl (default_ttl);
6259 copy->pid = htonl (t->pid + 1);
6260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6261 " calling generic handler...\n");
6262 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6263 send_client_tunnel_ack (t->owner, t);
6265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6266 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6272 * Handler for client traffic directed to the origin
6274 * @param cls closure
6275 * @param client identification of the client
6276 * @param message the actual message
6279 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6280 const struct GNUNET_MessageHeader *message)
6282 struct GNUNET_MESH_ToOrigin *data_msg;
6283 struct GNUNET_PeerIdentity id;
6284 struct MeshClient *c;
6285 struct MeshTunnel *t;
6286 MESH_TunnelNumber tid;
6289 /* Sanity check for client registration */
6290 if (NULL == (c = client_get (client)))
6293 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6296 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6297 /* Sanity check for message size */
6298 size = ntohs (message->size);
6299 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6300 sizeof (struct GNUNET_MessageHeader) > size)
6303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6307 /* Tunnel exists? */
6308 tid = ntohl (data_msg->tid);
6309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6310 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6311 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6317 t = tunnel_get_by_local_id (c, tid);
6321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6325 /* It should be sent by someone who has this as incoming tunnel. */
6326 if (-1 == client_knows_tunnel (c, t))
6329 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6332 GNUNET_PEER_resolve (t->id.oid, &id);
6334 /* Ok, everything is correct, send the message
6335 * (pretend we got it from a mesh peer)
6338 char buf[ntohs (message->size)] GNUNET_ALIGN;
6339 struct GNUNET_MESH_ToOrigin *copy;
6341 /* Work around const limitation */
6342 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6343 memcpy (buf, data_msg, size);
6345 copy->tid = htonl (t->id.tid);
6346 copy->sender = my_full_id;
6347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6348 " calling generic handler...\n");
6349 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6357 * Handler for client traffic directed to all peers in a tunnel
6359 * @param cls closure
6360 * @param client identification of the client
6361 * @param message the actual message
6364 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6365 const struct GNUNET_MessageHeader *message)
6367 struct MeshClient *c;
6368 struct MeshTunnel *t;
6369 struct GNUNET_MESH_Multicast *data_msg;
6370 MESH_TunnelNumber tid;
6372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6373 "Got a multicast request from a client!\n");
6375 /* Sanity check for client registration */
6376 if (NULL == (c = client_get (client)))
6379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6382 data_msg = (struct GNUNET_MESH_Multicast *) message;
6383 /* Sanity check for message size */
6384 if (sizeof (struct GNUNET_MESH_Multicast) +
6385 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6392 /* Tunnel exists? */
6393 tid = ntohl (data_msg->tid);
6394 t = tunnel_get_by_local_id (c, tid);
6398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6402 /* Does client own tunnel? */
6403 if (t->owner->handle != client)
6406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6411 char buf[ntohs (message->size)] GNUNET_ALIGN;
6412 struct GNUNET_MESH_Multicast *copy;
6414 copy = (struct GNUNET_MESH_Multicast *) buf;
6415 memcpy (buf, message, ntohs (message->size));
6416 copy->oid = my_full_id;
6417 copy->tid = htonl (t->id.tid);
6418 copy->ttl = htonl (default_ttl);
6419 copy->pid = htonl (t->pid + 1);
6420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6421 " calling generic handler...\n");
6422 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
6425 /* receive done gets called when last copy is sent to a neighbor */
6431 * Functions to handle messages from clients
6433 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6434 {&handle_local_new_client, NULL,
6435 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6436 {&handle_local_announce_regex, NULL,
6437 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
6438 {&handle_local_tunnel_create, NULL,
6439 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6440 sizeof (struct GNUNET_MESH_TunnelMessage)},
6441 {&handle_local_tunnel_destroy, NULL,
6442 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6443 sizeof (struct GNUNET_MESH_TunnelMessage)},
6444 {&handle_local_tunnel_speed, NULL,
6445 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
6446 sizeof (struct GNUNET_MESH_TunnelMessage)},
6447 {&handle_local_tunnel_speed, NULL,
6448 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
6449 sizeof (struct GNUNET_MESH_TunnelMessage)},
6450 {&handle_local_tunnel_buffer, NULL,
6451 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6452 sizeof (struct GNUNET_MESH_TunnelMessage)},
6453 {&handle_local_tunnel_buffer, NULL,
6454 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6455 sizeof (struct GNUNET_MESH_TunnelMessage)},
6456 {&handle_local_connect_add, NULL,
6457 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
6458 sizeof (struct GNUNET_MESH_PeerControl)},
6459 {&handle_local_connect_del, NULL,
6460 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
6461 sizeof (struct GNUNET_MESH_PeerControl)},
6462 {&handle_local_blacklist, NULL,
6463 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
6464 sizeof (struct GNUNET_MESH_PeerControl)},
6465 {&handle_local_unblacklist, NULL,
6466 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
6467 sizeof (struct GNUNET_MESH_PeerControl)},
6468 {&handle_local_connect_by_type, NULL,
6469 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
6470 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
6471 {&handle_local_connect_by_string, NULL,
6472 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
6473 {&handle_local_unicast, NULL,
6474 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6475 {&handle_local_to_origin, NULL,
6476 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6477 {&handle_local_multicast, NULL,
6478 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6484 * To be called on core init/fail.
6486 * @param cls service closure
6487 * @param server handle to the server for this service
6488 * @param identity the public identity of this peer
6491 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6492 const struct GNUNET_PeerIdentity *identity)
6494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6495 core_handle = server;
6496 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6500 GNUNET_SCHEDULER_shutdown ();
6507 * Method called whenever a given peer connects.
6509 * @param cls closure
6510 * @param peer peer identity this notification is about
6511 * @param atsi performance data for the connection
6512 * @param atsi_count number of records in 'atsi'
6515 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
6516 const struct GNUNET_ATS_Information *atsi,
6517 unsigned int atsi_count)
6519 struct MeshPeerInfo *peer_info;
6520 struct MeshPeerPath *path;
6522 DEBUG_CONN ("Peer connected\n");
6523 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6524 peer_info = peer_info_get (peer);
6525 if (myid == peer_info->id)
6527 DEBUG_CONN (" (self)\n");
6532 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6534 path = path_new (2);
6535 path->peers[0] = myid;
6536 path->peers[1] = peer_info->id;
6537 GNUNET_PEER_change_rc (myid, 1);
6538 GNUNET_PEER_change_rc (peer_info->id, 1);
6539 peer_info_add_path (peer_info, path, GNUNET_YES);
6540 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6546 * Method called whenever a peer disconnects.
6548 * @param cls closure
6549 * @param peer peer identity this notification is about
6552 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6554 struct MeshPeerInfo *pi;
6555 struct MeshPeerQueue *q;
6556 struct MeshPeerQueue *n;
6558 DEBUG_CONN ("Peer disconnected\n");
6559 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6571 /* try to reroute this traffic instead */
6572 queue_destroy(q, GNUNET_YES);
6576 peer_info_remove_path (pi, pi->id, myid);
6579 DEBUG_CONN (" (self)\n");
6581 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6586 /******************************************************************************/
6587 /************************ MAIN FUNCTIONS ****************************/
6588 /******************************************************************************/
6591 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6593 * @param cls closure
6594 * @param key current key code
6595 * @param value value in the hash map
6596 * @return GNUNET_YES if we should continue to iterate,
6600 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6602 struct MeshTunnel *t = value;
6609 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
6611 * @param cls closure
6612 * @param key current key code
6613 * @param value value in the hash map
6614 * @return GNUNET_YES if we should continue to iterate,
6618 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
6620 struct MeshPeerInfo *p = value;
6621 struct MeshPeerQueue *q;
6622 struct MeshPeerQueue *n;
6630 queue_destroy(q, GNUNET_YES);
6634 peer_info_destroy (p);
6639 * Task run during shutdown.
6645 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6649 if (core_handle != NULL)
6651 GNUNET_CORE_disconnect (core_handle);
6654 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6655 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6656 if (dht_handle != NULL)
6658 GNUNET_DHT_disconnect (dht_handle);
6663 GNUNET_SERVER_notification_context_destroy (nc);
6666 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6668 GNUNET_SCHEDULER_cancel (announce_id_task);
6669 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6675 * Process mesh requests.
6677 * @param cls closure
6678 * @param server the initialized server
6679 * @param c configuration to use
6682 run (void *cls, struct GNUNET_SERVER_Handle *server,
6683 const struct GNUNET_CONFIGURATION_Handle *c)
6685 struct MeshPeerInfo *peer;
6686 struct MeshPeerPath *p;
6689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6690 server_handle = server;
6691 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6692 NULL, /* Closure passed to MESH functions */
6693 &core_init, /* Call core_init once connected */
6694 &core_connect, /* Handle connects */
6695 &core_disconnect, /* remove peers on disconnects */
6696 NULL, /* Don't notify about all incoming messages */
6697 GNUNET_NO, /* For header only in notification */
6698 NULL, /* Don't notify about all outbound messages */
6699 GNUNET_NO, /* For header-only out notification */
6700 core_handlers); /* Register these handlers */
6702 if (core_handle == NULL)
6705 GNUNET_SCHEDULER_shutdown ();
6710 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
6713 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6715 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6717 GNUNET_SCHEDULER_shutdown ();
6722 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6723 &refresh_path_time))
6725 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6727 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6728 "refresh path time");
6729 GNUNET_SCHEDULER_shutdown ();
6734 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6735 &app_announce_time))
6737 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6739 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6740 "app announce time");
6741 GNUNET_SCHEDULER_shutdown ();
6746 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6751 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6752 "id announce time");
6753 GNUNET_SCHEDULER_shutdown ();
6758 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
6759 &unacknowledged_wait_time))
6761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6763 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6764 "unacknowledged wait time");
6765 GNUNET_SCHEDULER_shutdown ();
6770 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6773 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6775 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6777 GNUNET_SCHEDULER_shutdown ();
6782 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6787 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6789 GNUNET_SCHEDULER_shutdown ();
6794 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6799 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6801 GNUNET_SCHEDULER_shutdown ();
6806 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6809 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6811 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
6817 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6818 &dht_replication_level))
6820 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6822 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
6823 "dht replication level", 10);
6824 dht_replication_level = 10;
6828 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6829 GNUNET_free (keyfile);
6830 if (my_private_key == NULL)
6832 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6833 _("Mesh service could not access hostkey. Exiting.\n"));
6834 GNUNET_SCHEDULER_shutdown ();
6837 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6838 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6839 &my_full_id.hashPubKey);
6840 myid = GNUNET_PEER_intern (&my_full_id);
6842 // transport_handle = GNUNET_TRANSPORT_connect(c,
6849 dht_handle = GNUNET_DHT_connect (c, 64);
6850 if (dht_handle == NULL)
6855 stats = GNUNET_STATISTICS_create ("mesh", c);
6859 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6861 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6862 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6863 peers = GNUNET_CONTAINER_multihashmap_create (32);
6864 applications = GNUNET_CONTAINER_multihashmap_create (32);
6865 types = GNUNET_CONTAINER_multihashmap_create (32);
6867 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6868 nc = GNUNET_SERVER_notification_context_create (server_handle,
6870 GNUNET_SERVER_disconnect_notify (server_handle,
6871 &handle_local_client_disconnect, NULL);
6875 clients_tail = NULL;
6878 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6879 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6881 /* Create a peer_info for the local peer */
6882 peer = peer_info_get (&my_full_id);
6885 GNUNET_PEER_change_rc (myid, 1);
6886 peer_info_add_path (peer, p, GNUNET_YES);
6888 /* Scheduled the task to clean up when shutdown is called */
6889 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
6896 * The main function for the mesh service.
6898 * @param argc number of arguments from the command line
6899 * @param argv command line arguments
6900 * @return 0 ok, 1 on error
6903 main (int argc, char *const *argv)
6907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6910 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");