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 * Flag to signal the destruction of the tunnel.
340 * If this is set GNUNET_YES the tunnel will be destroyed
341 * when the queue is empty.
346 * Last time the tunnel was used
348 struct GNUNET_TIME_Absolute timestamp;
351 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
352 * containing peers added by id or by type, not intermediate peers.
354 struct GNUNET_CONTAINER_MultiHashMap *peers;
357 * Number of peers that are connected and potentially ready to receive data
359 unsigned int peers_ready;
362 * Number of peers that have been added to the tunnel
364 unsigned int peers_total;
367 * Client owner of the tunnel, if any
369 struct MeshClient *owner;
372 * Clients that have been informed about the tunnel, if any
374 struct MeshClient **clients;
377 * Number of elements in clients
379 unsigned int nclients;
382 * Clients that have requested to leave the tunnel
384 struct MeshClient **ignore;
387 * Number of elements in clients
389 unsigned int nignore;
394 GNUNET_PEER_Id *blacklisted;
397 * Number of elements in blacklisted
399 unsigned int nblacklisted;
402 * Bloomfilter (for peer identities) to stop circular routes
404 char bloomfilter[MESH_BLOOM_SIZE];
409 struct MeshTunnelTree *tree;
412 * Application type we are looking for in this tunnel
414 GNUNET_MESH_ApplicationType type;
417 * Used to search peers offering a service
419 struct GNUNET_DHT_GetHandle *dht_get_type;
422 * Initial context of the regex search for a connect_by_string
424 struct MeshRegexSearchContext *regex_ctx;
427 * Task to keep the used paths alive
429 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
432 * Task to destroy the tunnel after timeout
434 * FIXME: merge the two? a tunnel will have either
435 * a path refresh OR a timeout, never both!
437 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
442 * Info needed to work with tunnel paths and peers
449 struct MeshTunnel *t;
452 * Neighbouring peer to whom we send the packet to
454 struct MeshPeerInfo *peer;
459 struct MeshPeerPath *path;
464 * Struct containing information about a client of the service
471 struct MeshClient *next;
476 struct MeshClient *prev;
479 * Tunnels that belong to this client, indexed by local id
481 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
484 * Tunnels this client has accepted, indexed by incoming local id
486 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
489 * Tunnels this client has rejected, indexed by incoming local id
491 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
493 * Handle to communicate with the client
495 struct GNUNET_SERVER_Client *handle;
498 * Applications that this client has claimed to provide
500 struct GNUNET_CONTAINER_MultiHashMap *apps;
503 * Messages that this client has declared interest in
505 struct GNUNET_CONTAINER_MultiHashMap *types;
508 * Whether the client is active or shutting down (don't send confirmations
509 * to a client that is shutting down.
514 * ID of the client, mainly for debug messages
519 * Regular expressions describing the services offered by this client.
521 char **regexes; // FIXME add timeout? API to remove a regex?
524 * Number of regular expressions in regexes.
526 unsigned int n_regex;
529 * Task to refresh all regular expresions in the DHT.
531 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
537 * Struct to keep information of searches of services described by a regex
538 * using a user-provided string service description.
540 struct MeshRegexSearchInfo
543 * Which tunnel is this for
545 struct MeshTunnel *t;
548 * User provided description of the searched service.
553 * Part of the description already consumed by the search.
560 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
563 * Results from running DHT GETs.
565 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
568 * Contexts, for each running DHT GET. Free all on end of search.
570 struct MeshRegexSearchContext **contexts;
573 * Number of contexts (branches/steps in search).
575 unsigned int n_contexts;
578 * Peer that is connecting via connect_by_string. When connected, free ctx.
583 * Other peers that are found but not yet being connected to.
585 GNUNET_PEER_Id *peers;
588 * Number of elements in peers.
590 unsigned int n_peers;
593 * Next peer to try to connect to.
598 * Timeout for a connect attempt.
599 * When reached, try to connect to a different peer, if any. If not,
600 * try the same peer again.
602 GNUNET_SCHEDULER_TaskIdentifier timeout;
607 * Struct to keep state of running searches that have consumed a part of
610 struct MeshRegexSearchContext
613 * Part of the description already consumed by
614 * this particular search branch.
619 * Information about the search.
621 struct MeshRegexSearchInfo *info;
625 /******************************************************************************/
626 /************************ DEBUG FUNCTIONS ****************************/
627 /******************************************************************************/
631 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
632 * @param cls string to print
633 * @param success GNUNET_OK if the PUT was transmitted,
634 * GNUNET_NO on timeout,
635 * GNUNET_SYSERR on disconnect from service
636 * after the PUT message was transmitted
637 * (so we don't know if it was received or not)
642 mesh_debug (void *cls, int success)
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
652 /******************************************************************************/
653 /*********************** GLOBAL VARIABLES ****************************/
654 /******************************************************************************/
659 static struct MeshClient *clients;
660 static struct MeshClient *clients_tail;
663 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
665 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
668 * Tunnels incoming, indexed by MESH_TunnelNumber
669 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
671 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
674 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
676 static struct GNUNET_CONTAINER_MultiHashMap *peers;
679 * Handle to communicate with transport
681 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
684 * Handle to communicate with core
686 static struct GNUNET_CORE_Handle *core_handle;
691 static struct GNUNET_DHT_Handle *dht_handle;
696 static struct GNUNET_SERVER_Handle *server_handle;
699 * Notification context, to send messages to local clients
701 static struct GNUNET_SERVER_NotificationContext *nc;
704 * Local peer own ID (memory efficient handle)
706 static GNUNET_PEER_Id myid;
709 * Local peer own ID (full value)
711 static struct GNUNET_PeerIdentity my_full_id;
716 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
721 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
724 * Tunnel ID for the next created tunnel (global tunnel number)
726 static MESH_TunnelNumber next_tid;
729 * Tunnel ID for the next incoming tunnel (local tunnel number)
731 static MESH_TunnelNumber next_local_tid;
734 * All application types provided by this peer
736 static struct GNUNET_CONTAINER_MultiHashMap *applications;
739 * All message types clients of this peer are interested in
741 static struct GNUNET_CONTAINER_MultiHashMap *types;
744 * Task to periodically announce provided applications
746 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
749 * Task to periodically announce itself in the network
751 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
754 * Next ID to assign to a client
756 unsigned int next_client_id;
759 /******************************************************************************/
760 /*********************** DECLARATIONS **************************/
761 /******************************************************************************/
763 /* FIXME move declarations here */
766 * Function to process paths received for a new peer addition. The recorded
767 * paths form the initial tunnel, which can be optimized later.
768 * Called on each result obtained for the DHT search.
771 * @param exp when will this value expire
772 * @param key key of the result
773 * @param type type of the result
774 * @param size number of bytes in data
775 * @param data pointer to the result data
778 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
779 const struct GNUNET_HashCode * key,
780 const struct GNUNET_PeerIdentity *get_path,
781 unsigned int get_path_length,
782 const struct GNUNET_PeerIdentity *put_path,
783 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
784 size_t size, const void *data);
788 * Function to process DHT string to regex matching.
789 * Called on each result obtained for the DHT search.
791 * @param cls closure (search context)
792 * @param exp when will this value expire
793 * @param key key of the result
794 * @param get_path path of the get request (not used)
795 * @param get_path_length lenght of get_path (not used)
796 * @param put_path path of the put request (not used)
797 * @param put_path_length length of the put_path (not used)
798 * @param type type of the result
799 * @param size number of bytes in data
800 * @param data pointer to the result data
802 * TODO: re-issue the request after certain time? cancel after X results?
805 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
806 const struct GNUNET_HashCode * key,
807 const struct GNUNET_PeerIdentity *get_path,
808 unsigned int get_path_length,
809 const struct GNUNET_PeerIdentity *put_path,
810 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
811 size_t size, const void *data);
815 * Function to process DHT string to regex matching.
816 * Called on each result obtained for the DHT search.
818 * @param cls closure (search context)
819 * @param exp when will this value expire
820 * @param key key of the result
821 * @param get_path path of the get request (not used)
822 * @param get_path_length lenght of get_path (not used)
823 * @param put_path path of the put request (not used)
824 * @param put_path_length length of the put_path (not used)
825 * @param type type of the result
826 * @param size number of bytes in data
827 * @param data pointer to the result data
830 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
831 const struct GNUNET_HashCode * key,
832 const struct GNUNET_PeerIdentity *get_path,
833 unsigned int get_path_length,
834 const struct GNUNET_PeerIdentity *put_path,
835 unsigned int put_path_length,
836 enum GNUNET_BLOCK_Type type,
837 size_t size, const void *data);
841 * Iterator over edges in a regex block retrieved from the DHT.
843 * @param cls Closure.
844 * @param token Token that follows to next state.
845 * @param len Lenght of token.
846 * @param key Hash of next state.
848 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
851 regex_edge_iterator (void *cls,
854 const struct GNUNET_HashCode *key);
858 * Find a path to a peer that offers a regex servcie compatible
859 * with a given string.
861 * @param key The key of the accepting state.
862 * @param ctx Context containing info about the string, tunnel, etc.
865 regex_find_path (const struct GNUNET_HashCode *key,
866 struct MeshRegexSearchContext *ctx);
870 * Queue and pass message to core when possible.
872 * @param cls Closure (type dependant).
873 * @param type Type of the message.
874 * @param size Size of the message.
875 * @param dst Neighbor to send message to.
876 * @param t Tunnel this message belongs to.
879 queue_add (void *cls, uint16_t type, size_t size,
880 struct MeshPeerInfo *dst, struct MeshTunnel *t);
883 * Free a transmission that was already queued with all resources
884 * associated to the request.
886 * @param queue Queue handler to cancel.
887 * @param clear_cls Is it necessary to free associated cls?
890 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
892 /******************************************************************************/
893 /************************ ITERATORS ****************************/
894 /******************************************************************************/
896 /* FIXME move iterators here */
899 * Iterator over found existing mesh regex blocks that match an ongoing search.
902 * @param key current key code
903 * @param value value in the hash map
904 * @return GNUNET_YES if we should continue to iterate,
908 regex_result_iterator (void *cls,
909 const struct GNUNET_HashCode * key,
912 struct MeshRegexBlock *block = value;
913 struct MeshRegexSearchContext *ctx = cls;
915 if (GNUNET_YES == ntohl(block->accepting) &&
916 ctx->position == strlen (ctx->info->description))
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
919 regex_find_path (key, ctx);
920 return GNUNET_YES; // We found an accept state!
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
925 ctx->position, strlen(ctx->info->description),
926 ntohl(block->accepting));
929 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
930 ®ex_edge_iterator, ctx);
936 * Iterator over edges in a regex block retrieved from the DHT.
938 * @param cls Closure (context of the search).
939 * @param token Token that follows to next state.
940 * @param len Lenght of token.
941 * @param key Hash of next state.
943 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
946 regex_edge_iterator (void *cls,
949 const struct GNUNET_HashCode *key)
951 struct MeshRegexSearchContext *ctx = cls;
952 struct MeshRegexSearchContext *new_ctx;
953 struct MeshRegexSearchInfo *info = ctx->info;
954 struct GNUNET_DHT_GetHandle *get_h;
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
961 current = &info->description[ctx->position];
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
963 current_len = strlen (info->description) - ctx->position;
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
968 if (len > current_len)
970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
971 return GNUNET_YES; // Token too long, wont match
973 if (0 != strncmp (current, token, len))
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
976 return GNUNET_YES; // Token doesn't match
978 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
979 new_ctx->info = info;
980 new_ctx->position = ctx->position + len;
981 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
983 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
986 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
987 ®ex_result_iterator,
989 return GNUNET_YES; // We are already looking for it
991 /* Start search in DHT */
993 GNUNET_DHT_get_start (dht_handle, /* handle */
994 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
995 key, /* key to search */
996 DHT_REPLICATION_LEVEL, /* replication level */
997 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
998 NULL, /* xquery */ // FIXME BLOOMFILTER
999 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1000 &dht_get_string_handler, new_ctx);
1002 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1003 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1013 * Iterator over hash map entries to cancel DHT GET requests after a
1014 * successful connect_by_string.
1016 * @param cls Closure (unused).
1017 * @param key Current key code (unused).
1018 * @param value Value in the hash map (get handle).
1019 * @return GNUNET_YES if we should continue to iterate,
1023 regex_cancel_dht_get (void *cls,
1024 const struct GNUNET_HashCode * key,
1027 struct GNUNET_DHT_GetHandle *h = value;
1029 GNUNET_DHT_get_stop (h);
1035 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1036 * search for connect_by_string.
1038 * @param cls Closure (unused).
1039 * @param key Current key code (unused).
1040 * @param value MeshRegexBlock in the hash map.
1041 * @return GNUNET_YES if we should continue to iterate,
1045 regex_free_result (void *cls,
1046 const struct GNUNET_HashCode * key,
1050 GNUNET_free (value);
1056 * Regex callback iterator to store own service description in the DHT.
1058 * @param cls closure.
1059 * @param key hash for current state.
1060 * @param proof proof for current state.
1061 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1062 * @param num_edges number of edges leaving current state.
1063 * @param edges edges leaving current state.
1066 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1067 int accepting, unsigned int num_edges,
1068 const struct GNUNET_REGEX_Edge *edges)
1070 struct MeshRegexBlock *block;
1071 struct MeshRegexEdge *block_edge;
1072 enum GNUNET_DHT_RouteOption opt;
1076 unsigned int offset;
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 " regex dht put for state %s\n",
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1090 if (GNUNET_YES == accepting)
1092 struct MeshRegexAccept block;
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 " state %s is accepting, putting own id\n",
1097 size = sizeof (block);
1099 block.id = my_full_id;
1101 GNUNET_DHT_put(dht_handle, key,
1102 DHT_REPLICATION_LEVEL,
1103 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1104 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1107 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1112 len = strlen(proof);
1113 size = sizeof (struct MeshRegexBlock) + len;
1114 block = GNUNET_malloc (size);
1117 block->n_proof = htonl (len);
1118 block->n_edges = htonl (num_edges);
1119 block->accepting = htonl (accepting);
1121 /* Store the proof at the end of the block. */
1122 aux = (char *) &block[1];
1123 memcpy (aux, proof, len);
1126 /* Store each edge in a variable length MeshEdge struct at the
1127 * very end of the MeshRegexBlock structure.
1129 for (i = 0; i < num_edges; i++)
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 " edge %s towards %s\n",
1134 GNUNET_h2s(&edges[i].destination));
1136 /* aux points at the end of the last block */
1137 len = strlen (edges[i].label);
1138 size += sizeof (struct MeshRegexEdge) + len;
1139 // Calculate offset FIXME is this ok? use size instead?
1140 offset = aux - (char *) block;
1141 block = GNUNET_realloc (block, size);
1142 aux = &((char *) block)[offset];
1143 block_edge = (struct MeshRegexEdge *) aux;
1144 block_edge->key = edges[i].destination;
1145 block_edge->n_token = htonl (len);
1146 aux = (char *) &block_edge[1];
1147 memcpy (aux, edges[i].label, len);
1151 GNUNET_DHT_put(dht_handle, key,
1152 DHT_REPLICATION_LEVEL,
1154 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1156 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1160 GNUNET_free (block);
1165 * Store the regular expression describing a local service into the DHT.
1167 * @param regex The regular expresion.
1170 regex_put (const char *regex)
1172 struct GNUNET_REGEX_Automaton *dfa;
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1175 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1176 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1177 GNUNET_REGEX_automaton_destroy (dfa);
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1183 * Find a path to a peer that offers a regex servcie compatible
1184 * with a given string.
1186 * @param key The key of the accepting state.
1187 * @param ctx Context containing info about the string, tunnel, etc.
1190 regex_find_path (const struct GNUNET_HashCode *key,
1191 struct MeshRegexSearchContext *ctx)
1193 struct GNUNET_DHT_GetHandle *get_h;
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1196 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1197 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1198 key, /* key to search */
1199 DHT_REPLICATION_LEVEL, /* replication level */
1200 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1201 GNUNET_DHT_RO_RECORD_ROUTE,
1202 NULL, /* xquery */ // FIXME BLOOMFILTER
1203 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1204 &dht_get_string_accept_handler, ctx);
1205 GNUNET_break (GNUNET_OK ==
1206 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1209 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1213 /******************************************************************************/
1214 /************************ PERIODIC FUNCTIONS ****************************/
1215 /******************************************************************************/
1218 * Announce iterator over for each application provided by the peer
1220 * @param cls closure
1221 * @param key current key code
1222 * @param value value in the hash map
1223 * @return GNUNET_YES if we should continue to
1228 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1230 struct PBlock block;
1231 struct MeshClient *c;
1233 block.id = my_full_id;
1234 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1235 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1236 if (0 == block.type)
1241 block.type = htonl (block.type);
1242 /* FIXME are hashes in multihash map equal on all aquitectures? */
1243 /* FIXME: keep return value of 'put' to possibly cancel!? */
1244 GNUNET_DHT_put (dht_handle, key,
1245 DHT_REPLICATION_LEVEL,
1246 GNUNET_DHT_RO_RECORD_ROUTE |
1247 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1248 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1250 (const char *) &block,
1251 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1253 APP_ANNOUNCE_TIME, NULL, NULL);
1259 * Periodically announce what applications are provided by local clients
1262 * @param cls closure
1263 * @param tc task context
1266 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1268 struct MeshClient *c = cls;
1271 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1273 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1277 DEBUG_DHT ("Starting PUT for regex\n");
1279 for (i = 0; i < c->n_regex; i++)
1281 regex_put (c->regexes[i]);
1283 c->regex_announce_task =
1284 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_regex, cls);
1285 DEBUG_DHT ("Finished PUT for regex\n");
1292 * Periodically announce what applications are provided by local clients
1295 * @param cls closure
1296 * @param tc task context
1299 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1301 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1303 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1307 DEBUG_DHT ("Starting PUT for apps\n");
1309 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1311 announce_applications_task =
1312 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
1314 DEBUG_DHT ("Finished PUT for apps\n");
1321 * Periodically announce self id in the DHT
1323 * @param cls closure
1324 * @param tc task context
1327 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1329 struct PBlock block;
1331 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1333 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1337 * - Set data expiration in function of X
1338 * - Adapt X to churn
1340 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1342 block.id = my_full_id;
1343 block.type = htonl (0);
1344 GNUNET_DHT_put (dht_handle, /* DHT handle */
1345 &my_full_id.hashPubKey, /* Key to use */
1346 DHT_REPLICATION_LEVEL, /* Replication level */
1347 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1348 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1349 sizeof (block), /* Size of the data */
1350 (const char *) &block, /* Data itself */
1351 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1352 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1353 NULL, /* Continuation */
1354 NULL); /* Continuation closure */
1356 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
1360 /******************************************************************************/
1361 /****************** GENERAL HELPER FUNCTIONS ************************/
1362 /******************************************************************************/
1365 * Cancel an ongoing regex search in the DHT and free all resources.
1367 * @param ctx The search context.
1370 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1372 struct MeshRegexSearchInfo *info = ctx->info;
1375 GNUNET_free (info->description);
1376 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1377 ®ex_cancel_dht_get, NULL);
1378 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1379 ®ex_free_result, NULL);
1380 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1381 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1382 info->t->regex_ctx = NULL;
1383 for (i = 0; i < info->n_contexts; i++)
1385 GNUNET_free (info->contexts[i]);
1387 if (0 < info->n_contexts)
1388 GNUNET_free (info->contexts);
1389 if (0 < info->n_peers)
1390 GNUNET_free (info->peers);
1391 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1393 GNUNET_SCHEDULER_cancel(info->timeout);
1399 * Search for a tunnel by global ID using full PeerIdentities
1401 * @param oid owner of the tunnel
1402 * @param tid global tunnel number
1404 * @return tunnel handler, NULL if doesn't exist
1406 static struct MeshTunnel *
1407 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1411 * Delete an active client from the tunnel.
1417 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1420 * Notify a tunnel that a connection has broken that affects at least
1421 * some of its peers.
1423 * @param t Tunnel affected.
1424 * @param p1 Peer that got disconnected from p2.
1425 * @param p2 Peer that got disconnected from p1.
1427 * @return Short ID of the peer disconnected (either p1 or p2).
1428 * 0 if the tunnel remained unaffected.
1430 static GNUNET_PEER_Id
1431 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1436 * Check if client has registered with the service and has not disconnected
1438 * @param client the client to check
1440 * @return non-NULL if client exists in the global DLL
1442 static struct MeshClient *
1443 client_get (struct GNUNET_SERVER_Client *client)
1445 struct MeshClient *c;
1450 if (c->handle == client)
1459 * Checks if a given client has subscribed to certain message type
1461 * @param message_type Type of message to check
1462 * @param c Client to check
1464 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1469 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1471 struct GNUNET_HashCode hc;
1473 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1474 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1479 * Allow a client to send more data after transmitting a multicast message
1480 * which some neighbor has not yet accepted altough a reasonable time has
1483 * @param cls Closure (DataDescriptor containing the task identifier)
1484 * @param tc Task Context
1486 * FIXME reference counter cshould be just int
1489 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1491 struct MeshData *mdata = cls;
1493 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1495 GNUNET_assert (NULL != mdata->reference_counter);
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1498 *(mdata->reference_counter));
1499 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1500 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1505 * Check whether client wants traffic from a tunnel.
1507 * @param c Client to check.
1508 * @param t Tunnel to be found.
1510 * @return GNUNET_YES if client knows tunnel.
1512 * TODO look in client hashmap
1515 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1519 for (i = 0; i < t->nclients; i++)
1520 if (t->clients[i] == c)
1527 * Check whether client has been informed about a tunnel.
1529 * @param c Client to check.
1530 * @param t Tunnel to be found.
1532 * @return GNUNET_YES if client knows tunnel.
1534 * TODO look in client hashmap
1537 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1541 for (i = 0; i < t->nignore; i++)
1542 if (t->ignore[i] == c)
1544 return client_wants_tunnel(c, t);
1549 * Marks a client as uninterested in traffic from the tunnel, updating both
1550 * client and tunnel to reflect this.
1552 * @param c Client that doesn't want traffic anymore.
1553 * @param t Tunnel which should be ignored.
1555 * FIXME when to delete an incoming tunnel?
1558 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1560 struct GNUNET_HashCode hash;
1562 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1563 GNUNET_break (GNUNET_YES ==
1564 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1566 GNUNET_break (GNUNET_YES ==
1567 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1568 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1569 tunnel_delete_active_client (t, c);
1570 GNUNET_array_append (t->ignore, t->nignore, c);
1575 * Deletes a tunnel from a client (either owner or destination). To be used on
1576 * tunnel destroy, otherwise, use client_ignore_tunnel.
1578 * @param c Client whose tunnel to delete.
1579 * @param t Tunnel which should be deleted.
1582 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1584 struct GNUNET_HashCode hash;
1588 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1589 GNUNET_assert (GNUNET_YES ==
1590 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1596 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1598 GNUNET_assert (GNUNET_YES ==
1599 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1603 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1612 * Send the message to all clients that have subscribed to its type
1614 * @param msg Pointer to the message itself
1615 * @param payload Pointer to the payload of the message.
1616 * @return number of clients this message was sent to
1619 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1620 const struct GNUNET_MessageHeader *payload)
1622 struct GNUNET_PeerIdentity *oid;
1623 struct MeshClient *c;
1624 struct MeshTunnel *t;
1625 MESH_TunnelNumber *tid;
1628 char cbuf[htons (msg->size)];
1630 type = ntohs (payload->type);
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1634 memcpy (cbuf, msg, sizeof (cbuf));
1635 switch (htons (msg->type))
1637 struct GNUNET_MESH_Unicast *uc;
1638 struct GNUNET_MESH_Multicast *mc;
1639 struct GNUNET_MESH_ToOrigin *to;
1641 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1642 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1646 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1647 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1651 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1652 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1660 t = tunnel_get (oid, ntohl (*tid));
1667 for (count = 0, c = clients; c != NULL; c = c->next)
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1670 if (client_is_subscribed (type, c))
1672 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1676 *tid = htonl (t->local_tid);
1680 if (GNUNET_NO == client_knows_tunnel (c, t))
1682 /* This client doesn't know the tunnel */
1683 struct GNUNET_MESH_TunnelNotification tmsg;
1684 struct GNUNET_HashCode hash;
1686 tmsg.header.size = htons (sizeof (tmsg));
1687 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1688 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1689 tmsg.tunnel_id = htonl (t->local_tid_dest);
1690 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1691 &tmsg.header, GNUNET_NO);
1692 GNUNET_array_append (t->clients, t->nclients, c);
1693 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1695 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1696 c->incoming_tunnels, &hash, t,
1697 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1699 *tid = htonl (t->local_tid_dest);
1702 /* Check if the client wants to get traffic from the tunnel */
1703 if (GNUNET_NO == client_wants_tunnel(c, t))
1706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1707 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1708 (struct GNUNET_MessageHeader
1709 *) cbuf, GNUNET_YES);
1717 * Notify the client that owns the tunnel that a peer has connected to it
1718 * (the requested path to it has been confirmed).
1720 * @param t Tunnel whose owner to notify
1721 * @param id Short id of the peer that has connected
1724 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1726 struct GNUNET_MESH_PeerControl pc;
1728 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1729 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1730 pc.tunnel_id = htonl (t->local_tid);
1731 GNUNET_PEER_resolve (id, &pc.peer);
1732 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1738 * Notify all clients (not depending on registration status) that the incoming
1739 * tunnel is no longer valid.
1741 * @param t Tunnel that was destroyed.
1744 send_clients_tunnel_destroy (struct MeshTunnel *t)
1746 struct GNUNET_MESH_TunnelMessage msg;
1748 msg.header.size = htons (sizeof (msg));
1749 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1750 msg.tunnel_id = htonl (t->local_tid_dest);
1751 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1756 * Notify clients of tunnel disconnections, if needed.
1757 * In case the origin disconnects, the destination clients get a tunnel destroy
1758 * notification. If the last destination disconnects (only one remaining client
1759 * in tunnel), the origin gets a (local ID) peer disconnected.
1760 * Note that the function must be called BEFORE removing the client from
1763 * @param t Tunnel that was destroyed.
1764 * @param c Client that disconnected.
1767 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1773 struct GNUNET_MESH_TunnelMessage msg;
1775 msg.header.size = htons (sizeof (msg));
1776 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1777 msg.tunnel_id = htonl (t->local_tid_dest);
1778 for (i = 0; i < t->nclients; i++)
1779 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1780 &msg.header, GNUNET_NO);
1782 // FIXME when to disconnect an incoming tunnel?
1783 else if (1 == t->nclients && NULL != t->owner)
1785 struct GNUNET_MESH_PeerControl msg;
1787 msg.header.size = htons (sizeof (msg));
1788 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1789 msg.tunnel_id = htonl (t->local_tid);
1790 msg.peer = my_full_id;
1791 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1792 &msg.header, GNUNET_NO);
1798 * Decrements the reference counter and frees all resources if needed
1800 * @param mesh_data Data Descriptor used in a multicast message.
1801 * Freed no longer needed (last message).
1804 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1806 /* Make sure it's a multicast packet */
1807 GNUNET_assert (NULL != mesh_data->reference_counter);
1809 if (0 == --(*(mesh_data->reference_counter)))
1811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1812 if (NULL != mesh_data->task)
1814 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1816 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1818 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1820 GNUNET_free (mesh_data->task);
1822 GNUNET_free (mesh_data->reference_counter);
1823 GNUNET_free (mesh_data->data);
1824 GNUNET_free (mesh_data);
1830 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1831 * and insert it in the appropiate structures if the peer is not known yet.
1833 * @param peer Full identity of the peer.
1835 * @return Existing or newly created peer info.
1837 static struct MeshPeerInfo *
1838 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1840 struct MeshPeerInfo *peer_info;
1842 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1843 if (NULL == peer_info)
1846 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1847 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1848 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1849 peer_info->id = GNUNET_PEER_intern (peer);
1857 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1858 * and insert it in the appropiate structures if the peer is not known yet.
1860 * @param peer Short identity of the peer.
1862 * @return Existing or newly created peer info.
1864 static struct MeshPeerInfo *
1865 peer_info_get_short (const GNUNET_PEER_Id peer)
1867 struct GNUNET_PeerIdentity id;
1869 GNUNET_PEER_resolve (peer, &id);
1870 return peer_info_get (&id);
1875 * Iterator to remove the tunnel from the list of tunnels a peer participates
1878 * @param cls Closure (tunnel info)
1879 * @param key GNUNET_PeerIdentity of the peer (unused)
1880 * @param value PeerInfo of the peer
1882 * @return always GNUNET_YES, to keep iterating
1885 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1887 struct MeshTunnel *t = cls;
1888 struct MeshPeerInfo *peer = value;
1891 for (i = 0; i < peer->ntunnels; i++)
1894 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1897 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1898 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1907 * Core callback to write a pre-constructed data packet to core buffer
1909 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1910 * @param size Number of bytes available in buf.
1911 * @param buf Where the to write the message.
1913 * @return number of bytes written to buf
1916 send_core_data_raw (void *cls, size_t size, void *buf)
1918 struct MeshTransmissionDescriptor *info = cls;
1919 struct GNUNET_MessageHeader *msg;
1922 GNUNET_assert (NULL != info);
1923 GNUNET_assert (NULL != info->mesh_data);
1924 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1925 total_size = ntohs (msg->size);
1927 if (total_size > size)
1932 memcpy (buf, msg, total_size);
1933 data_descriptor_decrement_rc (info->mesh_data);
1940 * Sends an already built unicast message to a peer, properly registrating
1941 * all used resources.
1943 * @param message Message to send. Function makes a copy of it.
1944 * @param peer Short ID of the neighbor whom to send the message.
1945 * @param t Tunnel on which this message is transmitted.
1948 send_message (const struct GNUNET_MessageHeader *message,
1949 const struct GNUNET_PeerIdentity *peer,
1950 struct MeshTunnel *t)
1952 struct MeshTransmissionDescriptor *info;
1953 struct MeshPeerInfo *neighbor;
1954 struct MeshPeerPath *p;
1957 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1959 size = ntohs (message->size);
1960 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1961 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1962 info->mesh_data->data = GNUNET_malloc (size);
1963 memcpy (info->mesh_data->data, message, size);
1964 info->mesh_data->data_len = size;
1965 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
1966 *info->mesh_data->reference_counter = 1;
1967 neighbor = peer_info_get (peer);
1968 for (p = neighbor->path_head; NULL != p; p = p->next)
1977 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1978 GNUNET_free (info->mesh_data->data);
1979 GNUNET_free (info->mesh_data);
1983 info->peer = neighbor;
1985 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
1993 * Sends a CREATE PATH message for a path to a peer, properly registrating
1994 * all used resources.
1996 * @param peer PeerInfo of the final peer for whom this path is being created.
1997 * @param p Path itself.
1998 * @param t Tunnel for which the path is created.
2001 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2002 struct MeshTunnel *t)
2004 struct GNUNET_PeerIdentity id;
2005 struct MeshPathInfo *path_info;
2006 struct MeshPeerInfo *neighbor;
2012 p = tree_get_path_to_peer (t->tree, peer->id);
2019 for (i = 0; i < p->length; i++)
2021 if (p->peers[i] == myid)
2024 if (i >= p->length - 1)
2030 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2032 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2033 path_info->path = p;
2035 neighbor = peer_info_get (&id);
2036 path_info->peer = neighbor;
2037 queue_add (path_info,
2038 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2039 sizeof (struct GNUNET_MESH_ManipulatePath) +
2040 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2047 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2049 * @param t Tunnel whose path to destroy.
2050 * @param destination Short ID of the peer to whom the path to destroy.
2053 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2055 struct MeshPeerPath *p;
2058 p = tree_get_path_to_peer (t->tree, destination);
2064 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2065 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2067 struct GNUNET_MESH_ManipulatePath *msg;
2068 struct GNUNET_PeerIdentity *pi;
2072 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2073 msg->header.size = htons (size);
2074 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2075 msg->tid = htonl (t->id.tid);
2076 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2077 for (i = 0; i < p->length; i++)
2079 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2081 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2088 * Try to establish a new connection to this peer.
2089 * Use the best path for the given tunnel.
2090 * If the peer doesn't have any path to it yet, try to get one.
2091 * If the peer already has some path, send a CREATE PATH towards it.
2093 * @param peer PeerInfo of the peer.
2094 * @param t Tunnel for which to create the path, if possible.
2097 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2099 struct MeshPeerPath *p;
2100 struct MeshPathInfo *path_info;
2102 if (NULL != peer->path_head)
2104 p = tree_get_path_to_peer (t->tree, peer->id);
2111 // FIXME always send create path to self
2114 send_create_path (peer, p, t);
2118 struct GNUNET_HashCode hash;
2121 send_client_peer_connected (t, myid);
2122 t->local_tid_dest = next_local_tid++;
2123 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2126 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2127 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2134 else if (NULL == peer->dhtget)
2136 struct GNUNET_PeerIdentity id;
2138 GNUNET_PEER_resolve (peer->id, &id);
2139 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2140 path_info->peer = peer;
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2143 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2144 peer->dhtgetcls = path_info;
2145 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2146 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2147 &id.hashPubKey, /* key to search */
2148 DHT_REPLICATION_LEVEL, /* replication level */
2149 GNUNET_DHT_RO_RECORD_ROUTE |
2150 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2151 NULL, /* xquery */ // FIXME BLOOMFILTER
2152 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2153 &dht_get_id_handler, path_info);
2155 /* Otherwise, there is no path but the DHT get is already started. */
2160 * Task to delay the connection of a peer
2162 * @param cls Closure (path info with tunnel and peer to connect).
2163 * Will be free'd on exection.
2164 * @param tc TaskContext
2167 peer_info_connect_task (void *cls,
2168 const struct GNUNET_SCHEDULER_TaskContext *tc)
2170 struct MeshPathInfo *path_info = cls;
2172 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2177 peer_info_connect (path_info->peer, path_info->t);
2183 * Destroy the peer_info and free any allocated resources linked to it
2185 * @param pi The peer_info to destroy.
2187 * @return GNUNET_OK on success
2190 peer_info_destroy (struct MeshPeerInfo *pi)
2192 struct GNUNET_PeerIdentity id;
2193 struct MeshPeerPath *p;
2194 struct MeshPeerPath *nextp;
2196 GNUNET_PEER_resolve (pi->id, &id);
2197 GNUNET_PEER_change_rc (pi->id, -1);
2200 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2204 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2206 if (NULL != pi->dhtget)
2208 GNUNET_DHT_get_stop (pi->dhtget);
2209 GNUNET_free (pi->dhtgetcls);
2215 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2225 * Remove all paths that rely on a direct connection between p1 and p2
2226 * from the peer itself and notify all tunnels about it.
2228 * @param peer PeerInfo of affected peer.
2229 * @param p1 GNUNET_PEER_Id of one peer.
2230 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2233 * TODO: optimize (see below)
2236 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2239 struct MeshPeerPath *p;
2240 struct MeshPeerPath *aux;
2241 struct MeshPeerInfo *peer_d;
2243 unsigned int destroyed;
2248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2250 p = peer->path_head;
2254 for (i = 0; i < (p->length - 1); i++)
2256 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2257 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2259 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2270 for (i = 0; i < peer->ntunnels; i++)
2272 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2276 * Problem: one or more peers have been deleted from the tunnel tree.
2277 * We don't know who they are to try to add them again.
2278 * We need to try to find a new path for each of the disconnected peers.
2279 * Some of them might already have a path to reach them that does not
2280 * involve p1 and p2. Adding all anew might render in a better tree than
2281 * the trivial immediate fix.
2283 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2284 * its children will be reachable trough him.
2286 peer_d = peer_info_get_short (d);
2289 for (p = peer_d->path_head; NULL != p; p = p->next)
2291 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2299 /* No callback, as peer will be already disconnected and a connection
2300 * scheduled by tunnel_notify_connection_broken.
2302 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2306 peer_info_connect (peer_d, peer->tunnels[i]);
2309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2314 * Add the path to the peer and update the path used to reach it in case this
2317 * @param peer_info Destination peer to add the path to.
2318 * @param path New path to add. Last peer must be the peer in arg 1.
2319 * Path will be either used of freed if already known.
2320 * @param trusted Do we trust that this path is real?
2323 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2326 struct MeshPeerPath *aux;
2330 if ((NULL == peer_info) || (NULL == path))
2333 path_destroy (path);
2336 if (path->peers[path->length - 1] != peer_info->id)
2339 path_destroy (path);
2342 if (path->length <= 2 && GNUNET_NO == trusted)
2344 /* Only allow CORE to tell us about direct paths */
2345 path_destroy (path);
2348 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2349 for (l = 1; l < path->length; l++)
2351 if (path->peers[l] == myid)
2353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2354 for (l2 = 0; l2 < path->length - l; l2++)
2356 path->peers[l2] = path->peers[l + l2];
2361 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2366 struct GNUNET_PeerIdentity id;
2368 GNUNET_PEER_resolve (peer_info->id, &id);
2369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2370 path->length, GNUNET_i2s (&id));
2373 l = path_get_length (path);
2380 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2381 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2383 l2 = path_get_length (aux);
2386 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2387 peer_info->path_tail, aux, path);
2392 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2394 path_destroy (path);
2399 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2406 * Add the path to the origin peer and update the path used to reach it in case
2407 * this is the shortest.
2408 * The path is given in peer_info -> destination, therefore we turn the path
2409 * upside down first.
2411 * @param peer_info Peer to add the path to, being the origin of the path.
2412 * @param path New path to add after being inversed.
2413 * @param trusted Do we trust that this path is real?
2416 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2417 struct MeshPeerPath *path, int trusted)
2420 peer_info_add_path (peer_info, path, trusted);
2425 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2426 * to obtain a local peer -> destination path and interning the peer ids.
2428 * @return Newly allocated and created path
2430 static struct MeshPeerPath *
2431 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2432 unsigned int get_path_length,
2433 const struct GNUNET_PeerIdentity *put_path,
2434 unsigned int put_path_length)
2436 struct MeshPeerPath *p;
2442 GNUNET_PEER_change_rc (myid, 1);
2443 i = get_path_length;
2444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2445 for (i--; i >= 0; i--)
2447 id = GNUNET_PEER_intern (&get_path[i]);
2448 if (p->length > 0 && id == p->peers[p->length - 1])
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2451 GNUNET_PEER_change_rc (id, -1);
2455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2456 GNUNET_i2s (&get_path[i]));
2458 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2459 p->peers[p->length - 1] = id;
2462 i = put_path_length;
2463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2464 for (i--; i >= 0; i--)
2466 id = GNUNET_PEER_intern (&put_path[i]);
2469 /* PUT path went through us, so discard the path up until now and start
2470 * from here to get a much shorter (and loop-free) path.
2475 if (p->length > 0 && id == p->peers[p->length - 1])
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2478 GNUNET_PEER_change_rc (id, -1);
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2483 GNUNET_i2s (&put_path[i]));
2485 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2486 p->peers[p->length - 1] = id;
2490 if (get_path_length > 0)
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2492 GNUNET_i2s (&get_path[0]));
2493 if (put_path_length > 0)
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2495 GNUNET_i2s (&put_path[0]));
2496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2498 for (i = 0; i < p->length; i++)
2500 struct GNUNET_PeerIdentity peer_id;
2502 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2504 GNUNET_i2s (&peer_id));
2512 * Adds a path to the peer_infos of all the peers in the path
2514 * @param p Path to process.
2515 * @param confirmed Whether we know if the path works or not. FIXME use
2518 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2522 /* TODO: invert and add */
2523 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2524 for (i++; i < p->length; i++)
2526 struct MeshPeerInfo *aux;
2527 struct MeshPeerPath *copy;
2529 aux = peer_info_get_short (p->peers[i]);
2530 copy = path_duplicate (p);
2531 copy->length = i + 1;
2532 peer_info_add_path (aux, copy, GNUNET_NO);
2538 * Send keepalive packets for a peer
2540 * @param cls Closure (tunnel for which to send the keepalive).
2541 * @param tc Notification context.
2543 * TODO: implement explicit multicast keepalive?
2546 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2550 * Search for a tunnel among the incoming tunnels
2552 * @param tid the local id of the tunnel
2554 * @return tunnel handler, NULL if doesn't exist
2556 static struct MeshTunnel *
2557 tunnel_get_incoming (MESH_TunnelNumber tid)
2559 struct GNUNET_HashCode hash;
2561 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2562 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2563 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2568 * Search for a tunnel among the tunnels for a client
2570 * @param c the client whose tunnels to search in
2571 * @param tid the local id of the tunnel
2573 * @return tunnel handler, NULL if doesn't exist
2575 static struct MeshTunnel *
2576 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2578 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2580 return tunnel_get_incoming (tid);
2584 struct GNUNET_HashCode hash;
2586 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2587 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2593 * Search for a tunnel by global ID using PEER_ID
2595 * @param pi owner of the tunnel
2596 * @param tid global tunnel number
2598 * @return tunnel handler, NULL if doesn't exist
2600 static struct MeshTunnel *
2601 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2603 struct MESH_TunnelID id;
2604 struct GNUNET_HashCode hash;
2609 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2610 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2615 * Search for a tunnel by global ID using full PeerIdentities
2617 * @param oid owner of the tunnel
2618 * @param tid global tunnel number
2620 * @return tunnel handler, NULL if doesn't exist
2622 static struct MeshTunnel *
2623 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2625 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2630 * Delete an active client from the tunnel.
2636 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2640 for (i = 0; i < t->nclients; i++)
2642 if (t->clients[i] == c)
2644 t->clients[i] = t->clients[t->nclients - 1];
2645 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2653 * Delete an ignored client from the tunnel.
2659 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2663 for (i = 0; i < t->nignore; i++)
2665 if (t->ignore[i] == c)
2667 t->ignore[i] = t->ignore[t->nignore - 1];
2668 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2676 * Delete a client from the tunnel. It should be only done on
2677 * client disconnection, otherwise use client_ignore_tunnel.
2683 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2685 tunnel_delete_ignored_client (t, c);
2686 tunnel_delete_active_client (t, c);
2691 * Callback used to notify a client owner of a tunnel that a peer has
2692 * disconnected, most likely because of a path change.
2694 * @param cls Closure (tunnel this notification is about).
2695 * @param peer_id Short ID of disconnected peer.
2698 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2700 struct MeshTunnel *t = cls;
2701 struct MeshPeerInfo *peer;
2702 struct MeshPathInfo *path_info;
2704 if (NULL != t->owner && NULL != nc)
2706 struct GNUNET_MESH_PeerControl msg;
2708 msg.header.size = htons (sizeof (msg));
2709 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2710 msg.tunnel_id = htonl (t->local_tid);
2711 GNUNET_PEER_resolve (peer_id, &msg.peer);
2712 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2713 &msg.header, GNUNET_NO);
2715 peer = peer_info_get_short (peer_id);
2716 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2717 path_info->peer = peer;
2719 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2724 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2725 * needed rescources.
2726 * If peer already exists, reevaluate shortest path and change if different.
2728 * @param t Tunnel we want to add a new peer to
2729 * @param peer PeerInfo of the peer being added
2733 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2735 struct GNUNET_PeerIdentity id;
2736 struct MeshPeerPath *best_p;
2737 struct MeshPeerPath *p;
2738 unsigned int best_cost;
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2742 GNUNET_PEER_resolve (peer->id, &id);
2744 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2747 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2748 GNUNET_assert (GNUNET_OK ==
2749 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2751 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2754 if (NULL != (p = peer->path_head))
2757 best_cost = tree_get_path_cost (t->tree, p);
2760 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2767 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2768 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2769 t->path_refresh_task =
2770 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2774 /* Start a DHT get */
2775 peer_info_connect (peer, t);
2777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2781 * Add a path to a tunnel which we don't own, just to remember the next hop.
2782 * If destination node was already in the tunnel, the first hop information
2783 * will be replaced with the new path.
2785 * @param t Tunnel we want to add a new peer to
2786 * @param p Path to add
2787 * @param own_pos Position of local node in path.
2791 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2792 unsigned int own_pos)
2794 struct GNUNET_PeerIdentity id;
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2797 GNUNET_assert (0 != own_pos);
2798 tree_add_path (t->tree, p, NULL, NULL);
2799 if (own_pos < p->length - 1)
2801 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2802 tree_update_first_hops (t->tree, myid, &id);
2804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2809 * Notifies a tunnel that a connection has broken that affects at least
2810 * some of its peers. Sends a notification towards the root of the tree.
2811 * In case the peer is the owner of the tree, notifies the client that owns
2812 * the tunnel and tries to reconnect.
2814 * @param t Tunnel affected.
2815 * @param p1 Peer that got disconnected from p2.
2816 * @param p2 Peer that got disconnected from p1.
2818 * @return Short ID of the peer disconnected (either p1 or p2).
2819 * 0 if the tunnel remained unaffected.
2821 static GNUNET_PEER_Id
2822 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2828 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2830 if (myid != p1 && myid != p2)
2836 if (tree_get_predecessor (t->tree) != 0)
2838 /* We are the peer still connected, notify owner of the disconnection. */
2839 struct GNUNET_MESH_PathBroken msg;
2840 struct GNUNET_PeerIdentity neighbor;
2842 msg.header.size = htons (sizeof (msg));
2843 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2844 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2845 msg.tid = htonl (t->id.tid);
2846 msg.peer1 = my_full_id;
2847 GNUNET_PEER_resolve (pid, &msg.peer2);
2848 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2849 send_message (&msg.header, &neighbor, t);
2857 * Send a multicast packet to a neighbor.
2859 * @param cls Closure (Info about the multicast packet)
2860 * @param neighbor_id Short ID of the neighbor to send the packet to.
2863 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2865 struct MeshData *mdata = cls;
2866 struct MeshTransmissionDescriptor *info;
2867 struct GNUNET_PeerIdentity neighbor;
2869 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2871 info->mesh_data = mdata;
2872 (*(mdata->reference_counter)) ++;
2873 info->destination = neighbor_id;
2874 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2876 GNUNET_i2s (&neighbor));
2877 info->peer = peer_info_get (&neighbor);
2878 GNUNET_assert (NULL != info->peer);
2880 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2881 info->mesh_data->data_len,
2887 * Send a message in a tunnel in multicast, sending a copy to each child node
2888 * down the local one in the tunnel tree.
2890 * @param t Tunnel in which to send the data.
2891 * @param msg Message to be sent.
2892 * @param internal Has the service generated this message?
2895 tunnel_send_multicast (struct MeshTunnel *t,
2896 const struct GNUNET_MessageHeader *msg,
2899 struct MeshData *mdata;
2901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2902 " sending a multicast packet...\n");
2903 mdata = GNUNET_malloc (sizeof (struct MeshData));
2904 mdata->data_len = ntohs (msg->size);
2905 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2907 mdata->data = GNUNET_malloc (mdata->data_len);
2908 memcpy (mdata->data, msg, mdata->data_len);
2909 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2911 struct GNUNET_MESH_Multicast *mcast;
2913 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2914 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2916 ntohl (mcast->ttl));
2920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2922 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2923 && GNUNET_NO == internal)
2925 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2927 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2933 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2934 if (*(mdata->reference_counter) == 0)
2936 GNUNET_free (mdata->data);
2937 GNUNET_free (mdata->reference_counter);
2938 if (NULL != mdata->task)
2940 GNUNET_SCHEDULER_cancel(*(mdata->task));
2941 GNUNET_free (mdata->task);
2942 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2944 // FIXME change order?
2945 GNUNET_free (mdata);
2947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2948 " sending a multicast packet done\n");
2954 * Send a message to all peers in this tunnel that the tunnel is no longer
2957 * @param t The tunnel whose peers to notify.
2960 tunnel_send_destroy (struct MeshTunnel *t)
2962 struct GNUNET_MESH_TunnelDestroy msg;
2964 msg.header.size = htons (sizeof (msg));
2965 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2966 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2967 msg.tid = htonl (t->id.tid);
2968 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2972 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
2974 * @param cls Closure (Tunnel which to cancel).
2975 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
2978 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
2980 struct MeshTunnel *t = cls;
2981 struct MeshPeerInfo *peer_info;
2982 struct MeshPeerQueue *pq;
2983 struct MeshPeerQueue *next;
2985 peer_info = peer_info_get_short (neighbor_id);
2986 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2989 if (pq->tunnel == t)
2991 queue_destroy (pq, GNUNET_YES);
2994 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2996 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2997 peer_info->core_transmit = NULL;
3002 * Destroy the tunnel and free any allocated resources linked to it.
3004 * @param t the tunnel to destroy
3006 * @return GNUNET_OK on success
3009 tunnel_destroy (struct MeshTunnel *t)
3011 struct MeshClient *c;
3012 struct GNUNET_HashCode hash;
3019 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3025 struct GNUNET_PeerIdentity id;
3027 GNUNET_PEER_resolve (t->id.oid, &id);
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3029 GNUNET_i2s (&id), t->id.tid);
3031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3035 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3036 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3041 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3044 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3048 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3049 for (i = 0; i < t->nclients; i++)
3053 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3058 for (i = 0; i < t->nignore; i++)
3062 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3067 if (t->nclients > 0)
3070 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3074 GNUNET_free (t->clients);
3076 if (NULL != t->peers)
3078 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3080 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3083 tree_destroy (t->tree);
3085 if (NULL != t->regex_ctx)
3086 regex_cancel_search (t->regex_ctx);
3087 if (NULL != t->dht_get_type)
3088 GNUNET_DHT_get_stop (t->dht_get_type);
3089 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3090 GNUNET_SCHEDULER_cancel (t->timeout_task);
3091 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3092 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3100 * Create a new tunnel
3102 * @param owner Who is the owner of the tunnel (short ID).
3103 * @param tid Tunnel Number of the tunnel.
3104 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3105 * @param local Tunnel Number for the tunnel, for the client point of view.
3108 static struct MeshTunnel *
3109 tunnel_new (GNUNET_PEER_Id owner,
3110 MESH_TunnelNumber tid,
3111 struct MeshClient *client,
3112 MESH_TunnelNumber local)
3114 struct MeshTunnel *t;
3115 struct GNUNET_HashCode hash;
3117 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3120 t->queue_max = 1000; // FIXME API parameter
3121 t->tree = tree_new (owner);
3123 t->local_tid = local;
3125 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3127 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3128 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3133 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3139 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3141 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3142 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3146 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3156 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3157 * that are no longer needed, as well as nodes of no longer reachable peers.
3158 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3160 * @param t Tunnel from which to remove the path.
3161 * @param peer Short id of the peer which should be removed.
3164 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3166 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3172 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3173 * client when the client disconnects. If the client is not the owner, the
3174 * owner will get notified if no more clients are in the tunnel and the client
3175 * get removed from the tunnel's list.
3177 * @param cls closure (client that is disconnecting)
3178 * @param key the hash of the local tunnel id (used to access the hashmap)
3179 * @param value the value stored at the key (tunnel to destroy)
3181 * @return GNUNET_OK on success
3184 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3186 struct MeshTunnel *t = value;
3187 struct MeshClient *c = cls;
3190 send_client_tunnel_disconnect(t, c);
3193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3194 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3195 tunnel_delete_client(t, c);
3196 client_delete_tunnel(c, t);
3199 tunnel_send_destroy(t);
3200 r = tunnel_destroy (t);
3206 * Timeout function, destroys tunnel if called
3208 * @param cls Closure (tunnel to destroy).
3209 * @param tc TaskContext
3212 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3214 struct MeshTunnel *t = cls;
3216 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3218 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3223 * Resets the tunnel timeout. Starts it if no timeout was running.
3225 * @param t Tunnel whose timeout to reset.
3228 tunnel_reset_timeout (struct MeshTunnel *t)
3230 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3231 GNUNET_SCHEDULER_cancel (t->timeout_task);
3233 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3234 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
3238 /******************************************************************************/
3239 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3240 /******************************************************************************/
3243 * Function to send a create path packet to a peer.
3245 * @param cls closure
3246 * @param size number of bytes available in buf
3247 * @param buf where the callee should write the message
3248 * @return number of bytes written to buf
3251 send_core_path_create (void *cls, size_t size, void *buf)
3253 struct MeshPathInfo *info = cls;
3254 struct GNUNET_MESH_ManipulatePath *msg;
3255 struct GNUNET_PeerIdentity *peer_ptr;
3256 struct MeshTunnel *t = info->t;
3257 struct MeshPeerPath *p = info->path;
3261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3263 sizeof (struct GNUNET_MESH_ManipulatePath) +
3264 p->length * sizeof (struct GNUNET_PeerIdentity);
3266 if (size < size_needed || NULL == buf)
3271 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3272 msg->header.size = htons (size_needed);
3273 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3274 msg->tid = ntohl (t->id.tid);
3276 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3277 for (i = 0; i < p->length; i++)
3279 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3286 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3292 * Fill the core buffer
3294 * @param cls closure (data itself)
3295 * @param size number of bytes available in buf
3296 * @param buf where the callee should write the message
3298 * @return number of bytes written to buf
3301 send_core_data_multicast (void *cls, size_t size, void *buf)
3303 struct MeshTransmissionDescriptor *info = cls;
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3307 GNUNET_assert (NULL != info);
3308 GNUNET_assert (NULL != info->peer);
3309 total_size = info->mesh_data->data_len;
3310 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3312 if (total_size > size)
3317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3318 memcpy (buf, info->mesh_data->data, total_size);
3321 struct GNUNET_MESH_Multicast *mc;
3322 struct GNUNET_MessageHeader *mh;
3325 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3327 mc = (struct GNUNET_MESH_Multicast *) mh;
3328 mh = (struct GNUNET_MessageHeader *) &mc[1];
3329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3330 " multicast, payload type %u\n", ntohs (mh->type));
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3332 " multicast, payload size %u\n", ntohs (mh->size));
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3341 data_descriptor_decrement_rc (info->mesh_data);
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3350 * Creates a path ack message in buf and frees all unused resources.
3352 * @param cls closure (MeshTransmissionDescriptor)
3353 * @param size number of bytes available in buf
3354 * @param buf where the callee should write the message
3355 * @return number of bytes written to buf
3358 send_core_path_ack (void *cls, size_t size, void *buf)
3360 struct MeshTransmissionDescriptor *info = cls;
3361 struct GNUNET_MESH_PathACK *msg = buf;
3363 GNUNET_assert (NULL != info);
3364 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3369 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3370 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3371 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3372 msg->tid = htonl (info->origin->tid);
3373 msg->peer_id = my_full_id;
3376 /* TODO add signature */
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3379 return sizeof (struct GNUNET_MESH_PathACK);
3384 * Free a transmission that was already queued with all resources
3385 * associated to the request.
3387 * @param queue Queue handler to cancel.
3388 * @param clear_cls Is it necessary to free associated cls?
3391 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3393 struct MeshTransmissionDescriptor *dd;
3394 struct MeshPathInfo *path_info;
3396 if (GNUNET_YES == clear_cls)
3398 switch (queue->type)
3400 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3401 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3402 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
3405 data_descriptor_decrement_rc (dd->mesh_data);
3407 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3409 path_info = queue->cls;
3410 path_destroy (path_info->path);
3414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
3416 GNUNET_free_non_null (queue->cls);
3418 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3419 queue->peer->queue_tail,
3421 GNUNET_free (queue);
3426 * Core callback to write a queued packet to core buffer
3428 * @param cls Closure (peer info).
3429 * @param size Number of bytes available in buf.
3430 * @param buf Where the to write the message.
3432 * @return number of bytes written to buf
3435 queue_send (void *cls, size_t size, void *buf)
3437 struct MeshPeerInfo *peer = cls;
3438 struct MeshPeerQueue *queue;
3439 struct MeshTunnel *t;
3442 peer->core_transmit = NULL;
3443 queue = peer->queue_head;
3445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3447 /* If queue is empty, send should have been cancelled */
3453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3455 /* Check if buffer size is enough for the message */
3456 if (queue->size > size)
3458 struct GNUNET_PeerIdentity id;
3460 GNUNET_PEER_resolve (peer->id, &id);
3461 peer->core_transmit =
3462 GNUNET_CORE_notify_transmit_ready(core_handle,
3465 GNUNET_TIME_UNIT_FOREVER_REL,
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3475 switch (queue->type)
3477 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
3479 data_size = send_core_data_raw (queue->cls, size, buf);
3481 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
3483 data_size = send_core_data_multicast(queue->cls, size, buf);
3485 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3487 data_size = send_core_path_create(queue->cls, size, buf);
3489 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3491 data_size = send_core_path_ack(queue->cls, size, buf);
3495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
3502 /* Free queue, but cls was freed by send_core_* */
3503 queue_destroy(queue, GNUNET_NO);
3505 if (GNUNET_YES == t->destroy && 0 == t->queue_n)
3507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3511 /* If more data in queue, send next */
3512 if (NULL != peer->queue_head)
3514 struct GNUNET_PeerIdentity id;
3516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3517 GNUNET_PEER_resolve (peer->id, &id);
3518 peer->core_transmit =
3519 GNUNET_CORE_notify_transmit_ready(core_handle,
3522 GNUNET_TIME_UNIT_FOREVER_REL,
3524 peer->queue_head->size,
3528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3534 * Queue and pass message to core when possible.
3536 * @param cls Closure (type dependant).
3537 * @param type Type of the message.
3538 * @param size Size of the message.
3539 * @param dst Neighbor to send message to.
3540 * @param t Tunnel this message belongs to.
3543 queue_add (void *cls, uint16_t type, size_t size,
3544 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3546 struct MeshPeerQueue *queue;
3548 if (t->queue_n >= t->queue_max)
3550 if (NULL == t->owner)
3551 GNUNET_break_op(0); // TODO: kill connection?
3554 return; // Drop message
3557 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3563 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3564 if (NULL == dst->core_transmit)
3566 struct GNUNET_PeerIdentity id;
3568 GNUNET_PEER_resolve (dst->id, &id);
3569 dst->core_transmit =
3570 GNUNET_CORE_notify_transmit_ready(core_handle,
3573 GNUNET_TIME_UNIT_FOREVER_REL,
3582 /******************************************************************************/
3583 /******************** MESH NETWORK HANDLERS **************************/
3584 /******************************************************************************/
3588 * Core handler for path creation
3590 * @param cls closure
3591 * @param message message
3592 * @param peer peer identity this notification is about
3593 * @param atsi performance data
3594 * @param atsi_count number of records in 'atsi'
3596 * @return GNUNET_OK to keep the connection open,
3597 * GNUNET_SYSERR to close it (signal serious error)
3600 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3601 const struct GNUNET_MessageHeader *message,
3602 const struct GNUNET_ATS_Information *atsi,
3603 unsigned int atsi_count)
3605 unsigned int own_pos;
3608 MESH_TunnelNumber tid;
3609 struct GNUNET_MESH_ManipulatePath *msg;
3610 struct GNUNET_PeerIdentity *pi;
3611 struct GNUNET_HashCode hash;
3612 struct MeshPeerPath *path;
3613 struct MeshPeerInfo *dest_peer_info;
3614 struct MeshPeerInfo *orig_peer_info;
3615 struct MeshTunnel *t;
3617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3618 "Received a path create msg [%s]\n",
3619 GNUNET_i2s (&my_full_id));
3620 size = ntohs (message->size);
3621 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3623 GNUNET_break_op (0);
3627 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3628 if (size % sizeof (struct GNUNET_PeerIdentity))
3630 GNUNET_break_op (0);
3633 size /= sizeof (struct GNUNET_PeerIdentity);
3636 GNUNET_break_op (0);
3639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3640 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3642 tid = ntohl (msg->tid);
3643 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3645 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3646 t = tunnel_get (pi, tid);
3647 if (NULL == t) // FIXME only for INCOMING tunnels?
3649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3650 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3652 while (NULL != tunnel_get_incoming (next_local_tid))
3653 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3654 t->local_tid_dest = next_local_tid++;
3655 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3657 tunnel_reset_timeout (t);
3658 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3660 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3669 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3670 if (NULL == dest_peer_info)
3672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3673 " Creating PeerInfo for destination.\n");
3674 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3675 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3676 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3678 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3680 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3681 if (NULL == orig_peer_info)
3683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3684 " Creating PeerInfo for origin.\n");
3685 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3686 orig_peer_info->id = GNUNET_PEER_intern (pi);
3687 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3688 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3691 path = path_new (size);
3693 for (i = 0; i < size; i++)
3695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3696 GNUNET_i2s (&pi[i]));
3697 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3698 if (path->peers[i] == myid)
3701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3704 /* cannot be self, must be 'not found' */
3705 /* create path: self not found in path through self */
3706 GNUNET_break_op (0);
3707 path_destroy (path);
3708 /* FIXME error. destroy tunnel? leave for timeout? */
3711 path_add_to_peers (path, GNUNET_NO);
3712 tunnel_add_path (t, path, own_pos);
3713 if (own_pos == size - 1)
3715 /* It is for us! Send ack. */
3716 struct MeshTransmissionDescriptor *info;
3718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3719 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3720 if (NULL == t->peers)
3722 /* New tunnel! Notify clients on data. */
3723 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
3725 GNUNET_break (GNUNET_OK ==
3726 GNUNET_CONTAINER_multihashmap_put (t->peers,
3727 &my_full_id.hashPubKey,
3730 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
3731 // FIXME URGENT (GNUNET_NO?)
3732 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3733 info->origin = &t->id;
3734 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3735 GNUNET_assert (NULL != info->peer);
3737 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3738 sizeof (struct GNUNET_MESH_PathACK),
3744 struct MeshPeerPath *path2;
3746 /* It's for somebody else! Retransmit. */
3747 path2 = path_duplicate (path);
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3749 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3750 path2 = path_duplicate (path);
3751 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3752 send_create_path (dest_peer_info, path, t);
3759 * Core handler for path destruction
3761 * @param cls closure
3762 * @param message message
3763 * @param peer peer identity this notification is about
3764 * @param atsi performance data
3765 * @param atsi_count number of records in 'atsi'
3767 * @return GNUNET_OK to keep the connection open,
3768 * GNUNET_SYSERR to close it (signal serious error)
3771 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3772 const struct GNUNET_MessageHeader *message,
3773 const struct GNUNET_ATS_Information *atsi,
3774 unsigned int atsi_count)
3776 struct GNUNET_MESH_ManipulatePath *msg;
3777 struct GNUNET_PeerIdentity *pi;
3778 struct MeshPeerPath *path;
3779 struct MeshTunnel *t;
3780 unsigned int own_pos;
3784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3785 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3786 size = ntohs (message->size);
3787 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3789 GNUNET_break_op (0);
3793 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3794 if (size % sizeof (struct GNUNET_PeerIdentity))
3796 GNUNET_break_op (0);
3799 size /= sizeof (struct GNUNET_PeerIdentity);
3802 GNUNET_break_op (0);
3805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3807 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3808 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3810 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3812 t = tunnel_get (pi, ntohl (msg->tid));
3815 /* TODO notify back: we don't know this tunnel */
3816 GNUNET_break_op (0);
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3820 path = path_new (size);
3822 for (i = 0; i < size; i++)
3824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3825 GNUNET_i2s (&pi[i]));
3826 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3827 if (path->peers[i] == myid)
3830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3831 if (own_pos < path->length - 1)
3832 send_message (message, &pi[own_pos + 1], t);
3834 send_client_tunnel_disconnect(t, NULL);
3836 tunnel_delete_peer (t, path->peers[path->length - 1]);
3837 path_destroy (path);
3843 * Core handler for notifications of broken paths
3845 * @param cls closure
3846 * @param message message
3847 * @param peer peer identity this notification is about
3848 * @param atsi performance data
3849 * @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)
3855 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3856 const struct GNUNET_MessageHeader *message,
3857 const struct GNUNET_ATS_Information *atsi,
3858 unsigned int atsi_count)
3860 struct GNUNET_MESH_PathBroken *msg;
3861 struct MeshTunnel *t;
3863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3864 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3865 msg = (struct GNUNET_MESH_PathBroken *) message;
3866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3867 GNUNET_i2s (&msg->peer1));
3868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3869 GNUNET_i2s (&msg->peer2));
3870 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3873 GNUNET_break_op (0);
3876 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3877 GNUNET_PEER_search (&msg->peer2));
3884 * Core handler for tunnel destruction
3886 * @param cls closure
3887 * @param message message
3888 * @param peer peer identity this notification is about
3889 * @param atsi performance data
3890 * @param atsi_count number of records in 'atsi'
3892 * @return GNUNET_OK to keep the connection open,
3893 * GNUNET_SYSERR to close it (signal serious error)
3896 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3897 const struct GNUNET_MessageHeader *message,
3898 const struct GNUNET_ATS_Information *atsi,
3899 unsigned int atsi_count)
3901 struct GNUNET_MESH_TunnelDestroy *msg;
3902 struct MeshTunnel *t;
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3905 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3906 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3908 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3909 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3912 /* Probably already got the message from another path,
3913 * destroyed the tunnel and retransmitted to children.
3918 if (t->id.oid == myid)
3920 GNUNET_break_op (0);
3923 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3925 /* Tunnel was incoming, notify clients */
3926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3927 t->local_tid, t->local_tid_dest);
3928 send_clients_tunnel_destroy (t);
3930 tunnel_send_destroy (t);
3931 t->destroy = GNUNET_YES;
3932 // TODO: add timeout to destroy the tunnel anyway
3938 * Core handler for mesh network traffic going from the origin to a peer
3940 * @param cls closure
3941 * @param peer peer identity this notification is about
3942 * @param message message
3943 * @param atsi performance data
3944 * @param atsi_count number of records in 'atsi'
3945 * @return GNUNET_OK to keep the connection open,
3946 * GNUNET_SYSERR to close it (signal serious error)
3949 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3950 const struct GNUNET_MessageHeader *message,
3951 const struct GNUNET_ATS_Information *atsi,
3952 unsigned int atsi_count)
3954 struct GNUNET_MESH_Unicast *msg;
3955 struct MeshTunnel *t;
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3961 size = ntohs (message->size);
3963 sizeof (struct GNUNET_MESH_Unicast) +
3964 sizeof (struct GNUNET_MessageHeader))
3969 msg = (struct GNUNET_MESH_Unicast *) message;
3970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3971 ntohs (msg[1].header.type));
3972 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3975 /* TODO notify back: we don't know this tunnel */
3976 GNUNET_break_op (0);
3979 tunnel_reset_timeout (t);
3980 pid = GNUNET_PEER_search (&msg->destination);
3983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3984 " it's for us! sending to clients...\n");
3985 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3989 " not for us, retransmitting...\n");
3990 send_message (message, tree_get_first_hop (t->tree, pid), t);
3996 * Core handler for mesh network traffic going from the origin to all peers
3998 * @param cls closure
3999 * @param message message
4000 * @param peer peer identity this notification is about
4001 * @param atsi performance data
4002 * @param atsi_count number of records in 'atsi'
4003 * @return GNUNET_OK to keep the connection open,
4004 * GNUNET_SYSERR to close it (signal serious error)
4006 * TODO: Check who we got this from, to validate route.
4009 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4010 const struct GNUNET_MessageHeader *message,
4011 const struct GNUNET_ATS_Information *atsi,
4012 unsigned int atsi_count)
4014 struct GNUNET_MESH_Multicast *msg;
4015 struct MeshTunnel *t;
4018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
4020 size = ntohs (message->size);
4021 if (sizeof (struct GNUNET_MESH_Multicast) +
4022 sizeof (struct GNUNET_MessageHeader) > size)
4024 GNUNET_break_op (0);
4027 msg = (struct GNUNET_MESH_Multicast *) message;
4028 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4032 /* TODO notify that we dont know that tunnel */
4033 GNUNET_break_op (0);
4036 if (t->mid == ntohl (msg->mid))
4038 /* FIXME: already seen this packet, log dropping */
4039 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4040 " Already seen mid %u, DROPPING!\n", t->mid);
4045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4046 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
4048 t->mid = ntohl (msg->mid);
4049 tunnel_reset_timeout (t);
4051 /* Transmit to locally interested clients */
4052 if (NULL != t->peers &&
4053 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
4055 send_subscribed_clients (message, &msg[1].header);
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
4058 if (ntohl (msg->ttl) == 0)
4060 /* FIXME: ttl is 0, log dropping */
4061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4064 tunnel_send_multicast (t, message, GNUNET_NO);
4070 * Core handler for mesh network traffic toward the owner of a tunnel
4072 * @param cls closure
4073 * @param message message
4074 * @param peer peer identity this notification is about
4075 * @param atsi performance data
4076 * @param atsi_count number of records in 'atsi'
4078 * @return GNUNET_OK to keep the connection open,
4079 * GNUNET_SYSERR to close it (signal serious error)
4082 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4083 const struct GNUNET_MessageHeader *message,
4084 const struct GNUNET_ATS_Information *atsi,
4085 unsigned int atsi_count)
4087 struct GNUNET_MESH_ToOrigin *msg;
4088 struct GNUNET_PeerIdentity id;
4089 struct MeshPeerInfo *peer_info;
4090 struct MeshTunnel *t;
4093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4095 size = ntohs (message->size);
4096 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4097 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4099 GNUNET_break_op (0);
4102 msg = (struct GNUNET_MESH_ToOrigin *) message;
4103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4104 ntohs (msg[1].header.type));
4105 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4109 /* TODO notify that we dont know this tunnel (whom)? */
4110 GNUNET_break_op (0);
4114 if (t->id.oid == myid)
4117 struct GNUNET_MESH_ToOrigin *copy;
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4120 " it's for us! sending to clients...\n");
4121 if (NULL == t->owner)
4123 /* got data packet for ownerless tunnel */
4124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4125 GNUNET_break_op (0);
4128 /* TODO signature verification */
4129 memcpy (cbuf, message, size);
4130 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4131 copy->tid = htonl (t->local_tid);
4132 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4133 ©->header, GNUNET_YES);
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4137 " not for us, retransmitting...\n");
4139 peer_info = peer_info_get (&msg->oid);
4140 if (NULL == peer_info)
4142 /* unknown origin of tunnel */
4146 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4147 send_message (message, &id, t);
4154 * Core handler for path ACKs
4156 * @param cls closure
4157 * @param message message
4158 * @param peer peer identity this notification is about
4159 * @param atsi performance data
4160 * @param atsi_count number of records in 'atsi'
4162 * @return GNUNET_OK to keep the connection open,
4163 * GNUNET_SYSERR to close it (signal serious error)
4166 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4167 const struct GNUNET_MessageHeader *message,
4168 const struct GNUNET_ATS_Information *atsi,
4169 unsigned int atsi_count)
4171 struct GNUNET_MESH_PathACK *msg;
4172 struct GNUNET_PeerIdentity id;
4173 struct MeshPeerInfo *peer_info;
4174 struct MeshPeerPath *p;
4175 struct MeshTunnel *t;
4177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4178 GNUNET_i2s (&my_full_id));
4179 msg = (struct GNUNET_MESH_PathACK *) message;
4180 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4183 /* TODO notify that we don't know the tunnel */
4184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4185 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4189 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4191 peer_info = peer_info_get (&msg->peer_id);
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4193 GNUNET_i2s (&msg->peer_id));
4194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4197 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
4199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4200 "connect_by_string completed, stopping search\n");
4201 regex_cancel_search (t->regex_ctx);
4202 t->regex_ctx = NULL;
4205 /* Add paths to peers? */
4206 p = tree_get_path_to_peer (t->tree, peer_info->id);
4209 path_add_to_peers (p, GNUNET_YES);
4217 /* Message for us? */
4218 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4221 if (NULL == t->owner)
4223 GNUNET_break_op (0);
4226 if (NULL != t->dht_get_type)
4228 GNUNET_DHT_get_stop (t->dht_get_type);
4229 t->dht_get_type = NULL;
4231 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4233 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4234 send_client_peer_connected (t, peer_info->id);
4239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4240 " not for us, retransmitting...\n");
4241 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4242 peer_info = peer_info_get (&msg->oid);
4243 if (NULL == peer_info)
4245 /* If we know the tunnel, we should DEFINITELY know the peer */
4249 send_message (message, &id, t);
4255 * Functions to handle messages from core
4257 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4258 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4259 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4260 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4261 sizeof (struct GNUNET_MESH_PathBroken)},
4262 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
4263 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4264 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4265 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4266 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4267 sizeof (struct GNUNET_MESH_PathACK)},
4273 /******************************************************************************/
4274 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4275 /******************************************************************************/
4278 * deregister_app: iterator for removing each application registered by a client
4280 * @param cls closure
4281 * @param key the hash of the application id (used to access the hashmap)
4282 * @param value the value stored at the key (client)
4284 * @return GNUNET_OK on success
4287 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
4289 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
4290 GNUNET_break (GNUNET_YES ==
4291 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
4297 * notify_client_connection_failure: notify a client that the connection to the
4298 * requested remote peer is not possible (for instance, no route found)
4299 * Function called when the socket is ready to queue more data. "buf" will be
4300 * NULL and "size" zero if the socket was closed for writing in the meantime.
4302 * @param cls closure
4303 * @param size number of bytes available in buf
4304 * @param buf where the callee should write the message
4305 * @return number of bytes written to buf
4308 notify_client_connection_failure (void *cls, size_t size, void *buf)
4311 struct MeshPeerInfo *peer_info;
4312 struct GNUNET_MESH_PeerControl *msg;
4313 struct GNUNET_PeerIdentity id;
4315 if (0 == size && NULL == buf)
4317 // TODO retry? cancel?
4321 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4322 peer_info = (struct MeshPeerInfo *) cls;
4323 msg = (struct GNUNET_MESH_PeerControl *) buf;
4324 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4325 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4326 // msg->tunnel_id = htonl(peer_info->t->tid);
4327 GNUNET_PEER_resolve (peer_info->id, &id);
4328 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4336 * Send keepalive packets for a peer
4338 * @param cls Closure (tunnel for which to send the keepalive).
4339 * @param tc Notification context.
4341 * TODO: implement explicit multicast keepalive?
4344 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4346 struct MeshTunnel *t = cls;
4347 struct GNUNET_MessageHeader *payload;
4348 struct GNUNET_MESH_Multicast *msg;
4350 sizeof (struct GNUNET_MESH_Multicast) +
4351 sizeof (struct GNUNET_MessageHeader);
4354 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4358 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4361 "sending keepalive for tunnel %d\n", t->id.tid);
4363 msg = (struct GNUNET_MESH_Multicast *) cbuf;
4364 msg->header.size = htons (size);
4365 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4366 msg->oid = my_full_id;
4367 msg->tid = htonl (t->id.tid);
4368 msg->ttl = htonl (DEFAULT_TTL);
4369 msg->mid = htonl (t->mid + 1);
4371 payload = (struct GNUNET_MessageHeader *) &msg[1];
4372 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
4373 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4374 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
4376 t->path_refresh_task =
4377 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
4383 * Function to process paths received for a new peer addition. The recorded
4384 * paths form the initial tunnel, which can be optimized later.
4385 * Called on each result obtained for the DHT search.
4387 * @param cls closure
4388 * @param exp when will this value expire
4389 * @param key key of the result
4390 * @param get_path path of the get request
4391 * @param get_path_length lenght of get_path
4392 * @param put_path path of the put request
4393 * @param put_path_length length of the put_path
4394 * @param type type of the result
4395 * @param size number of bytes in data
4396 * @param data pointer to the result data
4398 * TODO: re-issue the request after certain time? cancel after X results?
4401 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4402 const struct GNUNET_HashCode * key,
4403 const struct GNUNET_PeerIdentity *get_path,
4404 unsigned int get_path_length,
4405 const struct GNUNET_PeerIdentity *put_path,
4406 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4407 size_t size, const void *data)
4409 struct MeshPathInfo *path_info = cls;
4410 struct MeshPeerPath *p;
4411 struct GNUNET_PeerIdentity pi;
4414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4415 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4418 p = path_build_from_dht (get_path, get_path_length, put_path,
4420 path_add_to_peers (p, GNUNET_NO);
4422 for (i = 0; i < path_info->peer->ntunnels; i++)
4424 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4425 peer_info_connect (path_info->peer, path_info->t);
4433 * Function to process paths received for a new peer addition. The recorded
4434 * paths form the initial tunnel, which can be optimized later.
4435 * Called on each result obtained for the DHT search.
4437 * @param cls closure
4438 * @param exp when will this value expire
4439 * @param key key of the result
4440 * @param get_path path of the get request
4441 * @param get_path_length lenght of get_path
4442 * @param put_path path of the put request
4443 * @param put_path_length length of the put_path
4444 * @param type type of the result
4445 * @param size number of bytes in data
4446 * @param data pointer to the result data
4449 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4450 const struct GNUNET_HashCode * key,
4451 const struct GNUNET_PeerIdentity *get_path,
4452 unsigned int get_path_length,
4453 const struct GNUNET_PeerIdentity *put_path,
4454 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4455 size_t size, const void *data)
4457 const struct PBlock *pb = data;
4458 const struct GNUNET_PeerIdentity *pi = &pb->id;
4459 struct MeshTunnel *t = cls;
4460 struct MeshPeerInfo *peer_info;
4461 struct MeshPeerPath *p;
4463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
4464 if (size != sizeof (struct PBlock))
4466 GNUNET_break_op (0);
4469 if (ntohl(pb->type) != t->type)
4471 GNUNET_break_op (0);
4474 GNUNET_assert (NULL != t->owner);
4475 peer_info = peer_info_get (pi);
4476 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
4478 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
4480 p = path_build_from_dht (get_path, get_path_length, put_path,
4482 path_add_to_peers (p, GNUNET_NO);
4484 tunnel_add_peer (t, peer_info);
4485 peer_info_connect (peer_info, t);
4490 * Function called if the connect attempt to a peer found via
4491 * connect_by_string times out. Try to connect to another peer, if any.
4492 * Otherwise try to reconnect to the same peer.
4494 * @param cls Closure (info about regex search).
4495 * @param tc TaskContext.
4498 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4500 struct MeshRegexSearchInfo *info = cls;
4501 struct MeshPeerInfo *peer_info;
4505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
4506 info->timeout = GNUNET_SCHEDULER_NO_TASK;
4507 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
4515 if (0 < info->n_peers)
4517 // Select next peer, put current in that spot.
4518 id = info->peers[info->i_peer];
4519 info->peers[info->i_peer] = info->peer;
4520 info->i_peer = (info->i_peer + 1) % info->n_peers;
4524 // Try to connect to same peer again.
4527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
4529 peer_info = peer_info_get_short(id);
4530 tunnel_add_peer (info->t, peer_info);
4532 tunnel_delete_peer (info->t, old);
4533 peer_info_connect (peer_info, info->t);
4534 info->timeout = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
4535 ®ex_connect_timeout,
4537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
4542 * Function to process DHT string to regex matching.
4543 * Called on each result obtained for the DHT search.
4545 * @param cls closure (search context)
4546 * @param exp when will this value expire
4547 * @param key key of the result
4548 * @param get_path path of the get request (not used)
4549 * @param get_path_length lenght of get_path (not used)
4550 * @param put_path path of the put request (not used)
4551 * @param put_path_length length of the put_path (not used)
4552 * @param type type of the result
4553 * @param size number of bytes in data
4554 * @param data pointer to the result data
4557 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4558 const struct GNUNET_HashCode * key,
4559 const struct GNUNET_PeerIdentity *get_path,
4560 unsigned int get_path_length,
4561 const struct GNUNET_PeerIdentity *put_path,
4562 unsigned int put_path_length,
4563 enum GNUNET_BLOCK_Type type,
4564 size_t size, const void *data)
4566 const struct MeshRegexAccept *block = data;
4567 struct MeshRegexSearchContext *ctx = cls;
4568 struct MeshRegexSearchInfo *info = ctx->info;
4569 struct MeshPeerPath *p;
4570 struct MeshPeerInfo *peer_info;
4572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
4573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
4575 peer_info = peer_info_get(&block->id);
4576 p = path_build_from_dht (get_path, get_path_length, put_path,
4578 path_add_to_peers (p, GNUNET_NO);
4581 tunnel_add_peer (info->t, peer_info);
4582 peer_info_connect (peer_info, info->t);
4583 if (0 == info->peer)
4585 info->peer = peer_info->id;
4589 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
4592 info->timeout = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
4593 ®ex_connect_timeout,
4601 * Function to process DHT string to regex matching..
4602 * Called on each result obtained for the DHT search.
4604 * @param cls closure (search context)
4605 * @param exp when will this value expire
4606 * @param key key of the result
4607 * @param get_path path of the get request (not used)
4608 * @param get_path_length lenght of get_path (not used)
4609 * @param put_path path of the put request (not used)
4610 * @param put_path_length length of the put_path (not used)
4611 * @param type type of the result
4612 * @param size number of bytes in data
4613 * @param data pointer to the result data
4615 * TODO: re-issue the request after certain time? cancel after X results?
4618 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4619 const struct GNUNET_HashCode * key,
4620 const struct GNUNET_PeerIdentity *get_path,
4621 unsigned int get_path_length,
4622 const struct GNUNET_PeerIdentity *put_path,
4623 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4624 size_t size, const void *data)
4626 const struct MeshRegexBlock *block = data;
4627 struct MeshRegexSearchContext *ctx = cls;
4628 struct MeshRegexSearchInfo *info = ctx->info;
4632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4633 "DHT GET STRING RETURNED RESULTS\n");
4634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4635 " key: %s\n", GNUNET_h2s (key));
4637 copy = GNUNET_malloc (size);
4638 memcpy (copy, data, size);
4639 GNUNET_break (GNUNET_OK ==
4640 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
4641 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4642 len = ntohl (block->n_proof);
4644 char proof[len + 1];
4646 memcpy (proof, &block[1], len);
4648 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
4650 GNUNET_break_op (0);
4654 len = strlen (info->description);
4655 if (len == ctx->position) // String processed
4657 if (GNUNET_YES == ntohl (block->accepting))
4659 regex_find_path(key, ctx);
4663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
4664 // FIXME REGEX this block not successful, wait for more? start timeout?
4668 GNUNET_break (GNUNET_OK ==
4669 GNUNET_MESH_regex_block_iterate (block, size,
4670 ®ex_edge_iterator, ctx));
4674 /******************************************************************************/
4675 /********************* MESH LOCAL HANDLES **************************/
4676 /******************************************************************************/
4680 * Handler for client disconnection
4682 * @param cls closure
4683 * @param client identification of the client; NULL
4684 * for the last call when the server is destroyed
4687 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4689 struct MeshClient *c;
4690 struct MeshClient *next;
4693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4702 if (c->handle != client)
4704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4710 GNUNET_SERVER_client_drop (c->handle);
4711 c->shutting_down = GNUNET_YES;
4712 GNUNET_assert (NULL != c->own_tunnels);
4713 GNUNET_assert (NULL != c->incoming_tunnels);
4714 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4715 &tunnel_destroy_iterator, c);
4716 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4717 &tunnel_destroy_iterator, c);
4718 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4719 &tunnel_destroy_iterator, c);
4720 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4721 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4722 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4724 /* deregister clients applications */
4725 if (NULL != c->apps)
4727 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
4728 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
4730 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
4731 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
4733 GNUNET_SCHEDULER_cancel (announce_applications_task);
4734 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4736 if (NULL != c->types)
4737 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4738 for (i = 0; i < c->n_regex; i++)
4740 GNUNET_free (c->regexes[i]);
4742 if (NULL != c->regexes)
4743 GNUNET_free (c->regexes);
4744 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
4745 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
4747 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
4748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
4758 * Handler for new clients
4760 * @param cls closure
4761 * @param client identification of the client
4762 * @param message the actual message, which includes messages the client wants
4765 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4766 const struct GNUNET_MessageHeader *message)
4768 struct GNUNET_MESH_ClientConnect *cc_msg;
4769 struct MeshClient *c;
4770 GNUNET_MESH_ApplicationType *a;
4777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4778 /* Check data sanity */
4779 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4780 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4781 ntypes = ntohs (cc_msg->types);
4782 napps = ntohs (cc_msg->applications);
4784 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
4787 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4791 /* Create new client structure */
4792 c = GNUNET_malloc (sizeof (struct MeshClient));
4793 c->id = next_client_id++;
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4796 GNUNET_SERVER_client_keep (client);
4797 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
4800 GNUNET_MESH_ApplicationType at;
4801 struct GNUNET_HashCode hc;
4803 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
4804 for (i = 0; i < napps; i++)
4807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
4808 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
4809 /* store in clients hashmap */
4810 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
4811 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4812 /* store in global hashmap, for announcements */
4813 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
4814 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4816 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
4817 announce_applications_task =
4818 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
4824 struct GNUNET_HashCode hc;
4826 t = (uint16_t *) & a[napps];
4827 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
4828 for (i = 0; i < ntypes; i++)
4831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4832 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
4834 /* store in clients hashmap */
4835 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4836 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4837 /* store in global hashmap */
4838 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4839 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4843 " client has %u+%u subscriptions\n", napps, ntypes);
4845 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
4846 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4847 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4848 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4849 GNUNET_SERVER_notification_context_add (nc, client);
4851 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4857 * Handler for clients announcing available services by a regular expression.
4859 * @param cls closure
4860 * @param client identification of the client
4861 * @param message the actual message, which includes messages the client wants
4864 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
4865 const struct GNUNET_MessageHeader *message)
4867 struct MeshClient *c;
4871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
4873 /* Sanity check for client registration */
4874 if (NULL == (c = client_get (client)))
4877 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4882 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
4883 regex = GNUNET_malloc (len + 1);
4884 memcpy (regex, &message[1], len);
4886 GNUNET_array_append (c->regexes, c->n_regex, regex);
4887 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
4889 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
4895 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
4901 * Handler for requests of new tunnels
4903 * @param cls closure
4904 * @param client identification of the client
4905 * @param message the actual message
4908 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4909 const struct GNUNET_MessageHeader *message)
4911 struct GNUNET_MESH_TunnelMessage *t_msg;
4912 struct MeshTunnel *t;
4913 struct MeshClient *c;
4915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4917 /* Sanity check for client registration */
4918 if (NULL == (c = client_get (client)))
4921 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4926 /* Message sanity check */
4927 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4934 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4935 /* Sanity check for tunnel numbering */
4936 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4942 /* Sanity check for duplicate tunnel IDs */
4943 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
4946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4950 while (NULL != tunnel_get_by_pi (myid, next_tid))
4951 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4952 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
4953 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4955 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4956 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
4958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
4959 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4965 * Handler for requests of deleting tunnels
4967 * @param cls closure
4968 * @param client identification of the client
4969 * @param message the actual message
4972 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4973 const struct GNUNET_MessageHeader *message)
4975 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4976 struct MeshClient *c;
4977 struct MeshTunnel *t;
4978 MESH_TunnelNumber tid;
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4981 "Got a DESTROY TUNNEL from client!\n");
4983 /* Sanity check for client registration */
4984 if (NULL == (c = client_get (client)))
4987 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4990 /* Message sanity check */
4991 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4998 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5000 /* Retrieve tunnel */
5001 tid = ntohl (tunnel_msg->tunnel_id);
5002 t = tunnel_get_by_local_id(c, tid);
5006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5007 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5010 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5012 client_ignore_tunnel (c, t);
5014 // TODO: when to destroy incoming tunnel?
5015 if (t->nclients == 0)
5017 GNUNET_assert (GNUNET_YES ==
5018 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
5020 GNUNET_assert (GNUNET_YES ==
5021 GNUNET_CONTAINER_multihashmap_remove (t->peers,
5022 &my_full_id.hashPubKey,
5026 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5029 send_client_tunnel_disconnect(t, c);
5030 client_delete_tunnel(c, t);
5032 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5034 tunnel_send_destroy (t);
5035 t->destroy = GNUNET_YES;
5036 // The tunnel will be destroyed when the last message is transmitted.
5037 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5043 * Handler for connection requests to new peers
5045 * @param cls closure
5046 * @param client identification of the client
5047 * @param message the actual message (PeerControl)
5050 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
5051 const struct GNUNET_MessageHeader *message)
5053 struct GNUNET_MESH_PeerControl *peer_msg;
5054 struct MeshPeerInfo *peer_info;
5055 struct MeshClient *c;
5056 struct MeshTunnel *t;
5057 MESH_TunnelNumber tid;
5059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
5060 /* Sanity check for client registration */
5061 if (NULL == (c = client_get (client)))
5064 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5068 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5069 /* Sanity check for message size */
5070 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5077 /* Tunnel exists? */
5078 tid = ntohl (peer_msg->tunnel_id);
5079 t = tunnel_get_by_local_id (c, tid);
5083 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5087 /* Does client own tunnel? */
5088 if (t->owner->handle != client)
5091 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
5095 GNUNET_i2s (&peer_msg->peer));
5096 peer_info = peer_info_get (&peer_msg->peer);
5098 tunnel_add_peer (t, peer_info);
5099 peer_info_connect (peer_info, t);
5101 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5107 * Handler for disconnection requests of peers in a tunnel
5109 * @param cls closure
5110 * @param client identification of the client
5111 * @param message the actual message (PeerControl)
5114 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
5115 const struct GNUNET_MessageHeader *message)
5117 struct GNUNET_MESH_PeerControl *peer_msg;
5118 struct MeshPeerInfo *peer_info;
5119 struct MeshClient *c;
5120 struct MeshTunnel *t;
5121 MESH_TunnelNumber tid;
5123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
5124 /* Sanity check for client registration */
5125 if (NULL == (c = client_get (client)))
5128 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5131 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5132 /* Sanity check for message size */
5133 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5140 /* Tunnel exists? */
5141 tid = ntohl (peer_msg->tunnel_id);
5142 t = tunnel_get_by_local_id (c, tid);
5146 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5151 /* Does client own tunnel? */
5152 if (t->owner->handle != client)
5155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
5160 GNUNET_i2s (&peer_msg->peer));
5161 /* Is the peer in the tunnel? */
5163 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
5164 if (NULL == peer_info)
5167 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5171 /* Ok, delete peer from tunnel */
5172 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
5173 &peer_msg->peer.hashPubKey);
5175 send_destroy_path (t, peer_info->id);
5176 tunnel_delete_peer (t, peer_info->id);
5177 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5182 * Handler for blacklist requests of peers in a tunnel
5184 * @param cls closure
5185 * @param client identification of the client
5186 * @param message the actual message (PeerControl)
5189 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
5190 const struct GNUNET_MessageHeader *message)
5192 struct GNUNET_MESH_PeerControl *peer_msg;
5193 struct MeshClient *c;
5194 struct MeshTunnel *t;
5195 MESH_TunnelNumber tid;
5197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
5198 /* Sanity check for client registration */
5199 if (NULL == (c = client_get (client)))
5202 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5205 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5207 /* Sanity check for message size */
5208 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5211 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5215 /* Tunnel exists? */
5216 tid = ntohl (peer_msg->tunnel_id);
5217 t = tunnel_get_by_local_id (c, tid);
5221 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5226 GNUNET_array_append(t->blacklisted, t->nblacklisted,
5227 GNUNET_PEER_intern(&peer_msg->peer));
5232 * Handler for unblacklist requests of peers in a tunnel
5234 * @param cls closure
5235 * @param client identification of the client
5236 * @param message the actual message (PeerControl)
5239 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
5240 const struct GNUNET_MessageHeader *message)
5242 struct GNUNET_MESH_PeerControl *peer_msg;
5243 struct MeshClient *c;
5244 struct MeshTunnel *t;
5245 MESH_TunnelNumber tid;
5249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
5250 /* Sanity check for client registration */
5251 if (NULL == (c = client_get (client)))
5254 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5257 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5259 /* Sanity check for message size */
5260 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5263 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5267 /* Tunnel exists? */
5268 tid = ntohl (peer_msg->tunnel_id);
5269 t = tunnel_get_by_local_id (c, tid);
5273 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5278 /* if peer is not known, complain */
5279 pid = GNUNET_PEER_search (&peer_msg->peer);
5286 /* search and remove from list */
5287 for (i = 0; i < t->nblacklisted; i++)
5289 if (t->blacklisted[i] == pid)
5291 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
5292 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
5297 /* if peer hasn't been blacklisted, complain */
5303 * Handler for connection requests to new peers by type
5305 * @param cls closure
5306 * @param client identification of the client
5307 * @param message the actual message (ConnectPeerByType)
5310 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
5311 const struct GNUNET_MessageHeader *message)
5313 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
5314 struct MeshClient *c;
5315 struct MeshTunnel *t;
5316 struct GNUNET_HashCode hash;
5317 MESH_TunnelNumber tid;
5319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
5320 /* Sanity check for client registration */
5321 if (NULL == (c = client_get (client)))
5324 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5328 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
5329 /* Sanity check for message size */
5330 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
5331 ntohs (connect_msg->header.size))
5334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5338 /* Tunnel exists? */
5339 tid = ntohl (connect_msg->tunnel_id);
5340 t = tunnel_get_by_local_id (c, tid);
5344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5348 /* Does client own tunnel? */
5349 if (t->owner->handle != client)
5352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5356 /* Do WE have the service? */
5357 t->type = ntohl (connect_msg->type);
5358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
5359 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
5360 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
5363 /* Yes! Fast forward, add ourselves to the tunnel and send the
5364 * good news to the client, and alert the destination client of
5365 * an incoming tunnel.
5367 * FIXME send a path create to self, avoid code duplication
5369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
5370 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
5371 peer_info_get (&my_full_id),
5372 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
5375 send_client_peer_connected (t, myid);
5376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
5377 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5379 t->local_tid_dest = next_local_tid++;
5380 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5381 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5382 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5386 /* Ok, lets find a peer offering the service */
5387 if (NULL != t->dht_get_type)
5389 GNUNET_DHT_get_stop (t->dht_get_type);
5391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
5392 GNUNET_h2s (&hash));
5394 GNUNET_DHT_get_start (dht_handle,
5395 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
5397 DHT_REPLICATION_LEVEL,
5398 GNUNET_DHT_RO_RECORD_ROUTE |
5399 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5401 &dht_get_type_handler, t);
5403 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5409 * Handler for connection requests to new peers by a string service description.
5411 * @param cls closure
5412 * @param client identification of the client
5413 * @param message the actual message, which includes messages the client wants
5416 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
5417 const struct GNUNET_MessageHeader *message)
5419 struct GNUNET_MESH_ConnectPeerByString *msg;
5420 struct MeshRegexSearchContext *ctx;
5421 struct MeshRegexSearchInfo *info;
5422 struct GNUNET_DHT_GetHandle *get_h;
5423 struct GNUNET_HashCode key;
5424 struct MeshTunnel *t;
5425 struct MeshClient *c;
5426 MESH_TunnelNumber tid;
5430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5431 "Connect by string started\n");
5432 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
5433 size = htons (message->size);
5435 /* Sanity check for client registration */
5436 if (NULL == (c = client_get (client)))
5439 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5443 /* Message size sanity check */
5444 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
5447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5451 /* Tunnel exists? */
5452 tid = ntohl (msg->tunnel_id);
5453 t = tunnel_get_by_local_id (c, tid);
5457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5461 /* Does client own tunnel? */
5462 if (t->owner->handle != client)
5465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5470 " on tunnel %s [%u]\n",
5471 GNUNET_i2s(&my_full_id),
5474 /* Only one connect_by_string allowed at the same time! */
5475 /* FIXME: allow more, return handle at api level to cancel, document */
5476 if (NULL != t->regex_ctx)
5479 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5483 /* Find string itself */
5484 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
5485 string = (const char *) &msg[1];
5487 /* Initialize context */
5488 size = GNUNET_REGEX_get_first_key(string, len, &key);
5489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5490 " consumed %u bits out of %u\n", size, len);
5491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5492 " looking for %s\n", GNUNET_h2s (&key));
5494 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
5496 info->description = GNUNET_malloc (len + 1);
5497 memcpy (info->description, string, len);
5498 info->description[len] = '\0';
5499 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
5500 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
5501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
5503 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
5504 ctx->position = size;
5508 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
5510 /* Start search in DHT */
5511 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
5512 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
5513 &key, /* key to search */
5514 DHT_REPLICATION_LEVEL, /* replication level */
5515 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5516 NULL, /* xquery */ // FIXME BLOOMFILTER
5517 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
5518 &dht_get_string_handler, ctx);
5520 GNUNET_break (GNUNET_OK ==
5521 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
5524 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
5526 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
5532 * Handler for client traffic directed to one peer
5534 * @param cls closure
5535 * @param client identification of the client
5536 * @param message the actual message
5539 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5540 const struct GNUNET_MessageHeader *message)
5542 struct MeshClient *c;
5543 struct MeshTunnel *t;
5544 struct MeshPeerInfo *pi;
5545 struct GNUNET_MESH_Unicast *data_msg;
5546 MESH_TunnelNumber tid;
5549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5550 "Got a unicast request from a client!\n");
5552 /* Sanity check for client registration */
5553 if (NULL == (c = client_get (client)))
5556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5559 data_msg = (struct GNUNET_MESH_Unicast *) message;
5560 /* Sanity check for message size */
5561 size = ntohs (message->size);
5562 if (sizeof (struct GNUNET_MESH_Unicast) +
5563 sizeof (struct GNUNET_MessageHeader) > size)
5566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5570 /* Tunnel exists? */
5571 tid = ntohl (data_msg->tid);
5572 t = tunnel_get_by_local_id (c, tid);
5576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5580 /* Is it a local tunnel? Then, does client own the tunnel? */
5581 if (t->owner->handle != client)
5584 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5588 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
5589 &data_msg->destination.hashPubKey);
5590 /* Is the selected peer in the tunnel? */
5594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5598 /* Ok, everything is correct, send the message
5599 * (pretend we got it from a mesh peer)
5602 char buf[ntohs (message->size)] GNUNET_ALIGN;
5603 struct GNUNET_MESH_Unicast *copy;
5605 /* Work around const limitation */
5606 copy = (struct GNUNET_MESH_Unicast *) buf;
5607 memcpy (buf, data_msg, size);
5608 copy->oid = my_full_id;
5609 copy->tid = htonl (t->id.tid);
5610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5611 " calling generic handler...\n");
5612 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
5614 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5620 * Handler for client traffic directed to the origin
5622 * @param cls closure
5623 * @param client identification of the client
5624 * @param message the actual message
5627 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5628 const struct GNUNET_MessageHeader *message)
5630 struct GNUNET_MESH_ToOrigin *data_msg;
5631 struct GNUNET_PeerIdentity id;
5632 struct MeshClient *c;
5633 struct MeshTunnel *t;
5634 MESH_TunnelNumber tid;
5637 /* Sanity check for client registration */
5638 if (NULL == (c = client_get (client)))
5641 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5644 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5645 /* Sanity check for message size */
5646 size = ntohs (message->size);
5647 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5648 sizeof (struct GNUNET_MessageHeader) > size)
5651 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5655 /* Tunnel exists? */
5656 tid = ntohl (data_msg->tid);
5657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5658 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
5659 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5665 t = tunnel_get_by_local_id (c, tid);
5669 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5673 /* It should be sent by someone who has this as incoming tunnel. */
5674 if (-1 == client_knows_tunnel (c, t))
5677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5680 GNUNET_PEER_resolve (t->id.oid, &id);
5682 /* Ok, everything is correct, send the message
5683 * (pretend we got it from a mesh peer)
5686 char buf[ntohs (message->size)] GNUNET_ALIGN;
5687 struct GNUNET_MESH_ToOrigin *copy;
5689 /* Work around const limitation */
5690 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5691 memcpy (buf, data_msg, size);
5693 copy->tid = htonl (t->id.tid);
5694 copy->sender = my_full_id;
5695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5696 " calling generic handler...\n");
5697 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
5699 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5705 * Handler for client traffic directed to all peers in a tunnel
5707 * @param cls closure
5708 * @param client identification of the client
5709 * @param message the actual message
5712 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
5713 const struct GNUNET_MessageHeader *message)
5715 struct MeshClient *c;
5716 struct MeshTunnel *t;
5717 struct GNUNET_MESH_Multicast *data_msg;
5718 MESH_TunnelNumber tid;
5720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5721 "Got a multicast request from a client!\n");
5723 /* Sanity check for client registration */
5724 if (NULL == (c = client_get (client)))
5727 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5730 data_msg = (struct GNUNET_MESH_Multicast *) message;
5731 /* Sanity check for message size */
5732 if (sizeof (struct GNUNET_MESH_Multicast) +
5733 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
5736 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5740 /* Tunnel exists? */
5741 tid = ntohl (data_msg->tid);
5742 t = tunnel_get_by_local_id (c, tid);
5746 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5750 /* Does client own tunnel? */
5751 if (t->owner->handle != client)
5754 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5759 char buf[ntohs (message->size)] GNUNET_ALIGN;
5760 struct GNUNET_MESH_Multicast *copy;
5762 copy = (struct GNUNET_MESH_Multicast *) buf;
5763 memcpy (buf, message, ntohs (message->size));
5764 copy->oid = my_full_id;
5765 copy->tid = htonl (t->id.tid);
5766 copy->ttl = htonl (DEFAULT_TTL);
5767 copy->mid = htonl (t->mid + 1);
5768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5769 " calling generic handler...\n");
5770 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
5773 /* receive done gets called when last copy is sent to a neighbor */
5778 * Functions to handle messages from clients
5780 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5781 {&handle_local_new_client, NULL,
5782 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5783 {&handle_local_announce_regex, NULL,
5784 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
5785 {&handle_local_tunnel_create, NULL,
5786 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5787 sizeof (struct GNUNET_MESH_TunnelMessage)},
5788 {&handle_local_tunnel_destroy, NULL,
5789 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5790 sizeof (struct GNUNET_MESH_TunnelMessage)},
5791 {&handle_local_connect_add, NULL,
5792 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
5793 sizeof (struct GNUNET_MESH_PeerControl)},
5794 {&handle_local_connect_del, NULL,
5795 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
5796 sizeof (struct GNUNET_MESH_PeerControl)},
5797 {&handle_local_blacklist, NULL,
5798 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
5799 sizeof (struct GNUNET_MESH_PeerControl)},
5800 {&handle_local_unblacklist, NULL,
5801 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
5802 sizeof (struct GNUNET_MESH_PeerControl)},
5803 {&handle_local_connect_by_type, NULL,
5804 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
5805 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
5806 {&handle_local_connect_by_string, NULL,
5807 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
5808 {&handle_local_unicast, NULL,
5809 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5810 {&handle_local_to_origin, NULL,
5811 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5812 {&handle_local_multicast, NULL,
5813 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5819 * To be called on core init/fail.
5821 * @param cls service closure
5822 * @param server handle to the server for this service
5823 * @param identity the public identity of this peer
5826 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5827 const struct GNUNET_PeerIdentity *identity)
5829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5830 core_handle = server;
5831 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5834 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5835 GNUNET_SCHEDULER_shutdown ();
5841 * Method called whenever a given peer connects.
5843 * @param cls closure
5844 * @param peer peer identity this notification is about
5845 * @param atsi performance data for the connection
5846 * @param atsi_count number of records in 'atsi'
5849 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
5850 const struct GNUNET_ATS_Information *atsi,
5851 unsigned int atsi_count)
5853 struct MeshPeerInfo *peer_info;
5854 struct MeshPeerPath *path;
5856 DEBUG_CONN ("Peer connected\n");
5857 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5858 peer_info = peer_info_get (peer);
5859 if (myid == peer_info->id)
5861 DEBUG_CONN (" (self)\n");
5866 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5868 path = path_new (2);
5869 path->peers[0] = myid;
5870 path->peers[1] = peer_info->id;
5871 GNUNET_PEER_change_rc (myid, 1);
5872 GNUNET_PEER_change_rc (peer_info->id, 1);
5873 peer_info_add_path (peer_info, path, GNUNET_YES);
5878 * Method called whenever a peer disconnects.
5880 * @param cls closure
5881 * @param peer peer identity this notification is about
5884 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5886 struct MeshPeerInfo *pi;
5887 struct MeshPeerQueue *q;
5888 struct MeshPeerQueue *n;
5890 DEBUG_CONN ("Peer disconnected\n");
5891 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5903 /* try to reroute this traffic instead */
5904 queue_destroy(q, GNUNET_YES);
5908 peer_info_remove_path (pi, pi->id, myid);
5911 DEBUG_CONN (" (self)\n");
5917 /******************************************************************************/
5918 /************************ MAIN FUNCTIONS ****************************/
5919 /******************************************************************************/
5922 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5924 * @param cls closure
5925 * @param key current key code
5926 * @param value value in the hash map
5927 * @return GNUNET_YES if we should continue to iterate,
5931 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5933 struct MeshTunnel *t = value;
5940 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5942 * @param cls closure
5943 * @param key current key code
5944 * @param value value in the hash map
5945 * @return GNUNET_YES if we should continue to iterate,
5949 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5951 struct MeshPeerInfo *p = value;
5952 struct MeshPeerQueue *q;
5953 struct MeshPeerQueue *n;
5961 queue_destroy(q, GNUNET_YES);
5965 peer_info_destroy (p);
5970 * Task run during shutdown.
5976 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5980 if (core_handle != NULL)
5982 GNUNET_CORE_disconnect (core_handle);
5985 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5986 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5987 if (dht_handle != NULL)
5989 GNUNET_DHT_disconnect (dht_handle);
5994 GNUNET_SERVER_notification_context_destroy (nc);
5997 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5999 GNUNET_SCHEDULER_cancel (announce_id_task);
6000 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6006 * Process mesh requests.
6008 * @param cls closure
6009 * @param server the initialized server
6010 * @param c configuration to use
6013 run (void *cls, struct GNUNET_SERVER_Handle *server,
6014 const struct GNUNET_CONFIGURATION_Handle *c)
6016 struct MeshPeerInfo *peer;
6017 struct MeshPeerPath *p;
6020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6021 server_handle = server;
6022 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6023 NULL, /* Closure passed to MESH functions */
6024 &core_init, /* Call core_init once connected */
6025 &core_connect, /* Handle connects */
6026 &core_disconnect, /* remove peers on disconnects */
6027 NULL, /* Don't notify about all incoming messages */
6028 GNUNET_NO, /* For header only in notification */
6029 NULL, /* Don't notify about all outbound messages */
6030 GNUNET_NO, /* For header-only out notification */
6031 core_handlers); /* Register these handlers */
6033 if (core_handle == NULL)
6036 GNUNET_SCHEDULER_shutdown ();
6041 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
6044 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6046 ("Mesh service is lacking key configuration settings. Exiting.\n"));
6047 GNUNET_SCHEDULER_shutdown ();
6050 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6051 GNUNET_free (keyfile);
6052 if (my_private_key == NULL)
6054 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6055 _("Mesh service could not access hostkey. Exiting.\n"));
6056 GNUNET_SCHEDULER_shutdown ();
6059 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6060 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6061 &my_full_id.hashPubKey);
6062 myid = GNUNET_PEER_intern (&my_full_id);
6064 // transport_handle = GNUNET_TRANSPORT_connect(c,
6071 dht_handle = GNUNET_DHT_connect (c, 64);
6072 if (dht_handle == NULL)
6078 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6080 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6081 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6082 peers = GNUNET_CONTAINER_multihashmap_create (32);
6083 applications = GNUNET_CONTAINER_multihashmap_create (32);
6084 types = GNUNET_CONTAINER_multihashmap_create (32);
6086 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6087 nc = GNUNET_SERVER_notification_context_create (server_handle,
6089 GNUNET_SERVER_disconnect_notify (server_handle,
6090 &handle_local_client_disconnect, NULL);
6094 clients_tail = NULL;
6097 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6098 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6100 /* Create a peer_info for the local peer */
6101 peer = peer_info_get (&my_full_id);
6104 GNUNET_PEER_change_rc (myid, 1);
6105 peer_info_add_path (peer, p, GNUNET_YES);
6107 /* Scheduled the task to clean up when shutdown is called */
6108 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
6115 * The main function for the mesh service.
6117 * @param argc number of arguments from the command line
6118 * @param argv command line arguments
6119 * @return 0 ok, 1 on error
6122 main (int argc, char *const *argv)
6126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6129 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");