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_regex_lib.h"
57 /* TODO: move into configuration file */
58 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
59 GNUNET_TIME_UNIT_SECONDS,\
61 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
62 GNUNET_TIME_UNIT_SECONDS,\
65 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
66 GNUNET_TIME_UNIT_SECONDS,\
69 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
70 GNUNET_TIME_UNIT_SECONDS,\
72 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply(\
73 GNUNET_TIME_UNIT_SECONDS,\
75 #define DEFAULT_TTL 64
77 #define DHT_REPLICATION_LEVEL 10
81 #define MESH_BLOOM_SIZE 128
83 #define MESH_DEBUG_DHT GNUNET_YES
84 #define MESH_DEBUG_CONNECTION GNUNET_NO
86 #if MESH_DEBUG_CONNECTION
87 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
89 #define DEBUG_CONN(...)
93 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
95 #define DEBUG_DHT(...)
98 /******************************************************************************/
99 /************************ DATA STRUCTURES ****************************/
100 /******************************************************************************/
102 /** FWD declaration */
107 * Struct representing a piece of data being sent to other peers
111 /** Tunnel it belongs to. */
112 struct MeshTunnel *t;
114 /** In case of a multicast, task to allow a client to send more data if
115 * some neighbor is too slow. */
116 GNUNET_SCHEDULER_TaskIdentifier *task;
118 /** How many remaining neighbors we need to send this to. */
119 unsigned int *reference_counter;
121 /** Size of the data. */
130 * Struct containing info about a queued transmission to this peer
137 struct MeshPeerQueue *next;
142 struct MeshPeerQueue *prev;
145 * Peer this transmission is directed to.
147 struct MeshPeerInfo *peer;
150 * Tunnel this message belongs to.
152 struct MeshTunnel *tunnel;
155 * Pointer to info stucture used as cls.
165 * Size of the message
172 * Struct containing all info possibly needed to build a package when called
175 struct MeshTransmissionDescriptor
177 /** ID of the tunnel this packet travels in */
178 struct MESH_TunnelID *origin;
180 /** Who was this message being sent to */
181 struct MeshPeerInfo *peer;
183 /** Ultimate destination of the packet */
184 GNUNET_PEER_Id destination;
186 /** Data descriptor */
187 struct MeshData* mesh_data;
192 * Struct containing all information regarding a given peer
202 * Last time we heard from this peer
204 struct GNUNET_TIME_Absolute last_contact;
207 * Number of attempts to reconnect so far
209 int n_reconnect_attempts;
212 * Paths to reach the peer, ordered by ascending hop count
214 struct MeshPeerPath *path_head;
217 * Paths to reach the peer, ordered by ascending hop count
219 struct MeshPeerPath *path_tail;
222 * Handle to stop the DHT search for a path to this peer
224 struct GNUNET_DHT_GetHandle *dhtget;
227 * Closure given to the DHT GET
229 struct MeshPathInfo *dhtgetcls;
232 * Array of tunnels this peer participates in
233 * (most probably a small amount, therefore not a hashmap)
234 * When the path to the peer changes, notify these tunnels to let them
235 * re-adjust their path trees.
237 struct MeshTunnel **tunnels;
240 * Number of tunnels this peers participates in
242 unsigned int ntunnels;
245 * Transmission queue to core DLL head
247 struct MeshPeerQueue *queue_head;
250 * Transmission queue to core DLL tail
252 struct MeshPeerQueue *queue_tail;
255 * How many messages are in the queue to this peer.
257 unsigned int queue_n;
260 * Handle to for queued transmissions
262 struct GNUNET_CORE_TransmitHandle *core_transmit;
267 * Globally unique tunnel identification (owner + number)
268 * DO NOT USE OVER THE NETWORK
273 * Node that owns the tunnel
278 * Tunnel number to differentiate all the tunnels owned by the node oid
279 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
281 MESH_TunnelNumber tid;
285 struct MeshClient; /* FWD declaration */
288 * Struct containing all information regarding a tunnel
289 * For an intermediate node the improtant info used will be:
290 * - id Tunnel unique identification
291 * - paths[0] To know where to send it next
292 * - metainfo: ready, speeds, accounting
299 struct MESH_TunnelID id;
302 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
304 MESH_TunnelNumber local_tid;
307 * Local tunnel number for local destination clients (incoming number)
308 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
311 MESH_TunnelNumber local_tid_dest;
314 * Global count ID of the last *multicast* packet seen/sent.
319 * Local count ID of the last packet seen/sent.
324 * SKIP value for this tunnel.
329 * How many messages are in the queue.
331 unsigned int queue_n;
334 * How many messages do we accept in the queue.
336 unsigned int queue_max;
339 * Last time the tunnel was used
341 struct GNUNET_TIME_Absolute timestamp;
344 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
345 * containing peers added by id or by type, not intermediate peers.
347 struct GNUNET_CONTAINER_MultiHashMap *peers;
350 * Number of peers that are connected and potentially ready to receive data
352 unsigned int peers_ready;
355 * Number of peers that have been added to the tunnel
357 unsigned int peers_total;
360 * Client owner of the tunnel, if any
362 struct MeshClient *owner;
365 * Clients that have been informed about the tunnel, if any
367 struct MeshClient **clients;
370 * Number of elements in clients
372 unsigned int nclients;
375 * Clients that have requested to leave the tunnel
377 struct MeshClient **ignore;
380 * Number of elements in clients
382 unsigned int nignore;
387 GNUNET_PEER_Id *blacklisted;
390 * Number of elements in blacklisted
392 unsigned int nblacklisted;
395 * Bloomfilter (for peer identities) to stop circular routes
397 char bloomfilter[MESH_BLOOM_SIZE];
402 struct MeshTunnelTree *tree;
405 * Application type we are looking for in this tunnel
407 GNUNET_MESH_ApplicationType type;
410 * Used to search peers offering a service
412 struct GNUNET_DHT_GetHandle *dht_get_type;
415 * Initial context of the regex search for a connect_by_string
417 struct MeshRegexSearchContext *regex_ctx;
420 * Task to keep the used paths alive
422 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
425 * Task to destroy the tunnel after timeout
427 * FIXME: merge the two? a tunnel will have either
428 * a path refresh OR a timeout, never both!
430 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
435 * Info needed to work with tunnel paths and peers
442 struct MeshTunnel *t;
445 * Neighbouring peer to whom we send the packet to
447 struct MeshPeerInfo *peer;
452 struct MeshPeerPath *path;
457 * Struct containing information about a client of the service
464 struct MeshClient *next;
469 struct MeshClient *prev;
472 * Tunnels that belong to this client, indexed by local id
474 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
477 * Tunnels this client has accepted, indexed by incoming local id
479 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
482 * Tunnels this client has rejected, indexed by incoming local id
484 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
486 * Handle to communicate with the client
488 struct GNUNET_SERVER_Client *handle;
491 * Applications that this client has claimed to provide
493 struct GNUNET_CONTAINER_MultiHashMap *apps;
496 * Messages that this client has declared interest in
498 struct GNUNET_CONTAINER_MultiHashMap *types;
501 * Whether the client is active or shutting down (don't send confirmations
502 * to a client that is shutting down.
507 * ID of the client, mainly for debug messages
512 * Regular expressions describing the services offered by this client.
514 char **regexes; // FIXME add timeout? API to remove a regex?
517 * Number of regular expressions in regexes.
519 unsigned int n_regex;
522 * Task to refresh all regular expresions in the DHT.
524 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
530 * Struct to keep information of searches of services described by a regex
531 * using a user-provided string service description.
533 struct MeshRegexSearchInfo
536 * Which tunnel is this for
538 struct MeshTunnel *t;
541 * User provided description of the searched service.
546 * Part of the description already consumed by the search.
553 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
556 * Results from running DHT GETs.
558 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
561 * Contexts, for each running DHT GET. Free all on end of search.
563 struct MeshRegexSearchContext **contexts;
566 * Number of contexts (branches/steps in search).
568 unsigned int n_contexts;
571 * Peer that is connecting via connect_by_string. When connected, free ctx.
576 * Other peers that are found but not yet being connected to.
578 GNUNET_PEER_Id *peers;
581 * Number of elements in peers.
583 unsigned int n_peers;
586 * Next peer to try to connect to.
591 * Timeout for a connect attempt.
592 * When reached, try to connect to a different peer, if any. If not,
593 * try the same peer again.
595 GNUNET_SCHEDULER_TaskIdentifier timeout;
600 * Struct to keep state of running searches that have consumed a part of
603 struct MeshRegexSearchContext
606 * Part of the description already consumed by
607 * this particular search branch.
612 * Information about the search.
614 struct MeshRegexSearchInfo *info;
618 /******************************************************************************/
619 /************************ DEBUG FUNCTIONS ****************************/
620 /******************************************************************************/
624 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
625 * @param cls string to print
626 * @param success GNUNET_OK if the PUT was transmitted,
627 * GNUNET_NO on timeout,
628 * GNUNET_SYSERR on disconnect from service
629 * after the PUT message was transmitted
630 * (so we don't know if it was received or not)
635 mesh_debug (void *cls, int success)
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
645 /******************************************************************************/
646 /*********************** GLOBAL VARIABLES ****************************/
647 /******************************************************************************/
652 static struct MeshClient *clients;
653 static struct MeshClient *clients_tail;
656 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
658 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
661 * Tunnels incoming, indexed by MESH_TunnelNumber
662 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
664 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
667 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
669 static struct GNUNET_CONTAINER_MultiHashMap *peers;
672 * Handle to communicate with transport
674 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
677 * Handle to communicate with core
679 static struct GNUNET_CORE_Handle *core_handle;
684 static struct GNUNET_DHT_Handle *dht_handle;
689 static struct GNUNET_SERVER_Handle *server_handle;
692 * Notification context, to send messages to local clients
694 static struct GNUNET_SERVER_NotificationContext *nc;
697 * Local peer own ID (memory efficient handle)
699 static GNUNET_PEER_Id myid;
702 * Local peer own ID (full value)
704 static struct GNUNET_PeerIdentity my_full_id;
709 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
714 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
717 * Tunnel ID for the next created tunnel (global tunnel number)
719 static MESH_TunnelNumber next_tid;
722 * Tunnel ID for the next incoming tunnel (local tunnel number)
724 static MESH_TunnelNumber next_local_tid;
727 * All application types provided by this peer
729 static struct GNUNET_CONTAINER_MultiHashMap *applications;
732 * All message types clients of this peer are interested in
734 static struct GNUNET_CONTAINER_MultiHashMap *types;
737 * Task to periodically announce provided applications
739 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
742 * Task to periodically announce itself in the network
744 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
747 * Next ID to assign to a client
749 unsigned int next_client_id;
752 /******************************************************************************/
753 /*********************** DECLARATIONS **************************/
754 /******************************************************************************/
756 /* FIXME move declarations here */
759 * Function to process paths received for a new peer addition. The recorded
760 * paths form the initial tunnel, which can be optimized later.
761 * Called on each result obtained for the DHT search.
764 * @param exp when will this value expire
765 * @param key key of the result
766 * @param type type of the result
767 * @param size number of bytes in data
768 * @param data pointer to the result data
771 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
772 const struct GNUNET_HashCode * key,
773 const struct GNUNET_PeerIdentity *get_path,
774 unsigned int get_path_length,
775 const struct GNUNET_PeerIdentity *put_path,
776 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
777 size_t size, const void *data);
781 * Function to process DHT string to regex matching.
782 * Called on each result obtained for the DHT search.
784 * @param cls closure (search context)
785 * @param exp when will this value expire
786 * @param key key of the result
787 * @param get_path path of the get request (not used)
788 * @param get_path_length lenght of get_path (not used)
789 * @param put_path path of the put request (not used)
790 * @param put_path_length length of the put_path (not used)
791 * @param type type of the result
792 * @param size number of bytes in data
793 * @param data pointer to the result data
795 * TODO: re-issue the request after certain time? cancel after X results?
798 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
799 const struct GNUNET_HashCode * key,
800 const struct GNUNET_PeerIdentity *get_path,
801 unsigned int get_path_length,
802 const struct GNUNET_PeerIdentity *put_path,
803 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
804 size_t size, const void *data);
807 /******************************************************************************/
808 /************************ ITERATORS ****************************/
809 /******************************************************************************/
811 /* FIXME move iterators here */
814 * Iterator over edges in a regex block retrieved from the DHT.
816 * @param cls Closure.
817 * @param token Token that follows to next state.
818 * @param len Lenght of token.
819 * @param key Hash of next state.
821 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
824 regex_edge_iterator (void *cls,
827 const struct GNUNET_HashCode *key);
829 * Iterator over hash map entries.
832 * @param key current key code
833 * @param value value in the hash map
834 * @return GNUNET_YES if we should continue to
839 regex_result_iterator (void *cls,
840 const struct GNUNET_HashCode * key,
843 struct MeshRegexBlock *block = value;
844 struct MeshRegexSearchContext *ctx = cls;
846 // block was checked when stored, no need to check again
847 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
848 ®ex_edge_iterator, ctx);
854 * Iterator over edges in a regex block retrieved from the DHT.
856 * @param cls Closure (context of the search).
857 * @param token Token that follows to next state.
858 * @param len Lenght of token.
859 * @param key Hash of next state.
861 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
864 regex_edge_iterator (void *cls,
867 const struct GNUNET_HashCode *key)
869 struct MeshRegexSearchContext *ctx = cls;
870 struct MeshRegexSearchContext *new_ctx;
871 struct MeshRegexSearchInfo *info = ctx->info;
872 struct GNUNET_DHT_GetHandle *get_h;
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
879 current = &info->description[ctx->position];
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
881 current_len = strlen (info->description) - ctx->position;
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
885 if (len > current_len)
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
888 return GNUNET_YES; // Token too long, wont match
890 if (0 != strncmp (current, token, len))
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
893 return GNUNET_YES; // Token doesn't match
895 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
896 new_ctx->info = info;
897 new_ctx->position += ctx->position + len;
898 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
900 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
903 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
904 ®ex_result_iterator,
906 return GNUNET_YES; // We are already looking for it
908 /* Start search in DHT */
910 GNUNET_DHT_get_start (dht_handle, /* handle */
911 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
912 key, /* key to search */
913 DHT_REPLICATION_LEVEL, /* replication level */
914 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
915 NULL, /* xquery */ // FIXME BLOOMFILTER
916 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
917 &dht_get_string_handler, new_ctx);
919 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
920 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
930 * Iterator over hash map entries to cancel DHT GET requests after a
931 * successful connect_by_string.
933 * @param cls Closure (unused).
934 * @param key Current key code (unused).
935 * @param value Value in the hash map (get handle).
936 * @return GNUNET_YES if we should continue to iterate,
940 regex_cancel_dht_get (void *cls,
941 const struct GNUNET_HashCode * key,
944 struct GNUNET_DHT_GetHandle *h = value;
946 GNUNET_DHT_get_stop (h);
952 * Iterator over hash map entries to free MeshRegexBlocks stored during the
953 * search for connect_by_string.
955 * @param cls Closure (unused).
956 * @param key Current key code (unused).
957 * @param value MeshRegexBlock in the hash map.
958 * @return GNUNET_YES if we should continue to iterate,
962 regex_free_result (void *cls,
963 const struct GNUNET_HashCode * key,
973 * Regex callback iterator to store own service description in the DHT.
975 * @param cls closure.
976 * @param key hash for current state.
977 * @param proof proof for current state.
978 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
979 * @param num_edges number of edges leaving current state.
980 * @param edges edges leaving current state.
983 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
984 int accepting, unsigned int num_edges,
985 const struct GNUNET_REGEX_Edge *edges)
987 struct MeshRegexBlock *block;
988 struct MeshRegexEdge *block_edge;
989 enum GNUNET_DHT_RouteOption opt;
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 " regex dht put for state %s\n",
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1007 if (GNUNET_YES == accepting)
1009 struct MeshRegexAccept block;
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 " state %s is accepting, putting own id\n",
1014 size = sizeof (block);
1016 block.id = my_full_id;
1018 GNUNET_DHT_put(dht_handle, key,
1019 DHT_REPLICATION_LEVEL,
1020 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1021 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1024 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1029 len = strlen(proof);
1030 size = sizeof (struct MeshRegexBlock) + len;
1031 block = GNUNET_malloc (size);
1034 block->n_proof = htonl (len);
1035 block->n_edges = htonl (num_edges);
1036 block->accepting = htonl (accepting);
1038 /* Store the proof at the end of the block. */
1039 aux = (char *) &block[1];
1040 memcpy (aux, proof, len);
1043 /* Store each edge in a variable length MeshEdge struct at the
1044 * very end of the MeshRegexBlock structure.
1046 for (i = 0; i < num_edges; i++)
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1049 " edge %s towards %s\n",
1051 GNUNET_h2s(&edges[i].destination));
1053 /* aux points at the end of the last block */
1054 len = strlen (edges[i].label);
1055 size += sizeof (struct MeshRegexEdge) + len;
1056 // Calculate offset FIXME is this ok? use size instead?
1057 offset = aux - (char *) block;
1058 block = GNUNET_realloc (block, size);
1059 aux = &((char *) block)[offset];
1060 block_edge = (struct MeshRegexEdge *) aux;
1061 block_edge->key = edges[i].destination;
1062 block_edge->n_token = htonl (len);
1063 aux = (char *) &block_edge[1];
1064 memcpy (aux, edges[i].label, len);
1068 GNUNET_DHT_put(dht_handle, key,
1069 DHT_REPLICATION_LEVEL,
1071 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1073 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1077 GNUNET_free (block);
1082 * Store the regular expression describing a local service into the DHT.
1084 * @param regex The regular expresion.
1087 regex_put (const char *regex)
1089 struct GNUNET_REGEX_Automaton *dfa;
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1092 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1093 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1094 GNUNET_REGEX_automaton_destroy (dfa);
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1100 /******************************************************************************/
1101 /************************ PERIODIC FUNCTIONS ****************************/
1102 /******************************************************************************/
1105 * Announce iterator over for each application provided by the peer
1107 * @param cls closure
1108 * @param key current key code
1109 * @param value value in the hash map
1110 * @return GNUNET_YES if we should continue to
1115 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1117 struct PBlock block;
1118 struct MeshClient *c;
1120 block.id = my_full_id;
1121 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1122 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1123 if (0 == block.type)
1128 block.type = htonl (block.type);
1129 /* FIXME are hashes in multihash map equal on all aquitectures? */
1130 /* FIXME: keep return value of 'put' to possibly cancel!? */
1131 GNUNET_DHT_put (dht_handle, key,
1132 DHT_REPLICATION_LEVEL,
1133 GNUNET_DHT_RO_RECORD_ROUTE |
1134 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1135 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1137 (const char *) &block,
1138 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1140 APP_ANNOUNCE_TIME, NULL, NULL);
1146 * Periodically announce what applications are provided by local clients
1149 * @param cls closure
1150 * @param tc task context
1153 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1155 struct MeshClient *c = cls;
1158 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1160 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1164 DEBUG_DHT ("Starting PUT for regex\n");
1166 for (i = 0; i < c->n_regex; i++)
1168 regex_put (c->regexes[i]);
1170 c->regex_announce_task =
1171 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_regex, cls);
1172 DEBUG_DHT ("Finished PUT for regex\n");
1179 * Periodically announce what applications are provided by local clients
1182 * @param cls closure
1183 * @param tc task context
1186 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1188 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1190 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1194 DEBUG_DHT ("Starting PUT for apps\n");
1196 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1198 announce_applications_task =
1199 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
1201 DEBUG_DHT ("Finished PUT for apps\n");
1208 * Periodically announce self id in the DHT
1210 * @param cls closure
1211 * @param tc task context
1214 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1216 struct PBlock block;
1218 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1220 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1224 * - Set data expiration in function of X
1225 * - Adapt X to churn
1227 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1229 block.id = my_full_id;
1230 block.type = htonl (0);
1231 GNUNET_DHT_put (dht_handle, /* DHT handle */
1232 &my_full_id.hashPubKey, /* Key to use */
1233 DHT_REPLICATION_LEVEL, /* Replication level */
1234 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1235 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1236 sizeof (block), /* Size of the data */
1237 (const char *) &block, /* Data itself */
1238 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1239 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1240 NULL, /* Continuation */
1241 NULL); /* Continuation closure */
1243 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
1247 /******************************************************************************/
1248 /****************** GENERAL HELPER FUNCTIONS ************************/
1249 /******************************************************************************/
1252 * Cancel an ongoing regex search in the DHT and free all resources.
1254 * @param ctx The search context.
1257 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1259 struct MeshRegexSearchInfo *info = ctx->info;
1262 GNUNET_free (info->description);
1263 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1264 ®ex_cancel_dht_get, NULL);
1265 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1266 ®ex_free_result, NULL);
1267 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1268 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1269 info->t->regex_ctx = NULL;
1270 for (i = 0; i < info->n_contexts; i++)
1272 GNUNET_free (info->contexts[i]);
1274 if (0 < info->n_contexts)
1275 GNUNET_free (info->contexts);
1276 if (0 < info->n_peers)
1277 GNUNET_free (info->peers);
1278 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1280 GNUNET_SCHEDULER_cancel(info->timeout);
1286 * Search for a tunnel by global ID using full PeerIdentities
1288 * @param oid owner of the tunnel
1289 * @param tid global tunnel number
1291 * @return tunnel handler, NULL if doesn't exist
1293 static struct MeshTunnel *
1294 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1298 * Delete an active client from the tunnel.
1304 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1307 * Notify a tunnel that a connection has broken that affects at least
1308 * some of its peers.
1310 * @param t Tunnel affected.
1311 * @param p1 Peer that got disconnected from p2.
1312 * @param p2 Peer that got disconnected from p1.
1314 * @return Short ID of the peer disconnected (either p1 or p2).
1315 * 0 if the tunnel remained unaffected.
1317 static GNUNET_PEER_Id
1318 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1323 * Check if client has registered with the service and has not disconnected
1325 * @param client the client to check
1327 * @return non-NULL if client exists in the global DLL
1329 static struct MeshClient *
1330 client_get (struct GNUNET_SERVER_Client *client)
1332 struct MeshClient *c;
1337 if (c->handle == client)
1346 * Checks if a given client has subscribed to certain message type
1348 * @param message_type Type of message to check
1349 * @param c Client to check
1351 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1356 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1358 struct GNUNET_HashCode hc;
1360 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1361 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1366 * Allow a client to send more data after transmitting a multicast message
1367 * which some neighbor has not yet accepted altough a reasonable time has
1370 * @param cls Closure (DataDescriptor containing the task identifier)
1371 * @param tc Task Context
1373 * FIXME reference counter cshould be just int
1376 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1378 struct MeshData *mdata = cls;
1380 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1382 GNUNET_assert (NULL != mdata->reference_counter);
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1384 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1385 *(mdata->reference_counter));
1386 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1387 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1392 * Check whether client wants traffic from a tunnel.
1394 * @param c Client to check.
1395 * @param t Tunnel to be found.
1397 * @return GNUNET_YES if client knows tunnel.
1399 * TODO look in client hashmap
1402 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1406 for (i = 0; i < t->nclients; i++)
1407 if (t->clients[i] == c)
1414 * Check whether client has been informed about a tunnel.
1416 * @param c Client to check.
1417 * @param t Tunnel to be found.
1419 * @return GNUNET_YES if client knows tunnel.
1421 * TODO look in client hashmap
1424 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1428 for (i = 0; i < t->nignore; i++)
1429 if (t->ignore[i] == c)
1431 return client_wants_tunnel(c, t);
1436 * Marks a client as uninterested in traffic from the tunnel, updating both
1437 * client and tunnel to reflect this.
1439 * @param c Client that doesn't want traffic anymore.
1440 * @param t Tunnel which should be ignored.
1442 * FIXME when to delete an incoming tunnel?
1445 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1447 struct GNUNET_HashCode hash;
1449 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1450 GNUNET_break (GNUNET_YES ==
1451 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1453 GNUNET_break (GNUNET_YES ==
1454 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1456 tunnel_delete_active_client (t, c);
1457 GNUNET_array_append (t->ignore, t->nignore, c);
1462 * Deletes a tunnel from a client (either owner or destination). To be used on
1463 * tunnel destroy, otherwise, use client_ignore_tunnel.
1465 * @param c Client whose tunnel to delete.
1466 * @param t Tunnel which should be deleted.
1469 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1471 struct GNUNET_HashCode hash;
1475 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1476 GNUNET_assert (GNUNET_YES ==
1477 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1483 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1485 GNUNET_assert (GNUNET_YES ==
1486 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1490 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1499 * Send the message to all clients that have subscribed to its type
1501 * @param msg Pointer to the message itself
1502 * @param payload Pointer to the payload of the message.
1503 * @return number of clients this message was sent to
1506 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1507 const struct GNUNET_MessageHeader *payload)
1509 struct GNUNET_PeerIdentity *oid;
1510 struct MeshClient *c;
1511 struct MeshTunnel *t;
1512 MESH_TunnelNumber *tid;
1515 char cbuf[htons (msg->size)];
1517 type = ntohs (payload->type);
1518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1521 memcpy (cbuf, msg, sizeof (cbuf));
1522 switch (htons (msg->type))
1524 struct GNUNET_MESH_Unicast *uc;
1525 struct GNUNET_MESH_Multicast *mc;
1526 struct GNUNET_MESH_ToOrigin *to;
1528 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1529 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1533 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1534 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1538 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1539 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1547 t = tunnel_get (oid, ntohl (*tid));
1554 for (count = 0, c = clients; c != NULL; c = c->next)
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1557 if (client_is_subscribed (type, c))
1559 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1563 *tid = htonl (t->local_tid);
1567 if (GNUNET_NO == client_knows_tunnel (c, t))
1569 /* This client doesn't know the tunnel */
1570 struct GNUNET_MESH_TunnelNotification tmsg;
1571 struct GNUNET_HashCode hash;
1573 tmsg.header.size = htons (sizeof (tmsg));
1574 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1575 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1576 tmsg.tunnel_id = htonl (t->local_tid_dest);
1577 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1578 &tmsg.header, GNUNET_NO);
1579 GNUNET_array_append (t->clients, t->nclients, c);
1580 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1582 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1583 c->incoming_tunnels, &hash, t,
1584 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1586 *tid = htonl (t->local_tid_dest);
1589 /* Check if the client wants to get traffic from the tunnel */
1590 if (GNUNET_NO == client_wants_tunnel(c, t))
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1594 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1595 (struct GNUNET_MessageHeader
1596 *) cbuf, GNUNET_YES);
1604 * Notify the client that owns the tunnel that a peer has connected to it
1605 * (the requested path to it has been confirmed).
1607 * @param t Tunnel whose owner to notify
1608 * @param id Short id of the peer that has connected
1611 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1613 struct GNUNET_MESH_PeerControl pc;
1615 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1616 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1617 pc.tunnel_id = htonl (t->local_tid);
1618 GNUNET_PEER_resolve (id, &pc.peer);
1619 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1625 * Notify all clients (not depending on registration status) that the incoming
1626 * tunnel is no longer valid.
1628 * @param t Tunnel that was destroyed.
1631 send_clients_tunnel_destroy (struct MeshTunnel *t)
1633 struct GNUNET_MESH_TunnelMessage msg;
1635 msg.header.size = htons (sizeof (msg));
1636 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1637 msg.tunnel_id = htonl (t->local_tid_dest);
1638 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1643 * Notify clients of tunnel disconnections, if needed.
1644 * In case the origin disconnects, the destination clients get a tunnel destroy
1645 * notification. If the last destination disconnects (only one remaining client
1646 * in tunnel), the origin gets a (local ID) peer disconnected.
1647 * Note that the function must be called BEFORE removing the client from
1650 * @param t Tunnel that was destroyed.
1651 * @param c Client that disconnected.
1654 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1660 struct GNUNET_MESH_TunnelMessage msg;
1662 msg.header.size = htons (sizeof (msg));
1663 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1664 msg.tunnel_id = htonl (t->local_tid_dest);
1665 for (i = 0; i < t->nclients; i++)
1666 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1667 &msg.header, GNUNET_NO);
1669 // FIXME when to disconnect an incoming tunnel?
1670 else if (1 == t->nclients && NULL != t->owner)
1672 struct GNUNET_MESH_PeerControl msg;
1674 msg.header.size = htons (sizeof (msg));
1675 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1676 msg.tunnel_id = htonl (t->local_tid);
1677 msg.peer = my_full_id;
1678 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1679 &msg.header, GNUNET_NO);
1685 * Decrements the reference counter and frees all resources if needed
1687 * @param mesh_data Data Descriptor used in a multicast message.
1688 * Freed no longer needed (last message).
1691 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1693 /* Make sure it's a multicast packet */
1694 GNUNET_assert (NULL != mesh_data->reference_counter);
1696 if (0 == --(*(mesh_data->reference_counter)))
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1699 if (NULL != mesh_data->task)
1701 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1703 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1705 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1707 GNUNET_free (mesh_data->task);
1709 GNUNET_free (mesh_data->reference_counter);
1710 GNUNET_free (mesh_data->data);
1711 GNUNET_free (mesh_data);
1717 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1718 * and insert it in the appropiate structures if the peer is not known yet.
1720 * @param peer Full identity of the peer.
1722 * @return Existing or newly created peer info.
1724 static struct MeshPeerInfo *
1725 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1727 struct MeshPeerInfo *peer_info;
1729 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1730 if (NULL == peer_info)
1733 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1734 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1735 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1736 peer_info->id = GNUNET_PEER_intern (peer);
1744 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1745 * and insert it in the appropiate structures if the peer is not known yet.
1747 * @param peer Short identity of the peer.
1749 * @return Existing or newly created peer info.
1751 static struct MeshPeerInfo *
1752 peer_info_get_short (const GNUNET_PEER_Id peer)
1754 struct GNUNET_PeerIdentity id;
1756 GNUNET_PEER_resolve (peer, &id);
1757 return peer_info_get (&id);
1762 * Iterator to remove the tunnel from the list of tunnels a peer participates
1765 * @param cls Closure (tunnel info)
1766 * @param key GNUNET_PeerIdentity of the peer (unused)
1767 * @param value PeerInfo of the peer
1769 * @return always GNUNET_YES, to keep iterating
1772 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1774 struct MeshTunnel *t = cls;
1775 struct MeshPeerInfo *peer = value;
1778 for (i = 0; i < peer->ntunnels; i++)
1781 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1784 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1785 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1794 * Queue and pass message to core when possible.
1796 * @param cls Closure (type dependant).
1797 * @param type Type of the message.
1798 * @param size Size of the message.
1799 * @param dst Neighbor to send message to.
1800 * @param t Tunnel this message belongs to.
1803 queue_add (void *cls, uint16_t type, size_t size,
1804 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1807 * Core callback to write a pre-constructed data packet to core buffer
1809 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1810 * @param size Number of bytes available in buf.
1811 * @param buf Where the to write the message.
1813 * @return number of bytes written to buf
1816 send_core_data_raw (void *cls, size_t size, void *buf)
1818 struct MeshTransmissionDescriptor *info = cls;
1819 struct GNUNET_MessageHeader *msg;
1822 GNUNET_assert (NULL != info);
1823 GNUNET_assert (NULL != info->mesh_data);
1824 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1825 total_size = ntohs (msg->size);
1827 if (total_size > size)
1832 memcpy (buf, msg, total_size);
1833 GNUNET_free (info->mesh_data);
1840 * Sends an already built unicast message to a peer, properly registrating
1841 * all used resources.
1843 * @param message Message to send. Function makes a copy of it.
1844 * @param peer Short ID of the neighbor whom to send the message.
1845 * @param t Tunnel on which this message is transmitted.
1848 send_message (const struct GNUNET_MessageHeader *message,
1849 const struct GNUNET_PeerIdentity *peer,
1850 struct MeshTunnel *t)
1852 struct MeshTransmissionDescriptor *info;
1853 struct MeshPeerInfo *neighbor;
1854 struct MeshPeerPath *p;
1857 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1859 size = ntohs (message->size);
1860 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1861 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1862 info->mesh_data->data = GNUNET_malloc (size);
1863 memcpy (info->mesh_data->data, message, size);
1864 info->mesh_data->data_len = size;
1865 neighbor = peer_info_get (peer);
1866 for (p = neighbor->path_head; NULL != p; p = p->next)
1876 GNUNET_free (info->mesh_data->data);
1877 GNUNET_free (info->mesh_data);
1881 info->peer = neighbor;
1883 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
1891 * Sends a CREATE PATH message for a path to a peer, properly registrating
1892 * all used resources.
1894 * @param peer PeerInfo of the final peer for whom this path is being created.
1895 * @param p Path itself.
1896 * @param t Tunnel for which the path is created.
1899 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1900 struct MeshTunnel *t)
1902 struct GNUNET_PeerIdentity id;
1903 struct MeshPathInfo *path_info;
1904 struct MeshPeerInfo *neighbor;
1910 p = tree_get_path_to_peer (t->tree, peer->id);
1917 for (i = 0; i < p->length; i++)
1919 if (p->peers[i] == myid)
1922 if (i >= p->length - 1)
1928 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1930 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1931 path_info->path = p;
1933 neighbor = peer_info_get (&id);
1934 path_info->peer = neighbor;
1935 queue_add (path_info,
1936 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1937 sizeof (struct GNUNET_MESH_ManipulatePath) +
1938 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1945 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1947 * @param t Tunnel whose path to destroy.
1948 * @param destination Short ID of the peer to whom the path to destroy.
1951 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1953 struct MeshPeerPath *p;
1956 p = tree_get_path_to_peer (t->tree, destination);
1962 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1963 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1965 struct GNUNET_MESH_ManipulatePath *msg;
1966 struct GNUNET_PeerIdentity *pi;
1970 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1971 msg->header.size = htons (size);
1972 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1973 msg->tid = htonl (t->id.tid);
1974 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1975 for (i = 0; i < p->length; i++)
1977 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1979 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1986 * Try to establish a new connection to this peer.
1987 * Use the best path for the given tunnel.
1988 * If the peer doesn't have any path to it yet, try to get one.
1989 * If the peer already has some path, send a CREATE PATH towards it.
1991 * @param peer PeerInfo of the peer.
1992 * @param t Tunnel for which to create the path, if possible.
1995 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1997 struct MeshPeerPath *p;
1998 struct MeshPathInfo *path_info;
2000 if (NULL != peer->path_head)
2002 p = tree_get_path_to_peer (t->tree, peer->id);
2009 // FIXME always send create path to self
2012 send_create_path (peer, p, t);
2016 struct GNUNET_HashCode hash;
2019 send_client_peer_connected (t, myid);
2020 t->local_tid_dest = next_local_tid++;
2021 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2024 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2025 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2032 else if (NULL == peer->dhtget)
2034 struct GNUNET_PeerIdentity id;
2036 GNUNET_PEER_resolve (peer->id, &id);
2037 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2038 path_info->peer = peer;
2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2041 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2042 peer->dhtgetcls = path_info;
2043 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2044 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2045 &id.hashPubKey, /* key to search */
2046 DHT_REPLICATION_LEVEL, /* replication level */
2047 GNUNET_DHT_RO_RECORD_ROUTE |
2048 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2049 NULL, /* xquery */ // FIXME BLOOMFILTER
2050 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2051 &dht_get_id_handler, path_info);
2053 /* Otherwise, there is no path but the DHT get is already started. */
2058 * Task to delay the connection of a peer
2060 * @param cls Closure (path info with tunnel and peer to connect).
2061 * Will be free'd on exection.
2062 * @param tc TaskContext
2065 peer_info_connect_task (void *cls,
2066 const struct GNUNET_SCHEDULER_TaskContext *tc)
2068 struct MeshPathInfo *path_info = cls;
2070 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2075 peer_info_connect (path_info->peer, path_info->t);
2081 * Destroy the peer_info and free any allocated resources linked to it
2083 * @param pi The peer_info to destroy.
2085 * @return GNUNET_OK on success
2088 peer_info_destroy (struct MeshPeerInfo *pi)
2090 struct GNUNET_PeerIdentity id;
2091 struct MeshPeerPath *p;
2092 struct MeshPeerPath *nextp;
2094 GNUNET_PEER_resolve (pi->id, &id);
2095 GNUNET_PEER_change_rc (pi->id, -1);
2098 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2102 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2104 if (NULL != pi->dhtget)
2106 GNUNET_DHT_get_stop (pi->dhtget);
2107 GNUNET_free (pi->dhtgetcls);
2113 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2123 * Remove all paths that rely on a direct connection between p1 and p2
2124 * from the peer itself and notify all tunnels about it.
2126 * @param peer PeerInfo of affected peer.
2127 * @param p1 GNUNET_PEER_Id of one peer.
2128 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2131 * TODO: optimize (see below)
2134 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2137 struct MeshPeerPath *p;
2138 struct MeshPeerPath *aux;
2139 struct MeshPeerInfo *peer_d;
2141 unsigned int destroyed;
2147 p = peer->path_head;
2151 for (i = 0; i < (p->length - 1); i++)
2153 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2154 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2156 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2167 for (i = 0; i < peer->ntunnels; i++)
2169 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2173 * Problem: one or more peers have been deleted from the tunnel tree.
2174 * We don't know who they are to try to add them again.
2175 * We need to try to find a new path for each of the disconnected peers.
2176 * Some of them might already have a path to reach them that does not
2177 * involve p1 and p2. Adding all anew might render in a better tree than
2178 * the trivial immediate fix.
2180 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2181 * its children will be reachable trough him.
2183 peer_d = peer_info_get_short (d);
2186 for (p = peer_d->path_head; NULL != p; p = p->next)
2188 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2196 /* No callback, as peer will be already disconnected and a connection
2197 * scheduled by tunnel_notify_connection_broken.
2199 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2203 peer_info_connect (peer_d, peer->tunnels[i]);
2210 * Add the path to the peer and update the path used to reach it in case this
2213 * @param peer_info Destination peer to add the path to.
2214 * @param path New path to add. Last peer must be the peer in arg 1.
2215 * Path will be either used of freed if already known.
2216 * @param trusted Do we trust that this path is real?
2219 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2222 struct MeshPeerPath *aux;
2226 if ((NULL == peer_info) || (NULL == path))
2229 path_destroy (path);
2232 if (path->peers[path->length - 1] != peer_info->id)
2235 path_destroy (path);
2238 if (path->length <= 2 && GNUNET_NO == trusted)
2240 /* Only allow CORE to tell us about direct paths */
2241 path_destroy (path);
2244 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2245 for (l = 1; l < path->length; l++)
2247 if (path->peers[l] == myid)
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2250 for (l2 = 0; l2 < path->length - l; l2++)
2252 path->peers[l2] = path->peers[l + l2];
2257 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2262 struct GNUNET_PeerIdentity id;
2264 GNUNET_PEER_resolve (peer_info->id, &id);
2265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2266 path->length, GNUNET_i2s (&id));
2269 l = path_get_length (path);
2276 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2277 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2279 l2 = path_get_length (aux);
2282 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2283 peer_info->path_tail, aux, path);
2288 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2290 path_destroy (path);
2295 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2302 * Add the path to the origin peer and update the path used to reach it in case
2303 * this is the shortest.
2304 * The path is given in peer_info -> destination, therefore we turn the path
2305 * upside down first.
2307 * @param peer_info Peer to add the path to, being the origin of the path.
2308 * @param path New path to add after being inversed.
2309 * @param trusted Do we trust that this path is real?
2312 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2313 struct MeshPeerPath *path, int trusted)
2316 peer_info_add_path (peer_info, path, trusted);
2321 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2322 * to obtain a local peer -> destination path and interning the peer ids.
2324 * @return Newly allocated and created path
2326 static struct MeshPeerPath *
2327 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2328 unsigned int get_path_length,
2329 const struct GNUNET_PeerIdentity *put_path,
2330 unsigned int put_path_length)
2332 struct MeshPeerPath *p;
2338 GNUNET_PEER_change_rc (myid, 1);
2339 i = get_path_length;
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2341 for (i--; i >= 0; i--)
2343 id = GNUNET_PEER_intern (&get_path[i]);
2344 if (p->length > 0 && id == p->peers[p->length - 1])
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2347 GNUNET_PEER_change_rc (id, -1);
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2352 GNUNET_i2s (&get_path[i]));
2354 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2355 p->peers[p->length - 1] = id;
2358 i = put_path_length;
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2360 for (i--; i >= 0; i--)
2362 id = GNUNET_PEER_intern (&put_path[i]);
2365 /* PUT path went through us, so discard the path up until now and start
2366 * from here to get a much shorter (and loop-free) path.
2371 if (p->length > 0 && id == p->peers[p->length - 1])
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2374 GNUNET_PEER_change_rc (id, -1);
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2379 GNUNET_i2s (&put_path[i]));
2381 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2382 p->peers[p->length - 1] = id;
2386 if (get_path_length > 0)
2387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2388 GNUNET_i2s (&get_path[0]));
2389 if (put_path_length > 0)
2390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2391 GNUNET_i2s (&put_path[0]));
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2394 for (i = 0; i < p->length; i++)
2396 struct GNUNET_PeerIdentity peer_id;
2398 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2400 GNUNET_i2s (&peer_id));
2408 * Adds a path to the peer_infos of all the peers in the path
2410 * @param p Path to process.
2411 * @param confirmed Whether we know if the path works or not. FIXME use
2414 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2418 /* TODO: invert and add */
2419 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2420 for (i++; i < p->length; i++)
2422 struct MeshPeerInfo *aux;
2423 struct MeshPeerPath *copy;
2425 aux = peer_info_get_short (p->peers[i]);
2426 copy = path_duplicate (p);
2427 copy->length = i + 1;
2428 peer_info_add_path (aux, copy, GNUNET_NO);
2434 * Send keepalive packets for a peer
2436 * @param cls Closure (tunnel for which to send the keepalive).
2437 * @param tc Notification context.
2439 * TODO: implement explicit multicast keepalive?
2442 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2446 * Search for a tunnel among the incoming tunnels
2448 * @param tid the local id of the tunnel
2450 * @return tunnel handler, NULL if doesn't exist
2452 static struct MeshTunnel *
2453 tunnel_get_incoming (MESH_TunnelNumber tid)
2455 struct GNUNET_HashCode hash;
2457 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2458 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2459 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2464 * Search for a tunnel among the tunnels for a client
2466 * @param c the client whose tunnels to search in
2467 * @param tid the local id of the tunnel
2469 * @return tunnel handler, NULL if doesn't exist
2471 static struct MeshTunnel *
2472 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2474 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2476 return tunnel_get_incoming (tid);
2480 struct GNUNET_HashCode hash;
2482 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2483 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2489 * Search for a tunnel by global ID using PEER_ID
2491 * @param pi owner of the tunnel
2492 * @param tid global tunnel number
2494 * @return tunnel handler, NULL if doesn't exist
2496 static struct MeshTunnel *
2497 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2499 struct MESH_TunnelID id;
2500 struct GNUNET_HashCode hash;
2505 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2506 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2511 * Search for a tunnel by global ID using full PeerIdentities
2513 * @param oid owner of the tunnel
2514 * @param tid global tunnel number
2516 * @return tunnel handler, NULL if doesn't exist
2518 static struct MeshTunnel *
2519 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2521 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2526 * Delete an active client from the tunnel.
2532 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2536 for (i = 0; i < t->nclients; i++)
2538 if (t->clients[i] == c)
2540 t->clients[i] = t->clients[t->nclients - 1];
2541 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2549 * Delete an ignored client from the tunnel.
2555 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2559 for (i = 0; i < t->nignore; i++)
2561 if (t->ignore[i] == c)
2563 t->ignore[i] = t->ignore[t->nignore - 1];
2564 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2572 * Delete a client from the tunnel. It should be only done on
2573 * client disconnection, otherwise use client_ignore_tunnel.
2579 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2581 tunnel_delete_ignored_client (t, c);
2582 tunnel_delete_active_client (t, c);
2587 * Callback used to notify a client owner of a tunnel that a peer has
2588 * disconnected, most likely because of a path change.
2590 * @param cls Closure (tunnel this notification is about).
2591 * @param peer_id Short ID of disconnected peer.
2594 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2596 struct MeshTunnel *t = cls;
2597 struct MeshPeerInfo *peer;
2598 struct MeshPathInfo *path_info;
2600 if (NULL != t->owner && NULL != nc)
2602 struct GNUNET_MESH_PeerControl msg;
2604 msg.header.size = htons (sizeof (msg));
2605 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2606 msg.tunnel_id = htonl (t->local_tid);
2607 GNUNET_PEER_resolve (peer_id, &msg.peer);
2608 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2609 &msg.header, GNUNET_NO);
2611 peer = peer_info_get_short (peer_id);
2612 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2613 path_info->peer = peer;
2615 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2620 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2621 * needed rescources.
2622 * If peer already exists, reevaluate shortest path and change if different.
2624 * @param t Tunnel we want to add a new peer to
2625 * @param peer PeerInfo of the peer being added
2629 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2631 struct GNUNET_PeerIdentity id;
2632 struct MeshPeerPath *best_p;
2633 struct MeshPeerPath *p;
2634 unsigned int best_cost;
2637 GNUNET_PEER_resolve (peer->id, &id);
2639 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2642 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2643 GNUNET_assert (GNUNET_OK ==
2644 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2646 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2649 if (NULL != (p = peer->path_head))
2652 best_cost = tree_get_path_cost (t->tree, p);
2655 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2662 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2663 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2664 t->path_refresh_task =
2665 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2669 /* Start a DHT get */
2670 peer_info_connect (peer, t);
2675 * Add a path to a tunnel which we don't own, just to remember the next hop.
2676 * If destination node was already in the tunnel, the first hop information
2677 * will be replaced with the new path.
2679 * @param t Tunnel we want to add a new peer to
2680 * @param p Path to add
2681 * @param own_pos Position of local node in path.
2685 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2686 unsigned int own_pos)
2688 struct GNUNET_PeerIdentity id;
2690 GNUNET_assert (0 != own_pos);
2691 tree_add_path (t->tree, p, NULL, NULL);
2692 if (own_pos < p->length - 1)
2694 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2695 tree_update_first_hops (t->tree, myid, &id);
2701 * Notifies a tunnel that a connection has broken that affects at least
2702 * some of its peers. Sends a notification towards the root of the tree.
2703 * In case the peer is the owner of the tree, notifies the client that owns
2704 * the tunnel and tries to reconnect.
2706 * @param t Tunnel affected.
2707 * @param p1 Peer that got disconnected from p2.
2708 * @param p2 Peer that got disconnected from p1.
2710 * @return Short ID of the peer disconnected (either p1 or p2).
2711 * 0 if the tunnel remained unaffected.
2713 static GNUNET_PEER_Id
2714 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2720 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2722 if (myid != p1 && myid != p2)
2728 if (tree_get_predecessor (t->tree) != 0)
2730 /* We are the peer still connected, notify owner of the disconnection. */
2731 struct GNUNET_MESH_PathBroken msg;
2732 struct GNUNET_PeerIdentity neighbor;
2734 msg.header.size = htons (sizeof (msg));
2735 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2736 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2737 msg.tid = htonl (t->id.tid);
2738 msg.peer1 = my_full_id;
2739 GNUNET_PEER_resolve (pid, &msg.peer2);
2740 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2741 send_message (&msg.header, &neighbor, t);
2749 * Send a multicast packet to a neighbor.
2751 * @param cls Closure (Info about the multicast packet)
2752 * @param neighbor_id Short ID of the neighbor to send the packet to.
2755 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2757 struct MeshData *mdata = cls;
2758 struct MeshTransmissionDescriptor *info;
2759 struct GNUNET_PeerIdentity neighbor;
2761 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2763 info->mesh_data = mdata;
2764 (*(mdata->reference_counter)) ++;
2765 info->destination = neighbor_id;
2766 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2768 GNUNET_i2s (&neighbor));
2769 info->peer = peer_info_get (&neighbor);
2770 GNUNET_assert (NULL != info->peer);
2772 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2773 info->mesh_data->data_len,
2779 * Send a message in a tunnel in multicast, sending a copy to each child node
2780 * down the local one in the tunnel tree.
2782 * @param t Tunnel in which to send the data.
2783 * @param msg Message to be sent.
2784 * @param internal Has the service generated this message?
2787 tunnel_send_multicast (struct MeshTunnel *t,
2788 const struct GNUNET_MessageHeader *msg,
2791 struct MeshData *mdata;
2793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2794 " sending a multicast packet...\n");
2795 mdata = GNUNET_malloc (sizeof (struct MeshData));
2796 mdata->data_len = ntohs (msg->size);
2797 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2799 mdata->data = GNUNET_malloc (mdata->data_len);
2800 memcpy (mdata->data, msg, mdata->data_len);
2801 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2803 struct GNUNET_MESH_Multicast *mcast;
2805 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2806 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2808 ntohl (mcast->ttl));
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2814 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2815 && GNUNET_NO == internal)
2817 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2819 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2825 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2826 if (*(mdata->reference_counter) == 0)
2828 GNUNET_free (mdata->data);
2829 GNUNET_free (mdata->reference_counter);
2830 if (NULL != mdata->task)
2832 GNUNET_SCHEDULER_cancel(*(mdata->task));
2833 GNUNET_free (mdata->task);
2834 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2836 // FIXME change order?
2837 GNUNET_free (mdata);
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2840 " sending a multicast packet done\n");
2846 * Send a message to all peers in this tunnel that the tunnel is no longer
2849 * @param t The tunnel whose peers to notify.
2852 tunnel_send_destroy (struct MeshTunnel *t)
2854 struct GNUNET_MESH_TunnelDestroy msg;
2856 msg.header.size = htons (sizeof (msg));
2857 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2858 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2859 msg.tid = htonl (t->id.tid);
2860 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2866 * Destroy the tunnel and free any allocated resources linked to it.
2868 * @param t the tunnel to destroy
2870 * @return GNUNET_OK on success
2873 tunnel_destroy (struct MeshTunnel *t)
2875 struct MeshClient *c;
2876 struct GNUNET_HashCode hash;
2887 struct GNUNET_PeerIdentity id;
2889 GNUNET_PEER_resolve (t->id.oid, &id);
2890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2891 GNUNET_i2s (&id), t->id.tid);
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2897 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2898 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2903 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2906 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2910 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2911 for (i = 0; i < t->nclients; i++)
2915 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2920 for (i = 0; i < t->nignore; i++)
2924 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2929 if (t->nclients > 0)
2932 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2936 GNUNET_free (t->clients);
2938 if (NULL != t->peers)
2940 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2942 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2945 tree_destroy (t->tree);
2946 if (NULL != t->regex_ctx)
2947 regex_cancel_search (t->regex_ctx);
2948 if (NULL != t->dht_get_type)
2949 GNUNET_DHT_get_stop (t->dht_get_type);
2950 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2951 GNUNET_SCHEDULER_cancel (t->timeout_task);
2952 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2953 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2960 * Create a new tunnel
2962 * @param owner Who is the owner of the tunnel (short ID).
2963 * @param tid Tunnel Number of the tunnel.
2964 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2965 * @param local Tunnel Number for the tunnel, for the client point of view.
2968 static struct MeshTunnel *
2969 tunnel_new (GNUNET_PEER_Id owner,
2970 MESH_TunnelNumber tid,
2971 struct MeshClient *client,
2972 MESH_TunnelNumber local)
2974 struct MeshTunnel *t;
2975 struct GNUNET_HashCode hash;
2977 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2980 t->queue_max = 1000; // FIXME API parameter
2981 t->tree = tree_new (owner);
2983 t->local_tid = local;
2985 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2987 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2988 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2993 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2999 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3001 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3002 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3006 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3016 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3017 * that are no longer needed, as well as nodes of no longer reachable peers.
3018 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3020 * @param t Tunnel from which to remove the path.
3021 * @param peer Short id of the peer which should be removed.
3024 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3026 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3032 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3033 * client when the client disconnects. If the client is not the owner, the
3034 * owner will get notified if no more clients are in the tunnel and the client
3035 * get removed from the tunnel's list.
3037 * @param cls closure (client that is disconnecting)
3038 * @param key the hash of the local tunnel id (used to access the hashmap)
3039 * @param value the value stored at the key (tunnel to destroy)
3041 * @return GNUNET_OK on success
3044 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3046 struct MeshTunnel *t = value;
3047 struct MeshClient *c = cls;
3050 send_client_tunnel_disconnect(t, c);
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3054 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3055 tunnel_delete_client(t, c);
3056 client_delete_tunnel(c, t);
3059 tunnel_send_destroy(t);
3060 r = tunnel_destroy (t);
3066 * Timeout function, destroys tunnel if called
3068 * @param cls Closure (tunnel to destroy).
3069 * @param tc TaskContext
3072 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3074 struct MeshTunnel *t = cls;
3076 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3078 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3083 * Resets the tunnel timeout. Starts it if no timeout was running.
3085 * @param t Tunnel whose timeout to reset.
3088 tunnel_reset_timeout (struct MeshTunnel *t)
3090 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3091 GNUNET_SCHEDULER_cancel (t->timeout_task);
3093 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3094 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
3098 /******************************************************************************/
3099 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3100 /******************************************************************************/
3103 * Function to send a create path packet to a peer.
3105 * @param cls closure
3106 * @param size number of bytes available in buf
3107 * @param buf where the callee should write the message
3108 * @return number of bytes written to buf
3111 send_core_path_create (void *cls, size_t size, void *buf)
3113 struct MeshPathInfo *info = cls;
3114 struct GNUNET_MESH_ManipulatePath *msg;
3115 struct GNUNET_PeerIdentity *peer_ptr;
3116 struct MeshTunnel *t = info->t;
3117 struct MeshPeerPath *p = info->path;
3121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3123 sizeof (struct GNUNET_MESH_ManipulatePath) +
3124 p->length * sizeof (struct GNUNET_PeerIdentity);
3126 if (size < size_needed || NULL == buf)
3131 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3132 msg->header.size = htons (size_needed);
3133 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3134 msg->tid = ntohl (t->id.tid);
3136 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3137 for (i = 0; i < p->length; i++)
3139 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3146 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3152 * Fill the core buffer
3154 * @param cls closure (data itself)
3155 * @param size number of bytes available in buf
3156 * @param buf where the callee should write the message
3158 * @return number of bytes written to buf
3161 send_core_data_multicast (void *cls, size_t size, void *buf)
3163 struct MeshTransmissionDescriptor *info = cls;
3166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3167 GNUNET_assert (NULL != info);
3168 GNUNET_assert (NULL != info->peer);
3169 total_size = info->mesh_data->data_len;
3170 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3172 if (total_size > size)
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3178 memcpy (buf, info->mesh_data->data, total_size);
3181 struct GNUNET_MESH_Multicast *mc;
3182 struct GNUNET_MessageHeader *mh;
3185 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3187 mc = (struct GNUNET_MESH_Multicast *) mh;
3188 mh = (struct GNUNET_MessageHeader *) &mc[1];
3189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3190 " multicast, payload type %u\n", ntohs (mh->type));
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3192 " multicast, payload size %u\n", ntohs (mh->size));
3196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3201 data_descriptor_decrement_multicast (info->mesh_data);
3202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3210 * Creates a path ack message in buf and frees all unused resources.
3212 * @param cls closure (MeshTransmissionDescriptor)
3213 * @param size number of bytes available in buf
3214 * @param buf where the callee should write the message
3215 * @return number of bytes written to buf
3218 send_core_path_ack (void *cls, size_t size, void *buf)
3220 struct MeshTransmissionDescriptor *info = cls;
3221 struct GNUNET_MESH_PathACK *msg = buf;
3223 GNUNET_assert (NULL != info);
3224 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3229 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3230 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3231 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3232 msg->tid = htonl (info->origin->tid);
3233 msg->peer_id = my_full_id;
3236 /* TODO add signature */
3238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3239 return sizeof (struct GNUNET_MESH_PathACK);
3244 * Free a transmission that was already queued with all resources
3245 * associated to the request.
3247 * @param queue Queue handler to cancel.
3248 * @param clear_cls Is it necessary to free associated cls?
3251 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3253 struct MeshTransmissionDescriptor *dd;
3254 struct MeshPathInfo *path_info;
3256 if (GNUNET_YES == clear_cls)
3258 switch (queue->type)
3260 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3261 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
3264 data_descriptor_decrement_multicast (dd->mesh_data);
3266 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3268 path_info = queue->cls;
3269 path_destroy (path_info->path);
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
3275 GNUNET_free_non_null (queue->cls);
3277 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3278 queue->peer->queue_tail,
3280 GNUNET_free (queue);
3285 * Core callback to write a queued packet to core buffer
3287 * @param cls Closure (peer info).
3288 * @param size Number of bytes available in buf.
3289 * @param buf Where the to write the message.
3291 * @return number of bytes written to buf
3294 queue_send (void *cls, size_t size, void *buf)
3296 struct MeshPeerInfo *peer = cls;
3297 struct MeshPeerQueue *queue;
3300 peer->core_transmit = NULL;
3301 queue = peer->queue_head;
3303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3305 /* If queue is empty, send should have been cancelled */
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3313 /* Check if buffer size is enough for the message */
3314 if (queue->size > size)
3316 struct GNUNET_PeerIdentity id;
3318 GNUNET_PEER_resolve (peer->id, &id);
3319 peer->core_transmit =
3320 GNUNET_CORE_notify_transmit_ready(core_handle,
3323 GNUNET_TIME_UNIT_FOREVER_REL,
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3333 switch (queue->type)
3335 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
3337 data_size = send_core_data_raw (queue->cls, size, buf);
3339 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
3341 data_size = send_core_data_multicast(queue->cls, size, buf);
3343 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3345 data_size = send_core_path_create(queue->cls, size, buf);
3347 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3349 data_size = send_core_path_ack(queue->cls, size, buf);
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
3356 queue->tunnel->queue_n--;
3358 /* Free queue, but cls was freed by send_core_* */
3359 queue_destroy(queue, GNUNET_NO);
3361 /* If more data in queue, send next */
3362 if (NULL != peer->queue_head)
3364 struct GNUNET_PeerIdentity id;
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3367 GNUNET_PEER_resolve (peer->id, &id);
3368 peer->core_transmit =
3369 GNUNET_CORE_notify_transmit_ready(core_handle,
3372 GNUNET_TIME_UNIT_FOREVER_REL,
3374 peer->queue_head->size,
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3384 * Queue and pass message to core when possible.
3386 * @param cls Closure (type dependant).
3387 * @param type Type of the message.
3388 * @param size Size of the message.
3389 * @param dst Neighbor to send message to.
3390 * @param t Tunnel this message belongs to.
3393 queue_add (void *cls, uint16_t type, size_t size,
3394 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3396 struct MeshPeerQueue *queue;
3398 if (t->queue_n >= t->queue_max)
3400 if (NULL == t->owner)
3401 GNUNET_break_op(0); // TODO: kill connection?
3404 return; // Drop message
3407 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3413 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3414 if (NULL == dst->core_transmit)
3416 struct GNUNET_PeerIdentity id;
3418 GNUNET_PEER_resolve (dst->id, &id);
3419 dst->core_transmit =
3420 GNUNET_CORE_notify_transmit_ready(core_handle,
3423 GNUNET_TIME_UNIT_FOREVER_REL,
3432 /******************************************************************************/
3433 /******************** MESH NETWORK HANDLERS **************************/
3434 /******************************************************************************/
3438 * Core handler for path creation
3440 * @param cls closure
3441 * @param message message
3442 * @param peer peer identity this notification is about
3443 * @param atsi performance data
3444 * @param atsi_count number of records in 'atsi'
3446 * @return GNUNET_OK to keep the connection open,
3447 * GNUNET_SYSERR to close it (signal serious error)
3450 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3451 const struct GNUNET_MessageHeader *message,
3452 const struct GNUNET_ATS_Information *atsi,
3453 unsigned int atsi_count)
3455 unsigned int own_pos;
3458 MESH_TunnelNumber tid;
3459 struct GNUNET_MESH_ManipulatePath *msg;
3460 struct GNUNET_PeerIdentity *pi;
3461 struct GNUNET_HashCode hash;
3462 struct MeshPeerPath *path;
3463 struct MeshPeerInfo *dest_peer_info;
3464 struct MeshPeerInfo *orig_peer_info;
3465 struct MeshTunnel *t;
3467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3468 "Received a path create msg [%s]\n",
3469 GNUNET_i2s (&my_full_id));
3470 size = ntohs (message->size);
3471 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3473 GNUNET_break_op (0);
3477 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3478 if (size % sizeof (struct GNUNET_PeerIdentity))
3480 GNUNET_break_op (0);
3483 size /= sizeof (struct GNUNET_PeerIdentity);
3486 GNUNET_break_op (0);
3489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3490 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3492 tid = ntohl (msg->tid);
3493 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3495 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3496 t = tunnel_get (pi, tid);
3497 if (NULL == t) // FIXME only for INCOMING tunnels?
3499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3500 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3502 while (NULL != tunnel_get_incoming (next_local_tid))
3503 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3504 t->local_tid_dest = next_local_tid++;
3505 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3507 tunnel_reset_timeout (t);
3508 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3510 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3511 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3519 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3520 if (NULL == dest_peer_info)
3522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3523 " Creating PeerInfo for destination.\n");
3524 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3525 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3526 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3528 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3530 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3531 if (NULL == orig_peer_info)
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3534 " Creating PeerInfo for origin.\n");
3535 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3536 orig_peer_info->id = GNUNET_PEER_intern (pi);
3537 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3538 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3541 path = path_new (size);
3543 for (i = 0; i < size; i++)
3545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3546 GNUNET_i2s (&pi[i]));
3547 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3548 if (path->peers[i] == myid)
3551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3554 /* cannot be self, must be 'not found' */
3555 /* create path: self not found in path through self */
3556 GNUNET_break_op (0);
3557 path_destroy (path);
3558 /* FIXME error. destroy tunnel? leave for timeout? */
3561 path_add_to_peers (path, GNUNET_NO);
3562 tunnel_add_path (t, path, own_pos);
3563 if (own_pos == size - 1)
3565 /* It is for us! Send ack. */
3566 struct MeshTransmissionDescriptor *info;
3568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3569 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3570 if (NULL == t->peers)
3572 /* New tunnel! Notify clients on data. */
3573 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
3575 GNUNET_break (GNUNET_OK ==
3576 GNUNET_CONTAINER_multihashmap_put (t->peers,
3577 &my_full_id.hashPubKey,
3580 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
3581 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3582 info->origin = &t->id;
3583 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3584 GNUNET_assert (NULL != info->peer);
3586 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3587 sizeof (struct GNUNET_MESH_PathACK),
3593 struct MeshPeerPath *path2;
3595 /* It's for somebody else! Retransmit. */
3596 path2 = path_duplicate (path);
3597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3598 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3599 path2 = path_duplicate (path);
3600 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3601 send_create_path (dest_peer_info, path, t);
3608 * Core handler for path destruction
3610 * @param cls closure
3611 * @param message message
3612 * @param peer peer identity this notification is about
3613 * @param atsi performance data
3614 * @param atsi_count number of records in 'atsi'
3616 * @return GNUNET_OK to keep the connection open,
3617 * GNUNET_SYSERR to close it (signal serious error)
3620 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3621 const struct GNUNET_MessageHeader *message,
3622 const struct GNUNET_ATS_Information *atsi,
3623 unsigned int atsi_count)
3625 struct GNUNET_MESH_ManipulatePath *msg;
3626 struct GNUNET_PeerIdentity *pi;
3627 struct MeshPeerPath *path;
3628 struct MeshTunnel *t;
3629 unsigned int own_pos;
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3634 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3635 size = ntohs (message->size);
3636 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3638 GNUNET_break_op (0);
3642 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3643 if (size % sizeof (struct GNUNET_PeerIdentity))
3645 GNUNET_break_op (0);
3648 size /= sizeof (struct GNUNET_PeerIdentity);
3651 GNUNET_break_op (0);
3654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3656 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3657 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3659 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3661 t = tunnel_get (pi, ntohl (msg->tid));
3664 /* TODO notify back: we don't know this tunnel */
3665 GNUNET_break_op (0);
3668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3669 path = path_new (size);
3671 for (i = 0; i < size; i++)
3673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3674 GNUNET_i2s (&pi[i]));
3675 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3676 if (path->peers[i] == myid)
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3680 if (own_pos < path->length - 1)
3681 send_message (message, &pi[own_pos + 1], t);
3683 send_client_tunnel_disconnect(t, NULL);
3685 tunnel_delete_peer (t, path->peers[path->length - 1]);
3686 path_destroy (path);
3692 * Core handler for notifications of broken paths
3694 * @param cls closure
3695 * @param message message
3696 * @param peer peer identity this notification is about
3697 * @param atsi performance data
3698 * @param atsi_count number of records in 'atsi'
3700 * @return GNUNET_OK to keep the connection open,
3701 * GNUNET_SYSERR to close it (signal serious error)
3704 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3705 const struct GNUNET_MessageHeader *message,
3706 const struct GNUNET_ATS_Information *atsi,
3707 unsigned int atsi_count)
3709 struct GNUNET_MESH_PathBroken *msg;
3710 struct MeshTunnel *t;
3712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3713 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3714 msg = (struct GNUNET_MESH_PathBroken *) message;
3715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3716 GNUNET_i2s (&msg->peer1));
3717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3718 GNUNET_i2s (&msg->peer2));
3719 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3722 GNUNET_break_op (0);
3725 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3726 GNUNET_PEER_search (&msg->peer2));
3733 * Core handler for tunnel destruction
3735 * @param cls closure
3736 * @param message message
3737 * @param peer peer identity this notification is about
3738 * @param atsi performance data
3739 * @param atsi_count number of records in 'atsi'
3741 * @return GNUNET_OK to keep the connection open,
3742 * GNUNET_SYSERR to close it (signal serious error)
3745 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3746 const struct GNUNET_MessageHeader *message,
3747 const struct GNUNET_ATS_Information *atsi,
3748 unsigned int atsi_count)
3750 struct GNUNET_MESH_TunnelDestroy *msg;
3751 struct MeshTunnel *t;
3753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3754 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3755 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3757 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3758 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3761 /* Probably already got the message from another path,
3762 * destroyed the tunnel and retransmitted to children.
3767 if (t->id.oid == myid)
3769 GNUNET_break_op (0);
3772 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3774 /* Tunnel was incoming, notify clients */
3775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3776 t->local_tid, t->local_tid_dest);
3777 send_clients_tunnel_destroy (t);
3779 tunnel_send_destroy (t);
3786 * Core handler for mesh network traffic going from the origin to a peer
3788 * @param cls closure
3789 * @param peer peer identity this notification is about
3790 * @param message message
3791 * @param atsi performance data
3792 * @param atsi_count number of records in 'atsi'
3793 * @return GNUNET_OK to keep the connection open,
3794 * GNUNET_SYSERR to close it (signal serious error)
3797 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3798 const struct GNUNET_MessageHeader *message,
3799 const struct GNUNET_ATS_Information *atsi,
3800 unsigned int atsi_count)
3802 struct GNUNET_MESH_Unicast *msg;
3803 struct MeshTunnel *t;
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3809 size = ntohs (message->size);
3811 sizeof (struct GNUNET_MESH_Unicast) +
3812 sizeof (struct GNUNET_MessageHeader))
3817 msg = (struct GNUNET_MESH_Unicast *) message;
3818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3819 ntohs (msg[1].header.type));
3820 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3823 /* TODO notify back: we don't know this tunnel */
3824 GNUNET_break_op (0);
3827 tunnel_reset_timeout (t);
3828 pid = GNUNET_PEER_search (&msg->destination);
3831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3832 " it's for us! sending to clients...\n");
3833 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3837 " not for us, retransmitting...\n");
3838 send_message (message, tree_get_first_hop (t->tree, pid), t);
3844 * Core handler for mesh network traffic going from the origin to all peers
3846 * @param cls closure
3847 * @param message message
3848 * @param peer peer identity this notification is about
3849 * @param atsi performance data
3850 * @param atsi_count number of records in 'atsi'
3851 * @return GNUNET_OK to keep the connection open,
3852 * GNUNET_SYSERR to close it (signal serious error)
3854 * TODO: Check who we got this from, to validate route.
3857 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3858 const struct GNUNET_MessageHeader *message,
3859 const struct GNUNET_ATS_Information *atsi,
3860 unsigned int atsi_count)
3862 struct GNUNET_MESH_Multicast *msg;
3863 struct MeshTunnel *t;
3866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3868 size = ntohs (message->size);
3869 if (sizeof (struct GNUNET_MESH_Multicast) +
3870 sizeof (struct GNUNET_MessageHeader) > size)
3872 GNUNET_break_op (0);
3875 msg = (struct GNUNET_MESH_Multicast *) message;
3876 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3880 /* TODO notify that we dont know that tunnel */
3881 GNUNET_break_op (0);
3884 if (t->mid == ntohl (msg->mid))
3886 /* FIXME: already seen this packet, log dropping */
3887 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3888 " Already seen mid %u, DROPPING!\n", t->mid);
3893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3894 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3896 t->mid = ntohl (msg->mid);
3897 tunnel_reset_timeout (t);
3899 /* Transmit to locally interested clients */
3900 if (NULL != t->peers &&
3901 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3903 send_subscribed_clients (message, &msg[1].header);
3905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3906 if (ntohl (msg->ttl) == 0)
3908 /* FIXME: ttl is 0, log dropping */
3909 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3912 tunnel_send_multicast (t, message, GNUNET_NO);
3918 * Core handler for mesh network traffic toward the owner of a tunnel
3920 * @param cls closure
3921 * @param message message
3922 * @param peer peer identity this notification is about
3923 * @param atsi performance data
3924 * @param atsi_count number of records in 'atsi'
3926 * @return GNUNET_OK to keep the connection open,
3927 * GNUNET_SYSERR to close it (signal serious error)
3930 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3931 const struct GNUNET_MessageHeader *message,
3932 const struct GNUNET_ATS_Information *atsi,
3933 unsigned int atsi_count)
3935 struct GNUNET_MESH_ToOrigin *msg;
3936 struct GNUNET_PeerIdentity id;
3937 struct MeshPeerInfo *peer_info;
3938 struct MeshTunnel *t;
3941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3943 size = ntohs (message->size);
3944 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3945 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3947 GNUNET_break_op (0);
3950 msg = (struct GNUNET_MESH_ToOrigin *) message;
3951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3952 ntohs (msg[1].header.type));
3953 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3957 /* TODO notify that we dont know this tunnel (whom)? */
3958 GNUNET_break_op (0);
3962 if (t->id.oid == myid)
3965 struct GNUNET_MESH_ToOrigin *copy;
3967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3968 " it's for us! sending to clients...\n");
3969 if (NULL == t->owner)
3971 /* got data packet for ownerless tunnel */
3972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3973 GNUNET_break_op (0);
3976 /* TODO signature verification */
3977 memcpy (cbuf, message, size);
3978 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3979 copy->tid = htonl (t->local_tid);
3980 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3981 ©->header, GNUNET_YES);
3984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3985 " not for us, retransmitting...\n");
3987 peer_info = peer_info_get (&msg->oid);
3988 if (NULL == peer_info)
3990 /* unknown origin of tunnel */
3994 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3995 send_message (message, &id, t);
4002 * Core handler for path ACKs
4004 * @param cls closure
4005 * @param message message
4006 * @param peer peer identity this notification is about
4007 * @param atsi performance data
4008 * @param atsi_count number of records in 'atsi'
4010 * @return GNUNET_OK to keep the connection open,
4011 * GNUNET_SYSERR to close it (signal serious error)
4014 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4015 const struct GNUNET_MessageHeader *message,
4016 const struct GNUNET_ATS_Information *atsi,
4017 unsigned int atsi_count)
4019 struct GNUNET_MESH_PathACK *msg;
4020 struct GNUNET_PeerIdentity id;
4021 struct MeshPeerInfo *peer_info;
4022 struct MeshPeerPath *p;
4023 struct MeshTunnel *t;
4025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4026 GNUNET_i2s (&my_full_id));
4027 msg = (struct GNUNET_MESH_PathACK *) message;
4028 t = tunnel_get (&msg->oid, msg->tid);
4031 /* TODO notify that we don't know the tunnel */
4032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4033 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4037 peer_info = peer_info_get (&msg->peer_id);
4039 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
4041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4042 "connect_by_string completed, stopping search\n");
4043 regex_cancel_search (t->regex_ctx);
4044 t->regex_ctx = NULL;
4047 /* Add paths to peers? */
4048 p = tree_get_path_to_peer (t->tree, peer_info->id);
4051 path_add_to_peers (p, GNUNET_YES);
4059 /* Message for us? */
4060 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4063 if (NULL == t->owner)
4065 GNUNET_break_op (0);
4068 if (NULL != t->dht_get_type)
4070 GNUNET_DHT_get_stop (t->dht_get_type);
4071 t->dht_get_type = NULL;
4073 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4075 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4076 send_client_peer_connected (t, peer_info->id);
4081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4082 " not for us, retransmitting...\n");
4083 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4084 peer_info = peer_info_get (&msg->oid);
4085 if (NULL == peer_info)
4087 /* If we know the tunnel, we should DEFINITELY know the peer */
4091 send_message (message, &id, t);
4097 * Functions to handle messages from core
4099 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4100 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4101 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4102 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4103 sizeof (struct GNUNET_MESH_PathBroken)},
4104 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
4105 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4106 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4107 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4108 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4109 sizeof (struct GNUNET_MESH_PathACK)},
4115 /******************************************************************************/
4116 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4117 /******************************************************************************/
4120 * deregister_app: iterator for removing each application registered by a client
4122 * @param cls closure
4123 * @param key the hash of the application id (used to access the hashmap)
4124 * @param value the value stored at the key (client)
4126 * @return GNUNET_OK on success
4129 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
4131 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
4132 GNUNET_break (GNUNET_YES ==
4133 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
4139 * notify_client_connection_failure: notify a client that the connection to the
4140 * requested remote peer is not possible (for instance, no route found)
4141 * Function called when the socket is ready to queue more data. "buf" will be
4142 * NULL and "size" zero if the socket was closed for writing in the meantime.
4144 * @param cls closure
4145 * @param size number of bytes available in buf
4146 * @param buf where the callee should write the message
4147 * @return number of bytes written to buf
4150 notify_client_connection_failure (void *cls, size_t size, void *buf)
4153 struct MeshPeerInfo *peer_info;
4154 struct GNUNET_MESH_PeerControl *msg;
4155 struct GNUNET_PeerIdentity id;
4157 if (0 == size && NULL == buf)
4159 // TODO retry? cancel?
4163 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4164 peer_info = (struct MeshPeerInfo *) cls;
4165 msg = (struct GNUNET_MESH_PeerControl *) buf;
4166 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4167 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4168 // msg->tunnel_id = htonl(peer_info->t->tid);
4169 GNUNET_PEER_resolve (peer_info->id, &id);
4170 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4178 * Send keepalive packets for a peer
4180 * @param cls Closure (tunnel for which to send the keepalive).
4181 * @param tc Notification context.
4183 * TODO: implement explicit multicast keepalive?
4186 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4188 struct MeshTunnel *t = cls;
4189 struct GNUNET_MessageHeader *payload;
4190 struct GNUNET_MESH_Multicast *msg;
4192 sizeof (struct GNUNET_MESH_Multicast) +
4193 sizeof (struct GNUNET_MessageHeader);
4196 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4200 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4203 "sending keepalive for tunnel %d\n", t->id.tid);
4205 msg = (struct GNUNET_MESH_Multicast *) cbuf;
4206 msg->header.size = htons (size);
4207 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4208 msg->oid = my_full_id;
4209 msg->tid = htonl (t->id.tid);
4210 msg->ttl = htonl (DEFAULT_TTL);
4211 msg->mid = htonl (t->mid + 1);
4213 payload = (struct GNUNET_MessageHeader *) &msg[1];
4214 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
4215 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4216 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
4218 t->path_refresh_task =
4219 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
4225 * Function to process paths received for a new peer addition. The recorded
4226 * paths form the initial tunnel, which can be optimized later.
4227 * Called on each result obtained for the DHT search.
4229 * @param cls closure
4230 * @param exp when will this value expire
4231 * @param key key of the result
4232 * @param get_path path of the get request
4233 * @param get_path_length lenght of get_path
4234 * @param put_path path of the put request
4235 * @param put_path_length length of the put_path
4236 * @param type type of the result
4237 * @param size number of bytes in data
4238 * @param data pointer to the result data
4240 * TODO: re-issue the request after certain time? cancel after X results?
4243 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4244 const struct GNUNET_HashCode * key,
4245 const struct GNUNET_PeerIdentity *get_path,
4246 unsigned int get_path_length,
4247 const struct GNUNET_PeerIdentity *put_path,
4248 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4249 size_t size, const void *data)
4251 struct MeshPathInfo *path_info = cls;
4252 struct MeshPeerPath *p;
4253 struct GNUNET_PeerIdentity pi;
4256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4257 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4260 p = path_build_from_dht (get_path, get_path_length, put_path,
4262 path_add_to_peers (p, GNUNET_NO);
4264 for (i = 0; i < path_info->peer->ntunnels; i++)
4266 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4267 peer_info_connect (path_info->peer, path_info->t);
4275 * Function to process paths received for a new peer addition. The recorded
4276 * paths form the initial tunnel, which can be optimized later.
4277 * Called on each result obtained for the DHT search.
4279 * @param cls closure
4280 * @param exp when will this value expire
4281 * @param key key of the result
4282 * @param get_path path of the get request
4283 * @param get_path_length lenght of get_path
4284 * @param put_path path of the put request
4285 * @param put_path_length length of the put_path
4286 * @param type type of the result
4287 * @param size number of bytes in data
4288 * @param data pointer to the result data
4291 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4292 const struct GNUNET_HashCode * key,
4293 const struct GNUNET_PeerIdentity *get_path,
4294 unsigned int get_path_length,
4295 const struct GNUNET_PeerIdentity *put_path,
4296 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4297 size_t size, const void *data)
4299 const struct PBlock *pb = data;
4300 const struct GNUNET_PeerIdentity *pi = &pb->id;
4301 struct MeshTunnel *t = cls;
4302 struct MeshPeerInfo *peer_info;
4303 struct MeshPeerPath *p;
4305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
4306 if (size != sizeof (struct PBlock))
4308 GNUNET_break_op (0);
4311 if (ntohl(pb->type) != t->type)
4313 GNUNET_break_op (0);
4316 GNUNET_assert (NULL != t->owner);
4317 peer_info = peer_info_get (pi);
4318 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
4320 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
4322 p = path_build_from_dht (get_path, get_path_length, put_path,
4324 path_add_to_peers (p, GNUNET_NO);
4326 tunnel_add_peer (t, peer_info);
4327 peer_info_connect (peer_info, t);
4332 * Function called if the connect attempt to a peer found via
4333 * connect_by_string times out. Try to connect to another peer, if any.
4334 * Otherwise try to reconnect to the same peer.
4336 * @param cls Closure (info about regex search).
4337 * @param tc TaskContext.
4340 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4342 struct MeshRegexSearchInfo *info = cls;
4343 struct MeshPeerInfo *peer_info;
4347 info->timeout = GNUNET_SCHEDULER_NO_TASK;
4348 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4355 if (0 < info->n_peers)
4357 // Select next peer, put current in that spot.
4358 id = info->peers[info->i_peer];
4359 info->peers[info->i_peer] = info->peer;
4360 info->i_peer = (info->i_peer + 1) % info->n_peers;
4364 // Try to connect to same peer again.
4368 peer_info = peer_info_get_short(id);
4369 tunnel_add_peer (info->t, peer_info);
4370 tunnel_delete_peer (info->t, old);
4371 peer_info_connect (peer_info, info->t);
4372 info->timeout = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
4373 ®ex_connect_timeout,
4379 * Function to process DHT string to regex matching.
4380 * Called on each result obtained for the DHT search.
4382 * @param cls closure (search context)
4383 * @param exp when will this value expire
4384 * @param key key of the result
4385 * @param get_path path of the get request (not used)
4386 * @param get_path_length lenght of get_path (not used)
4387 * @param put_path path of the put request (not used)
4388 * @param put_path_length length of the put_path (not used)
4389 * @param type type of the result
4390 * @param size number of bytes in data
4391 * @param data pointer to the result data
4394 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4395 const struct GNUNET_HashCode * key,
4396 const struct GNUNET_PeerIdentity *get_path,
4397 unsigned int get_path_length,
4398 const struct GNUNET_PeerIdentity *put_path,
4399 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4400 size_t size, const void *data)
4402 const struct MeshRegexAccept *block = data;
4403 struct MeshRegexSearchContext *ctx = cls;
4404 struct MeshRegexSearchInfo *info = ctx->info;
4405 struct MeshPeerPath *p;
4406 struct MeshPeerInfo *peer_info;
4408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
4411 peer_info = peer_info_get(&block->id);
4412 p = path_build_from_dht (get_path, get_path_length, put_path,
4414 path_add_to_peers (p, GNUNET_NO);
4417 tunnel_add_peer (info->t, peer_info);
4418 peer_info_connect (peer_info, info->t);
4419 if (0 != info->peer)
4421 info->peer = peer_info->id;
4425 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
4428 info->timeout = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
4429 ®ex_connect_timeout,
4437 * Function to process DHT string to regex matching..
4438 * Called on each result obtained for the DHT search.
4440 * @param cls closure (search context)
4441 * @param exp when will this value expire
4442 * @param key key of the result
4443 * @param get_path path of the get request (not used)
4444 * @param get_path_length lenght of get_path (not used)
4445 * @param put_path path of the put request (not used)
4446 * @param put_path_length length of the put_path (not used)
4447 * @param type type of the result
4448 * @param size number of bytes in data
4449 * @param data pointer to the result data
4451 * TODO: re-issue the request after certain time? cancel after X results?
4454 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4455 const struct GNUNET_HashCode * key,
4456 const struct GNUNET_PeerIdentity *get_path,
4457 unsigned int get_path_length,
4458 const struct GNUNET_PeerIdentity *put_path,
4459 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4460 size_t size, const void *data)
4462 const struct MeshRegexBlock *block = data;
4463 struct MeshRegexSearchContext *ctx = cls;
4464 struct MeshRegexSearchInfo *info = ctx->info;
4469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4470 "DHT GET STRING RETURNED RESULTS\n");
4471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4472 " key: %s\n", GNUNET_h2s (key));
4474 copy = GNUNET_malloc (size);
4475 memcpy (copy, data, size);
4476 GNUNET_break (GNUNET_OK ==
4477 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
4478 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4479 len = ntohl (block->n_proof);
4480 proof = GNUNET_malloc (len + 1);
4481 memcpy (proof, &block[1], len);
4483 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
4485 GNUNET_break_op (0);
4488 len = strlen (info->description);
4489 if (len == ctx->position) // String processed
4491 if (GNUNET_YES == ntohl (block->accepting))
4493 struct GNUNET_DHT_GetHandle *get_h;
4494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
4495 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
4496 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
4497 key, /* key to search */
4498 DHT_REPLICATION_LEVEL, /* replication level */
4499 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
4500 GNUNET_DHT_RO_RECORD_ROUTE,
4501 NULL, /* xquery */ // FIXME BLOOMFILTER
4502 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
4503 &dht_get_string_accept_handler, ctx);
4504 GNUNET_break (GNUNET_OK ==
4505 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
4508 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
4513 // FIXME REGEX this block not successful, wait for more? start timeout?
4517 GNUNET_break (GNUNET_OK ==
4518 GNUNET_MESH_regex_block_iterate (block, size,
4519 ®ex_edge_iterator, ctx));
4523 /******************************************************************************/
4524 /********************* MESH LOCAL HANDLES **************************/
4525 /******************************************************************************/
4529 * Handler for client disconnection
4531 * @param cls closure
4532 * @param client identification of the client; NULL
4533 * for the last call when the server is destroyed
4536 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4538 struct MeshClient *c;
4539 struct MeshClient *next;
4542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4551 if (c->handle != client)
4553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4559 GNUNET_SERVER_client_drop (c->handle);
4560 c->shutting_down = GNUNET_YES;
4561 GNUNET_assert (NULL != c->own_tunnels);
4562 GNUNET_assert (NULL != c->incoming_tunnels);
4563 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4564 &tunnel_destroy_iterator, c);
4565 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4566 &tunnel_destroy_iterator, c);
4567 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4568 &tunnel_destroy_iterator, c);
4569 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4570 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4571 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4573 /* deregister clients applications */
4574 if (NULL != c->apps)
4576 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
4577 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
4579 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
4580 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
4582 GNUNET_SCHEDULER_cancel (announce_applications_task);
4583 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4585 if (NULL != c->types)
4586 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4587 for (i = 0; i < c->n_regex; i++)
4589 GNUNET_free (c->regexes[i]);
4591 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
4592 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
4594 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
4595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
4605 * Handler for new clients
4607 * @param cls closure
4608 * @param client identification of the client
4609 * @param message the actual message, which includes messages the client wants
4612 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4613 const struct GNUNET_MessageHeader *message)
4615 struct GNUNET_MESH_ClientConnect *cc_msg;
4616 struct MeshClient *c;
4617 GNUNET_MESH_ApplicationType *a;
4624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4625 /* Check data sanity */
4626 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4627 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4628 ntypes = ntohs (cc_msg->types);
4629 napps = ntohs (cc_msg->applications);
4631 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
4634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4638 /* Create new client structure */
4639 c = GNUNET_malloc (sizeof (struct MeshClient));
4640 c->id = next_client_id++;
4641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4643 GNUNET_SERVER_client_keep (client);
4644 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
4647 GNUNET_MESH_ApplicationType at;
4648 struct GNUNET_HashCode hc;
4650 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
4651 for (i = 0; i < napps; i++)
4654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
4655 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
4656 /* store in clients hashmap */
4657 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
4658 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4659 /* store in global hashmap, for announcements */
4660 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
4661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4663 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
4664 announce_applications_task =
4665 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
4671 struct GNUNET_HashCode hc;
4673 t = (uint16_t *) & a[napps];
4674 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
4675 for (i = 0; i < ntypes; i++)
4678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4679 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
4681 /* store in clients hashmap */
4682 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4683 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4684 /* store in global hashmap */
4685 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4686 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4690 " client has %u+%u subscriptions\n", napps, ntypes);
4692 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
4693 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4694 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4695 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4696 GNUNET_SERVER_notification_context_add (nc, client);
4698 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4704 * Handler for clients announcing available services by a regular expression.
4706 * @param cls closure
4707 * @param client identification of the client
4708 * @param message the actual message, which includes messages the client wants
4711 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
4712 const struct GNUNET_MessageHeader *message)
4714 struct MeshClient *c;
4718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
4720 /* Sanity check for client registration */
4721 if (NULL == (c = client_get (client)))
4724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4729 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
4730 regex = GNUNET_malloc (len + 1);
4731 memcpy (regex, &message[1], len);
4733 GNUNET_array_append (c->regexes, c->n_regex, regex);
4734 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
4736 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
4742 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
4748 * Handler for requests of new tunnels
4750 * @param cls closure
4751 * @param client identification of the client
4752 * @param message the actual message
4755 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4756 const struct GNUNET_MessageHeader *message)
4758 struct GNUNET_MESH_TunnelMessage *t_msg;
4759 struct MeshTunnel *t;
4760 struct MeshClient *c;
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4764 /* Sanity check for client registration */
4765 if (NULL == (c = client_get (client)))
4768 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4773 /* Message sanity check */
4774 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4781 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4782 /* Sanity check for tunnel numbering */
4783 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4786 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4789 /* Sanity check for duplicate tunnel IDs */
4790 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
4793 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4797 while (NULL != tunnel_get_by_pi (myid, next_tid))
4798 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4799 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
4800 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4802 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4803 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
4805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
4806 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4812 * Handler for requests of deleting tunnels
4814 * @param cls closure
4815 * @param client identification of the client
4816 * @param message the actual message
4819 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4820 const struct GNUNET_MessageHeader *message)
4822 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4823 struct MeshClient *c;
4824 struct MeshTunnel *t;
4825 MESH_TunnelNumber tid;
4827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4828 "Got a DESTROY TUNNEL from client!\n");
4830 /* Sanity check for client registration */
4831 if (NULL == (c = client_get (client)))
4834 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4837 /* Message sanity check */
4838 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4841 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4845 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4847 /* Retrieve tunnel */
4848 tid = ntohl (tunnel_msg->tunnel_id);
4849 t = tunnel_get_by_local_id(c, tid);
4853 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4854 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4857 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4859 client_ignore_tunnel (c, t);
4861 // TODO: when to destroy incoming tunnel?
4862 if (t->nclients == 0)
4864 GNUNET_assert (GNUNET_YES ==
4865 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4867 GNUNET_assert (GNUNET_YES ==
4868 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4869 &my_full_id.hashPubKey,
4873 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4876 send_client_tunnel_disconnect(t, c);
4877 client_delete_tunnel(c, t);
4879 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4881 tunnel_send_destroy (t);
4883 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4889 * Handler for connection requests to new peers
4891 * @param cls closure
4892 * @param client identification of the client
4893 * @param message the actual message (PeerControl)
4896 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4897 const struct GNUNET_MessageHeader *message)
4899 struct GNUNET_MESH_PeerControl *peer_msg;
4900 struct MeshPeerInfo *peer_info;
4901 struct MeshClient *c;
4902 struct MeshTunnel *t;
4903 MESH_TunnelNumber tid;
4905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4906 /* Sanity check for client registration */
4907 if (NULL == (c = client_get (client)))
4910 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4914 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4915 /* Sanity check for message size */
4916 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4923 /* Tunnel exists? */
4924 tid = ntohl (peer_msg->tunnel_id);
4925 t = tunnel_get_by_local_id (c, tid);
4929 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4933 /* Does client own tunnel? */
4934 if (t->owner->handle != client)
4937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4941 GNUNET_i2s (&peer_msg->peer));
4942 peer_info = peer_info_get (&peer_msg->peer);
4944 tunnel_add_peer (t, peer_info);
4945 peer_info_connect (peer_info, t);
4947 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4953 * Handler for disconnection requests of peers in a tunnel
4955 * @param cls closure
4956 * @param client identification of the client
4957 * @param message the actual message (PeerControl)
4960 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4961 const struct GNUNET_MessageHeader *message)
4963 struct GNUNET_MESH_PeerControl *peer_msg;
4964 struct MeshPeerInfo *peer_info;
4965 struct MeshClient *c;
4966 struct MeshTunnel *t;
4967 MESH_TunnelNumber tid;
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4970 /* Sanity check for client registration */
4971 if (NULL == (c = client_get (client)))
4974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4977 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4978 /* Sanity check for message size */
4979 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4986 /* Tunnel exists? */
4987 tid = ntohl (peer_msg->tunnel_id);
4988 t = tunnel_get_by_local_id (c, tid);
4992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4997 /* Does client own tunnel? */
4998 if (t->owner->handle != client)
5001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
5006 GNUNET_i2s (&peer_msg->peer));
5007 /* Is the peer in the tunnel? */
5009 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
5010 if (NULL == peer_info)
5013 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5017 /* Ok, delete peer from tunnel */
5018 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
5019 &peer_msg->peer.hashPubKey);
5021 send_destroy_path (t, peer_info->id);
5022 tunnel_delete_peer (t, peer_info->id);
5023 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5028 * Handler for blacklist requests of peers in a tunnel
5030 * @param cls closure
5031 * @param client identification of the client
5032 * @param message the actual message (PeerControl)
5035 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
5036 const struct GNUNET_MessageHeader *message)
5038 struct GNUNET_MESH_PeerControl *peer_msg;
5039 struct MeshClient *c;
5040 struct MeshTunnel *t;
5041 MESH_TunnelNumber tid;
5043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
5044 /* Sanity check for client registration */
5045 if (NULL == (c = client_get (client)))
5048 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5051 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5053 /* Sanity check for message size */
5054 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5057 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5061 /* Tunnel exists? */
5062 tid = ntohl (peer_msg->tunnel_id);
5063 t = tunnel_get_by_local_id (c, tid);
5067 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5072 GNUNET_array_append(t->blacklisted, t->nblacklisted,
5073 GNUNET_PEER_intern(&peer_msg->peer));
5078 * Handler for unblacklist requests of peers in a tunnel
5080 * @param cls closure
5081 * @param client identification of the client
5082 * @param message the actual message (PeerControl)
5085 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
5086 const struct GNUNET_MessageHeader *message)
5088 struct GNUNET_MESH_PeerControl *peer_msg;
5089 struct MeshClient *c;
5090 struct MeshTunnel *t;
5091 MESH_TunnelNumber tid;
5095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
5096 /* Sanity check for client registration */
5097 if (NULL == (c = client_get (client)))
5100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5103 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5105 /* Sanity check for message size */
5106 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5113 /* Tunnel exists? */
5114 tid = ntohl (peer_msg->tunnel_id);
5115 t = tunnel_get_by_local_id (c, tid);
5119 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5124 /* if peer is not known, complain */
5125 pid = GNUNET_PEER_search (&peer_msg->peer);
5132 /* search and remove from list */
5133 for (i = 0; i < t->nblacklisted; i++)
5135 if (t->blacklisted[i] == pid)
5137 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
5138 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
5143 /* if peer hasn't been blacklisted, complain */
5149 * Handler for connection requests to new peers by type
5151 * @param cls closure
5152 * @param client identification of the client
5153 * @param message the actual message (ConnectPeerByType)
5156 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
5157 const struct GNUNET_MessageHeader *message)
5159 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
5160 struct MeshClient *c;
5161 struct MeshTunnel *t;
5162 struct GNUNET_HashCode hash;
5163 MESH_TunnelNumber tid;
5165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
5166 /* Sanity check for client registration */
5167 if (NULL == (c = client_get (client)))
5170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5174 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
5175 /* Sanity check for message size */
5176 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
5177 ntohs (connect_msg->header.size))
5180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5184 /* Tunnel exists? */
5185 tid = ntohl (connect_msg->tunnel_id);
5186 t = tunnel_get_by_local_id (c, tid);
5190 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5194 /* Does client own tunnel? */
5195 if (t->owner->handle != client)
5198 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5202 /* Do WE have the service? */
5203 t->type = ntohl (connect_msg->type);
5204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
5205 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
5206 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
5209 /* Yes! Fast forward, add ourselves to the tunnel and send the
5210 * good news to the client, and alert the destination client of
5211 * an incoming tunnel.
5213 * FIXME send a path create to self, avoid code duplication
5215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
5216 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
5217 peer_info_get (&my_full_id),
5218 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
5221 send_client_peer_connected (t, myid);
5222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
5223 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5225 t->local_tid_dest = next_local_tid++;
5226 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5227 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5228 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5232 /* Ok, lets find a peer offering the service */
5233 if (NULL != t->dht_get_type)
5235 GNUNET_DHT_get_stop (t->dht_get_type);
5237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
5238 GNUNET_h2s (&hash));
5240 GNUNET_DHT_get_start (dht_handle,
5241 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
5243 DHT_REPLICATION_LEVEL,
5244 GNUNET_DHT_RO_RECORD_ROUTE |
5245 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5247 &dht_get_type_handler, t);
5249 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5255 * Handler for connection requests to new peers by a string service description.
5257 * @param cls closure
5258 * @param client identification of the client
5259 * @param message the actual message, which includes messages the client wants
5262 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
5263 const struct GNUNET_MessageHeader *message)
5265 struct GNUNET_MESH_ConnectPeerByString *msg;
5266 struct MeshRegexSearchContext *ctx;
5267 struct MeshRegexSearchInfo *info;
5268 struct GNUNET_DHT_GetHandle *get_h;
5269 struct GNUNET_HashCode key;
5270 struct MeshTunnel *t;
5271 struct MeshClient *c;
5272 MESH_TunnelNumber tid;
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5277 "Connect by string started\n");
5278 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
5279 size = htons (message->size);
5281 /* Sanity check for client registration */
5282 if (NULL == (c = client_get (client)))
5285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5289 /* Message size sanity check */
5290 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
5293 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5297 /* Tunnel exists? */
5298 tid = ntohl (msg->tunnel_id);
5299 t = tunnel_get_by_local_id (c, tid);
5303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5307 /* Does client own tunnel? */
5308 if (t->owner->handle != client)
5311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5316 " on tunnel %s [%u]\n",
5317 GNUNET_i2s(&my_full_id),
5320 /* Only one connect_by_string allowed at the same time! */
5321 /* FIXME: allow more, return handle at api level to cancel, document */
5322 if (NULL != t->regex_ctx)
5325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5329 /* Find string itself */
5330 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
5331 string = (const char *) &msg[1];
5333 /* Initialize context */
5334 size = GNUNET_REGEX_get_first_key(string, len, &key);
5335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5336 " consumed %u bits out of %u\n", size, len);
5337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5338 " looking for %s\n", GNUNET_h2s (&key));
5340 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
5342 info->description = GNUNET_malloc (len + 1);
5343 memcpy (info->description, string, len);
5344 info->description[len] = '\0';
5345 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
5346 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
5347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
5349 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
5350 ctx->position = size;
5354 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
5356 /* Start search in DHT */
5357 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
5358 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
5359 &key, /* key to search */
5360 DHT_REPLICATION_LEVEL, /* replication level */
5361 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5362 NULL, /* xquery */ // FIXME BLOOMFILTER
5363 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
5364 &dht_get_string_handler, ctx);
5366 GNUNET_break (GNUNET_OK ==
5367 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
5370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
5372 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
5378 * Handler for client traffic directed to one peer
5380 * @param cls closure
5381 * @param client identification of the client
5382 * @param message the actual message
5385 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5386 const struct GNUNET_MessageHeader *message)
5388 struct MeshClient *c;
5389 struct MeshTunnel *t;
5390 struct MeshPeerInfo *pi;
5391 struct GNUNET_MESH_Unicast *data_msg;
5392 MESH_TunnelNumber tid;
5395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5396 "Got a unicast request from a client!\n");
5398 /* Sanity check for client registration */
5399 if (NULL == (c = client_get (client)))
5402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5405 data_msg = (struct GNUNET_MESH_Unicast *) message;
5406 /* Sanity check for message size */
5407 size = ntohs (message->size);
5408 if (sizeof (struct GNUNET_MESH_Unicast) +
5409 sizeof (struct GNUNET_MessageHeader) > size)
5412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5416 /* Tunnel exists? */
5417 tid = ntohl (data_msg->tid);
5418 t = tunnel_get_by_local_id (c, tid);
5422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5426 /* Is it a local tunnel? Then, does client own the tunnel? */
5427 if (t->owner->handle != client)
5430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5434 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
5435 &data_msg->destination.hashPubKey);
5436 /* Is the selected peer in the tunnel? */
5440 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5444 /* Ok, everything is correct, send the message
5445 * (pretend we got it from a mesh peer)
5448 char buf[ntohs (message->size)] GNUNET_ALIGN;
5449 struct GNUNET_MESH_Unicast *copy;
5451 /* Work around const limitation */
5452 copy = (struct GNUNET_MESH_Unicast *) buf;
5453 memcpy (buf, data_msg, size);
5454 copy->oid = my_full_id;
5455 copy->tid = htonl (t->id.tid);
5456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5457 " calling generic handler...\n");
5458 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
5460 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5466 * Handler for client traffic directed to the origin
5468 * @param cls closure
5469 * @param client identification of the client
5470 * @param message the actual message
5473 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5474 const struct GNUNET_MessageHeader *message)
5476 struct GNUNET_MESH_ToOrigin *data_msg;
5477 struct GNUNET_PeerIdentity id;
5478 struct MeshClient *c;
5479 struct MeshTunnel *t;
5480 MESH_TunnelNumber tid;
5483 /* Sanity check for client registration */
5484 if (NULL == (c = client_get (client)))
5487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5490 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5491 /* Sanity check for message size */
5492 size = ntohs (message->size);
5493 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5494 sizeof (struct GNUNET_MessageHeader) > size)
5497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5501 /* Tunnel exists? */
5502 tid = ntohl (data_msg->tid);
5503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5504 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
5505 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5511 t = tunnel_get_by_local_id (c, tid);
5515 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5519 /* It should be sent by someone who has this as incoming tunnel. */
5520 if (-1 == client_knows_tunnel (c, t))
5523 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5526 GNUNET_PEER_resolve (t->id.oid, &id);
5528 /* Ok, everything is correct, send the message
5529 * (pretend we got it from a mesh peer)
5532 char buf[ntohs (message->size)] GNUNET_ALIGN;
5533 struct GNUNET_MESH_ToOrigin *copy;
5535 /* Work around const limitation */
5536 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5537 memcpy (buf, data_msg, size);
5539 copy->tid = htonl (t->id.tid);
5540 copy->sender = my_full_id;
5541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5542 " calling generic handler...\n");
5543 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
5545 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5551 * Handler for client traffic directed to all peers in a tunnel
5553 * @param cls closure
5554 * @param client identification of the client
5555 * @param message the actual message
5558 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
5559 const struct GNUNET_MessageHeader *message)
5561 struct MeshClient *c;
5562 struct MeshTunnel *t;
5563 struct GNUNET_MESH_Multicast *data_msg;
5564 MESH_TunnelNumber tid;
5566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5567 "Got a multicast request from a client!\n");
5569 /* Sanity check for client registration */
5570 if (NULL == (c = client_get (client)))
5573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5576 data_msg = (struct GNUNET_MESH_Multicast *) message;
5577 /* Sanity check for message size */
5578 if (sizeof (struct GNUNET_MESH_Multicast) +
5579 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
5582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5586 /* Tunnel exists? */
5587 tid = ntohl (data_msg->tid);
5588 t = tunnel_get_by_local_id (c, tid);
5592 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5596 /* Does client own tunnel? */
5597 if (t->owner->handle != client)
5600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5605 char buf[ntohs (message->size)] GNUNET_ALIGN;
5606 struct GNUNET_MESH_Multicast *copy;
5608 copy = (struct GNUNET_MESH_Multicast *) buf;
5609 memcpy (buf, message, ntohs (message->size));
5610 copy->oid = my_full_id;
5611 copy->tid = htonl (t->id.tid);
5612 copy->ttl = htonl (DEFAULT_TTL);
5613 copy->mid = htonl (t->mid + 1);
5614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5615 " calling generic handler...\n");
5616 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
5619 /* receive done gets called when last copy is sent to a neighbor */
5624 * Functions to handle messages from clients
5626 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5627 {&handle_local_new_client, NULL,
5628 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5629 {&handle_local_announce_regex, NULL,
5630 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
5631 {&handle_local_tunnel_create, NULL,
5632 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5633 sizeof (struct GNUNET_MESH_TunnelMessage)},
5634 {&handle_local_tunnel_destroy, NULL,
5635 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5636 sizeof (struct GNUNET_MESH_TunnelMessage)},
5637 {&handle_local_connect_add, NULL,
5638 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
5639 sizeof (struct GNUNET_MESH_PeerControl)},
5640 {&handle_local_connect_del, NULL,
5641 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
5642 sizeof (struct GNUNET_MESH_PeerControl)},
5643 {&handle_local_blacklist, NULL,
5644 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
5645 sizeof (struct GNUNET_MESH_PeerControl)},
5646 {&handle_local_unblacklist, NULL,
5647 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
5648 sizeof (struct GNUNET_MESH_PeerControl)},
5649 {&handle_local_connect_by_type, NULL,
5650 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
5651 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
5652 {&handle_local_connect_by_string, NULL,
5653 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
5654 {&handle_local_unicast, NULL,
5655 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5656 {&handle_local_to_origin, NULL,
5657 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5658 {&handle_local_multicast, NULL,
5659 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5665 * To be called on core init/fail.
5667 * @param cls service closure
5668 * @param server handle to the server for this service
5669 * @param identity the public identity of this peer
5672 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5673 const struct GNUNET_PeerIdentity *identity)
5675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5676 core_handle = server;
5677 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5680 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5681 GNUNET_SCHEDULER_shutdown ();
5687 * Method called whenever a given peer connects.
5689 * @param cls closure
5690 * @param peer peer identity this notification is about
5691 * @param atsi performance data for the connection
5692 * @param atsi_count number of records in 'atsi'
5695 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
5696 const struct GNUNET_ATS_Information *atsi,
5697 unsigned int atsi_count)
5699 struct MeshPeerInfo *peer_info;
5700 struct MeshPeerPath *path;
5702 DEBUG_CONN ("Peer connected\n");
5703 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5704 peer_info = peer_info_get (peer);
5705 if (myid == peer_info->id)
5707 DEBUG_CONN (" (self)\n");
5712 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5714 path = path_new (2);
5715 path->peers[0] = myid;
5716 path->peers[1] = peer_info->id;
5717 GNUNET_PEER_change_rc (myid, 1);
5718 GNUNET_PEER_change_rc (peer_info->id, 1);
5719 peer_info_add_path (peer_info, path, GNUNET_YES);
5724 * Method called whenever a peer disconnects.
5726 * @param cls closure
5727 * @param peer peer identity this notification is about
5730 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5732 struct MeshPeerInfo *pi;
5733 struct MeshPeerQueue *q;
5734 struct MeshPeerQueue *n;
5736 DEBUG_CONN ("Peer disconnected\n");
5737 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5749 /* try to reroute this traffic instead */
5750 queue_destroy(q, GNUNET_YES);
5754 peer_info_remove_path (pi, pi->id, myid);
5757 DEBUG_CONN (" (self)\n");
5763 /******************************************************************************/
5764 /************************ MAIN FUNCTIONS ****************************/
5765 /******************************************************************************/
5768 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5770 * @param cls closure
5771 * @param key current key code
5772 * @param value value in the hash map
5773 * @return GNUNET_YES if we should continue to iterate,
5777 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5779 struct MeshTunnel *t = value;
5786 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5788 * @param cls closure
5789 * @param key current key code
5790 * @param value value in the hash map
5791 * @return GNUNET_YES if we should continue to iterate,
5795 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5797 struct MeshPeerInfo *p = value;
5798 struct MeshPeerQueue *q;
5799 struct MeshPeerQueue *n;
5807 queue_destroy(q, GNUNET_YES);
5811 peer_info_destroy (p);
5816 * Task run during shutdown.
5822 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5826 if (core_handle != NULL)
5828 GNUNET_CORE_disconnect (core_handle);
5831 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5832 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5833 if (dht_handle != NULL)
5835 GNUNET_DHT_disconnect (dht_handle);
5840 GNUNET_SERVER_notification_context_destroy (nc);
5843 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5845 GNUNET_SCHEDULER_cancel (announce_id_task);
5846 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5852 * Process mesh requests.
5854 * @param cls closure
5855 * @param server the initialized server
5856 * @param c configuration to use
5859 run (void *cls, struct GNUNET_SERVER_Handle *server,
5860 const struct GNUNET_CONFIGURATION_Handle *c)
5862 struct MeshPeerInfo *peer;
5863 struct MeshPeerPath *p;
5866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5867 server_handle = server;
5868 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5869 NULL, /* Closure passed to MESH functions */
5870 &core_init, /* Call core_init once connected */
5871 &core_connect, /* Handle connects */
5872 &core_disconnect, /* remove peers on disconnects */
5873 NULL, /* Don't notify about all incoming messages */
5874 GNUNET_NO, /* For header only in notification */
5875 NULL, /* Don't notify about all outbound messages */
5876 GNUNET_NO, /* For header-only out notification */
5877 core_handlers); /* Register these handlers */
5879 if (core_handle == NULL)
5882 GNUNET_SCHEDULER_shutdown ();
5887 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
5890 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5892 ("Mesh service is lacking key configuration settings. Exiting.\n"));
5893 GNUNET_SCHEDULER_shutdown ();
5896 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
5897 GNUNET_free (keyfile);
5898 if (my_private_key == NULL)
5900 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5901 _("Mesh service could not access hostkey. Exiting.\n"));
5902 GNUNET_SCHEDULER_shutdown ();
5905 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
5906 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5907 &my_full_id.hashPubKey);
5908 myid = GNUNET_PEER_intern (&my_full_id);
5910 // transport_handle = GNUNET_TRANSPORT_connect(c,
5917 dht_handle = GNUNET_DHT_connect (c, 64);
5918 if (dht_handle == NULL)
5924 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5926 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5927 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5928 peers = GNUNET_CONTAINER_multihashmap_create (32);
5929 applications = GNUNET_CONTAINER_multihashmap_create (32);
5930 types = GNUNET_CONTAINER_multihashmap_create (32);
5932 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5933 nc = GNUNET_SERVER_notification_context_create (server_handle,
5935 GNUNET_SERVER_disconnect_notify (server_handle,
5936 &handle_local_client_disconnect, NULL);
5940 clients_tail = NULL;
5943 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5944 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5946 /* Create a peer_info for the local peer */
5947 peer = peer_info_get (&my_full_id);
5950 GNUNET_PEER_change_rc (myid, 1);
5951 peer_info_add_path (peer, p, GNUNET_YES);
5953 /* Scheduled the task to clean up when shutdown is called */
5954 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
5961 * The main function for the mesh service.
5963 * @param argc number of arguments from the command line
5964 * @param argv command line arguments
5965 * @return 0 ok, 1 on error
5968 main (int argc, char *const *argv)
5972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5975 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");