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 * Iterator over edges in a regex block retrieved from the DHT.
981 * @param cls Closure.
982 * @param token Token that follows to next state.
983 * @param len Lenght of token.
984 * @param key Hash of next state.
986 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
989 regex_edge_iterator (void *cls,
992 const struct GNUNET_HashCode *key);
996 * Find a path to a peer that offers a regex servcie compatible
997 * with a given string.
999 * @param key The key of the accepting state.
1000 * @param ctx Context containing info about the string, tunnel, etc.
1003 regex_find_path (const struct GNUNET_HashCode *key,
1004 struct MeshRegexSearchContext *ctx);
1008 * Queue and pass message to core when possible.
1010 * @param cls Closure (type dependant).
1011 * @param type Type of the message, 0 for a raw message.
1012 * @param size Size of the message.
1013 * @param dst Neighbor to send message to.
1014 * @param t Tunnel this message belongs to.
1017 queue_add (void *cls, uint16_t type, size_t size,
1018 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1021 * Free a transmission that was already queued with all resources
1022 * associated to the request.
1024 * @param queue Queue handler to cancel.
1025 * @param clear_cls Is it necessary to free associated cls?
1028 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1030 /******************************************************************************/
1031 /************************ ITERATORS ****************************/
1032 /******************************************************************************/
1035 * Iterator over found existing mesh regex blocks that match an ongoing search.
1037 * @param cls closure
1038 * @param key current key code
1039 * @param value value in the hash map
1040 * @return GNUNET_YES if we should continue to iterate,
1044 regex_result_iterator (void *cls,
1045 const struct GNUNET_HashCode * key,
1048 struct MeshRegexBlock *block = value;
1049 struct MeshRegexSearchContext *ctx = cls;
1051 if (GNUNET_YES == ntohl(block->accepting) &&
1052 ctx->position == strlen (ctx->info->description))
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1055 regex_find_path (key, ctx);
1056 return GNUNET_YES; // We found an accept state!
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1061 ctx->position, strlen(ctx->info->description),
1062 ntohl(block->accepting));
1065 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1066 ®ex_edge_iterator, ctx);
1073 * Iterator over edges in a regex block retrieved from the DHT.
1075 * @param cls Closure (context of the search).
1076 * @param token Token that follows to next state.
1077 * @param len Lenght of token.
1078 * @param key Hash of next state.
1080 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1083 regex_edge_iterator (void *cls,
1086 const struct GNUNET_HashCode *key)
1088 struct MeshRegexSearchContext *ctx = cls;
1089 struct MeshRegexSearchContext *new_ctx;
1090 struct MeshRegexSearchInfo *info = ctx->info;
1091 struct GNUNET_DHT_GetHandle *get_h;
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1098 current = &info->description[ctx->position];
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1100 current_len = strlen (info->description) - ctx->position;
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1105 if (len > current_len)
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1108 return GNUNET_YES; // Token too long, wont match
1110 if (0 != strncmp (current, token, len))
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1113 return GNUNET_YES; // Token doesn't match
1115 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1116 new_ctx->info = info;
1117 new_ctx->position = ctx->position + len;
1118 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1120 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1123 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1124 ®ex_result_iterator,
1126 return GNUNET_YES; // We are already looking for it
1128 /* Start search in DHT */
1130 GNUNET_DHT_get_start (dht_handle, /* handle */
1131 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1132 key, /* key to search */
1133 dht_replication_level, /* replication level */
1134 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1135 NULL, /* xquery */ // FIXME BLOOMFILTER
1136 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1137 &dht_get_string_handler, new_ctx);
1139 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1140 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1150 * Iterator over hash map entries to cancel DHT GET requests after a
1151 * successful connect_by_string.
1153 * @param cls Closure (unused).
1154 * @param key Current key code (unused).
1155 * @param value Value in the hash map (get handle).
1156 * @return GNUNET_YES if we should continue to iterate,
1160 regex_cancel_dht_get (void *cls,
1161 const struct GNUNET_HashCode * key,
1164 struct GNUNET_DHT_GetHandle *h = value;
1166 GNUNET_DHT_get_stop (h);
1172 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1173 * search for connect_by_string.
1175 * @param cls Closure (unused).
1176 * @param key Current key code (unused).
1177 * @param value MeshRegexBlock in the hash map.
1178 * @return GNUNET_YES if we should continue to iterate,
1182 regex_free_result (void *cls,
1183 const struct GNUNET_HashCode * key,
1187 GNUNET_free (value);
1193 * Regex callback iterator to store own service description in the DHT.
1195 * @param cls closure.
1196 * @param key hash for current state.
1197 * @param proof proof for current state.
1198 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1199 * @param num_edges number of edges leaving current state.
1200 * @param edges edges leaving current state.
1203 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1204 int accepting, unsigned int num_edges,
1205 const struct GNUNET_REGEX_Edge *edges)
1207 struct MeshRegexBlock *block;
1208 struct MeshRegexEdge *block_edge;
1209 enum GNUNET_DHT_RouteOption opt;
1213 unsigned int offset;
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1217 " regex dht put for state %s\n",
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1227 if (GNUNET_YES == accepting)
1229 struct MeshRegexAccept block;
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 " state %s is accepting, putting own id\n",
1234 size = sizeof (block);
1236 block.id = my_full_id;
1238 GNUNET_DHT_put(dht_handle, key,
1239 dht_replication_level,
1240 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1241 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1244 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1249 len = strlen(proof);
1250 size = sizeof (struct MeshRegexBlock) + len;
1251 block = GNUNET_malloc (size);
1254 block->n_proof = htonl (len);
1255 block->n_edges = htonl (num_edges);
1256 block->accepting = htonl (accepting);
1258 /* Store the proof at the end of the block. */
1259 aux = (char *) &block[1];
1260 memcpy (aux, proof, len);
1263 /* Store each edge in a variable length MeshEdge struct at the
1264 * very end of the MeshRegexBlock structure.
1266 for (i = 0; i < num_edges; i++)
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 " edge %s towards %s\n",
1271 GNUNET_h2s(&edges[i].destination));
1273 /* aux points at the end of the last block */
1274 len = strlen (edges[i].label);
1275 size += sizeof (struct MeshRegexEdge) + len;
1276 // Calculate offset FIXME is this ok? use size instead?
1277 offset = aux - (char *) block;
1278 block = GNUNET_realloc (block, size);
1279 aux = &((char *) block)[offset];
1280 block_edge = (struct MeshRegexEdge *) aux;
1281 block_edge->key = edges[i].destination;
1282 block_edge->n_token = htonl (len);
1283 aux = (char *) &block_edge[1];
1284 memcpy (aux, edges[i].label, len);
1288 GNUNET_DHT_put(dht_handle, key,
1289 dht_replication_level,
1291 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1293 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1297 GNUNET_free (block);
1302 * Store the regular expression describing a local service into the DHT.
1304 * @param regex The regular expresion.
1307 regex_put (const char *regex)
1309 struct GNUNET_REGEX_Automaton *dfa;
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1312 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1313 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1314 GNUNET_REGEX_automaton_destroy (dfa);
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1320 * Find a path to a peer that offers a regex servcie compatible
1321 * with a given string.
1323 * @param key The key of the accepting state.
1324 * @param ctx Context containing info about the string, tunnel, etc.
1327 regex_find_path (const struct GNUNET_HashCode *key,
1328 struct MeshRegexSearchContext *ctx)
1330 struct GNUNET_DHT_GetHandle *get_h;
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1333 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1334 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1335 key, /* key to search */
1336 dht_replication_level, /* replication level */
1337 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1338 GNUNET_DHT_RO_RECORD_ROUTE,
1339 NULL, /* xquery */ // FIXME BLOOMFILTER
1340 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1341 &dht_get_string_accept_handler, ctx);
1342 GNUNET_break (GNUNET_OK ==
1343 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1346 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1351 * Function called if the connect attempt to a peer found via
1352 * connect_by_string times out. Try to connect to another peer, if any.
1353 * Otherwise try to reconnect to the same peer.
1355 * @param cls Closure (info about regex search).
1356 * @param tc TaskContext.
1359 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1361 struct MeshRegexSearchInfo *info = cls;
1362 struct MeshPeerInfo *peer_info;
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1367 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1368 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1376 if (0 < info->n_peers)
1378 // Select next peer, put current in that spot.
1379 id = info->peers[info->i_peer];
1380 info->peers[info->i_peer] = info->peer;
1381 info->i_peer = (info->i_peer + 1) % info->n_peers;
1385 // Try to connect to same peer again.
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1390 peer_info = peer_info_get_short(id);
1391 tunnel_add_peer (info->t, peer_info);
1393 tunnel_delete_peer (info->t, old);
1394 peer_info_connect (peer_info, info->t);
1395 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1396 ®ex_connect_timeout,
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1403 * Cancel an ongoing regex search in the DHT and free all resources.
1405 * @param ctx The search context.
1408 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1410 struct MeshRegexSearchInfo *info = ctx->info;
1413 GNUNET_free (info->description);
1414 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1415 ®ex_cancel_dht_get, NULL);
1416 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1417 ®ex_free_result, NULL);
1418 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1419 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1420 info->t->regex_ctx = NULL;
1421 for (i = 0; i < info->n_contexts; i++)
1423 GNUNET_free (info->contexts[i]);
1425 if (0 < info->n_contexts)
1426 GNUNET_free (info->contexts);
1427 if (0 < info->n_peers)
1428 GNUNET_free (info->peers);
1429 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1431 GNUNET_SCHEDULER_cancel(info->timeout);
1437 /******************************************************************************/
1438 /************************ PERIODIC FUNCTIONS ****************************/
1439 /******************************************************************************/
1442 * Announce iterator over for each application provided by the peer
1444 * @param cls closure
1445 * @param key current key code
1446 * @param value value in the hash map
1447 * @return GNUNET_YES if we should continue to
1452 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1454 struct PBlock block;
1455 struct MeshClient *c;
1457 block.id = my_full_id;
1458 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1459 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1460 if (0 == block.type)
1465 block.type = htonl (block.type);
1467 GNUNET_break (NULL !=
1468 GNUNET_DHT_put (dht_handle, key,
1469 dht_replication_level,
1470 GNUNET_DHT_RO_RECORD_ROUTE |
1471 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1472 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1474 (const char *) &block,
1475 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1477 app_announce_time, NULL, NULL));
1483 * Periodically announce what applications are provided by local clients
1486 * @param cls closure
1487 * @param tc task context
1490 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1492 struct MeshClient *c = cls;
1495 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1497 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1501 DEBUG_DHT ("Starting PUT for regex\n");
1503 for (i = 0; i < c->n_regex; i++)
1505 regex_put (c->regexes[i]);
1507 c->regex_announce_task =
1508 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1509 DEBUG_DHT ("Finished PUT for regex\n");
1516 * Periodically announce what applications are provided by local clients
1519 * @param cls closure
1520 * @param tc task context
1523 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1525 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1527 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1531 DEBUG_DHT ("Starting PUT for apps\n");
1533 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1535 announce_applications_task =
1536 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1538 DEBUG_DHT ("Finished PUT for apps\n");
1545 * Periodically announce self id in the DHT
1547 * @param cls closure
1548 * @param tc task context
1551 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1553 struct PBlock block;
1555 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1557 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1561 * - Set data expiration in function of X
1562 * - Adapt X to churn
1564 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1566 block.id = my_full_id;
1567 block.type = htonl (0);
1568 GNUNET_DHT_put (dht_handle, /* DHT handle */
1569 &my_full_id.hashPubKey, /* Key to use */
1570 dht_replication_level, /* Replication level */
1571 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1572 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1573 sizeof (block), /* Size of the data */
1574 (const char *) &block, /* Data itself */
1575 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1576 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1577 NULL, /* Continuation */
1578 NULL); /* Continuation closure */
1580 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1584 /******************************************************************************/
1585 /****************** GENERAL HELPER FUNCTIONS ************************/
1586 /******************************************************************************/
1590 * Decrements the reference counter and frees all resources if needed
1592 * @param mesh_data Data Descriptor used in a multicast message.
1593 * Freed no longer needed (last message).
1596 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1598 /* Make sure it's a multicast packet */
1599 GNUNET_assert (NULL != mesh_data->reference_counter);
1601 if (0 == --(*(mesh_data->reference_counter)))
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1604 if (NULL != mesh_data->task)
1606 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1608 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1610 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1612 GNUNET_free (mesh_data->task);
1614 GNUNET_free (mesh_data->reference_counter);
1615 GNUNET_free (mesh_data->data);
1616 GNUNET_free (mesh_data);
1622 * Check if client has registered with the service and has not disconnected
1624 * @param client the client to check
1626 * @return non-NULL if client exists in the global DLL
1628 static struct MeshClient *
1629 client_get (struct GNUNET_SERVER_Client *client)
1631 struct MeshClient *c;
1636 if (c->handle == client)
1645 * Checks if a given client has subscribed to certain message type
1647 * @param message_type Type of message to check
1648 * @param c Client to check
1650 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1653 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1655 struct GNUNET_HashCode hc;
1657 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1658 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1663 * Allow a client to send more data after transmitting a multicast message
1664 * which some neighbor has not yet accepted altough a reasonable time has
1667 * @param cls Closure (DataDescriptor containing the task identifier)
1668 * @param tc Task Context
1670 * FIXME reference counter cshould be just int
1673 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1675 struct MeshData *mdata = cls;
1677 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1679 GNUNET_assert (NULL != mdata->reference_counter);
1680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1681 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1682 *(mdata->reference_counter));
1683 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1684 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1689 * Check whether client wants traffic from a tunnel.
1691 * @param c Client to check.
1692 * @param t Tunnel to be found.
1694 * @return GNUNET_YES if client knows tunnel.
1696 * TODO look in client hashmap
1699 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1703 for (i = 0; i < t->nclients; i++)
1704 if (t->clients[i] == c)
1711 * Check whether client has been informed about a tunnel.
1713 * @param c Client to check.
1714 * @param t Tunnel to be found.
1716 * @return GNUNET_YES if client knows tunnel.
1718 * TODO look in client hashmap
1721 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1725 for (i = 0; i < t->nignore; i++)
1726 if (t->ignore[i] == c)
1728 return client_wants_tunnel(c, t);
1733 * Marks a client as uninterested in traffic from the tunnel, updating both
1734 * client and tunnel to reflect this.
1736 * @param c Client that doesn't want traffic anymore.
1737 * @param t Tunnel which should be ignored.
1739 * FIXME when to delete an incoming tunnel?
1742 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1744 struct GNUNET_HashCode hash;
1746 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1747 GNUNET_break (GNUNET_YES ==
1748 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1750 GNUNET_break (GNUNET_YES ==
1751 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1752 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1753 tunnel_delete_active_client (t, c);
1754 GNUNET_array_append (t->ignore, t->nignore, c);
1759 * Deletes a tunnel from a client (either owner or destination). To be used on
1760 * tunnel destroy, otherwise, use client_ignore_tunnel.
1762 * @param c Client whose tunnel to delete.
1763 * @param t Tunnel which should be deleted.
1766 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1768 struct GNUNET_HashCode hash;
1772 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1773 GNUNET_assert (GNUNET_YES ==
1774 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1780 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1782 GNUNET_assert (GNUNET_YES ==
1783 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1787 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1796 * Send the message to all clients that have subscribed to its type
1798 * @param msg Pointer to the message itself
1799 * @param payload Pointer to the payload of the message.
1800 * @return number of clients this message was sent to
1803 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1804 const struct GNUNET_MessageHeader *payload)
1806 struct GNUNET_PeerIdentity *oid;
1807 struct MeshClient *c;
1808 struct MeshTunnel *t;
1809 MESH_TunnelNumber *tid;
1812 char cbuf[htons (msg->size)];
1814 type = ntohs (payload->type);
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1818 memcpy (cbuf, msg, sizeof (cbuf));
1819 switch (htons (msg->type))
1821 struct GNUNET_MESH_Unicast *uc;
1822 struct GNUNET_MESH_Multicast *mc;
1823 struct GNUNET_MESH_ToOrigin *to;
1825 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1826 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1830 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1831 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1835 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1836 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1844 t = tunnel_get (oid, ntohl (*tid));
1851 for (count = 0, c = clients; c != NULL; c = c->next)
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1854 if (client_is_subscribed (type, c))
1856 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1860 *tid = htonl (t->local_tid);
1864 if (GNUNET_NO == client_knows_tunnel (c, t))
1866 /* This client doesn't know the tunnel */
1867 struct GNUNET_MESH_TunnelNotification tmsg;
1868 struct GNUNET_HashCode hash;
1870 tmsg.header.size = htons (sizeof (tmsg));
1871 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1872 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1873 tmsg.tunnel_id = htonl (t->local_tid_dest);
1874 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1875 &tmsg.header, GNUNET_NO);
1876 GNUNET_array_append (t->clients, t->nclients, c);
1877 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1879 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1880 c->incoming_tunnels, &hash, t,
1881 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1883 *tid = htonl (t->local_tid_dest);
1886 /* Check if the client wants to get traffic from the tunnel */
1887 if (GNUNET_NO == client_wants_tunnel(c, t))
1890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1891 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1892 (struct GNUNET_MessageHeader
1893 *) cbuf, GNUNET_YES);
1901 * Notify the client that owns the tunnel that a peer has connected to it
1902 * (the requested path to it has been confirmed).
1904 * @param t Tunnel whose owner to notify
1905 * @param id Short id of the peer that has connected
1908 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1910 struct GNUNET_MESH_PeerControl pc;
1912 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1913 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1914 pc.tunnel_id = htonl (t->local_tid);
1915 GNUNET_PEER_resolve (id, &pc.peer);
1916 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1922 * Notify all clients (not depending on registration status) that the incoming
1923 * tunnel is no longer valid.
1925 * @param t Tunnel that was destroyed.
1928 send_clients_tunnel_destroy (struct MeshTunnel *t)
1930 struct GNUNET_MESH_TunnelMessage msg;
1932 msg.header.size = htons (sizeof (msg));
1933 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1934 msg.tunnel_id = htonl (t->local_tid_dest);
1935 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1940 * Notify clients of tunnel disconnections, if needed.
1941 * In case the origin disconnects, the destination clients get a tunnel destroy
1942 * notification. If the last destination disconnects (only one remaining client
1943 * in tunnel), the origin gets a (local ID) peer disconnected.
1944 * Note that the function must be called BEFORE removing the client from
1947 * @param t Tunnel that was destroyed.
1948 * @param c Client that disconnected.
1951 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1957 struct GNUNET_MESH_TunnelMessage msg;
1959 msg.header.size = htons (sizeof (msg));
1960 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1961 msg.tunnel_id = htonl (t->local_tid_dest);
1962 for (i = 0; i < t->nclients; i++)
1963 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1964 &msg.header, GNUNET_NO);
1966 // FIXME when to disconnect an incoming tunnel?
1967 else if (1 == t->nclients && NULL != t->owner)
1969 struct GNUNET_MESH_PeerControl msg;
1971 msg.header.size = htons (sizeof (msg));
1972 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1973 msg.tunnel_id = htonl (t->local_tid);
1974 msg.peer = my_full_id;
1975 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1976 &msg.header, GNUNET_NO);
1982 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1983 * and insert it in the appropiate structures if the peer is not known yet.
1985 * @param peer Full identity of the peer.
1987 * @return Existing or newly created peer info.
1989 static struct MeshPeerInfo *
1990 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1992 struct MeshPeerInfo *peer_info;
1994 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1995 if (NULL == peer_info)
1998 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1999 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2000 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2001 peer_info->id = GNUNET_PEER_intern (peer);
2009 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2010 * and insert it in the appropiate structures if the peer is not known yet.
2012 * @param peer Short identity of the peer.
2014 * @return Existing or newly created peer info.
2016 static struct MeshPeerInfo *
2017 peer_info_get_short (const GNUNET_PEER_Id peer)
2019 struct GNUNET_PeerIdentity id;
2021 GNUNET_PEER_resolve (peer, &id);
2022 return peer_info_get (&id);
2027 * Iterator to remove the tunnel from the list of tunnels a peer participates
2030 * @param cls Closure (tunnel info)
2031 * @param key GNUNET_PeerIdentity of the peer (unused)
2032 * @param value PeerInfo of the peer
2034 * @return always GNUNET_YES, to keep iterating
2037 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2039 struct MeshTunnel *t = cls;
2040 struct MeshPeerInfo *peer = value;
2043 for (i = 0; i < peer->ntunnels; i++)
2046 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2049 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2050 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2059 * Core callback to write a pre-constructed data packet to core buffer
2061 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2062 * @param size Number of bytes available in buf.
2063 * @param buf Where the to write the message.
2065 * @return number of bytes written to buf
2068 send_core_data_raw (void *cls, size_t size, void *buf)
2070 struct MeshTransmissionDescriptor *info = cls;
2071 struct GNUNET_MessageHeader *msg;
2074 GNUNET_assert (NULL != info);
2075 GNUNET_assert (NULL != info->mesh_data);
2076 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2077 total_size = ntohs (msg->size);
2079 if (total_size > size)
2084 memcpy (buf, msg, total_size);
2085 data_descriptor_decrement_rc (info->mesh_data);
2092 * Sends an already built non-multicast message to a peer,
2093 * properly registrating all used resources.
2095 * @param message Message to send. Function makes a copy of it.
2096 * @param peer Short ID of the neighbor whom to send the message.
2097 * @param t Tunnel on which this message is transmitted.
2100 send_message (const struct GNUNET_MessageHeader *message,
2101 const struct GNUNET_PeerIdentity *peer,
2102 struct MeshTunnel *t)
2104 struct MeshTransmissionDescriptor *info;
2105 struct MeshPeerInfo *neighbor;
2106 struct MeshPeerPath *p;
2109 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2111 size = ntohs (message->size);
2112 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2113 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2114 info->mesh_data->data = GNUNET_malloc (size);
2115 memcpy (info->mesh_data->data, message, size);
2116 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
2118 struct GNUNET_MESH_Unicast *m;
2120 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2121 m->ttl = htonl (ntohl (m->ttl) - 1);
2123 info->mesh_data->data_len = size;
2124 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2125 *info->mesh_data->reference_counter = 1;
2126 neighbor = peer_info_get (peer);
2127 for (p = neighbor->path_head; NULL != p; p = p->next)
2136 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2137 GNUNET_free (info->mesh_data->data);
2138 GNUNET_free (info->mesh_data);
2142 info->peer = neighbor;
2152 * Sends a CREATE PATH message for a path to a peer, properly registrating
2153 * all used resources.
2155 * @param peer PeerInfo of the final peer for whom this path is being created.
2156 * @param p Path itself.
2157 * @param t Tunnel for which the path is created.
2160 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2161 struct MeshTunnel *t)
2163 struct GNUNET_PeerIdentity id;
2164 struct MeshPathInfo *path_info;
2165 struct MeshPeerInfo *neighbor;
2171 p = tree_get_path_to_peer (t->tree, peer->id);
2178 for (i = 0; i < p->length; i++)
2180 if (p->peers[i] == myid)
2183 if (i >= p->length - 1)
2189 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2191 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2192 path_info->path = p;
2194 neighbor = peer_info_get (&id);
2195 path_info->peer = neighbor;
2196 queue_add (path_info,
2197 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2198 sizeof (struct GNUNET_MESH_ManipulatePath) +
2199 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2206 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2208 * @param t Tunnel whose path to destroy.
2209 * @param destination Short ID of the peer to whom the path to destroy.
2212 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2214 struct MeshPeerPath *p;
2217 p = tree_get_path_to_peer (t->tree, destination);
2223 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2224 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2226 struct GNUNET_MESH_ManipulatePath *msg;
2227 struct GNUNET_PeerIdentity *pi;
2231 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2232 msg->header.size = htons (size);
2233 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2234 msg->tid = htonl (t->id.tid);
2235 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2236 for (i = 0; i < p->length; i++)
2238 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2240 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2247 * Try to establish a new connection to this peer.
2248 * Use the best path for the given tunnel.
2249 * If the peer doesn't have any path to it yet, try to get one.
2250 * If the peer already has some path, send a CREATE PATH towards it.
2252 * @param peer PeerInfo of the peer.
2253 * @param t Tunnel for which to create the path, if possible.
2256 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2258 struct MeshPeerPath *p;
2259 struct MeshPathInfo *path_info;
2261 if (NULL != peer->path_head)
2263 p = tree_get_path_to_peer (t->tree, peer->id);
2270 // FIXME always send create path to self
2273 send_create_path (peer, p, t);
2277 struct GNUNET_HashCode hash;
2280 send_client_peer_connected (t, myid);
2281 t->local_tid_dest = next_local_tid++;
2282 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2285 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2286 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2293 else if (NULL == peer->dhtget)
2295 struct GNUNET_PeerIdentity id;
2297 GNUNET_PEER_resolve (peer->id, &id);
2298 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2299 path_info->peer = peer;
2301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2302 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2303 peer->dhtgetcls = path_info;
2304 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2305 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2306 &id.hashPubKey, /* key to search */
2307 dht_replication_level, /* replication level */
2308 GNUNET_DHT_RO_RECORD_ROUTE |
2309 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2310 NULL, /* xquery */ // FIXME BLOOMFILTER
2311 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2312 &dht_get_id_handler, path_info);
2314 /* Otherwise, there is no path but the DHT get is already started. */
2319 * Task to delay the connection of a peer
2321 * @param cls Closure (path info with tunnel and peer to connect).
2322 * Will be free'd on exection.
2323 * @param tc TaskContext
2326 peer_info_connect_task (void *cls,
2327 const struct GNUNET_SCHEDULER_TaskContext *tc)
2329 struct MeshPathInfo *path_info = cls;
2331 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2336 peer_info_connect (path_info->peer, path_info->t);
2342 * Destroy the peer_info and free any allocated resources linked to it
2344 * @param pi The peer_info to destroy.
2346 * @return GNUNET_OK on success
2349 peer_info_destroy (struct MeshPeerInfo *pi)
2351 struct GNUNET_PeerIdentity id;
2352 struct MeshPeerPath *p;
2353 struct MeshPeerPath *nextp;
2355 GNUNET_PEER_resolve (pi->id, &id);
2356 GNUNET_PEER_change_rc (pi->id, -1);
2359 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2362 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2363 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2365 if (NULL != pi->dhtget)
2367 GNUNET_DHT_get_stop (pi->dhtget);
2368 GNUNET_free (pi->dhtgetcls);
2374 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2384 * Remove all paths that rely on a direct connection between p1 and p2
2385 * from the peer itself and notify all tunnels about it.
2387 * @param peer PeerInfo of affected peer.
2388 * @param p1 GNUNET_PEER_Id of one peer.
2389 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2392 * TODO: optimize (see below)
2395 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2398 struct MeshPeerPath *p;
2399 struct MeshPeerPath *aux;
2400 struct MeshPeerInfo *peer_d;
2402 unsigned int destroyed;
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2409 p = peer->path_head;
2413 for (i = 0; i < (p->length - 1); i++)
2415 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2416 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2418 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2429 for (i = 0; i < peer->ntunnels; i++)
2431 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2435 * Problem: one or more peers have been deleted from the tunnel tree.
2436 * We don't know who they are to try to add them again.
2437 * We need to try to find a new path for each of the disconnected peers.
2438 * Some of them might already have a path to reach them that does not
2439 * involve p1 and p2. Adding all anew might render in a better tree than
2440 * the trivial immediate fix.
2442 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2443 * its children will be reachable trough him.
2445 peer_d = peer_info_get_short (d);
2448 for (p = peer_d->path_head; NULL != p; p = p->next)
2450 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2458 /* No callback, as peer will be already disconnected and a connection
2459 * scheduled by tunnel_notify_connection_broken.
2461 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2465 peer_info_connect (peer_d, peer->tunnels[i]);
2468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2473 * Add the path to the peer and update the path used to reach it in case this
2476 * @param peer_info Destination peer to add the path to.
2477 * @param path New path to add. Last peer must be the peer in arg 1.
2478 * Path will be either used of freed if already known.
2479 * @param trusted Do we trust that this path is real?
2482 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2485 struct MeshPeerPath *aux;
2489 if ((NULL == peer_info) || (NULL == path))
2492 path_destroy (path);
2495 if (path->peers[path->length - 1] != peer_info->id)
2498 path_destroy (path);
2501 if (path->length <= 2 && GNUNET_NO == trusted)
2503 /* Only allow CORE to tell us about direct paths */
2504 path_destroy (path);
2507 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2508 for (l = 1; l < path->length; l++)
2510 if (path->peers[l] == myid)
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2513 for (l2 = 0; l2 < path->length - l; l2++)
2515 path->peers[l2] = path->peers[l + l2];
2520 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2525 struct GNUNET_PeerIdentity id;
2527 GNUNET_PEER_resolve (peer_info->id, &id);
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2529 path->length, GNUNET_i2s (&id));
2532 l = path_get_length (path);
2539 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2540 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2542 l2 = path_get_length (aux);
2545 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2546 peer_info->path_tail, aux, path);
2551 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2553 path_destroy (path);
2558 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2565 * Add the path to the origin peer and update the path used to reach it in case
2566 * this is the shortest.
2567 * The path is given in peer_info -> destination, therefore we turn the path
2568 * upside down first.
2570 * @param peer_info Peer to add the path to, being the origin of the path.
2571 * @param path New path to add after being inversed.
2572 * @param trusted Do we trust that this path is real?
2575 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2576 struct MeshPeerPath *path, int trusted)
2579 peer_info_add_path (peer_info, path, trusted);
2584 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2585 * to obtain a local peer -> destination path and interning the peer ids.
2587 * @return Newly allocated and created path
2589 static struct MeshPeerPath *
2590 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2591 unsigned int get_path_length,
2592 const struct GNUNET_PeerIdentity *put_path,
2593 unsigned int put_path_length)
2595 struct MeshPeerPath *p;
2601 GNUNET_PEER_change_rc (myid, 1);
2602 i = get_path_length;
2603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2604 for (i--; i >= 0; i--)
2606 id = GNUNET_PEER_intern (&get_path[i]);
2607 if (p->length > 0 && id == p->peers[p->length - 1])
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2610 GNUNET_PEER_change_rc (id, -1);
2614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2615 GNUNET_i2s (&get_path[i]));
2617 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2618 p->peers[p->length - 1] = id;
2621 i = put_path_length;
2622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2623 for (i--; i >= 0; i--)
2625 id = GNUNET_PEER_intern (&put_path[i]);
2628 /* PUT path went through us, so discard the path up until now and start
2629 * from here to get a much shorter (and loop-free) path.
2634 if (p->length > 0 && id == p->peers[p->length - 1])
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2637 GNUNET_PEER_change_rc (id, -1);
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2642 GNUNET_i2s (&put_path[i]));
2644 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2645 p->peers[p->length - 1] = id;
2649 if (get_path_length > 0)
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2651 GNUNET_i2s (&get_path[0]));
2652 if (put_path_length > 0)
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2654 GNUNET_i2s (&put_path[0]));
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2657 for (i = 0; i < p->length; i++)
2659 struct GNUNET_PeerIdentity peer_id;
2661 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2663 GNUNET_i2s (&peer_id));
2671 * Adds a path to the peer_infos of all the peers in the path
2673 * @param p Path to process.
2674 * @param confirmed Whether we know if the path works or not.
2677 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2681 /* TODO: invert and add */
2682 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2683 for (i++; i < p->length; i++)
2685 struct MeshPeerInfo *aux;
2686 struct MeshPeerPath *copy;
2688 aux = peer_info_get_short (p->peers[i]);
2689 copy = path_duplicate (p);
2690 copy->length = i + 1;
2691 peer_info_add_path (aux, copy, GNUNET_NO);
2697 * Send keepalive packets for a peer
2699 * @param cls Closure (tunnel for which to send the keepalive).
2700 * @param tc Notification context.
2702 * TODO: implement explicit multicast keepalive?
2705 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2709 * Search for a tunnel among the incoming tunnels
2711 * @param tid the local id of the tunnel
2713 * @return tunnel handler, NULL if doesn't exist
2715 static struct MeshTunnel *
2716 tunnel_get_incoming (MESH_TunnelNumber tid)
2718 struct GNUNET_HashCode hash;
2720 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2721 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2722 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2727 * Search for a tunnel among the tunnels for a client
2729 * @param c the client whose tunnels to search in
2730 * @param tid the local id of the tunnel
2732 * @return tunnel handler, NULL if doesn't exist
2734 static struct MeshTunnel *
2735 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2737 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2739 return tunnel_get_incoming (tid);
2743 struct GNUNET_HashCode hash;
2745 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2746 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2752 * Search for a tunnel by global ID using PEER_ID
2754 * @param pi owner of the tunnel
2755 * @param tid global tunnel number
2757 * @return tunnel handler, NULL if doesn't exist
2759 static struct MeshTunnel *
2760 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2762 struct MESH_TunnelID id;
2763 struct GNUNET_HashCode hash;
2768 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2769 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2774 * Search for a tunnel by global ID using full PeerIdentities
2776 * @param oid owner of the tunnel
2777 * @param tid global tunnel number
2779 * @return tunnel handler, NULL if doesn't exist
2781 static struct MeshTunnel *
2782 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2784 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2789 * Delete an active client from the tunnel.
2795 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2799 for (i = 0; i < t->nclients; i++)
2801 if (t->clients[i] == c)
2803 t->clients[i] = t->clients[t->nclients - 1];
2804 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2812 * Delete an ignored client from the tunnel.
2818 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2822 for (i = 0; i < t->nignore; i++)
2824 if (t->ignore[i] == c)
2826 t->ignore[i] = t->ignore[t->nignore - 1];
2827 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2835 * Delete a client from the tunnel. It should be only done on
2836 * client disconnection, otherwise use client_ignore_tunnel.
2842 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2844 tunnel_delete_ignored_client (t, c);
2845 tunnel_delete_active_client (t, c);
2850 * Iterator to free MeshTunnelChildInfo of tunnel children.
2852 * @param cls Closure (tunnel info).
2853 * @param key Hash of GNUNET_PEER_Id (unused).
2854 * @param value MeshTunnelChildInfo of the child.
2856 * @return always GNUNET_YES, to keep iterating
2859 tunnel_destroy_child (void *cls,
2860 const struct GNUNET_HashCode * key,
2863 GNUNET_free (value);
2869 * Callback used to notify a client owner of a tunnel that a peer has
2870 * disconnected, most likely because of a path change.
2872 * @param cls Closure (tunnel this notification is about).
2873 * @param peer_id Short ID of disconnected peer.
2876 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2878 struct MeshTunnel *t = cls;
2879 struct MeshPeerInfo *peer;
2880 struct MeshPathInfo *path_info;
2882 if (NULL != t->owner && NULL != nc)
2884 struct GNUNET_MESH_PeerControl msg;
2886 msg.header.size = htons (sizeof (msg));
2887 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2888 msg.tunnel_id = htonl (t->local_tid);
2889 GNUNET_PEER_resolve (peer_id, &msg.peer);
2890 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2891 &msg.header, GNUNET_NO);
2893 peer = peer_info_get_short (peer_id);
2894 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2895 path_info->peer = peer;
2897 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2902 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2903 * needed rescources.
2904 * If peer already exists, reevaluate shortest path and change if different.
2906 * @param t Tunnel we want to add a new peer to
2907 * @param peer PeerInfo of the peer being added
2911 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2913 struct GNUNET_PeerIdentity id;
2914 struct MeshPeerPath *best_p;
2915 struct MeshPeerPath *p;
2916 unsigned int best_cost;
2919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2920 GNUNET_PEER_resolve (peer->id, &id);
2922 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2925 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2926 GNUNET_assert (GNUNET_OK ==
2927 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2929 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2932 if (NULL != (p = peer->path_head))
2935 best_cost = tree_get_path_cost (t->tree, p);
2938 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2945 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
2946 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2947 t->path_refresh_task =
2948 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2952 /* Start a DHT get */
2953 peer_info_connect (peer, t);
2955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2959 * Add a path to a tunnel which we don't own, just to remember the next hop.
2960 * If destination node was already in the tunnel, the first hop information
2961 * will be replaced with the new path.
2963 * @param t Tunnel we want to add a new peer to
2964 * @param p Path to add
2965 * @param own_pos Position of local node in path.
2969 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2970 unsigned int own_pos)
2972 struct GNUNET_PeerIdentity id;
2974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2975 GNUNET_assert (0 != own_pos);
2976 tree_add_path (t->tree, p, NULL, NULL);
2977 if (own_pos < p->length - 1)
2979 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2980 tree_update_first_hops (t->tree, myid, &id);
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2987 * Notifies a tunnel that a connection has broken that affects at least
2988 * some of its peers. Sends a notification towards the root of the tree.
2989 * In case the peer is the owner of the tree, notifies the client that owns
2990 * the tunnel and tries to reconnect.
2992 * @param t Tunnel affected.
2993 * @param p1 Peer that got disconnected from p2.
2994 * @param p2 Peer that got disconnected from p1.
2996 * @return Short ID of the peer disconnected (either p1 or p2).
2997 * 0 if the tunnel remained unaffected.
2999 static GNUNET_PEER_Id
3000 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3006 tree_notify_connection_broken (t->tree, p1, p2,
3007 &tunnel_notify_client_peer_disconnected,
3009 if (myid != p1 && myid != p2)
3015 if (tree_get_predecessor (t->tree) != 0)
3017 /* We are the peer still connected, notify owner of the disconnection. */
3018 struct GNUNET_MESH_PathBroken msg;
3019 struct GNUNET_PeerIdentity neighbor;
3021 msg.header.size = htons (sizeof (msg));
3022 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3023 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3024 msg.tid = htonl (t->id.tid);
3025 msg.peer1 = my_full_id;
3026 GNUNET_PEER_resolve (pid, &msg.peer2);
3027 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3028 send_message (&msg.header, &neighbor, t);
3036 * Send a multicast packet to a neighbor.
3038 * @param cls Closure (Info about the multicast packet)
3039 * @param neighbor_id Short ID of the neighbor to send the packet to.
3042 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3044 struct MeshData *mdata = cls;
3045 struct MeshTransmissionDescriptor *info;
3046 struct GNUNET_PeerIdentity neighbor;
3048 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3050 info->mesh_data = mdata;
3051 (*(mdata->reference_counter)) ++;
3052 info->destination = neighbor_id;
3053 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3055 GNUNET_i2s (&neighbor));
3056 info->peer = peer_info_get (&neighbor);
3057 GNUNET_assert (NULL != info->peer);
3059 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3060 info->mesh_data->data_len,
3067 * Send a message in a tunnel in multicast, sending a copy to each child node
3068 * down the local one in the tunnel tree.
3070 * @param t Tunnel in which to send the data.
3071 * @param msg Message to be sent.
3072 * @param internal Has the service generated this message?
3075 tunnel_send_multicast (struct MeshTunnel *t,
3076 const struct GNUNET_MessageHeader *msg,
3079 struct MeshData *mdata;
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3082 " sending a multicast packet...\n");
3083 mdata = GNUNET_malloc (sizeof (struct MeshData));
3084 mdata->data_len = ntohs (msg->size);
3085 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3087 mdata->data = GNUNET_malloc (mdata->data_len);
3088 memcpy (mdata->data, msg, mdata->data_len);
3089 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3091 struct GNUNET_MESH_Multicast *mcast;
3093 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3094 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3096 ntohl (mcast->ttl));
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3102 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3103 && GNUNET_NO == internal)
3105 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3107 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3113 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3114 if (*(mdata->reference_counter) == 0)
3116 GNUNET_free (mdata->data);
3117 GNUNET_free (mdata->reference_counter);
3118 if (NULL != mdata->task)
3120 GNUNET_SCHEDULER_cancel(*(mdata->task));
3121 GNUNET_free (mdata->task);
3122 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
3124 // FIXME change order?
3125 GNUNET_free (mdata);
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3128 " sending a multicast packet done\n");
3134 * Increase the SKIP value of all peers that
3135 * have not received a unicast message.
3137 * @param cls Closure (ID of the peer that HAS received the message).
3138 * @param key ID of the neighbor.
3139 * @param value Information about the neighbor.
3141 * @return GNUNET_YES to keep iterating.
3144 tunnel_add_skip (void *cls,
3145 const struct GNUNET_HashCode * key,
3148 struct GNUNET_PeerIdentity *neighbor = cls;
3149 struct MeshTunnelChildInfo *cinfo = value;
3151 /* TODO compare only pointers? key == neighbor? */
3152 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3163 * Iterator to get the appropiate ACK value from all children nodes.
3165 * @param cls Closue (tunnel).
3166 * @param id Id of the child node.
3169 tunnel_get_child_ack (void *cls,
3172 struct GNUNET_PeerIdentity peer_id;
3173 struct MeshTunnelChildInfo *cinfo;
3174 struct MeshTunnel *t = cls;
3177 GNUNET_PEER_resolve (id, &peer_id);
3178 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3179 &peer_id.hashPubKey);
3182 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3184 cinfo->pid = t->pid;
3185 cinfo->skip = t->pid;
3186 cinfo->max_pid = ack = t->pid + 1;
3187 GNUNET_assert (GNUNET_OK ==
3188 GNUNET_CONTAINER_multihashmap_put(t->children_fc,
3189 &peer_id.hashPubKey,
3191 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3195 ack = cinfo->max_pid;
3198 if (0 == t->max_child_ack)
3199 t->max_child_ack = ack;
3201 if (GNUNET_YES == t->speed_min)
3203 t->max_child_ack = t->max_child_ack > ack ? ack : t->max_child_ack;
3207 t->max_child_ack = t->max_child_ack > ack ? t->max_child_ack : ack;
3214 * Get the maximum PID allowed to transmit to any
3215 * tunnel child of the local peer.
3219 * @return Maximum PID allowed.
3222 tunnel_get_children_ack (struct MeshTunnel *t)
3224 t->max_child_ack = 0;
3225 tree_iterate_children (t->tree, tunnel_get_child_ack, t);
3226 return t->max_child_ack;
3231 * Send an ACK informing the predecessor about the available buffer space.
3232 * If buffering is off, send only on behalf of children or self if endpoint.
3233 * If buffering is on, send when sent to children and buffer space is free.
3235 * @param t Tunnel on which to send the ACK.
3238 tunnel_send_ack (struct MeshTunnel *t, uint16_t type)
3240 struct GNUNET_MESH_ACK msg;
3241 struct GNUNET_PeerIdentity id;
3243 uint32_t buffer_free;
3247 /* Is it after unicast / multicast retransmission? */
3248 if (GNUNET_MESSAGE_TYPE_MESH_ACK != type)
3250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ACK via DATA retransmission\n");
3251 if (GNUNET_YES == t->nobuffer)
3253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3256 if (t->queue_max > t->queue_n * 2)
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3263 /* Ok, ACK might be necessary, what PID to ACK? */
3264 count = t->pid - t->skip;
3265 buffer_free = t->queue_max - t->queue_n;
3266 ack = count + buffer_free;
3267 child_ack = tunnel_get_children_ack (t);
3269 if (GNUNET_YES == t->speed_min)
3271 ack = child_ack > ack ? ack : child_ack;
3275 ack = child_ack > ack ? child_ack : ack;
3278 /* If speed_min and not all children have ack'd, dont send yet */
3279 if (ack == t->last_ack)
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, not ready\n");
3286 msg.pid = htonl (ack);
3288 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3290 msg.header.size = htons (sizeof (msg));
3291 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3292 msg.tid = htonl (t->id.tid);
3293 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3294 send_message (&msg.header, &id, t);
3299 * Send a message to all peers in this tunnel that the tunnel is no longer
3302 * @param t The tunnel whose peers to notify.
3305 tunnel_send_destroy (struct MeshTunnel *t)
3307 struct GNUNET_MESH_TunnelDestroy msg;
3309 msg.header.size = htons (sizeof (msg));
3310 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3311 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3312 msg.tid = htonl (t->id.tid);
3313 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3318 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3320 * @param cls Closure (Tunnel which to cancel).
3321 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3324 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3326 struct MeshTunnel *t = cls;
3327 struct MeshPeerInfo *peer_info;
3328 struct MeshPeerQueue *pq;
3329 struct MeshPeerQueue *next;
3331 peer_info = peer_info_get_short (neighbor_id);
3332 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3335 if (pq->tunnel == t)
3337 queue_destroy (pq, GNUNET_YES);
3340 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3342 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3343 peer_info->core_transmit = NULL;
3348 * Destroy the tunnel and free any allocated resources linked to it.
3350 * @param t the tunnel to destroy
3352 * @return GNUNET_OK on success
3355 tunnel_destroy (struct MeshTunnel *t)
3357 struct MeshClient *c;
3358 struct GNUNET_HashCode hash;
3365 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3371 struct GNUNET_PeerIdentity id;
3373 GNUNET_PEER_resolve (t->id.oid, &id);
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3375 GNUNET_i2s (&id), t->id.tid);
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3381 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3382 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3387 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3390 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3394 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3395 for (i = 0; i < t->nclients; i++)
3399 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3404 for (i = 0; i < t->nignore; i++)
3408 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3413 if (t->nclients > 0)
3416 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3420 GNUNET_free (t->clients);
3422 if (NULL != t->peers)
3424 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3426 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3429 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3430 &tunnel_destroy_child,
3432 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3434 tree_destroy (t->tree);
3436 if (NULL != t->regex_ctx)
3437 regex_cancel_search (t->regex_ctx);
3438 if (NULL != t->dht_get_type)
3439 GNUNET_DHT_get_stop (t->dht_get_type);
3440 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3441 GNUNET_SCHEDULER_cancel (t->timeout_task);
3442 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3443 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3446 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3447 GNUNET_assert (0 <= n_tunnels);
3454 * Create a new tunnel
3456 * @param owner Who is the owner of the tunnel (short ID).
3457 * @param tid Tunnel Number of the tunnel.
3458 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3459 * @param local Tunnel Number for the tunnel, for the client point of view.
3461 * @return A new initialized tunnel. NULL on error.
3463 static struct MeshTunnel *
3464 tunnel_new (GNUNET_PEER_Id owner,
3465 MESH_TunnelNumber tid,
3466 struct MeshClient *client,
3467 MESH_TunnelNumber local)
3469 struct MeshTunnel *t;
3470 struct GNUNET_HashCode hash;
3472 if (n_tunnels >= max_tunnels && NULL == client)
3475 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3478 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
3479 t->tree = tree_new (owner);
3481 t->local_tid = local;
3482 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
3484 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3486 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3488 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3494 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3500 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3502 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3503 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3507 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3517 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3518 * that are no longer needed, as well as nodes of no longer reachable peers.
3519 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3521 * @param t Tunnel from which to remove the path.
3522 * @param peer Short id of the peer which should be removed.
3525 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3527 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3533 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3534 * client when the client disconnects. If the client is not the owner, the
3535 * owner will get notified if no more clients are in the tunnel and the client
3536 * get removed from the tunnel's list.
3538 * @param cls closure (client that is disconnecting)
3539 * @param key the hash of the local tunnel id (used to access the hashmap)
3540 * @param value the value stored at the key (tunnel to destroy)
3542 * @return GNUNET_OK on success
3545 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3547 struct MeshTunnel *t = value;
3548 struct MeshClient *c = cls;
3551 send_client_tunnel_disconnect(t, c);
3554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3555 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3556 tunnel_delete_client(t, c);
3557 client_delete_tunnel(c, t);
3560 tunnel_send_destroy(t);
3561 r = tunnel_destroy (t);
3567 * Timeout function, destroys tunnel if called
3569 * @param cls Closure (tunnel to destroy).
3570 * @param tc TaskContext
3573 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3575 struct MeshTunnel *t = cls;
3577 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3579 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3584 * Resets the tunnel timeout. Starts it if no timeout was running.
3586 * @param t Tunnel whose timeout to reset.
3589 tunnel_reset_timeout (struct MeshTunnel *t)
3591 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3592 GNUNET_SCHEDULER_cancel (t->timeout_task);
3594 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3595 (refresh_path_time, 4), &tunnel_timeout, t);
3599 /******************************************************************************/
3600 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3601 /******************************************************************************/
3604 * Function to send a create path packet to a peer.
3606 * @param cls closure
3607 * @param size number of bytes available in buf
3608 * @param buf where the callee should write the message
3609 * @return number of bytes written to buf
3612 send_core_path_create (void *cls, size_t size, void *buf)
3614 struct MeshPathInfo *info = cls;
3615 struct GNUNET_MESH_ManipulatePath *msg;
3616 struct GNUNET_PeerIdentity *peer_ptr;
3617 struct MeshTunnel *t = info->t;
3618 struct MeshPeerPath *p = info->path;
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3625 sizeof (struct GNUNET_MESH_ManipulatePath) +
3626 p->length * sizeof (struct GNUNET_PeerIdentity);
3628 if (size < size_needed || NULL == buf)
3633 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3634 msg->header.size = htons (size_needed);
3635 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3636 msg->tid = ntohl (t->id.tid);
3638 if (GNUNET_YES == t->speed_min)
3639 opt = MESH_TUNNEL_OPT_SPEED_MIN;
3640 if (GNUNET_YES == t->nobuffer)
3641 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3642 msg->opt = htonl(opt);
3644 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3645 for (i = 0; i < p->length; i++)
3647 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3654 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3660 * Fill the core buffer
3662 * @param cls closure (data itself)
3663 * @param size number of bytes available in buf
3664 * @param buf where the callee should write the message
3666 * @return number of bytes written to buf
3669 send_core_data_multicast (void *cls, size_t size, void *buf)
3671 struct MeshTransmissionDescriptor *info = cls;
3674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3675 GNUNET_assert (NULL != info);
3676 GNUNET_assert (NULL != info->peer);
3677 total_size = info->mesh_data->data_len;
3678 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3680 if (total_size > size)
3685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3686 memcpy (buf, info->mesh_data->data, total_size);
3689 struct GNUNET_MESH_Multicast *mc;
3690 struct GNUNET_MessageHeader *mh;
3693 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3695 mc = (struct GNUNET_MESH_Multicast *) mh;
3696 mh = (struct GNUNET_MessageHeader *) &mc[1];
3697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3698 " multicast, payload type %u\n", ntohs (mh->type));
3699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3700 " multicast, payload size %u\n", ntohs (mh->size));
3704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3709 data_descriptor_decrement_rc (info->mesh_data);
3710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3718 * Creates a path ack message in buf and frees all unused resources.
3720 * @param cls closure (MeshTransmissionDescriptor)
3721 * @param size number of bytes available in buf
3722 * @param buf where the callee should write the message
3723 * @return number of bytes written to buf
3726 send_core_path_ack (void *cls, size_t size, void *buf)
3728 struct MeshTransmissionDescriptor *info = cls;
3729 struct GNUNET_MESH_PathACK *msg = buf;
3731 GNUNET_assert (NULL != info);
3732 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3737 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3738 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3739 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3740 msg->tid = htonl (info->origin->tid);
3741 msg->peer_id = my_full_id;
3744 /* TODO add signature */
3746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3747 return sizeof (struct GNUNET_MESH_PathACK);
3752 * Free a transmission that was already queued with all resources
3753 * associated to the request.
3755 * @param queue Queue handler to cancel.
3756 * @param clear_cls Is it necessary to free associated cls?
3759 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3761 struct MeshTransmissionDescriptor *dd;
3762 struct MeshPathInfo *path_info;
3764 if (GNUNET_YES == clear_cls)
3766 switch (queue->type)
3768 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3769 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3770 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
3773 data_descriptor_decrement_rc (dd->mesh_data);
3775 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3777 path_info = queue->cls;
3778 path_destroy (path_info->path);
3782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
3784 GNUNET_free_non_null (queue->cls);
3786 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3787 queue->peer->queue_tail,
3789 GNUNET_free (queue);
3794 * Core callback to write a queued packet to core buffer
3796 * @param cls Closure (peer info).
3797 * @param size Number of bytes available in buf.
3798 * @param buf Where the to write the message.
3800 * @return number of bytes written to buf
3803 queue_send (void *cls, size_t size, void *buf)
3805 struct MeshPeerInfo *peer = cls;
3806 struct GNUNET_MessageHeader *msg;
3807 struct MeshPeerQueue *queue;
3808 struct MeshTunnel *t;
3811 peer->core_transmit = NULL;
3812 queue = peer->queue_head;
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3816 /* If queue is empty, send should have been cancelled */
3822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3824 /* Check if buffer size is enough for the message */
3825 if (queue->size > size)
3827 struct GNUNET_PeerIdentity id;
3829 GNUNET_PEER_resolve (peer->id, &id);
3830 peer->core_transmit =
3831 GNUNET_CORE_notify_transmit_ready(core_handle,
3834 GNUNET_TIME_UNIT_FOREVER_REL,
3841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3847 switch (queue->type)
3850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
3851 data_size = send_core_data_raw (queue->cls, size, buf);
3852 msg = (struct GNUNET_MessageHeader *) buf;
3853 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
3854 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3856 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
3858 data_size = send_core_data_multicast(queue->cls, size, buf);
3859 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3861 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3863 data_size = send_core_path_create(queue->cls, size, buf);
3865 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3867 data_size = send_core_path_ack(queue->cls, size, buf);
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
3875 /* Free queue, but cls was freed by send_core_* */
3876 queue_destroy (queue, GNUNET_NO);
3878 if (GNUNET_YES == t->destroy && 0 == t->queue_n)
3880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3884 /* If more data in queue, send next */
3885 if (NULL != peer->queue_head)
3887 struct GNUNET_PeerIdentity id;
3889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3890 GNUNET_PEER_resolve (peer->id, &id);
3891 peer->core_transmit =
3892 GNUNET_CORE_notify_transmit_ready(core_handle,
3895 GNUNET_TIME_UNIT_FOREVER_REL,
3897 peer->queue_head->size,
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3907 * Queue and pass message to core when possible.
3909 * @param cls Closure (type dependant).
3910 * @param type Type of the message, 0 for a raw message.
3911 * @param size Size of the message.
3912 * @param dst Neighbor to send message to.
3913 * @param t Tunnel this message belongs to.
3916 queue_add (void *cls, uint16_t type, size_t size,
3917 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3919 struct MeshPeerQueue *queue;
3921 if (t->queue_n >= t->queue_max)
3923 if (NULL == t->owner)
3924 GNUNET_break_op(0); // TODO: kill connection?
3927 return; // Drop message
3930 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3936 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3937 if (NULL == dst->core_transmit)
3939 struct GNUNET_PeerIdentity id;
3941 GNUNET_PEER_resolve (dst->id, &id);
3942 dst->core_transmit =
3943 GNUNET_CORE_notify_transmit_ready(core_handle,
3946 GNUNET_TIME_UNIT_FOREVER_REL,
3955 /******************************************************************************/
3956 /******************** MESH NETWORK HANDLERS **************************/
3957 /******************************************************************************/
3961 * Core handler for path creation
3963 * @param cls closure
3964 * @param message message
3965 * @param peer peer identity this notification is about
3966 * @param atsi performance data
3967 * @param atsi_count number of records in 'atsi'
3969 * @return GNUNET_OK to keep the connection open,
3970 * GNUNET_SYSERR to close it (signal serious error)
3973 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3974 const struct GNUNET_MessageHeader *message,
3975 const struct GNUNET_ATS_Information *atsi,
3976 unsigned int atsi_count)
3978 unsigned int own_pos;
3981 MESH_TunnelNumber tid;
3982 struct GNUNET_MESH_ManipulatePath *msg;
3983 struct GNUNET_PeerIdentity *pi;
3984 struct GNUNET_HashCode hash;
3985 struct MeshPeerPath *path;
3986 struct MeshPeerInfo *dest_peer_info;
3987 struct MeshPeerInfo *orig_peer_info;
3988 struct MeshTunnel *t;
3990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3991 "Received a path create msg [%s]\n",
3992 GNUNET_i2s (&my_full_id));
3993 size = ntohs (message->size);
3994 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3996 GNUNET_break_op (0);
4000 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4001 if (size % sizeof (struct GNUNET_PeerIdentity))
4003 GNUNET_break_op (0);
4006 size /= sizeof (struct GNUNET_PeerIdentity);
4009 GNUNET_break_op (0);
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4013 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4015 tid = ntohl (msg->tid);
4016 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4018 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4019 t = tunnel_get (pi, tid);
4020 if (NULL == t) // FIXME only for INCOMING tunnels?
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4025 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4028 // FIXME notify failure
4031 opt = ntohl (msg->opt);
4032 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4033 GNUNET_YES : GNUNET_NO;
4034 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4035 GNUNET_YES : GNUNET_NO;
4037 if (GNUNET_YES == t->nobuffer)
4040 while (NULL != tunnel_get_incoming (next_local_tid))
4041 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4042 t->local_tid_dest = next_local_tid++;
4043 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4045 tunnel_reset_timeout (t);
4046 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4048 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4049 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4057 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4058 if (NULL == dest_peer_info)
4060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4061 " Creating PeerInfo for destination.\n");
4062 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4063 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4064 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4066 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4068 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4069 if (NULL == orig_peer_info)
4071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4072 " Creating PeerInfo for origin.\n");
4073 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4074 orig_peer_info->id = GNUNET_PEER_intern (pi);
4075 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4076 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4079 path = path_new (size);
4081 for (i = 0; i < size; i++)
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4084 GNUNET_i2s (&pi[i]));
4085 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4086 if (path->peers[i] == myid)
4089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4092 /* cannot be self, must be 'not found' */
4093 /* create path: self not found in path through self */
4094 GNUNET_break_op (0);
4095 path_destroy (path);
4096 /* FIXME error. destroy tunnel? leave for timeout? */
4099 path_add_to_peers (path, GNUNET_NO);
4100 tunnel_add_path (t, path, own_pos);
4101 if (own_pos == size - 1)
4103 /* It is for us! Send ack. */
4104 struct MeshTransmissionDescriptor *info;
4106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4107 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4108 if (NULL == t->peers)
4110 /* New tunnel! Notify clients on data. */
4111 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4113 GNUNET_break (GNUNET_SYSERR !=
4114 GNUNET_CONTAINER_multihashmap_put (t->peers,
4115 &my_full_id.hashPubKey,
4118 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4119 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4120 info->origin = &t->id;
4121 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4122 GNUNET_assert (NULL != info->peer);
4124 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4125 sizeof (struct GNUNET_MESH_PathACK),
4131 struct MeshPeerPath *path2;
4133 /* It's for somebody else! Retransmit. */
4134 path2 = path_duplicate (path);
4135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4136 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4137 path2 = path_duplicate (path);
4138 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4139 send_create_path (dest_peer_info, path, t);
4146 * Core handler for path destruction
4148 * @param cls closure
4149 * @param message message
4150 * @param peer peer identity this notification is about
4151 * @param atsi performance data
4152 * @param atsi_count number of records in 'atsi'
4154 * @return GNUNET_OK to keep the connection open,
4155 * GNUNET_SYSERR to close it (signal serious error)
4158 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4159 const struct GNUNET_MessageHeader *message,
4160 const struct GNUNET_ATS_Information *atsi,
4161 unsigned int atsi_count)
4163 struct GNUNET_MESH_ManipulatePath *msg;
4164 struct GNUNET_PeerIdentity *pi;
4165 struct MeshPeerPath *path;
4166 struct MeshTunnel *t;
4167 unsigned int own_pos;
4171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4172 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4173 size = ntohs (message->size);
4174 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4176 GNUNET_break_op (0);
4180 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4181 if (size % sizeof (struct GNUNET_PeerIdentity))
4183 GNUNET_break_op (0);
4186 size /= sizeof (struct GNUNET_PeerIdentity);
4189 GNUNET_break_op (0);
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4194 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4195 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4197 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4199 t = tunnel_get (pi, ntohl (msg->tid));
4202 /* TODO notify back: we don't know this tunnel */
4203 GNUNET_break_op (0);
4206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4207 path = path_new (size);
4209 for (i = 0; i < size; i++)
4211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4212 GNUNET_i2s (&pi[i]));
4213 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4214 if (path->peers[i] == myid)
4217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4218 if (own_pos < path->length - 1)
4219 send_message (message, &pi[own_pos + 1], t);
4221 send_client_tunnel_disconnect(t, NULL);
4223 tunnel_delete_peer (t, path->peers[path->length - 1]);
4224 path_destroy (path);
4230 * Core handler for notifications of broken paths
4232 * @param cls closure
4233 * @param message message
4234 * @param peer peer identity this notification is about
4235 * @param atsi performance data
4236 * @param atsi_count number of records in 'atsi'
4238 * @return GNUNET_OK to keep the connection open,
4239 * GNUNET_SYSERR to close it (signal serious error)
4242 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4243 const struct GNUNET_MessageHeader *message,
4244 const struct GNUNET_ATS_Information *atsi,
4245 unsigned int atsi_count)
4247 struct GNUNET_MESH_PathBroken *msg;
4248 struct MeshTunnel *t;
4250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4251 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4252 msg = (struct GNUNET_MESH_PathBroken *) message;
4253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4254 GNUNET_i2s (&msg->peer1));
4255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4256 GNUNET_i2s (&msg->peer2));
4257 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4260 GNUNET_break_op (0);
4263 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4264 GNUNET_PEER_search (&msg->peer2));
4271 * Core handler for tunnel destruction
4273 * @param cls closure
4274 * @param message message
4275 * @param peer peer identity this notification is about
4276 * @param atsi performance data
4277 * @param atsi_count number of records in 'atsi'
4279 * @return GNUNET_OK to keep the connection open,
4280 * GNUNET_SYSERR to close it (signal serious error)
4283 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4284 const struct GNUNET_MessageHeader *message,
4285 const struct GNUNET_ATS_Information *atsi,
4286 unsigned int atsi_count)
4288 struct GNUNET_MESH_TunnelDestroy *msg;
4289 struct MeshTunnel *t;
4291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4292 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4293 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4295 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4296 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4299 /* Probably already got the message from another path,
4300 * destroyed the tunnel and retransmitted to children.
4303 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4306 if (t->id.oid == myid)
4308 GNUNET_break_op (0);
4311 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4313 /* Tunnel was incoming, notify clients */
4314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4315 t->local_tid, t->local_tid_dest);
4316 send_clients_tunnel_destroy (t);
4318 tunnel_send_destroy (t);
4319 t->destroy = GNUNET_YES;
4320 // TODO: add timeout to destroy the tunnel anyway
4326 * Core handler for mesh network traffic going from the origin to a peer
4328 * @param cls closure
4329 * @param peer peer identity this notification is about
4330 * @param message message
4331 * @param atsi performance data
4332 * @param atsi_count number of records in 'atsi'
4333 * @return GNUNET_OK to keep the connection open,
4334 * GNUNET_SYSERR to close it (signal serious error)
4337 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4338 const struct GNUNET_MessageHeader *message,
4339 const struct GNUNET_ATS_Information *atsi,
4340 unsigned int atsi_count)
4342 struct GNUNET_MESH_Unicast *msg;
4343 struct GNUNET_PeerIdentity *neighbor;
4344 struct MeshTunnelChildInfo *cinfo;
4345 struct MeshTunnel *t;
4346 GNUNET_PEER_Id dest_id;
4351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4353 size = ntohs (message->size);
4355 sizeof (struct GNUNET_MESH_Unicast) +
4356 sizeof (struct GNUNET_MessageHeader))
4361 msg = (struct GNUNET_MESH_Unicast *) message;
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4363 ntohs (msg[1].header.type));
4364 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4367 /* TODO notify back: we don't know this tunnel */
4368 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4369 GNUNET_break_op (0);
4372 pid = ntohl (msg->pid);
4375 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4377 " Already seen pid %u, DROPPING!\n", pid);
4382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4383 " pid %u not seen yet, forwarding\n", pid);
4385 t->skip += (pid - t->pid) - 1;
4387 tunnel_reset_timeout (t);
4388 dest_id = GNUNET_PEER_search (&msg->destination);
4389 if (dest_id == myid)
4391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4392 " it's for us! sending to clients...\n");
4393 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4394 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4395 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK); // FIXME send after client processes the packet
4398 ttl = ntohl (msg->ttl);
4399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4402 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4407 " not for us, retransmitting...\n");
4408 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4410 neighbor = tree_get_first_hop (t->tree, dest_id);
4411 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4412 &neighbor->hashPubKey);
4415 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
4416 cinfo->id = GNUNET_PEER_intern (neighbor);
4418 cinfo->max_pid = pid + t->queue_max - t->queue_n; // FIXME review
4420 GNUNET_assert (GNUNET_OK ==
4421 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
4422 &neighbor->hashPubKey,
4424 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
4427 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4430 send_message (message, neighbor, t);
4436 * Core handler for mesh network traffic going from the origin to all peers
4438 * @param cls closure
4439 * @param message message
4440 * @param peer peer identity this notification is about
4441 * @param atsi performance data
4442 * @param atsi_count number of records in 'atsi'
4443 * @return GNUNET_OK to keep the connection open,
4444 * GNUNET_SYSERR to close it (signal serious error)
4446 * TODO: Check who we got this from, to validate route.
4449 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4450 const struct GNUNET_MessageHeader *message,
4451 const struct GNUNET_ATS_Information *atsi,
4452 unsigned int atsi_count)
4454 struct GNUNET_MESH_Multicast *msg;
4455 struct MeshTunnel *t;
4459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
4461 size = ntohs (message->size);
4462 if (sizeof (struct GNUNET_MESH_Multicast) +
4463 sizeof (struct GNUNET_MessageHeader) > size)
4465 GNUNET_break_op (0);
4468 msg = (struct GNUNET_MESH_Multicast *) message;
4469 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4473 /* TODO notify that we dont know that tunnel */
4474 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4475 GNUNET_break_op (0);
4478 pid = ntohl (msg->pid);
4481 /* already seen this packet, drop */
4482 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4483 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4484 " Already seen pid %u, DROPPING!\n", pid);
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4490 " pid %u not seen yet, forwarding\n", pid);
4492 t->skip += (pid - t->pid) - 1;
4494 tunnel_reset_timeout (t);
4496 /* Transmit to locally interested clients */
4497 if (NULL != t->peers &&
4498 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
4500 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
4501 send_subscribed_clients (message, &msg[1].header);
4503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
4504 if (ntohl (msg->ttl) == 0)
4506 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4507 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4510 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
4511 tunnel_send_multicast (t, message, GNUNET_NO);
4517 * Core handler for mesh network traffic toward the owner of a tunnel
4519 * @param cls closure
4520 * @param message message
4521 * @param peer peer identity this notification is about
4522 * @param atsi performance data
4523 * @param atsi_count number of records in 'atsi'
4525 * @return GNUNET_OK to keep the connection open,
4526 * GNUNET_SYSERR to close it (signal serious error)
4529 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4530 const struct GNUNET_MessageHeader *message,
4531 const struct GNUNET_ATS_Information *atsi,
4532 unsigned int atsi_count)
4534 struct GNUNET_MESH_ToOrigin *msg;
4535 struct GNUNET_PeerIdentity id;
4536 struct MeshPeerInfo *peer_info;
4537 struct MeshTunnel *t;
4540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4542 size = ntohs (message->size);
4543 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4544 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4546 GNUNET_break_op (0);
4549 msg = (struct GNUNET_MESH_ToOrigin *) message;
4550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4551 ntohs (msg[1].header.type));
4552 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4556 /* TODO notify that we dont know this tunnel (whom)? */
4557 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4558 GNUNET_break_op (0);
4562 if (t->id.oid == myid)
4565 struct GNUNET_MESH_ToOrigin *copy;
4567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4568 " it's for us! sending to clients...\n");
4569 if (NULL == t->owner)
4571 /* got data packet for ownerless tunnel */
4572 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
4574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4575 GNUNET_break_op (0);
4578 /* TODO signature verification */
4579 memcpy (cbuf, message, size);
4580 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4581 copy->tid = htonl (t->local_tid);
4582 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4583 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4584 ©->header, GNUNET_YES);
4587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4588 " not for us, retransmitting...\n");
4590 peer_info = peer_info_get (&msg->oid);
4591 if (NULL == peer_info)
4593 /* unknown origin of tunnel */
4597 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4598 send_message (message, &id, t);
4599 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4606 * Core handler for mesh network traffic point-to-point acks.
4608 * @param cls closure
4609 * @param message message
4610 * @param peer peer identity this notification is about
4611 * @param atsi performance data
4612 * @param atsi_count number of records in 'atsi'
4614 * @return GNUNET_OK to keep the connection open,
4615 * GNUNET_SYSERR to close it (signal serious error)
4618 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4619 const struct GNUNET_MessageHeader *message,
4620 const struct GNUNET_ATS_Information *atsi,
4621 unsigned int atsi_count)
4623 struct GNUNET_MESH_ACK *msg;
4624 struct MeshTunnelChildInfo *cinfo;
4625 struct MeshTunnel *t;
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got an ACK packet from %s\n",
4630 msg = (struct GNUNET_MESH_ACK *) message;
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4632 ntohs (msg[1].header.type));
4633 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4637 /* TODO notify that we dont know this tunnel (whom)? */
4638 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4639 GNUNET_break_op (0);
4642 ack = ntohl (msg->pid);
4643 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4647 GNUNET_break_op (0);
4650 cinfo->max_pid = ack;
4651 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4657 * Core handler for path ACKs
4659 * @param cls closure
4660 * @param message message
4661 * @param peer peer identity this notification is about
4662 * @param atsi performance data
4663 * @param atsi_count number of records in 'atsi'
4665 * @return GNUNET_OK to keep the connection open,
4666 * GNUNET_SYSERR to close it (signal serious error)
4669 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4670 const struct GNUNET_MessageHeader *message,
4671 const struct GNUNET_ATS_Information *atsi,
4672 unsigned int atsi_count)
4674 struct GNUNET_MESH_PathACK *msg;
4675 struct GNUNET_PeerIdentity id;
4676 struct MeshPeerInfo *peer_info;
4677 struct MeshPeerPath *p;
4678 struct MeshTunnel *t;
4680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4681 GNUNET_i2s (&my_full_id));
4682 msg = (struct GNUNET_MESH_PathACK *) message;
4683 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4686 /* TODO notify that we don't know the tunnel */
4687 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4689 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4693 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4695 peer_info = peer_info_get (&msg->peer_id);
4696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4697 GNUNET_i2s (&msg->peer_id));
4698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4701 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
4703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4704 "connect_by_string completed, stopping search\n");
4705 regex_cancel_search (t->regex_ctx);
4706 t->regex_ctx = NULL;
4709 /* Add paths to peers? */
4710 p = tree_get_path_to_peer (t->tree, peer_info->id);
4713 path_add_to_peers (p, GNUNET_YES);
4721 /* Message for us? */
4722 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4725 if (NULL == t->owner)
4727 GNUNET_break_op (0);
4730 if (NULL != t->dht_get_type)
4732 GNUNET_DHT_get_stop (t->dht_get_type);
4733 t->dht_get_type = NULL;
4735 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4737 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4738 send_client_peer_connected (t, peer_info->id);
4743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4744 " not for us, retransmitting...\n");
4745 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4746 peer_info = peer_info_get (&msg->oid);
4747 if (NULL == peer_info)
4749 /* If we know the tunnel, we should DEFINITELY know the peer */
4753 send_message (message, &id, t);
4759 * Functions to handle messages from core
4761 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4762 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4763 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4764 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4765 sizeof (struct GNUNET_MESH_PathBroken)},
4766 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4767 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4768 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4769 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4770 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4771 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4772 sizeof (struct GNUNET_MESH_ACK)},
4773 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4774 sizeof (struct GNUNET_MESH_PathACK)},
4780 /******************************************************************************/
4781 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4782 /******************************************************************************/
4785 * deregister_app: iterator for removing each application registered by a client
4787 * @param cls closure
4788 * @param key the hash of the application id (used to access the hashmap)
4789 * @param value the value stored at the key (client)
4791 * @return GNUNET_OK on success
4794 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
4796 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
4797 GNUNET_break (GNUNET_YES ==
4798 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
4804 * notify_client_connection_failure: notify a client that the connection to the
4805 * requested remote peer is not possible (for instance, no route found)
4806 * Function called when the socket is ready to queue more data. "buf" will be
4807 * NULL and "size" zero if the socket was closed for writing in the meantime.
4809 * @param cls closure
4810 * @param size number of bytes available in buf
4811 * @param buf where the callee should write the message
4812 * @return number of bytes written to buf
4815 notify_client_connection_failure (void *cls, size_t size, void *buf)
4818 struct MeshPeerInfo *peer_info;
4819 struct GNUNET_MESH_PeerControl *msg;
4820 struct GNUNET_PeerIdentity id;
4822 if (0 == size && NULL == buf)
4824 // TODO retry? cancel?
4828 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4829 peer_info = (struct MeshPeerInfo *) cls;
4830 msg = (struct GNUNET_MESH_PeerControl *) buf;
4831 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4832 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4833 // msg->tunnel_id = htonl(peer_info->t->tid);
4834 GNUNET_PEER_resolve (peer_info->id, &id);
4835 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4843 * Send keepalive packets for a peer
4845 * @param cls Closure (tunnel for which to send the keepalive).
4846 * @param tc Notification context.
4848 * TODO: implement explicit multicast keepalive?
4851 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4853 struct MeshTunnel *t = cls;
4854 struct GNUNET_MessageHeader *payload;
4855 struct GNUNET_MESH_Multicast *msg;
4857 sizeof (struct GNUNET_MESH_Multicast) +
4858 sizeof (struct GNUNET_MessageHeader);
4861 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4865 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4868 "sending keepalive for tunnel %d\n", t->id.tid);
4870 msg = (struct GNUNET_MESH_Multicast *) cbuf;
4871 msg->header.size = htons (size);
4872 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4873 msg->oid = my_full_id;
4874 msg->tid = htonl (t->id.tid);
4875 msg->ttl = htonl (default_ttl);
4876 msg->pid = htonl (t->pid + 1);
4878 payload = (struct GNUNET_MessageHeader *) &msg[1];
4879 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
4880 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4881 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
4883 t->path_refresh_task =
4884 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4890 * Function to process paths received for a new peer addition. The recorded
4891 * paths form the initial tunnel, which can be optimized later.
4892 * Called on each result obtained for the DHT search.
4894 * @param cls closure
4895 * @param exp when will this value expire
4896 * @param key key of the result
4897 * @param get_path path of the get request
4898 * @param get_path_length lenght of get_path
4899 * @param put_path path of the put request
4900 * @param put_path_length length of the put_path
4901 * @param type type of the result
4902 * @param size number of bytes in data
4903 * @param data pointer to the result data
4905 * TODO: re-issue the request after certain time? cancel after X results?
4908 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4909 const struct GNUNET_HashCode * key,
4910 const struct GNUNET_PeerIdentity *get_path,
4911 unsigned int get_path_length,
4912 const struct GNUNET_PeerIdentity *put_path,
4913 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4914 size_t size, const void *data)
4916 struct MeshPathInfo *path_info = cls;
4917 struct MeshPeerPath *p;
4918 struct GNUNET_PeerIdentity pi;
4921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4922 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4925 p = path_build_from_dht (get_path, get_path_length, put_path,
4927 path_add_to_peers (p, GNUNET_NO);
4929 for (i = 0; i < path_info->peer->ntunnels; i++)
4931 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4932 peer_info_connect (path_info->peer, path_info->t);
4940 * Function to process paths received for a new peer addition. The recorded
4941 * paths form the initial tunnel, which can be optimized later.
4942 * Called on each result obtained for the DHT search.
4944 * @param cls closure
4945 * @param exp when will this value expire
4946 * @param key key of the result
4947 * @param get_path path of the get request
4948 * @param get_path_length lenght of get_path
4949 * @param put_path path of the put request
4950 * @param put_path_length length of the put_path
4951 * @param type type of the result
4952 * @param size number of bytes in data
4953 * @param data pointer to the result data
4956 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4957 const struct GNUNET_HashCode * key,
4958 const struct GNUNET_PeerIdentity *get_path,
4959 unsigned int get_path_length,
4960 const struct GNUNET_PeerIdentity *put_path,
4961 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4962 size_t size, const void *data)
4964 const struct PBlock *pb = data;
4965 const struct GNUNET_PeerIdentity *pi = &pb->id;
4966 struct MeshTunnel *t = cls;
4967 struct MeshPeerInfo *peer_info;
4968 struct MeshPeerPath *p;
4970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
4971 if (size != sizeof (struct PBlock))
4973 GNUNET_break_op (0);
4976 if (ntohl(pb->type) != t->type)
4978 GNUNET_break_op (0);
4981 GNUNET_assert (NULL != t->owner);
4982 peer_info = peer_info_get (pi);
4983 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
4985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
4987 p = path_build_from_dht (get_path, get_path_length, put_path,
4989 path_add_to_peers (p, GNUNET_NO);
4991 tunnel_add_peer (t, peer_info);
4992 peer_info_connect (peer_info, t);
4997 * Function to process DHT string to regex matching.
4998 * Called on each result obtained for the DHT search.
5000 * @param cls closure (search context)
5001 * @param exp when will this value expire
5002 * @param key key of the result
5003 * @param get_path path of the get request (not used)
5004 * @param get_path_length lenght of get_path (not used)
5005 * @param put_path path of the put request (not used)
5006 * @param put_path_length length of the put_path (not used)
5007 * @param type type of the result
5008 * @param size number of bytes in data
5009 * @param data pointer to the result data
5012 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5013 const struct GNUNET_HashCode * key,
5014 const struct GNUNET_PeerIdentity *get_path,
5015 unsigned int get_path_length,
5016 const struct GNUNET_PeerIdentity *put_path,
5017 unsigned int put_path_length,
5018 enum GNUNET_BLOCK_Type type,
5019 size_t size, const void *data)
5021 const struct MeshRegexAccept *block = data;
5022 struct MeshRegexSearchContext *ctx = cls;
5023 struct MeshRegexSearchInfo *info = ctx->info;
5024 struct MeshPeerPath *p;
5025 struct MeshPeerInfo *peer_info;
5027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5030 peer_info = peer_info_get(&block->id);
5031 p = path_build_from_dht (get_path, get_path_length, put_path,
5033 path_add_to_peers (p, GNUNET_NO);
5036 tunnel_add_peer (info->t, peer_info);
5037 peer_info_connect (peer_info, info->t);
5038 if (0 == info->peer)
5040 info->peer = peer_info->id;
5044 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5047 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5048 ®ex_connect_timeout,
5056 * Function to process DHT string to regex matching.
5057 * Called on each result obtained for the DHT search.
5059 * @param cls closure (search context)
5060 * @param exp when will this value expire
5061 * @param key key of the result
5062 * @param get_path path of the get request (not used)
5063 * @param get_path_length lenght of get_path (not used)
5064 * @param put_path path of the put request (not used)
5065 * @param put_path_length length of the put_path (not used)
5066 * @param type type of the result
5067 * @param size number of bytes in data
5068 * @param data pointer to the result data
5070 * TODO: re-issue the request after certain time? cancel after X results?
5073 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5074 const struct GNUNET_HashCode * key,
5075 const struct GNUNET_PeerIdentity *get_path,
5076 unsigned int get_path_length,
5077 const struct GNUNET_PeerIdentity *put_path,
5078 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5079 size_t size, const void *data)
5081 const struct MeshRegexBlock *block = data;
5082 struct MeshRegexSearchContext *ctx = cls;
5083 struct MeshRegexSearchInfo *info = ctx->info;
5087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5088 "DHT GET STRING RETURNED RESULTS\n");
5089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5090 " key: %s\n", GNUNET_h2s (key));
5092 copy = GNUNET_malloc (size);
5093 memcpy (copy, data, size);
5094 GNUNET_break (GNUNET_OK ==
5095 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5096 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5097 len = ntohl (block->n_proof);
5099 char proof[len + 1];
5101 memcpy (proof, &block[1], len);
5103 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5105 GNUNET_break_op (0);
5109 len = strlen (info->description);
5110 if (len == ctx->position) // String processed
5112 if (GNUNET_YES == ntohl (block->accepting))
5114 regex_find_path(key, ctx);
5118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5119 // FIXME REGEX this block not successful, wait for more? start timeout?
5123 GNUNET_break (GNUNET_OK ==
5124 GNUNET_MESH_regex_block_iterate (block, size,
5125 ®ex_edge_iterator, ctx));
5129 /******************************************************************************/
5130 /********************* MESH LOCAL HANDLES **************************/
5131 /******************************************************************************/
5135 * Handler for client disconnection
5137 * @param cls closure
5138 * @param client identification of the client; NULL
5139 * for the last call when the server is destroyed
5142 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5144 struct MeshClient *c;
5145 struct MeshClient *next;
5148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5157 if (c->handle != client)
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5165 GNUNET_SERVER_client_drop (c->handle);
5166 c->shutting_down = GNUNET_YES;
5167 GNUNET_assert (NULL != c->own_tunnels);
5168 GNUNET_assert (NULL != c->incoming_tunnels);
5169 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5170 &tunnel_destroy_iterator, c);
5171 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5172 &tunnel_destroy_iterator, c);
5173 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5174 &tunnel_destroy_iterator, c);
5175 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5176 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5177 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5179 /* deregister clients applications */
5180 if (NULL != c->apps)
5182 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5183 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5185 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5186 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5188 GNUNET_SCHEDULER_cancel (announce_applications_task);
5189 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5191 if (NULL != c->types)
5192 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5193 for (i = 0; i < c->n_regex; i++)
5195 GNUNET_free (c->regexes[i]);
5197 GNUNET_free_non_null (c->regexes);
5198 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5199 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5201 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5204 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5213 * Handler for new clients
5215 * @param cls closure
5216 * @param client identification of the client
5217 * @param message the actual message, which includes messages the client wants
5220 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5221 const struct GNUNET_MessageHeader *message)
5223 struct GNUNET_MESH_ClientConnect *cc_msg;
5224 struct MeshClient *c;
5225 GNUNET_MESH_ApplicationType *a;
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5233 /* Check data sanity */
5234 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5235 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5236 ntypes = ntohs (cc_msg->types);
5237 napps = ntohs (cc_msg->applications);
5239 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5246 /* Create new client structure */
5247 c = GNUNET_malloc (sizeof (struct MeshClient));
5248 c->id = next_client_id++;
5249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5251 GNUNET_SERVER_client_keep (client);
5252 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5255 GNUNET_MESH_ApplicationType at;
5256 struct GNUNET_HashCode hc;
5258 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5259 for (i = 0; i < napps; i++)
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5263 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5264 /* store in clients hashmap */
5265 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5267 /* store in global hashmap, for announcements */
5268 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5269 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5271 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5272 announce_applications_task =
5273 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5279 struct GNUNET_HashCode hc;
5281 t = (uint16_t *) & a[napps];
5282 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5283 for (i = 0; i < ntypes; i++)
5286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5287 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5289 /* store in clients hashmap */
5290 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5291 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5292 /* store in global hashmap */
5293 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5294 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5298 " client has %u+%u subscriptions\n", napps, ntypes);
5300 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5301 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5302 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5303 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5304 GNUNET_SERVER_notification_context_add (nc, client);
5305 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5307 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5313 * Handler for clients announcing available services by a regular expression.
5315 * @param cls closure
5316 * @param client identification of the client
5317 * @param message the actual message, which includes messages the client wants
5320 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5321 const struct GNUNET_MessageHeader *message)
5323 struct MeshClient *c;
5327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5329 /* Sanity check for client registration */
5330 if (NULL == (c = client_get (client)))
5333 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5338 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5339 regex = GNUNET_malloc (len + 1);
5340 memcpy (regex, &message[1], len);
5342 GNUNET_array_append (c->regexes, c->n_regex, regex);
5343 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5345 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5357 * Handler for requests of new tunnels
5359 * @param cls closure
5360 * @param client identification of the client
5361 * @param message the actual message
5364 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5365 const struct GNUNET_MessageHeader *message)
5367 struct GNUNET_MESH_TunnelMessage *t_msg;
5368 struct MeshTunnel *t;
5369 struct MeshClient *c;
5371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5373 /* Sanity check for client registration */
5374 if (NULL == (c = client_get (client)))
5377 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5382 /* Message sanity check */
5383 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5390 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5391 /* Sanity check for tunnel numbering */
5392 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5395 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5398 /* Sanity check for duplicate tunnel IDs */
5399 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
5402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5406 while (NULL != tunnel_get_by_pi (myid, next_tid))
5407 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5408 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
5411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5415 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5417 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5418 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
5420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5421 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5427 * Handler for requests of deleting tunnels
5429 * @param cls closure
5430 * @param client identification of the client
5431 * @param message the actual message
5434 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5435 const struct GNUNET_MessageHeader *message)
5437 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5438 struct MeshClient *c;
5439 struct MeshTunnel *t;
5440 MESH_TunnelNumber tid;
5442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5443 "Got a DESTROY TUNNEL from client!\n");
5445 /* Sanity check for client registration */
5446 if (NULL == (c = client_get (client)))
5449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5452 /* Message sanity check */
5453 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5460 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5462 /* Retrieve tunnel */
5463 tid = ntohl (tunnel_msg->tunnel_id);
5464 t = tunnel_get_by_local_id(c, tid);
5468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5469 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5472 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5474 client_ignore_tunnel (c, t);
5476 // TODO: when to destroy incoming tunnel?
5477 if (t->nclients == 0)
5479 GNUNET_assert (GNUNET_YES ==
5480 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
5482 GNUNET_assert (GNUNET_YES ==
5483 GNUNET_CONTAINER_multihashmap_remove (t->peers,
5484 &my_full_id.hashPubKey,
5488 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5491 send_client_tunnel_disconnect(t, c);
5492 client_delete_tunnel(c, t);
5494 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5496 tunnel_send_destroy (t);
5497 t->destroy = GNUNET_YES;
5498 // The tunnel will be destroyed when the last message is transmitted.
5499 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5505 * Handler for requests of seeting tunnel's speed.
5507 * @param cls Closure (unused).
5508 * @param client Identification of the client.
5509 * @param message The actual message.
5512 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
5513 const struct GNUNET_MessageHeader *message)
5515 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5516 struct MeshClient *c;
5517 struct MeshTunnel *t;
5518 MESH_TunnelNumber tid;
5520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5521 "Got a SPEED request from client!\n");
5523 /* Sanity check for client registration */
5524 if (NULL == (c = client_get (client)))
5527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5532 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5534 /* Retrieve tunnel */
5535 tid = ntohl (tunnel_msg->tunnel_id);
5536 t = tunnel_get_by_local_id(c, tid);
5540 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5541 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5545 switch (ntohs(message->type))
5547 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
5548 t->speed_min = GNUNET_YES;
5550 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
5551 t->speed_min = GNUNET_NO;
5556 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5561 * Handler for requests of seeting tunnel's buffering policy.
5563 * @param cls Closure (unused).
5564 * @param client Identification of the client.
5565 * @param message The actual message.
5568 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5569 const struct GNUNET_MessageHeader *message)
5571 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5572 struct MeshClient *c;
5573 struct MeshTunnel *t;
5574 MESH_TunnelNumber tid;
5576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5577 "Got a BUFFER request from client!\n");
5579 /* Sanity check for client registration */
5580 if (NULL == (c = client_get (client)))
5583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5588 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5590 /* Retrieve tunnel */
5591 tid = ntohl (tunnel_msg->tunnel_id);
5592 t = tunnel_get_by_local_id(c, tid);
5596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5597 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5601 switch (ntohs(message->type))
5603 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5604 t->nobuffer = GNUNET_NO;
5606 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5607 t->nobuffer = GNUNET_YES;
5613 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5618 * Handler for connection requests to new peers
5620 * @param cls closure
5621 * @param client identification of the client
5622 * @param message the actual message (PeerControl)
5625 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
5626 const struct GNUNET_MessageHeader *message)
5628 struct GNUNET_MESH_PeerControl *peer_msg;
5629 struct MeshPeerInfo *peer_info;
5630 struct MeshClient *c;
5631 struct MeshTunnel *t;
5632 MESH_TunnelNumber tid;
5634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
5635 /* Sanity check for client registration */
5636 if (NULL == (c = client_get (client)))
5639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5643 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5644 /* Sanity check for message size */
5645 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5652 /* Tunnel exists? */
5653 tid = ntohl (peer_msg->tunnel_id);
5654 t = tunnel_get_by_local_id (c, tid);
5658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5662 /* Does client own tunnel? */
5663 if (t->owner->handle != client)
5666 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
5670 GNUNET_i2s (&peer_msg->peer));
5671 peer_info = peer_info_get (&peer_msg->peer);
5673 tunnel_add_peer (t, peer_info);
5674 peer_info_connect (peer_info, t);
5676 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5682 * Handler for disconnection requests of peers in a tunnel
5684 * @param cls closure
5685 * @param client identification of the client
5686 * @param message the actual message (PeerControl)
5689 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
5690 const struct GNUNET_MessageHeader *message)
5692 struct GNUNET_MESH_PeerControl *peer_msg;
5693 struct MeshPeerInfo *peer_info;
5694 struct MeshClient *c;
5695 struct MeshTunnel *t;
5696 MESH_TunnelNumber tid;
5698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
5699 /* Sanity check for client registration */
5700 if (NULL == (c = client_get (client)))
5703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5706 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5707 /* Sanity check for message size */
5708 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5715 /* Tunnel exists? */
5716 tid = ntohl (peer_msg->tunnel_id);
5717 t = tunnel_get_by_local_id (c, tid);
5721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5726 /* Does client own tunnel? */
5727 if (t->owner->handle != client)
5730 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
5735 GNUNET_i2s (&peer_msg->peer));
5736 /* Is the peer in the tunnel? */
5738 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
5739 if (NULL == peer_info)
5742 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5746 /* Ok, delete peer from tunnel */
5747 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
5748 &peer_msg->peer.hashPubKey);
5750 send_destroy_path (t, peer_info->id);
5751 tunnel_delete_peer (t, peer_info->id);
5752 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5757 * Handler for blacklist requests of peers in a tunnel
5759 * @param cls closure
5760 * @param client identification of the client
5761 * @param message the actual message (PeerControl)
5764 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
5765 const struct GNUNET_MessageHeader *message)
5767 struct GNUNET_MESH_PeerControl *peer_msg;
5768 struct MeshClient *c;
5769 struct MeshTunnel *t;
5770 MESH_TunnelNumber tid;
5772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
5773 /* Sanity check for client registration */
5774 if (NULL == (c = client_get (client)))
5777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5780 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5782 /* Sanity check for message size */
5783 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5786 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5790 /* Tunnel exists? */
5791 tid = ntohl (peer_msg->tunnel_id);
5792 t = tunnel_get_by_local_id (c, tid);
5796 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5801 GNUNET_array_append(t->blacklisted, t->nblacklisted,
5802 GNUNET_PEER_intern(&peer_msg->peer));
5807 * Handler for unblacklist requests of peers in a tunnel
5809 * @param cls closure
5810 * @param client identification of the client
5811 * @param message the actual message (PeerControl)
5814 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
5815 const struct GNUNET_MessageHeader *message)
5817 struct GNUNET_MESH_PeerControl *peer_msg;
5818 struct MeshClient *c;
5819 struct MeshTunnel *t;
5820 MESH_TunnelNumber tid;
5824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
5825 /* Sanity check for client registration */
5826 if (NULL == (c = client_get (client)))
5829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5832 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5834 /* Sanity check for message size */
5835 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5838 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5842 /* Tunnel exists? */
5843 tid = ntohl (peer_msg->tunnel_id);
5844 t = tunnel_get_by_local_id (c, tid);
5848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5853 /* if peer is not known, complain */
5854 pid = GNUNET_PEER_search (&peer_msg->peer);
5861 /* search and remove from list */
5862 for (i = 0; i < t->nblacklisted; i++)
5864 if (t->blacklisted[i] == pid)
5866 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
5867 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
5872 /* if peer hasn't been blacklisted, complain */
5878 * Handler for connection requests to new peers by type
5880 * @param cls closure
5881 * @param client identification of the client
5882 * @param message the actual message (ConnectPeerByType)
5885 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
5886 const struct GNUNET_MessageHeader *message)
5888 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
5889 struct MeshClient *c;
5890 struct MeshTunnel *t;
5891 struct GNUNET_HashCode hash;
5892 MESH_TunnelNumber tid;
5894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
5895 /* Sanity check for client registration */
5896 if (NULL == (c = client_get (client)))
5899 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5903 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
5904 /* Sanity check for message size */
5905 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
5906 ntohs (connect_msg->header.size))
5909 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5913 /* Tunnel exists? */
5914 tid = ntohl (connect_msg->tunnel_id);
5915 t = tunnel_get_by_local_id (c, tid);
5919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5923 /* Does client own tunnel? */
5924 if (t->owner->handle != client)
5927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5931 /* Do WE have the service? */
5932 t->type = ntohl (connect_msg->type);
5933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
5934 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
5935 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
5938 /* Yes! Fast forward, add ourselves to the tunnel and send the
5939 * good news to the client, and alert the destination client of
5940 * an incoming tunnel.
5942 * FIXME send a path create to self, avoid code duplication
5944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
5945 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
5946 peer_info_get (&my_full_id),
5947 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
5950 send_client_peer_connected (t, myid);
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
5952 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5954 t->local_tid_dest = next_local_tid++;
5955 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5956 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5957 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5961 /* Ok, lets find a peer offering the service */
5962 if (NULL != t->dht_get_type)
5964 GNUNET_DHT_get_stop (t->dht_get_type);
5966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
5967 GNUNET_h2s (&hash));
5969 GNUNET_DHT_get_start (dht_handle,
5970 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
5972 dht_replication_level,
5973 GNUNET_DHT_RO_RECORD_ROUTE |
5974 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5976 &dht_get_type_handler, t);
5978 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5984 * Handler for connection requests to new peers by a string service description.
5986 * @param cls closure
5987 * @param client identification of the client
5988 * @param message the actual message, which includes messages the client wants
5991 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
5992 const struct GNUNET_MessageHeader *message)
5994 struct GNUNET_MESH_ConnectPeerByString *msg;
5995 struct MeshRegexSearchContext *ctx;
5996 struct MeshRegexSearchInfo *info;
5997 struct GNUNET_DHT_GetHandle *get_h;
5998 struct GNUNET_HashCode key;
5999 struct MeshTunnel *t;
6000 struct MeshClient *c;
6001 MESH_TunnelNumber tid;
6005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6006 "Connect by string started\n");
6007 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6008 size = htons (message->size);
6010 /* Sanity check for client registration */
6011 if (NULL == (c = client_get (client)))
6014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6018 /* Message size sanity check */
6019 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6022 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6026 /* Tunnel exists? */
6027 tid = ntohl (msg->tunnel_id);
6028 t = tunnel_get_by_local_id (c, tid);
6032 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6036 /* Does client own tunnel? */
6037 if (t->owner->handle != client)
6040 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6045 " on tunnel %s [%u]\n",
6046 GNUNET_i2s(&my_full_id),
6049 /* Only one connect_by_string allowed at the same time! */
6050 /* FIXME: allow more, return handle at api level to cancel, document */
6051 if (NULL != t->regex_ctx)
6054 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6058 /* Find string itself */
6059 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6060 string = (const char *) &msg[1];
6062 /* Initialize context */
6063 size = GNUNET_REGEX_get_first_key(string, len, &key);
6064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6065 " consumed %u bits out of %u\n", size, len);
6066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6067 " looking for %s\n", GNUNET_h2s (&key));
6069 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6071 info->description = GNUNET_malloc (len + 1);
6072 memcpy (info->description, string, len);
6073 info->description[len] = '\0';
6074 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6075 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6078 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6079 ctx->position = size;
6083 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6085 /* Start search in DHT */
6086 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6087 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6088 &key, /* key to search */
6089 dht_replication_level, /* replication level */
6090 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6091 NULL, /* xquery */ // FIXME BLOOMFILTER
6092 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6093 &dht_get_string_handler, ctx);
6095 GNUNET_break (GNUNET_OK ==
6096 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6099 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6101 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6107 * Handler for client traffic directed to one peer
6109 * @param cls closure
6110 * @param client identification of the client
6111 * @param message the actual message
6114 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6115 const struct GNUNET_MessageHeader *message)
6117 struct MeshClient *c;
6118 struct MeshTunnel *t;
6119 struct MeshPeerInfo *pi;
6120 struct GNUNET_MESH_Unicast *data_msg;
6121 MESH_TunnelNumber tid;
6124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6125 "Got a unicast request from a client!\n");
6127 /* Sanity check for client registration */
6128 if (NULL == (c = client_get (client)))
6131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6134 data_msg = (struct GNUNET_MESH_Unicast *) message;
6135 /* Sanity check for message size */
6136 size = ntohs (message->size);
6137 if (sizeof (struct GNUNET_MESH_Unicast) +
6138 sizeof (struct GNUNET_MessageHeader) > size)
6141 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6145 /* Tunnel exists? */
6146 tid = ntohl (data_msg->tid);
6147 t = tunnel_get_by_local_id (c, tid);
6151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6155 /* Is it a local tunnel? Then, does client own the tunnel? */
6156 if (t->owner->handle != client)
6159 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6163 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6164 &data_msg->destination.hashPubKey);
6165 /* Is the selected peer in the tunnel? */
6169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6173 /* Ok, everything is correct, send the message
6174 * (pretend we got it from a mesh peer)
6177 char buf[ntohs (message->size)] GNUNET_ALIGN;
6178 struct GNUNET_MESH_Unicast *copy;
6180 /* Work around const limitation */
6181 copy = (struct GNUNET_MESH_Unicast *) buf;
6182 memcpy (buf, data_msg, size);
6183 copy->oid = my_full_id;
6184 copy->tid = htonl (t->id.tid);
6185 copy->ttl = htonl (default_ttl);
6186 copy->pid = htonl (t->pid + 1);
6187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6188 " calling generic handler...\n");
6189 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6191 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6197 * Handler for client traffic directed to the origin
6199 * @param cls closure
6200 * @param client identification of the client
6201 * @param message the actual message
6204 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6205 const struct GNUNET_MessageHeader *message)
6207 struct GNUNET_MESH_ToOrigin *data_msg;
6208 struct GNUNET_PeerIdentity id;
6209 struct MeshClient *c;
6210 struct MeshTunnel *t;
6211 MESH_TunnelNumber tid;
6214 /* Sanity check for client registration */
6215 if (NULL == (c = client_get (client)))
6218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6221 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6222 /* Sanity check for message size */
6223 size = ntohs (message->size);
6224 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6225 sizeof (struct GNUNET_MessageHeader) > size)
6228 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6232 /* Tunnel exists? */
6233 tid = ntohl (data_msg->tid);
6234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6235 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6236 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6239 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6242 t = tunnel_get_by_local_id (c, tid);
6246 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6250 /* It should be sent by someone who has this as incoming tunnel. */
6251 if (-1 == client_knows_tunnel (c, t))
6254 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6257 GNUNET_PEER_resolve (t->id.oid, &id);
6259 /* Ok, everything is correct, send the message
6260 * (pretend we got it from a mesh peer)
6263 char buf[ntohs (message->size)] GNUNET_ALIGN;
6264 struct GNUNET_MESH_ToOrigin *copy;
6266 /* Work around const limitation */
6267 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6268 memcpy (buf, data_msg, size);
6270 copy->tid = htonl (t->id.tid);
6271 copy->sender = my_full_id;
6272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6273 " calling generic handler...\n");
6274 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6276 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6282 * Handler for client traffic directed to all peers in a tunnel
6284 * @param cls closure
6285 * @param client identification of the client
6286 * @param message the actual message
6289 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6290 const struct GNUNET_MessageHeader *message)
6292 struct MeshClient *c;
6293 struct MeshTunnel *t;
6294 struct GNUNET_MESH_Multicast *data_msg;
6295 MESH_TunnelNumber tid;
6297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6298 "Got a multicast request from a client!\n");
6300 /* Sanity check for client registration */
6301 if (NULL == (c = client_get (client)))
6304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6307 data_msg = (struct GNUNET_MESH_Multicast *) message;
6308 /* Sanity check for message size */
6309 if (sizeof (struct GNUNET_MESH_Multicast) +
6310 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6313 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6317 /* Tunnel exists? */
6318 tid = ntohl (data_msg->tid);
6319 t = tunnel_get_by_local_id (c, tid);
6323 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6327 /* Does client own tunnel? */
6328 if (t->owner->handle != client)
6331 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6336 char buf[ntohs (message->size)] GNUNET_ALIGN;
6337 struct GNUNET_MESH_Multicast *copy;
6339 copy = (struct GNUNET_MESH_Multicast *) buf;
6340 memcpy (buf, message, ntohs (message->size));
6341 copy->oid = my_full_id;
6342 copy->tid = htonl (t->id.tid);
6343 copy->ttl = htonl (default_ttl);
6344 copy->pid = htonl (t->pid + 1);
6345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6346 " calling generic handler...\n");
6347 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
6350 /* receive done gets called when last copy is sent to a neighbor */
6356 * Functions to handle messages from clients
6358 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6359 {&handle_local_new_client, NULL,
6360 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6361 {&handle_local_announce_regex, NULL,
6362 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
6363 {&handle_local_tunnel_create, NULL,
6364 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6365 sizeof (struct GNUNET_MESH_TunnelMessage)},
6366 {&handle_local_tunnel_destroy, NULL,
6367 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6368 sizeof (struct GNUNET_MESH_TunnelMessage)},
6369 {&handle_local_tunnel_speed, NULL,
6370 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
6371 sizeof (struct GNUNET_MESH_TunnelMessage)},
6372 {&handle_local_tunnel_speed, NULL,
6373 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
6374 sizeof (struct GNUNET_MESH_TunnelMessage)},
6375 {&handle_local_tunnel_buffer, NULL,
6376 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6377 sizeof (struct GNUNET_MESH_TunnelMessage)},
6378 {&handle_local_tunnel_buffer, NULL,
6379 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6380 sizeof (struct GNUNET_MESH_TunnelMessage)},
6381 {&handle_local_connect_add, NULL,
6382 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
6383 sizeof (struct GNUNET_MESH_PeerControl)},
6384 {&handle_local_connect_del, NULL,
6385 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
6386 sizeof (struct GNUNET_MESH_PeerControl)},
6387 {&handle_local_blacklist, NULL,
6388 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
6389 sizeof (struct GNUNET_MESH_PeerControl)},
6390 {&handle_local_unblacklist, NULL,
6391 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
6392 sizeof (struct GNUNET_MESH_PeerControl)},
6393 {&handle_local_connect_by_type, NULL,
6394 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
6395 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
6396 {&handle_local_connect_by_string, NULL,
6397 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
6398 {&handle_local_unicast, NULL,
6399 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6400 {&handle_local_to_origin, NULL,
6401 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6402 {&handle_local_multicast, NULL,
6403 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6409 * To be called on core init/fail.
6411 * @param cls service closure
6412 * @param server handle to the server for this service
6413 * @param identity the public identity of this peer
6416 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6417 const struct GNUNET_PeerIdentity *identity)
6419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6420 core_handle = server;
6421 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6425 GNUNET_SCHEDULER_shutdown ();
6432 * Method called whenever a given peer connects.
6434 * @param cls closure
6435 * @param peer peer identity this notification is about
6436 * @param atsi performance data for the connection
6437 * @param atsi_count number of records in 'atsi'
6440 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
6441 const struct GNUNET_ATS_Information *atsi,
6442 unsigned int atsi_count)
6444 struct MeshPeerInfo *peer_info;
6445 struct MeshPeerPath *path;
6447 DEBUG_CONN ("Peer connected\n");
6448 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6449 peer_info = peer_info_get (peer);
6450 if (myid == peer_info->id)
6452 DEBUG_CONN (" (self)\n");
6457 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6459 path = path_new (2);
6460 path->peers[0] = myid;
6461 path->peers[1] = peer_info->id;
6462 GNUNET_PEER_change_rc (myid, 1);
6463 GNUNET_PEER_change_rc (peer_info->id, 1);
6464 peer_info_add_path (peer_info, path, GNUNET_YES);
6465 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6471 * Method called whenever a peer disconnects.
6473 * @param cls closure
6474 * @param peer peer identity this notification is about
6477 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6479 struct MeshPeerInfo *pi;
6480 struct MeshPeerQueue *q;
6481 struct MeshPeerQueue *n;
6483 DEBUG_CONN ("Peer disconnected\n");
6484 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6496 /* try to reroute this traffic instead */
6497 queue_destroy(q, GNUNET_YES);
6501 peer_info_remove_path (pi, pi->id, myid);
6504 DEBUG_CONN (" (self)\n");
6506 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6511 /******************************************************************************/
6512 /************************ MAIN FUNCTIONS ****************************/
6513 /******************************************************************************/
6516 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6518 * @param cls closure
6519 * @param key current key code
6520 * @param value value in the hash map
6521 * @return GNUNET_YES if we should continue to iterate,
6525 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6527 struct MeshTunnel *t = value;
6534 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
6536 * @param cls closure
6537 * @param key current key code
6538 * @param value value in the hash map
6539 * @return GNUNET_YES if we should continue to iterate,
6543 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
6545 struct MeshPeerInfo *p = value;
6546 struct MeshPeerQueue *q;
6547 struct MeshPeerQueue *n;
6555 queue_destroy(q, GNUNET_YES);
6559 peer_info_destroy (p);
6564 * Task run during shutdown.
6570 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6574 if (core_handle != NULL)
6576 GNUNET_CORE_disconnect (core_handle);
6579 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6580 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6581 if (dht_handle != NULL)
6583 GNUNET_DHT_disconnect (dht_handle);
6588 GNUNET_SERVER_notification_context_destroy (nc);
6591 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6593 GNUNET_SCHEDULER_cancel (announce_id_task);
6594 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6600 * Process mesh requests.
6602 * @param cls closure
6603 * @param server the initialized server
6604 * @param c configuration to use
6607 run (void *cls, struct GNUNET_SERVER_Handle *server,
6608 const struct GNUNET_CONFIGURATION_Handle *c)
6610 struct MeshPeerInfo *peer;
6611 struct MeshPeerPath *p;
6614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6615 server_handle = server;
6616 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6617 NULL, /* Closure passed to MESH functions */
6618 &core_init, /* Call core_init once connected */
6619 &core_connect, /* Handle connects */
6620 &core_disconnect, /* remove peers on disconnects */
6621 NULL, /* Don't notify about all incoming messages */
6622 GNUNET_NO, /* For header only in notification */
6623 NULL, /* Don't notify about all outbound messages */
6624 GNUNET_NO, /* For header-only out notification */
6625 core_handlers); /* Register these handlers */
6627 if (core_handle == NULL)
6630 GNUNET_SCHEDULER_shutdown ();
6635 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
6638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6640 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6642 GNUNET_SCHEDULER_shutdown ();
6647 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6648 &refresh_path_time))
6650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6652 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6653 "refresh path time");
6654 GNUNET_SCHEDULER_shutdown ();
6659 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6660 &app_announce_time))
6662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6664 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6665 "app announce time");
6666 GNUNET_SCHEDULER_shutdown ();
6671 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6676 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6677 "id announce time");
6678 GNUNET_SCHEDULER_shutdown ();
6683 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
6684 &unacknowledged_wait_time))
6686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6688 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6689 "unacknowledged wait time");
6690 GNUNET_SCHEDULER_shutdown ();
6695 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6698 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6700 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6702 GNUNET_SCHEDULER_shutdown ();
6707 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6712 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6714 GNUNET_SCHEDULER_shutdown ();
6719 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6722 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6724 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6726 GNUNET_SCHEDULER_shutdown ();
6731 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6734 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6736 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
6742 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6743 &dht_replication_level))
6745 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6747 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
6748 "dht replication level", 10);
6749 dht_replication_level = 10;
6753 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6754 GNUNET_free (keyfile);
6755 if (my_private_key == NULL)
6757 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6758 _("Mesh service could not access hostkey. Exiting.\n"));
6759 GNUNET_SCHEDULER_shutdown ();
6762 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6763 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6764 &my_full_id.hashPubKey);
6765 myid = GNUNET_PEER_intern (&my_full_id);
6767 // transport_handle = GNUNET_TRANSPORT_connect(c,
6774 dht_handle = GNUNET_DHT_connect (c, 64);
6775 if (dht_handle == NULL)
6780 stats = GNUNET_STATISTICS_create ("mesh", c);
6784 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6786 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6787 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6788 peers = GNUNET_CONTAINER_multihashmap_create (32);
6789 applications = GNUNET_CONTAINER_multihashmap_create (32);
6790 types = GNUNET_CONTAINER_multihashmap_create (32);
6792 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6793 nc = GNUNET_SERVER_notification_context_create (server_handle,
6795 GNUNET_SERVER_disconnect_notify (server_handle,
6796 &handle_local_client_disconnect, NULL);
6800 clients_tail = NULL;
6803 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6804 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6806 /* Create a peer_info for the local peer */
6807 peer = peer_info_get (&my_full_id);
6810 GNUNET_PEER_change_rc (myid, 1);
6811 peer_info_add_path (peer, p, GNUNET_YES);
6813 /* Scheduled the task to clean up when shutdown is called */
6814 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
6821 * The main function for the mesh service.
6823 * @param argc number of arguments from the command line
6824 * @param argv command line arguments
6825 * @return 0 ok, 1 on error
6828 main (int argc, char *const *argv)
6832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6835 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");