2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
44 * - set ttl relative to tree depth
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_regex_lib.h"
57 /* TODO: move into configuration file */
58 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
59 GNUNET_TIME_UNIT_SECONDS,\
61 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
62 GNUNET_TIME_UNIT_SECONDS,\
65 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
66 GNUNET_TIME_UNIT_SECONDS,\
69 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
70 GNUNET_TIME_UNIT_SECONDS,\
72 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply(\
73 GNUNET_TIME_UNIT_SECONDS,\
75 #define DEFAULT_TTL 64
77 #define DHT_REPLICATION_LEVEL 10
81 #define MESH_BLOOM_SIZE 128
83 #define MESH_DEBUG_DHT GNUNET_YES
84 #define MESH_DEBUG_CONNECTION GNUNET_NO
86 #if MESH_DEBUG_CONNECTION
87 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
89 #define DEBUG_CONN(...)
93 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
95 #define DEBUG_DHT(...)
98 /******************************************************************************/
99 /************************ DATA STRUCTURES ****************************/
100 /******************************************************************************/
102 /** FWD declaration */
107 * Struct representing a piece of data being sent to other peers
111 /** Tunnel it belongs to. */
112 struct MeshTunnel *t;
114 /** In case of a multicast, task to allow a client to send more data if
115 * some neighbor is too slow. */
116 GNUNET_SCHEDULER_TaskIdentifier *task;
118 /** How many remaining neighbors we need to send this to. */
119 unsigned int *reference_counter;
121 /** Size of the data. */
130 * Struct containing info about a queued transmission to this peer
137 struct MeshPeerQueue *next;
142 struct MeshPeerQueue *prev;
145 * Peer this transmission is directed to.
147 struct MeshPeerInfo *peer;
150 * Tunnel this message belongs to.
152 struct MeshTunnel *tunnel;
155 * Pointer to info stucture used as cls.
165 * Size of the message
172 * Struct containing all info possibly needed to build a package when called
175 struct MeshTransmissionDescriptor
177 /** ID of the tunnel this packet travels in */
178 struct MESH_TunnelID *origin;
180 /** Who was this message being sent to */
181 struct MeshPeerInfo *peer;
183 /** Ultimate destination of the packet */
184 GNUNET_PEER_Id destination;
186 /** Data descriptor */
187 struct MeshData* mesh_data;
192 * Struct containing all information regarding a given peer
202 * Last time we heard from this peer
204 struct GNUNET_TIME_Absolute last_contact;
207 * Number of attempts to reconnect so far
209 int n_reconnect_attempts;
212 * Paths to reach the peer, ordered by ascending hop count
214 struct MeshPeerPath *path_head;
217 * Paths to reach the peer, ordered by ascending hop count
219 struct MeshPeerPath *path_tail;
222 * Handle to stop the DHT search for a path to this peer
224 struct GNUNET_DHT_GetHandle *dhtget;
227 * Closure given to the DHT GET
229 struct MeshPathInfo *dhtgetcls;
232 * Array of tunnels this peer participates in
233 * (most probably a small amount, therefore not a hashmap)
234 * When the path to the peer changes, notify these tunnels to let them
235 * re-adjust their path trees.
237 struct MeshTunnel **tunnels;
240 * Number of tunnels this peers participates in
242 unsigned int ntunnels;
245 * Transmission queue to core DLL head
247 struct MeshPeerQueue *queue_head;
250 * Transmission queue to core DLL tail
252 struct MeshPeerQueue *queue_tail;
255 * How many messages are in the queue to this peer.
257 unsigned int queue_n;
260 * Handle to for queued transmissions
262 struct GNUNET_CORE_TransmitHandle *core_transmit;
267 * Globally unique tunnel identification (owner + number)
268 * DO NOT USE OVER THE NETWORK
273 * Node that owns the tunnel
278 * Tunnel number to differentiate all the tunnels owned by the node oid
279 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
281 MESH_TunnelNumber tid;
285 struct MeshClient; /* FWD declaration */
288 * Struct containing all information regarding a tunnel
289 * For an intermediate node the improtant info used will be:
290 * - id Tunnel unique identification
291 * - paths[0] To know where to send it next
292 * - metainfo: ready, speeds, accounting
299 struct MESH_TunnelID id;
302 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
304 MESH_TunnelNumber local_tid;
307 * Local tunnel number for local destination clients (incoming number)
308 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
311 MESH_TunnelNumber local_tid_dest;
314 * Global count ID of the last *multicast* packet seen/sent.
319 * Local count ID of the last packet seen/sent.
324 * SKIP value for this tunnel.
329 * How many messages are in the queue.
331 unsigned int queue_n;
334 * How many messages do we accept in the queue.
336 unsigned int queue_max;
339 * Last time the tunnel was used
341 struct GNUNET_TIME_Absolute timestamp;
344 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
345 * containing peers added by id or by type, not intermediate peers.
347 struct GNUNET_CONTAINER_MultiHashMap *peers;
350 * Number of peers that are connected and potentially ready to receive data
352 unsigned int peers_ready;
355 * Number of peers that have been added to the tunnel
357 unsigned int peers_total;
360 * Client owner of the tunnel, if any
362 struct MeshClient *owner;
365 * Clients that have been informed about the tunnel, if any
367 struct MeshClient **clients;
370 * Number of elements in clients
372 unsigned int nclients;
375 * Clients that have requested to leave the tunnel
377 struct MeshClient **ignore;
380 * Number of elements in clients
382 unsigned int nignore;
387 GNUNET_PEER_Id *blacklisted;
390 * Number of elements in blacklisted
392 unsigned int nblacklisted;
395 * Bloomfilter (for peer identities) to stop circular routes
397 char bloomfilter[MESH_BLOOM_SIZE];
402 struct MeshTunnelTree *tree;
405 * Application type we are looking for in this tunnel
407 GNUNET_MESH_ApplicationType type;
410 * Used to search peers offering a service
412 struct GNUNET_DHT_GetHandle *dht_get_type;
415 * Initial context of the regex search for a connect_by_string
417 struct MeshRegexSearchContext *regex_ctx;
420 * Task to keep the used paths alive
422 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
425 * Task to destroy the tunnel after timeout
427 * FIXME: merge the two? a tunnel will have either
428 * a path refresh OR a timeout, never both!
430 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
435 * Info needed to work with tunnel paths and peers
442 struct MeshTunnel *t;
445 * Neighbouring peer to whom we send the packet to
447 struct MeshPeerInfo *peer;
452 struct MeshPeerPath *path;
457 * Struct containing information about a client of the service
464 struct MeshClient *next;
469 struct MeshClient *prev;
472 * Tunnels that belong to this client, indexed by local id
474 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
477 * Tunnels this client has accepted, indexed by incoming local id
479 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
482 * Tunnels this client has rejected, indexed by incoming local id
484 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
486 * Handle to communicate with the client
488 struct GNUNET_SERVER_Client *handle;
491 * Applications that this client has claimed to provide
493 struct GNUNET_CONTAINER_MultiHashMap *apps;
496 * Messages that this client has declared interest in
498 struct GNUNET_CONTAINER_MultiHashMap *types;
501 * Whether the client is active or shutting down (don't send confirmations
502 * to a client that is shutting down.
507 * ID of the client, mainly for debug messages
512 * Regular expressions describing the services offered by this client.
514 char **regexes; // FIXME add timeout? API to remove a regex?
517 * Number of regular expressions in regexes.
519 unsigned int n_regex;
522 * Task to refresh all regular expresions in the DHT.
524 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
530 * Struct to keep information of searches of services described by a regex
531 * using a user-provided string service description.
533 struct MeshRegexSearchInfo
536 * Which tunnel is this for
538 struct MeshTunnel *t;
541 * User provided description of the searched service.
546 * Part of the description already consumed by the search.
553 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
556 * Results from running DHT GETs.
558 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
561 * Contexts, for each running DHT GET. Free all on end of search.
563 struct MeshRegexSearchContext **contexts;
566 * Number of contexts (branches/steps in search).
568 unsigned int n_contexts;
571 * Peer that is connecting via connect_by_string. When connected, free ctx.
576 * Other peers that are found but not yet being connected to.
578 GNUNET_PEER_Id *peers;
581 * Number of elements in peers.
583 unsigned int n_peers;
586 * Next peer to try to connect to.
591 * Timeout for a connect attempt.
592 * When reached, try to connect to a different peer, if any. If not,
593 * try the same peer again.
595 GNUNET_SCHEDULER_TaskIdentifier timeout;
600 * Struct to keep state of running searches that have consumed a part of
603 struct MeshRegexSearchContext
606 * Part of the description already consumed by
607 * this particular search branch.
612 * Information about the search.
614 struct MeshRegexSearchInfo *info;
618 /******************************************************************************/
619 /************************ DEBUG FUNCTIONS ****************************/
620 /******************************************************************************/
624 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
625 * @param cls string to print
626 * @param success GNUNET_OK if the PUT was transmitted,
627 * GNUNET_NO on timeout,
628 * GNUNET_SYSERR on disconnect from service
629 * after the PUT message was transmitted
630 * (so we don't know if it was received or not)
635 mesh_debug (void *cls, int success)
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
645 /******************************************************************************/
646 /*********************** GLOBAL VARIABLES ****************************/
647 /******************************************************************************/
652 static struct MeshClient *clients;
653 static struct MeshClient *clients_tail;
656 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
658 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
661 * Tunnels incoming, indexed by MESH_TunnelNumber
662 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
664 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
667 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
669 static struct GNUNET_CONTAINER_MultiHashMap *peers;
672 * Handle to communicate with transport
674 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
677 * Handle to communicate with core
679 static struct GNUNET_CORE_Handle *core_handle;
684 static struct GNUNET_DHT_Handle *dht_handle;
689 static struct GNUNET_SERVER_Handle *server_handle;
692 * Notification context, to send messages to local clients
694 static struct GNUNET_SERVER_NotificationContext *nc;
697 * Local peer own ID (memory efficient handle)
699 static GNUNET_PEER_Id myid;
702 * Local peer own ID (full value)
704 static struct GNUNET_PeerIdentity my_full_id;
709 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
714 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
717 * Tunnel ID for the next created tunnel (global tunnel number)
719 static MESH_TunnelNumber next_tid;
722 * Tunnel ID for the next incoming tunnel (local tunnel number)
724 static MESH_TunnelNumber next_local_tid;
727 * All application types provided by this peer
729 static struct GNUNET_CONTAINER_MultiHashMap *applications;
732 * All message types clients of this peer are interested in
734 static struct GNUNET_CONTAINER_MultiHashMap *types;
737 * Task to periodically announce provided applications
739 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
742 * Task to periodically announce itself in the network
744 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
747 * Next ID to assign to a client
749 unsigned int next_client_id;
752 /******************************************************************************/
753 /*********************** DECLARATIONS **************************/
754 /******************************************************************************/
756 /* FIXME move declarations here */
759 * Function to process paths received for a new peer addition. The recorded
760 * paths form the initial tunnel, which can be optimized later.
761 * Called on each result obtained for the DHT search.
764 * @param exp when will this value expire
765 * @param key key of the result
766 * @param type type of the result
767 * @param size number of bytes in data
768 * @param data pointer to the result data
771 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
772 const struct GNUNET_HashCode * key,
773 const struct GNUNET_PeerIdentity *get_path,
774 unsigned int get_path_length,
775 const struct GNUNET_PeerIdentity *put_path,
776 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
777 size_t size, const void *data);
781 * Function to process DHT string to regex matching.
782 * Called on each result obtained for the DHT search.
784 * @param cls closure (search context)
785 * @param exp when will this value expire
786 * @param key key of the result
787 * @param get_path path of the get request (not used)
788 * @param get_path_length lenght of get_path (not used)
789 * @param put_path path of the put request (not used)
790 * @param put_path_length length of the put_path (not used)
791 * @param type type of the result
792 * @param size number of bytes in data
793 * @param data pointer to the result data
795 * TODO: re-issue the request after certain time? cancel after X results?
798 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
799 const struct GNUNET_HashCode * key,
800 const struct GNUNET_PeerIdentity *get_path,
801 unsigned int get_path_length,
802 const struct GNUNET_PeerIdentity *put_path,
803 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
804 size_t size, const void *data);
808 * Function to process DHT string to regex matching.
809 * Called on each result obtained for the DHT search.
811 * @param cls closure (search context)
812 * @param exp when will this value expire
813 * @param key key of the result
814 * @param get_path path of the get request (not used)
815 * @param get_path_length lenght of get_path (not used)
816 * @param put_path path of the put request (not used)
817 * @param put_path_length length of the put_path (not used)
818 * @param type type of the result
819 * @param size number of bytes in data
820 * @param data pointer to the result data
823 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
824 const struct GNUNET_HashCode * key,
825 const struct GNUNET_PeerIdentity *get_path,
826 unsigned int get_path_length,
827 const struct GNUNET_PeerIdentity *put_path,
828 unsigned int put_path_length,
829 enum GNUNET_BLOCK_Type type,
830 size_t size, const void *data);
834 * Iterator over edges in a regex block retrieved from the DHT.
836 * @param cls Closure.
837 * @param token Token that follows to next state.
838 * @param len Lenght of token.
839 * @param key Hash of next state.
841 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
844 regex_edge_iterator (void *cls,
847 const struct GNUNET_HashCode *key);
851 * Find a path to a peer that offers a regex servcie compatible
852 * with a given string.
854 * @param key The key of the accepting state.
855 * @param ctx Context containing info about the string, tunnel, etc.
858 regex_find_path (const struct GNUNET_HashCode *key,
859 struct MeshRegexSearchContext *ctx);
861 /******************************************************************************/
862 /************************ ITERATORS ****************************/
863 /******************************************************************************/
865 /* FIXME move iterators here */
868 * Iterator over found existing mesh regex blocks that match an ongoing search.
871 * @param key current key code
872 * @param value value in the hash map
873 * @return GNUNET_YES if we should continue to iterate,
877 regex_result_iterator (void *cls,
878 const struct GNUNET_HashCode * key,
881 struct MeshRegexBlock *block = value;
882 struct MeshRegexSearchContext *ctx = cls;
884 if (GNUNET_YES == ntohl(block->accepting) &&
885 ctx->position == strlen (ctx->info->description))
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
888 regex_find_path (key, ctx);
889 return GNUNET_YES; // We found an accept state!
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
894 ctx->position, strlen(ctx->info->description),
895 ntohl(block->accepting));
898 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
899 ®ex_edge_iterator, ctx);
905 * Iterator over edges in a regex block retrieved from the DHT.
907 * @param cls Closure (context of the search).
908 * @param token Token that follows to next state.
909 * @param len Lenght of token.
910 * @param key Hash of next state.
912 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
915 regex_edge_iterator (void *cls,
918 const struct GNUNET_HashCode *key)
920 struct MeshRegexSearchContext *ctx = cls;
921 struct MeshRegexSearchContext *new_ctx;
922 struct MeshRegexSearchInfo *info = ctx->info;
923 struct GNUNET_DHT_GetHandle *get_h;
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
930 current = &info->description[ctx->position];
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
932 current_len = strlen (info->description) - ctx->position;
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
937 if (len > current_len)
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
940 return GNUNET_YES; // Token too long, wont match
942 if (0 != strncmp (current, token, len))
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
945 return GNUNET_YES; // Token doesn't match
947 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
948 new_ctx->info = info;
949 new_ctx->position = ctx->position + len;
950 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
952 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
955 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
956 ®ex_result_iterator,
958 return GNUNET_YES; // We are already looking for it
960 /* Start search in DHT */
962 GNUNET_DHT_get_start (dht_handle, /* handle */
963 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
964 key, /* key to search */
965 DHT_REPLICATION_LEVEL, /* replication level */
966 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
967 NULL, /* xquery */ // FIXME BLOOMFILTER
968 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
969 &dht_get_string_handler, new_ctx);
971 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
972 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
982 * Iterator over hash map entries to cancel DHT GET requests after a
983 * successful connect_by_string.
985 * @param cls Closure (unused).
986 * @param key Current key code (unused).
987 * @param value Value in the hash map (get handle).
988 * @return GNUNET_YES if we should continue to iterate,
992 regex_cancel_dht_get (void *cls,
993 const struct GNUNET_HashCode * key,
996 struct GNUNET_DHT_GetHandle *h = value;
998 GNUNET_DHT_get_stop (h);
1004 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1005 * search for connect_by_string.
1007 * @param cls Closure (unused).
1008 * @param key Current key code (unused).
1009 * @param value MeshRegexBlock in the hash map.
1010 * @return GNUNET_YES if we should continue to iterate,
1014 regex_free_result (void *cls,
1015 const struct GNUNET_HashCode * key,
1019 GNUNET_free (value);
1025 * Regex callback iterator to store own service description in the DHT.
1027 * @param cls closure.
1028 * @param key hash for current state.
1029 * @param proof proof for current state.
1030 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1031 * @param num_edges number of edges leaving current state.
1032 * @param edges edges leaving current state.
1035 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1036 int accepting, unsigned int num_edges,
1037 const struct GNUNET_REGEX_Edge *edges)
1039 struct MeshRegexBlock *block;
1040 struct MeshRegexEdge *block_edge;
1041 enum GNUNET_DHT_RouteOption opt;
1045 unsigned int offset;
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1049 " regex dht put for state %s\n",
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1059 if (GNUNET_YES == accepting)
1061 struct MeshRegexAccept block;
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 " state %s is accepting, putting own id\n",
1066 size = sizeof (block);
1068 block.id = my_full_id;
1070 GNUNET_DHT_put(dht_handle, key,
1071 DHT_REPLICATION_LEVEL,
1072 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1073 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1076 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1081 len = strlen(proof);
1082 size = sizeof (struct MeshRegexBlock) + len;
1083 block = GNUNET_malloc (size);
1086 block->n_proof = htonl (len);
1087 block->n_edges = htonl (num_edges);
1088 block->accepting = htonl (accepting);
1090 /* Store the proof at the end of the block. */
1091 aux = (char *) &block[1];
1092 memcpy (aux, proof, len);
1095 /* Store each edge in a variable length MeshEdge struct at the
1096 * very end of the MeshRegexBlock structure.
1098 for (i = 0; i < num_edges; i++)
1100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1101 " edge %s towards %s\n",
1103 GNUNET_h2s(&edges[i].destination));
1105 /* aux points at the end of the last block */
1106 len = strlen (edges[i].label);
1107 size += sizeof (struct MeshRegexEdge) + len;
1108 // Calculate offset FIXME is this ok? use size instead?
1109 offset = aux - (char *) block;
1110 block = GNUNET_realloc (block, size);
1111 aux = &((char *) block)[offset];
1112 block_edge = (struct MeshRegexEdge *) aux;
1113 block_edge->key = edges[i].destination;
1114 block_edge->n_token = htonl (len);
1115 aux = (char *) &block_edge[1];
1116 memcpy (aux, edges[i].label, len);
1120 GNUNET_DHT_put(dht_handle, key,
1121 DHT_REPLICATION_LEVEL,
1123 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1125 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1129 GNUNET_free (block);
1134 * Store the regular expression describing a local service into the DHT.
1136 * @param regex The regular expresion.
1139 regex_put (const char *regex)
1141 struct GNUNET_REGEX_Automaton *dfa;
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1144 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1145 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1146 GNUNET_REGEX_automaton_destroy (dfa);
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1152 * Find a path to a peer that offers a regex servcie compatible
1153 * with a given string.
1155 * @param key The key of the accepting state.
1156 * @param ctx Context containing info about the string, tunnel, etc.
1159 regex_find_path (const struct GNUNET_HashCode *key,
1160 struct MeshRegexSearchContext *ctx)
1162 struct GNUNET_DHT_GetHandle *get_h;
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1165 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1166 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1167 key, /* key to search */
1168 DHT_REPLICATION_LEVEL, /* replication level */
1169 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1170 GNUNET_DHT_RO_RECORD_ROUTE,
1171 NULL, /* xquery */ // FIXME BLOOMFILTER
1172 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1173 &dht_get_string_accept_handler, ctx);
1174 GNUNET_break (GNUNET_OK ==
1175 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1178 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1182 /******************************************************************************/
1183 /************************ PERIODIC FUNCTIONS ****************************/
1184 /******************************************************************************/
1187 * Announce iterator over for each application provided by the peer
1189 * @param cls closure
1190 * @param key current key code
1191 * @param value value in the hash map
1192 * @return GNUNET_YES if we should continue to
1197 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1199 struct PBlock block;
1200 struct MeshClient *c;
1202 block.id = my_full_id;
1203 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1204 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1205 if (0 == block.type)
1210 block.type = htonl (block.type);
1211 /* FIXME are hashes in multihash map equal on all aquitectures? */
1212 /* FIXME: keep return value of 'put' to possibly cancel!? */
1213 GNUNET_DHT_put (dht_handle, key,
1214 DHT_REPLICATION_LEVEL,
1215 GNUNET_DHT_RO_RECORD_ROUTE |
1216 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1217 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1219 (const char *) &block,
1220 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1222 APP_ANNOUNCE_TIME, NULL, NULL);
1228 * Periodically announce what applications are provided by local clients
1231 * @param cls closure
1232 * @param tc task context
1235 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1237 struct MeshClient *c = cls;
1240 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1242 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1246 DEBUG_DHT ("Starting PUT for regex\n");
1248 for (i = 0; i < c->n_regex; i++)
1250 regex_put (c->regexes[i]);
1252 c->regex_announce_task =
1253 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_regex, cls);
1254 DEBUG_DHT ("Finished PUT for regex\n");
1261 * Periodically announce what applications are provided by local clients
1264 * @param cls closure
1265 * @param tc task context
1268 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1270 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1272 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1276 DEBUG_DHT ("Starting PUT for apps\n");
1278 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1280 announce_applications_task =
1281 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
1283 DEBUG_DHT ("Finished PUT for apps\n");
1290 * Periodically announce self id in the DHT
1292 * @param cls closure
1293 * @param tc task context
1296 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1298 struct PBlock block;
1300 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1302 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1306 * - Set data expiration in function of X
1307 * - Adapt X to churn
1309 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1311 block.id = my_full_id;
1312 block.type = htonl (0);
1313 GNUNET_DHT_put (dht_handle, /* DHT handle */
1314 &my_full_id.hashPubKey, /* Key to use */
1315 DHT_REPLICATION_LEVEL, /* Replication level */
1316 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1317 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1318 sizeof (block), /* Size of the data */
1319 (const char *) &block, /* Data itself */
1320 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1321 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1322 NULL, /* Continuation */
1323 NULL); /* Continuation closure */
1325 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
1329 /******************************************************************************/
1330 /****************** GENERAL HELPER FUNCTIONS ************************/
1331 /******************************************************************************/
1334 * Cancel an ongoing regex search in the DHT and free all resources.
1336 * @param ctx The search context.
1339 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1341 struct MeshRegexSearchInfo *info = ctx->info;
1344 GNUNET_free (info->description);
1345 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1346 ®ex_cancel_dht_get, NULL);
1347 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1348 ®ex_free_result, NULL);
1349 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1350 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1351 info->t->regex_ctx = NULL;
1352 for (i = 0; i < info->n_contexts; i++)
1354 GNUNET_free (info->contexts[i]);
1356 if (0 < info->n_contexts)
1357 GNUNET_free (info->contexts);
1358 if (0 < info->n_peers)
1359 GNUNET_free (info->peers);
1360 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1362 GNUNET_SCHEDULER_cancel(info->timeout);
1368 * Search for a tunnel by global ID using full PeerIdentities
1370 * @param oid owner of the tunnel
1371 * @param tid global tunnel number
1373 * @return tunnel handler, NULL if doesn't exist
1375 static struct MeshTunnel *
1376 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1380 * Delete an active client from the tunnel.
1386 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1389 * Notify a tunnel that a connection has broken that affects at least
1390 * some of its peers.
1392 * @param t Tunnel affected.
1393 * @param p1 Peer that got disconnected from p2.
1394 * @param p2 Peer that got disconnected from p1.
1396 * @return Short ID of the peer disconnected (either p1 or p2).
1397 * 0 if the tunnel remained unaffected.
1399 static GNUNET_PEER_Id
1400 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1405 * Check if client has registered with the service and has not disconnected
1407 * @param client the client to check
1409 * @return non-NULL if client exists in the global DLL
1411 static struct MeshClient *
1412 client_get (struct GNUNET_SERVER_Client *client)
1414 struct MeshClient *c;
1419 if (c->handle == client)
1428 * Checks if a given client has subscribed to certain message type
1430 * @param message_type Type of message to check
1431 * @param c Client to check
1433 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1438 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1440 struct GNUNET_HashCode hc;
1442 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1443 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1448 * Allow a client to send more data after transmitting a multicast message
1449 * which some neighbor has not yet accepted altough a reasonable time has
1452 * @param cls Closure (DataDescriptor containing the task identifier)
1453 * @param tc Task Context
1455 * FIXME reference counter cshould be just int
1458 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1460 struct MeshData *mdata = cls;
1462 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1464 GNUNET_assert (NULL != mdata->reference_counter);
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1466 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1467 *(mdata->reference_counter));
1468 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1469 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1474 * Check whether client wants traffic from a tunnel.
1476 * @param c Client to check.
1477 * @param t Tunnel to be found.
1479 * @return GNUNET_YES if client knows tunnel.
1481 * TODO look in client hashmap
1484 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1488 for (i = 0; i < t->nclients; i++)
1489 if (t->clients[i] == c)
1496 * Check whether client has been informed about a tunnel.
1498 * @param c Client to check.
1499 * @param t Tunnel to be found.
1501 * @return GNUNET_YES if client knows tunnel.
1503 * TODO look in client hashmap
1506 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1510 for (i = 0; i < t->nignore; i++)
1511 if (t->ignore[i] == c)
1513 return client_wants_tunnel(c, t);
1518 * Marks a client as uninterested in traffic from the tunnel, updating both
1519 * client and tunnel to reflect this.
1521 * @param c Client that doesn't want traffic anymore.
1522 * @param t Tunnel which should be ignored.
1524 * FIXME when to delete an incoming tunnel?
1527 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1529 struct GNUNET_HashCode hash;
1531 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1532 GNUNET_break (GNUNET_YES ==
1533 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1535 GNUNET_break (GNUNET_YES ==
1536 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1537 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1538 tunnel_delete_active_client (t, c);
1539 GNUNET_array_append (t->ignore, t->nignore, c);
1544 * Deletes a tunnel from a client (either owner or destination). To be used on
1545 * tunnel destroy, otherwise, use client_ignore_tunnel.
1547 * @param c Client whose tunnel to delete.
1548 * @param t Tunnel which should be deleted.
1551 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1553 struct GNUNET_HashCode hash;
1557 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1558 GNUNET_assert (GNUNET_YES ==
1559 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1565 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1567 GNUNET_assert (GNUNET_YES ==
1568 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1572 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1581 * Send the message to all clients that have subscribed to its type
1583 * @param msg Pointer to the message itself
1584 * @param payload Pointer to the payload of the message.
1585 * @return number of clients this message was sent to
1588 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1589 const struct GNUNET_MessageHeader *payload)
1591 struct GNUNET_PeerIdentity *oid;
1592 struct MeshClient *c;
1593 struct MeshTunnel *t;
1594 MESH_TunnelNumber *tid;
1597 char cbuf[htons (msg->size)];
1599 type = ntohs (payload->type);
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1603 memcpy (cbuf, msg, sizeof (cbuf));
1604 switch (htons (msg->type))
1606 struct GNUNET_MESH_Unicast *uc;
1607 struct GNUNET_MESH_Multicast *mc;
1608 struct GNUNET_MESH_ToOrigin *to;
1610 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1611 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1615 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1616 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1620 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1621 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1629 t = tunnel_get (oid, ntohl (*tid));
1636 for (count = 0, c = clients; c != NULL; c = c->next)
1638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1639 if (client_is_subscribed (type, c))
1641 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1645 *tid = htonl (t->local_tid);
1649 if (GNUNET_NO == client_knows_tunnel (c, t))
1651 /* This client doesn't know the tunnel */
1652 struct GNUNET_MESH_TunnelNotification tmsg;
1653 struct GNUNET_HashCode hash;
1655 tmsg.header.size = htons (sizeof (tmsg));
1656 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1657 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1658 tmsg.tunnel_id = htonl (t->local_tid_dest);
1659 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1660 &tmsg.header, GNUNET_NO);
1661 GNUNET_array_append (t->clients, t->nclients, c);
1662 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1664 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1665 c->incoming_tunnels, &hash, t,
1666 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1668 *tid = htonl (t->local_tid_dest);
1671 /* Check if the client wants to get traffic from the tunnel */
1672 if (GNUNET_NO == client_wants_tunnel(c, t))
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1676 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1677 (struct GNUNET_MessageHeader
1678 *) cbuf, GNUNET_YES);
1686 * Notify the client that owns the tunnel that a peer has connected to it
1687 * (the requested path to it has been confirmed).
1689 * @param t Tunnel whose owner to notify
1690 * @param id Short id of the peer that has connected
1693 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1695 struct GNUNET_MESH_PeerControl pc;
1697 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1698 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1699 pc.tunnel_id = htonl (t->local_tid);
1700 GNUNET_PEER_resolve (id, &pc.peer);
1701 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1707 * Notify all clients (not depending on registration status) that the incoming
1708 * tunnel is no longer valid.
1710 * @param t Tunnel that was destroyed.
1713 send_clients_tunnel_destroy (struct MeshTunnel *t)
1715 struct GNUNET_MESH_TunnelMessage msg;
1717 msg.header.size = htons (sizeof (msg));
1718 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1719 msg.tunnel_id = htonl (t->local_tid_dest);
1720 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1725 * Notify clients of tunnel disconnections, if needed.
1726 * In case the origin disconnects, the destination clients get a tunnel destroy
1727 * notification. If the last destination disconnects (only one remaining client
1728 * in tunnel), the origin gets a (local ID) peer disconnected.
1729 * Note that the function must be called BEFORE removing the client from
1732 * @param t Tunnel that was destroyed.
1733 * @param c Client that disconnected.
1736 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1742 struct GNUNET_MESH_TunnelMessage msg;
1744 msg.header.size = htons (sizeof (msg));
1745 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1746 msg.tunnel_id = htonl (t->local_tid_dest);
1747 for (i = 0; i < t->nclients; i++)
1748 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1749 &msg.header, GNUNET_NO);
1751 // FIXME when to disconnect an incoming tunnel?
1752 else if (1 == t->nclients && NULL != t->owner)
1754 struct GNUNET_MESH_PeerControl msg;
1756 msg.header.size = htons (sizeof (msg));
1757 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1758 msg.tunnel_id = htonl (t->local_tid);
1759 msg.peer = my_full_id;
1760 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1761 &msg.header, GNUNET_NO);
1767 * Decrements the reference counter and frees all resources if needed
1769 * @param mesh_data Data Descriptor used in a multicast message.
1770 * Freed no longer needed (last message).
1773 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1775 /* Make sure it's a multicast packet */
1776 GNUNET_assert (NULL != mesh_data->reference_counter); // FIXME URGENT #2499
1778 if (0 == --(*(mesh_data->reference_counter)))
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1781 if (NULL != mesh_data->task)
1783 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1785 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1787 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1789 GNUNET_free (mesh_data->task);
1791 GNUNET_free (mesh_data->reference_counter);
1792 GNUNET_free (mesh_data->data);
1793 GNUNET_free (mesh_data);
1799 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1800 * and insert it in the appropiate structures if the peer is not known yet.
1802 * @param peer Full identity of the peer.
1804 * @return Existing or newly created peer info.
1806 static struct MeshPeerInfo *
1807 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1809 struct MeshPeerInfo *peer_info;
1811 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1812 if (NULL == peer_info)
1815 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1816 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1817 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1818 peer_info->id = GNUNET_PEER_intern (peer);
1826 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1827 * and insert it in the appropiate structures if the peer is not known yet.
1829 * @param peer Short identity of the peer.
1831 * @return Existing or newly created peer info.
1833 static struct MeshPeerInfo *
1834 peer_info_get_short (const GNUNET_PEER_Id peer)
1836 struct GNUNET_PeerIdentity id;
1838 GNUNET_PEER_resolve (peer, &id);
1839 return peer_info_get (&id);
1844 * Iterator to remove the tunnel from the list of tunnels a peer participates
1847 * @param cls Closure (tunnel info)
1848 * @param key GNUNET_PeerIdentity of the peer (unused)
1849 * @param value PeerInfo of the peer
1851 * @return always GNUNET_YES, to keep iterating
1854 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1856 struct MeshTunnel *t = cls;
1857 struct MeshPeerInfo *peer = value;
1860 for (i = 0; i < peer->ntunnels; i++)
1863 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1866 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1867 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1876 * Queue and pass message to core when possible.
1878 * @param cls Closure (type dependant).
1879 * @param type Type of the message.
1880 * @param size Size of the message.
1881 * @param dst Neighbor to send message to.
1882 * @param t Tunnel this message belongs to.
1885 queue_add (void *cls, uint16_t type, size_t size,
1886 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1889 * Core callback to write a pre-constructed data packet to core buffer
1891 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1892 * @param size Number of bytes available in buf.
1893 * @param buf Where the to write the message.
1895 * @return number of bytes written to buf
1898 send_core_data_raw (void *cls, size_t size, void *buf)
1900 struct MeshTransmissionDescriptor *info = cls;
1901 struct GNUNET_MessageHeader *msg;
1904 GNUNET_assert (NULL != info);
1905 GNUNET_assert (NULL != info->mesh_data);
1906 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1907 total_size = ntohs (msg->size);
1909 if (total_size > size)
1914 memcpy (buf, msg, total_size);
1915 GNUNET_free (info->mesh_data);
1922 * Sends an already built unicast message to a peer, properly registrating
1923 * all used resources.
1925 * @param message Message to send. Function makes a copy of it.
1926 * @param peer Short ID of the neighbor whom to send the message.
1927 * @param t Tunnel on which this message is transmitted.
1930 send_message (const struct GNUNET_MessageHeader *message,
1931 const struct GNUNET_PeerIdentity *peer,
1932 struct MeshTunnel *t)
1934 struct MeshTransmissionDescriptor *info;
1935 struct MeshPeerInfo *neighbor;
1936 struct MeshPeerPath *p;
1939 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1941 size = ntohs (message->size);
1942 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1943 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1944 info->mesh_data->data = GNUNET_malloc (size);
1945 memcpy (info->mesh_data->data, message, size);
1946 info->mesh_data->data_len = size;
1947 neighbor = peer_info_get (peer);
1948 for (p = neighbor->path_head; NULL != p; p = p->next)
1957 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1958 GNUNET_free (info->mesh_data->data);
1959 GNUNET_free (info->mesh_data);
1963 info->peer = neighbor;
1965 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
1973 * Sends a CREATE PATH message for a path to a peer, properly registrating
1974 * all used resources.
1976 * @param peer PeerInfo of the final peer for whom this path is being created.
1977 * @param p Path itself.
1978 * @param t Tunnel for which the path is created.
1981 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1982 struct MeshTunnel *t)
1984 struct GNUNET_PeerIdentity id;
1985 struct MeshPathInfo *path_info;
1986 struct MeshPeerInfo *neighbor;
1992 p = tree_get_path_to_peer (t->tree, peer->id);
1999 for (i = 0; i < p->length; i++)
2001 if (p->peers[i] == myid)
2004 if (i >= p->length - 1)
2010 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2012 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2013 path_info->path = p;
2015 neighbor = peer_info_get (&id);
2016 path_info->peer = neighbor;
2017 queue_add (path_info,
2018 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2019 sizeof (struct GNUNET_MESH_ManipulatePath) +
2020 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2027 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2029 * @param t Tunnel whose path to destroy.
2030 * @param destination Short ID of the peer to whom the path to destroy.
2033 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2035 struct MeshPeerPath *p;
2038 p = tree_get_path_to_peer (t->tree, destination);
2044 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2045 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2047 struct GNUNET_MESH_ManipulatePath *msg;
2048 struct GNUNET_PeerIdentity *pi;
2052 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2053 msg->header.size = htons (size);
2054 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2055 msg->tid = htonl (t->id.tid);
2056 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2057 for (i = 0; i < p->length; i++)
2059 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2061 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2068 * Try to establish a new connection to this peer.
2069 * Use the best path for the given tunnel.
2070 * If the peer doesn't have any path to it yet, try to get one.
2071 * If the peer already has some path, send a CREATE PATH towards it.
2073 * @param peer PeerInfo of the peer.
2074 * @param t Tunnel for which to create the path, if possible.
2077 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2079 struct MeshPeerPath *p;
2080 struct MeshPathInfo *path_info;
2082 if (NULL != peer->path_head)
2084 p = tree_get_path_to_peer (t->tree, peer->id);
2091 // FIXME always send create path to self
2094 send_create_path (peer, p, t);
2098 struct GNUNET_HashCode hash;
2101 send_client_peer_connected (t, myid);
2102 t->local_tid_dest = next_local_tid++;
2103 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2106 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2107 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2114 else if (NULL == peer->dhtget)
2116 struct GNUNET_PeerIdentity id;
2118 GNUNET_PEER_resolve (peer->id, &id);
2119 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2120 path_info->peer = peer;
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2124 peer->dhtgetcls = path_info;
2125 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2126 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2127 &id.hashPubKey, /* key to search */
2128 DHT_REPLICATION_LEVEL, /* replication level */
2129 GNUNET_DHT_RO_RECORD_ROUTE |
2130 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2131 NULL, /* xquery */ // FIXME BLOOMFILTER
2132 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2133 &dht_get_id_handler, path_info);
2135 /* Otherwise, there is no path but the DHT get is already started. */
2140 * Task to delay the connection of a peer
2142 * @param cls Closure (path info with tunnel and peer to connect).
2143 * Will be free'd on exection.
2144 * @param tc TaskContext
2147 peer_info_connect_task (void *cls,
2148 const struct GNUNET_SCHEDULER_TaskContext *tc)
2150 struct MeshPathInfo *path_info = cls;
2152 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2157 peer_info_connect (path_info->peer, path_info->t);
2163 * Destroy the peer_info and free any allocated resources linked to it
2165 * @param pi The peer_info to destroy.
2167 * @return GNUNET_OK on success
2170 peer_info_destroy (struct MeshPeerInfo *pi)
2172 struct GNUNET_PeerIdentity id;
2173 struct MeshPeerPath *p;
2174 struct MeshPeerPath *nextp;
2176 GNUNET_PEER_resolve (pi->id, &id);
2177 GNUNET_PEER_change_rc (pi->id, -1);
2180 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2183 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2184 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2186 if (NULL != pi->dhtget)
2188 GNUNET_DHT_get_stop (pi->dhtget);
2189 GNUNET_free (pi->dhtgetcls);
2195 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2205 * Remove all paths that rely on a direct connection between p1 and p2
2206 * from the peer itself and notify all tunnels about it.
2208 * @param peer PeerInfo of affected peer.
2209 * @param p1 GNUNET_PEER_Id of one peer.
2210 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2213 * TODO: optimize (see below)
2216 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2219 struct MeshPeerPath *p;
2220 struct MeshPeerPath *aux;
2221 struct MeshPeerInfo *peer_d;
2223 unsigned int destroyed;
2228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2230 p = peer->path_head;
2234 for (i = 0; i < (p->length - 1); i++)
2236 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2237 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2239 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2250 for (i = 0; i < peer->ntunnels; i++)
2252 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2256 * Problem: one or more peers have been deleted from the tunnel tree.
2257 * We don't know who they are to try to add them again.
2258 * We need to try to find a new path for each of the disconnected peers.
2259 * Some of them might already have a path to reach them that does not
2260 * involve p1 and p2. Adding all anew might render in a better tree than
2261 * the trivial immediate fix.
2263 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2264 * its children will be reachable trough him.
2266 peer_d = peer_info_get_short (d);
2269 for (p = peer_d->path_head; NULL != p; p = p->next)
2271 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2279 /* No callback, as peer will be already disconnected and a connection
2280 * scheduled by tunnel_notify_connection_broken.
2282 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2286 peer_info_connect (peer_d, peer->tunnels[i]);
2289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2294 * Add the path to the peer and update the path used to reach it in case this
2297 * @param peer_info Destination peer to add the path to.
2298 * @param path New path to add. Last peer must be the peer in arg 1.
2299 * Path will be either used of freed if already known.
2300 * @param trusted Do we trust that this path is real?
2303 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2306 struct MeshPeerPath *aux;
2310 if ((NULL == peer_info) || (NULL == path))
2313 path_destroy (path);
2316 if (path->peers[path->length - 1] != peer_info->id)
2319 path_destroy (path);
2322 if (path->length <= 2 && GNUNET_NO == trusted)
2324 /* Only allow CORE to tell us about direct paths */
2325 path_destroy (path);
2328 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2329 for (l = 1; l < path->length; l++)
2331 if (path->peers[l] == myid)
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2334 for (l2 = 0; l2 < path->length - l; l2++)
2336 path->peers[l2] = path->peers[l + l2];
2341 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2346 struct GNUNET_PeerIdentity id;
2348 GNUNET_PEER_resolve (peer_info->id, &id);
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2350 path->length, GNUNET_i2s (&id));
2353 l = path_get_length (path);
2360 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2361 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2363 l2 = path_get_length (aux);
2366 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2367 peer_info->path_tail, aux, path);
2372 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2374 path_destroy (path);
2379 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2386 * Add the path to the origin peer and update the path used to reach it in case
2387 * this is the shortest.
2388 * The path is given in peer_info -> destination, therefore we turn the path
2389 * upside down first.
2391 * @param peer_info Peer to add the path to, being the origin of the path.
2392 * @param path New path to add after being inversed.
2393 * @param trusted Do we trust that this path is real?
2396 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2397 struct MeshPeerPath *path, int trusted)
2400 peer_info_add_path (peer_info, path, trusted);
2405 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2406 * to obtain a local peer -> destination path and interning the peer ids.
2408 * @return Newly allocated and created path
2410 static struct MeshPeerPath *
2411 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2412 unsigned int get_path_length,
2413 const struct GNUNET_PeerIdentity *put_path,
2414 unsigned int put_path_length)
2416 struct MeshPeerPath *p;
2422 GNUNET_PEER_change_rc (myid, 1);
2423 i = get_path_length;
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2425 for (i--; i >= 0; i--)
2427 id = GNUNET_PEER_intern (&get_path[i]);
2428 if (p->length > 0 && id == p->peers[p->length - 1])
2430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2431 GNUNET_PEER_change_rc (id, -1);
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2436 GNUNET_i2s (&get_path[i]));
2438 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2439 p->peers[p->length - 1] = id;
2442 i = put_path_length;
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2444 for (i--; i >= 0; i--)
2446 id = GNUNET_PEER_intern (&put_path[i]);
2449 /* PUT path went through us, so discard the path up until now and start
2450 * from here to get a much shorter (and loop-free) path.
2455 if (p->length > 0 && id == p->peers[p->length - 1])
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2458 GNUNET_PEER_change_rc (id, -1);
2462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2463 GNUNET_i2s (&put_path[i]));
2465 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2466 p->peers[p->length - 1] = id;
2470 if (get_path_length > 0)
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2472 GNUNET_i2s (&get_path[0]));
2473 if (put_path_length > 0)
2474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2475 GNUNET_i2s (&put_path[0]));
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2478 for (i = 0; i < p->length; i++)
2480 struct GNUNET_PeerIdentity peer_id;
2482 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2484 GNUNET_i2s (&peer_id));
2492 * Adds a path to the peer_infos of all the peers in the path
2494 * @param p Path to process.
2495 * @param confirmed Whether we know if the path works or not. FIXME use
2498 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2502 /* TODO: invert and add */
2503 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2504 for (i++; i < p->length; i++)
2506 struct MeshPeerInfo *aux;
2507 struct MeshPeerPath *copy;
2509 aux = peer_info_get_short (p->peers[i]);
2510 copy = path_duplicate (p);
2511 copy->length = i + 1;
2512 peer_info_add_path (aux, copy, GNUNET_NO);
2518 * Send keepalive packets for a peer
2520 * @param cls Closure (tunnel for which to send the keepalive).
2521 * @param tc Notification context.
2523 * TODO: implement explicit multicast keepalive?
2526 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2530 * Search for a tunnel among the incoming tunnels
2532 * @param tid the local id of the tunnel
2534 * @return tunnel handler, NULL if doesn't exist
2536 static struct MeshTunnel *
2537 tunnel_get_incoming (MESH_TunnelNumber tid)
2539 struct GNUNET_HashCode hash;
2541 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2542 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2543 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2548 * Search for a tunnel among the tunnels for a client
2550 * @param c the client whose tunnels to search in
2551 * @param tid the local id of the tunnel
2553 * @return tunnel handler, NULL if doesn't exist
2555 static struct MeshTunnel *
2556 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2558 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2560 return tunnel_get_incoming (tid);
2564 struct GNUNET_HashCode hash;
2566 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2567 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2573 * Search for a tunnel by global ID using PEER_ID
2575 * @param pi owner of the tunnel
2576 * @param tid global tunnel number
2578 * @return tunnel handler, NULL if doesn't exist
2580 static struct MeshTunnel *
2581 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2583 struct MESH_TunnelID id;
2584 struct GNUNET_HashCode hash;
2589 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2590 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2595 * Search for a tunnel by global ID using full PeerIdentities
2597 * @param oid owner of the tunnel
2598 * @param tid global tunnel number
2600 * @return tunnel handler, NULL if doesn't exist
2602 static struct MeshTunnel *
2603 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2605 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2610 * Delete an active client from the tunnel.
2616 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2620 for (i = 0; i < t->nclients; i++)
2622 if (t->clients[i] == c)
2624 t->clients[i] = t->clients[t->nclients - 1];
2625 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2633 * Delete an ignored client from the tunnel.
2639 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2643 for (i = 0; i < t->nignore; i++)
2645 if (t->ignore[i] == c)
2647 t->ignore[i] = t->ignore[t->nignore - 1];
2648 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2656 * Delete a client from the tunnel. It should be only done on
2657 * client disconnection, otherwise use client_ignore_tunnel.
2663 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2665 tunnel_delete_ignored_client (t, c);
2666 tunnel_delete_active_client (t, c);
2671 * Callback used to notify a client owner of a tunnel that a peer has
2672 * disconnected, most likely because of a path change.
2674 * @param cls Closure (tunnel this notification is about).
2675 * @param peer_id Short ID of disconnected peer.
2678 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2680 struct MeshTunnel *t = cls;
2681 struct MeshPeerInfo *peer;
2682 struct MeshPathInfo *path_info;
2684 if (NULL != t->owner && NULL != nc)
2686 struct GNUNET_MESH_PeerControl msg;
2688 msg.header.size = htons (sizeof (msg));
2689 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2690 msg.tunnel_id = htonl (t->local_tid);
2691 GNUNET_PEER_resolve (peer_id, &msg.peer);
2692 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2693 &msg.header, GNUNET_NO);
2695 peer = peer_info_get_short (peer_id);
2696 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2697 path_info->peer = peer;
2699 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2704 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2705 * needed rescources.
2706 * If peer already exists, reevaluate shortest path and change if different.
2708 * @param t Tunnel we want to add a new peer to
2709 * @param peer PeerInfo of the peer being added
2713 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2715 struct GNUNET_PeerIdentity id;
2716 struct MeshPeerPath *best_p;
2717 struct MeshPeerPath *p;
2718 unsigned int best_cost;
2721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2722 GNUNET_PEER_resolve (peer->id, &id);
2724 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2727 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2728 GNUNET_assert (GNUNET_OK ==
2729 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2731 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2734 if (NULL != (p = peer->path_head))
2737 best_cost = tree_get_path_cost (t->tree, p);
2740 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2747 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2748 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2749 t->path_refresh_task =
2750 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2754 /* Start a DHT get */
2755 peer_info_connect (peer, t);
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2761 * Add a path to a tunnel which we don't own, just to remember the next hop.
2762 * If destination node was already in the tunnel, the first hop information
2763 * will be replaced with the new path.
2765 * @param t Tunnel we want to add a new peer to
2766 * @param p Path to add
2767 * @param own_pos Position of local node in path.
2771 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2772 unsigned int own_pos)
2774 struct GNUNET_PeerIdentity id;
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2777 GNUNET_assert (0 != own_pos);
2778 tree_add_path (t->tree, p, NULL, NULL);
2779 if (own_pos < p->length - 1)
2781 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2782 tree_update_first_hops (t->tree, myid, &id);
2784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2789 * Notifies a tunnel that a connection has broken that affects at least
2790 * some of its peers. Sends a notification towards the root of the tree.
2791 * In case the peer is the owner of the tree, notifies the client that owns
2792 * the tunnel and tries to reconnect.
2794 * @param t Tunnel affected.
2795 * @param p1 Peer that got disconnected from p2.
2796 * @param p2 Peer that got disconnected from p1.
2798 * @return Short ID of the peer disconnected (either p1 or p2).
2799 * 0 if the tunnel remained unaffected.
2801 static GNUNET_PEER_Id
2802 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2808 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2810 if (myid != p1 && myid != p2)
2816 if (tree_get_predecessor (t->tree) != 0)
2818 /* We are the peer still connected, notify owner of the disconnection. */
2819 struct GNUNET_MESH_PathBroken msg;
2820 struct GNUNET_PeerIdentity neighbor;
2822 msg.header.size = htons (sizeof (msg));
2823 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2824 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2825 msg.tid = htonl (t->id.tid);
2826 msg.peer1 = my_full_id;
2827 GNUNET_PEER_resolve (pid, &msg.peer2);
2828 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2829 send_message (&msg.header, &neighbor, t);
2837 * Send a multicast packet to a neighbor.
2839 * @param cls Closure (Info about the multicast packet)
2840 * @param neighbor_id Short ID of the neighbor to send the packet to.
2843 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2845 struct MeshData *mdata = cls;
2846 struct MeshTransmissionDescriptor *info;
2847 struct GNUNET_PeerIdentity neighbor;
2849 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2851 info->mesh_data = mdata;
2852 (*(mdata->reference_counter)) ++;
2853 info->destination = neighbor_id;
2854 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2856 GNUNET_i2s (&neighbor));
2857 info->peer = peer_info_get (&neighbor);
2858 GNUNET_assert (NULL != info->peer);
2860 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2861 info->mesh_data->data_len,
2867 * Send a message in a tunnel in multicast, sending a copy to each child node
2868 * down the local one in the tunnel tree.
2870 * @param t Tunnel in which to send the data.
2871 * @param msg Message to be sent.
2872 * @param internal Has the service generated this message?
2875 tunnel_send_multicast (struct MeshTunnel *t,
2876 const struct GNUNET_MessageHeader *msg,
2879 struct MeshData *mdata;
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2882 " sending a multicast packet...\n");
2883 mdata = GNUNET_malloc (sizeof (struct MeshData));
2884 mdata->data_len = ntohs (msg->size);
2885 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2887 mdata->data = GNUNET_malloc (mdata->data_len);
2888 memcpy (mdata->data, msg, mdata->data_len);
2889 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2891 struct GNUNET_MESH_Multicast *mcast;
2893 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2894 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2896 ntohl (mcast->ttl));
2900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2902 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2903 && GNUNET_NO == internal)
2905 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2907 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2913 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2914 if (*(mdata->reference_counter) == 0)
2916 GNUNET_free (mdata->data);
2917 GNUNET_free (mdata->reference_counter);
2918 if (NULL != mdata->task)
2920 GNUNET_SCHEDULER_cancel(*(mdata->task));
2921 GNUNET_free (mdata->task);
2922 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2924 // FIXME change order?
2925 GNUNET_free (mdata);
2927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2928 " sending a multicast packet done\n");
2934 * Send a message to all peers in this tunnel that the tunnel is no longer
2937 * @param t The tunnel whose peers to notify.
2940 tunnel_send_destroy (struct MeshTunnel *t)
2942 struct GNUNET_MESH_TunnelDestroy msg;
2944 msg.header.size = htons (sizeof (msg));
2945 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2946 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2947 msg.tid = htonl (t->id.tid);
2948 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2954 * Destroy the tunnel and free any allocated resources linked to it.
2956 * @param t the tunnel to destroy
2958 * @return GNUNET_OK on success
2961 tunnel_destroy (struct MeshTunnel *t)
2963 struct MeshClient *c;
2964 struct GNUNET_HashCode hash;
2975 struct GNUNET_PeerIdentity id;
2977 GNUNET_PEER_resolve (t->id.oid, &id);
2978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2979 GNUNET_i2s (&id), t->id.tid);
2981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2985 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2986 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2991 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2994 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2998 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2999 for (i = 0; i < t->nclients; i++)
3003 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3008 for (i = 0; i < t->nignore; i++)
3012 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3017 if (t->nclients > 0)
3020 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3024 GNUNET_free (t->clients);
3026 if (NULL != t->peers)
3028 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3030 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3033 tree_destroy (t->tree);
3034 if (NULL != t->regex_ctx)
3035 regex_cancel_search (t->regex_ctx);
3036 if (NULL != t->dht_get_type)
3037 GNUNET_DHT_get_stop (t->dht_get_type);
3038 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3039 GNUNET_SCHEDULER_cancel (t->timeout_task);
3040 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3041 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3048 * Create a new tunnel
3050 * @param owner Who is the owner of the tunnel (short ID).
3051 * @param tid Tunnel Number of the tunnel.
3052 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3053 * @param local Tunnel Number for the tunnel, for the client point of view.
3056 static struct MeshTunnel *
3057 tunnel_new (GNUNET_PEER_Id owner,
3058 MESH_TunnelNumber tid,
3059 struct MeshClient *client,
3060 MESH_TunnelNumber local)
3062 struct MeshTunnel *t;
3063 struct GNUNET_HashCode hash;
3065 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3068 t->queue_max = 1000; // FIXME API parameter
3069 t->tree = tree_new (owner);
3071 t->local_tid = local;
3073 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3075 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3076 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3081 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3087 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3089 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3090 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3094 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3104 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3105 * that are no longer needed, as well as nodes of no longer reachable peers.
3106 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3108 * @param t Tunnel from which to remove the path.
3109 * @param peer Short id of the peer which should be removed.
3112 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3114 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3120 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3121 * client when the client disconnects. If the client is not the owner, the
3122 * owner will get notified if no more clients are in the tunnel and the client
3123 * get removed from the tunnel's list.
3125 * @param cls closure (client that is disconnecting)
3126 * @param key the hash of the local tunnel id (used to access the hashmap)
3127 * @param value the value stored at the key (tunnel to destroy)
3129 * @return GNUNET_OK on success
3132 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3134 struct MeshTunnel *t = value;
3135 struct MeshClient *c = cls;
3138 send_client_tunnel_disconnect(t, c);
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3142 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3143 tunnel_delete_client(t, c);
3144 client_delete_tunnel(c, t);
3147 tunnel_send_destroy(t);
3148 r = tunnel_destroy (t);
3154 * Timeout function, destroys tunnel if called
3156 * @param cls Closure (tunnel to destroy).
3157 * @param tc TaskContext
3160 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3162 struct MeshTunnel *t = cls;
3164 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3166 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3171 * Resets the tunnel timeout. Starts it if no timeout was running.
3173 * @param t Tunnel whose timeout to reset.
3176 tunnel_reset_timeout (struct MeshTunnel *t)
3178 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3179 GNUNET_SCHEDULER_cancel (t->timeout_task);
3181 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3182 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
3186 /******************************************************************************/
3187 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3188 /******************************************************************************/
3191 * Function to send a create path packet to a peer.
3193 * @param cls closure
3194 * @param size number of bytes available in buf
3195 * @param buf where the callee should write the message
3196 * @return number of bytes written to buf
3199 send_core_path_create (void *cls, size_t size, void *buf)
3201 struct MeshPathInfo *info = cls;
3202 struct GNUNET_MESH_ManipulatePath *msg;
3203 struct GNUNET_PeerIdentity *peer_ptr;
3204 struct MeshTunnel *t = info->t;
3205 struct MeshPeerPath *p = info->path;
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3211 sizeof (struct GNUNET_MESH_ManipulatePath) +
3212 p->length * sizeof (struct GNUNET_PeerIdentity);
3214 if (size < size_needed || NULL == buf)
3219 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3220 msg->header.size = htons (size_needed);
3221 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3222 msg->tid = ntohl (t->id.tid);
3224 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3225 for (i = 0; i < p->length; i++)
3227 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3234 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3240 * Fill the core buffer
3242 * @param cls closure (data itself)
3243 * @param size number of bytes available in buf
3244 * @param buf where the callee should write the message
3246 * @return number of bytes written to buf
3249 send_core_data_multicast (void *cls, size_t size, void *buf)
3251 struct MeshTransmissionDescriptor *info = cls;
3254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3255 GNUNET_assert (NULL != info);
3256 GNUNET_assert (NULL != info->peer);
3257 total_size = info->mesh_data->data_len;
3258 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3260 if (total_size > size)
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3266 memcpy (buf, info->mesh_data->data, total_size);
3269 struct GNUNET_MESH_Multicast *mc;
3270 struct GNUNET_MessageHeader *mh;
3273 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3275 mc = (struct GNUNET_MESH_Multicast *) mh;
3276 mh = (struct GNUNET_MessageHeader *) &mc[1];
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3278 " multicast, payload type %u\n", ntohs (mh->type));
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3280 " multicast, payload size %u\n", ntohs (mh->size));
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3289 data_descriptor_decrement_multicast (info->mesh_data);
3290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3298 * Creates a path ack message in buf and frees all unused resources.
3300 * @param cls closure (MeshTransmissionDescriptor)
3301 * @param size number of bytes available in buf
3302 * @param buf where the callee should write the message
3303 * @return number of bytes written to buf
3306 send_core_path_ack (void *cls, size_t size, void *buf)
3308 struct MeshTransmissionDescriptor *info = cls;
3309 struct GNUNET_MESH_PathACK *msg = buf;
3311 GNUNET_assert (NULL != info);
3312 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3317 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3318 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3319 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3320 msg->tid = htonl (info->origin->tid);
3321 msg->peer_id = my_full_id;
3324 /* TODO add signature */
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3327 return sizeof (struct GNUNET_MESH_PathACK);
3332 * Free a transmission that was already queued with all resources
3333 * associated to the request.
3335 * @param queue Queue handler to cancel.
3336 * @param clear_cls Is it necessary to free associated cls?
3339 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3341 struct MeshTransmissionDescriptor *dd;
3342 struct MeshPathInfo *path_info;
3344 if (GNUNET_YES == clear_cls)
3346 switch (queue->type)
3348 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3349 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
3352 data_descriptor_decrement_multicast (dd->mesh_data);
3354 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3356 path_info = queue->cls;
3357 path_destroy (path_info->path);
3361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
3363 GNUNET_free_non_null (queue->cls);
3365 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3366 queue->peer->queue_tail,
3368 GNUNET_free (queue);
3373 * Core callback to write a queued packet to core buffer
3375 * @param cls Closure (peer info).
3376 * @param size Number of bytes available in buf.
3377 * @param buf Where the to write the message.
3379 * @return number of bytes written to buf
3382 queue_send (void *cls, size_t size, void *buf)
3384 struct MeshPeerInfo *peer = cls;
3385 struct MeshPeerQueue *queue;
3388 peer->core_transmit = NULL;
3389 queue = peer->queue_head;
3391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3393 /* If queue is empty, send should have been cancelled */
3399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3401 /* Check if buffer size is enough for the message */
3402 if (queue->size > size)
3404 struct GNUNET_PeerIdentity id;
3406 GNUNET_PEER_resolve (peer->id, &id);
3407 peer->core_transmit =
3408 GNUNET_CORE_notify_transmit_ready(core_handle,
3411 GNUNET_TIME_UNIT_FOREVER_REL,
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3421 switch (queue->type)
3423 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
3425 data_size = send_core_data_raw (queue->cls, size, buf);
3427 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
3429 data_size = send_core_data_multicast(queue->cls, size, buf);
3431 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3433 data_size = send_core_path_create(queue->cls, size, buf);
3435 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3437 data_size = send_core_path_ack(queue->cls, size, buf);
3441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
3444 queue->tunnel->queue_n--;
3446 /* Free queue, but cls was freed by send_core_* */
3447 queue_destroy(queue, GNUNET_NO);
3449 /* If more data in queue, send next */
3450 if (NULL != peer->queue_head)
3452 struct GNUNET_PeerIdentity id;
3454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3455 GNUNET_PEER_resolve (peer->id, &id);
3456 peer->core_transmit =
3457 GNUNET_CORE_notify_transmit_ready(core_handle,
3460 GNUNET_TIME_UNIT_FOREVER_REL,
3462 peer->queue_head->size,
3466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3472 * Queue and pass message to core when possible.
3474 * @param cls Closure (type dependant).
3475 * @param type Type of the message.
3476 * @param size Size of the message.
3477 * @param dst Neighbor to send message to.
3478 * @param t Tunnel this message belongs to.
3481 queue_add (void *cls, uint16_t type, size_t size,
3482 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3484 struct MeshPeerQueue *queue;
3486 if (t->queue_n >= t->queue_max)
3488 if (NULL == t->owner)
3489 GNUNET_break_op(0); // TODO: kill connection?
3492 return; // Drop message
3495 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3501 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3502 if (NULL == dst->core_transmit)
3504 struct GNUNET_PeerIdentity id;
3506 GNUNET_PEER_resolve (dst->id, &id);
3507 dst->core_transmit =
3508 GNUNET_CORE_notify_transmit_ready(core_handle,
3511 GNUNET_TIME_UNIT_FOREVER_REL,
3520 /******************************************************************************/
3521 /******************** MESH NETWORK HANDLERS **************************/
3522 /******************************************************************************/
3526 * Core handler for path creation
3528 * @param cls closure
3529 * @param message message
3530 * @param peer peer identity this notification is about
3531 * @param atsi performance data
3532 * @param atsi_count number of records in 'atsi'
3534 * @return GNUNET_OK to keep the connection open,
3535 * GNUNET_SYSERR to close it (signal serious error)
3538 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3539 const struct GNUNET_MessageHeader *message,
3540 const struct GNUNET_ATS_Information *atsi,
3541 unsigned int atsi_count)
3543 unsigned int own_pos;
3546 MESH_TunnelNumber tid;
3547 struct GNUNET_MESH_ManipulatePath *msg;
3548 struct GNUNET_PeerIdentity *pi;
3549 struct GNUNET_HashCode hash;
3550 struct MeshPeerPath *path;
3551 struct MeshPeerInfo *dest_peer_info;
3552 struct MeshPeerInfo *orig_peer_info;
3553 struct MeshTunnel *t;
3555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3556 "Received a path create msg [%s]\n",
3557 GNUNET_i2s (&my_full_id));
3558 size = ntohs (message->size);
3559 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3561 GNUNET_break_op (0);
3565 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3566 if (size % sizeof (struct GNUNET_PeerIdentity))
3568 GNUNET_break_op (0);
3571 size /= sizeof (struct GNUNET_PeerIdentity);
3574 GNUNET_break_op (0);
3577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3578 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3580 tid = ntohl (msg->tid);
3581 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3583 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3584 t = tunnel_get (pi, tid);
3585 if (NULL == t) // FIXME only for INCOMING tunnels?
3587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3588 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3590 while (NULL != tunnel_get_incoming (next_local_tid))
3591 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3592 t->local_tid_dest = next_local_tid++;
3593 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3595 tunnel_reset_timeout (t);
3596 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3598 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3599 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3607 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3608 if (NULL == dest_peer_info)
3610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3611 " Creating PeerInfo for destination.\n");
3612 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3613 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3614 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3616 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3618 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3619 if (NULL == orig_peer_info)
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3622 " Creating PeerInfo for origin.\n");
3623 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3624 orig_peer_info->id = GNUNET_PEER_intern (pi);
3625 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3626 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3629 path = path_new (size);
3631 for (i = 0; i < size; i++)
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3634 GNUNET_i2s (&pi[i]));
3635 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3636 if (path->peers[i] == myid)
3639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3642 /* cannot be self, must be 'not found' */
3643 /* create path: self not found in path through self */
3644 GNUNET_break_op (0);
3645 path_destroy (path);
3646 /* FIXME error. destroy tunnel? leave for timeout? */
3649 path_add_to_peers (path, GNUNET_NO);
3650 tunnel_add_path (t, path, own_pos);
3651 if (own_pos == size - 1)
3653 /* It is for us! Send ack. */
3654 struct MeshTransmissionDescriptor *info;
3656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3657 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3658 if (NULL == t->peers)
3660 /* New tunnel! Notify clients on data. */
3661 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
3663 GNUNET_break (GNUNET_OK ==
3664 GNUNET_CONTAINER_multihashmap_put (t->peers,
3665 &my_full_id.hashPubKey,
3668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
3669 // FIXME URGENT (GNUNET_NO?)
3670 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3671 info->origin = &t->id;
3672 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3673 GNUNET_assert (NULL != info->peer);
3675 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3676 sizeof (struct GNUNET_MESH_PathACK),
3682 struct MeshPeerPath *path2;
3684 /* It's for somebody else! Retransmit. */
3685 path2 = path_duplicate (path);
3686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3687 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3688 path2 = path_duplicate (path);
3689 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3690 send_create_path (dest_peer_info, path, t);
3697 * Core handler for path destruction
3699 * @param cls closure
3700 * @param message message
3701 * @param peer peer identity this notification is about
3702 * @param atsi performance data
3703 * @param atsi_count number of records in 'atsi'
3705 * @return GNUNET_OK to keep the connection open,
3706 * GNUNET_SYSERR to close it (signal serious error)
3709 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3710 const struct GNUNET_MessageHeader *message,
3711 const struct GNUNET_ATS_Information *atsi,
3712 unsigned int atsi_count)
3714 struct GNUNET_MESH_ManipulatePath *msg;
3715 struct GNUNET_PeerIdentity *pi;
3716 struct MeshPeerPath *path;
3717 struct MeshTunnel *t;
3718 unsigned int own_pos;
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3723 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3724 size = ntohs (message->size);
3725 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3727 GNUNET_break_op (0);
3731 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3732 if (size % sizeof (struct GNUNET_PeerIdentity))
3734 GNUNET_break_op (0);
3737 size /= sizeof (struct GNUNET_PeerIdentity);
3740 GNUNET_break_op (0);
3743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3745 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3746 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3748 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3750 t = tunnel_get (pi, ntohl (msg->tid));
3753 /* TODO notify back: we don't know this tunnel */
3754 GNUNET_break_op (0);
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3758 path = path_new (size);
3760 for (i = 0; i < size; i++)
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3763 GNUNET_i2s (&pi[i]));
3764 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3765 if (path->peers[i] == myid)
3768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3769 if (own_pos < path->length - 1)
3770 send_message (message, &pi[own_pos + 1], t);
3772 send_client_tunnel_disconnect(t, NULL);
3774 tunnel_delete_peer (t, path->peers[path->length - 1]);
3775 path_destroy (path);
3781 * Core handler for notifications of broken paths
3783 * @param cls closure
3784 * @param message message
3785 * @param peer peer identity this notification is about
3786 * @param atsi performance data
3787 * @param atsi_count number of records in 'atsi'
3789 * @return GNUNET_OK to keep the connection open,
3790 * GNUNET_SYSERR to close it (signal serious error)
3793 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3794 const struct GNUNET_MessageHeader *message,
3795 const struct GNUNET_ATS_Information *atsi,
3796 unsigned int atsi_count)
3798 struct GNUNET_MESH_PathBroken *msg;
3799 struct MeshTunnel *t;
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3802 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3803 msg = (struct GNUNET_MESH_PathBroken *) message;
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3805 GNUNET_i2s (&msg->peer1));
3806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3807 GNUNET_i2s (&msg->peer2));
3808 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3811 GNUNET_break_op (0);
3814 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3815 GNUNET_PEER_search (&msg->peer2));
3822 * Core handler for tunnel destruction
3824 * @param cls closure
3825 * @param message message
3826 * @param peer peer identity this notification is about
3827 * @param atsi performance data
3828 * @param atsi_count number of records in 'atsi'
3830 * @return GNUNET_OK to keep the connection open,
3831 * GNUNET_SYSERR to close it (signal serious error)
3834 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3835 const struct GNUNET_MessageHeader *message,
3836 const struct GNUNET_ATS_Information *atsi,
3837 unsigned int atsi_count)
3839 struct GNUNET_MESH_TunnelDestroy *msg;
3840 struct MeshTunnel *t;
3842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3843 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3844 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3846 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3847 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3850 /* Probably already got the message from another path,
3851 * destroyed the tunnel and retransmitted to children.
3856 if (t->id.oid == myid)
3858 GNUNET_break_op (0);
3861 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3863 /* Tunnel was incoming, notify clients */
3864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3865 t->local_tid, t->local_tid_dest);
3866 send_clients_tunnel_destroy (t);
3868 tunnel_send_destroy (t);
3875 * Core handler for mesh network traffic going from the origin to a peer
3877 * @param cls closure
3878 * @param peer peer identity this notification is about
3879 * @param message message
3880 * @param atsi performance data
3881 * @param atsi_count number of records in 'atsi'
3882 * @return GNUNET_OK to keep the connection open,
3883 * GNUNET_SYSERR to close it (signal serious error)
3886 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3887 const struct GNUNET_MessageHeader *message,
3888 const struct GNUNET_ATS_Information *atsi,
3889 unsigned int atsi_count)
3891 struct GNUNET_MESH_Unicast *msg;
3892 struct MeshTunnel *t;
3896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3898 size = ntohs (message->size);
3900 sizeof (struct GNUNET_MESH_Unicast) +
3901 sizeof (struct GNUNET_MessageHeader))
3906 msg = (struct GNUNET_MESH_Unicast *) message;
3907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3908 ntohs (msg[1].header.type));
3909 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3912 /* TODO notify back: we don't know this tunnel */
3913 GNUNET_break_op (0);
3916 tunnel_reset_timeout (t);
3917 pid = GNUNET_PEER_search (&msg->destination);
3920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3921 " it's for us! sending to clients...\n");
3922 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3926 " not for us, retransmitting...\n");
3927 send_message (message, tree_get_first_hop (t->tree, pid), t);
3933 * Core handler for mesh network traffic going from the origin to all peers
3935 * @param cls closure
3936 * @param message message
3937 * @param peer peer identity this notification is about
3938 * @param atsi performance data
3939 * @param atsi_count number of records in 'atsi'
3940 * @return GNUNET_OK to keep the connection open,
3941 * GNUNET_SYSERR to close it (signal serious error)
3943 * TODO: Check who we got this from, to validate route.
3946 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3947 const struct GNUNET_MessageHeader *message,
3948 const struct GNUNET_ATS_Information *atsi,
3949 unsigned int atsi_count)
3951 struct GNUNET_MESH_Multicast *msg;
3952 struct MeshTunnel *t;
3955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3957 size = ntohs (message->size);
3958 if (sizeof (struct GNUNET_MESH_Multicast) +
3959 sizeof (struct GNUNET_MessageHeader) > size)
3961 GNUNET_break_op (0);
3964 msg = (struct GNUNET_MESH_Multicast *) message;
3965 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3969 /* TODO notify that we dont know that tunnel */
3970 GNUNET_break_op (0);
3973 if (t->mid == ntohl (msg->mid))
3975 /* FIXME: already seen this packet, log dropping */
3976 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3977 " Already seen mid %u, DROPPING!\n", t->mid);
3982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3983 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3985 t->mid = ntohl (msg->mid);
3986 tunnel_reset_timeout (t);
3988 /* Transmit to locally interested clients */
3989 if (NULL != t->peers &&
3990 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3992 send_subscribed_clients (message, &msg[1].header);
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3995 if (ntohl (msg->ttl) == 0)
3997 /* FIXME: ttl is 0, log dropping */
3998 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4001 tunnel_send_multicast (t, message, GNUNET_NO);
4007 * Core handler for mesh network traffic toward the owner of a tunnel
4009 * @param cls closure
4010 * @param message message
4011 * @param peer peer identity this notification is about
4012 * @param atsi performance data
4013 * @param atsi_count number of records in 'atsi'
4015 * @return GNUNET_OK to keep the connection open,
4016 * GNUNET_SYSERR to close it (signal serious error)
4019 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4020 const struct GNUNET_MessageHeader *message,
4021 const struct GNUNET_ATS_Information *atsi,
4022 unsigned int atsi_count)
4024 struct GNUNET_MESH_ToOrigin *msg;
4025 struct GNUNET_PeerIdentity id;
4026 struct MeshPeerInfo *peer_info;
4027 struct MeshTunnel *t;
4030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4032 size = ntohs (message->size);
4033 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4034 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4036 GNUNET_break_op (0);
4039 msg = (struct GNUNET_MESH_ToOrigin *) message;
4040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4041 ntohs (msg[1].header.type));
4042 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4046 /* TODO notify that we dont know this tunnel (whom)? */
4047 GNUNET_break_op (0);
4051 if (t->id.oid == myid)
4054 struct GNUNET_MESH_ToOrigin *copy;
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4057 " it's for us! sending to clients...\n");
4058 if (NULL == t->owner)
4060 /* got data packet for ownerless tunnel */
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4062 GNUNET_break_op (0);
4065 /* TODO signature verification */
4066 memcpy (cbuf, message, size);
4067 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4068 copy->tid = htonl (t->local_tid);
4069 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4070 ©->header, GNUNET_YES);
4073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4074 " not for us, retransmitting...\n");
4076 peer_info = peer_info_get (&msg->oid);
4077 if (NULL == peer_info)
4079 /* unknown origin of tunnel */
4083 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4084 send_message (message, &id, t);
4091 * Core handler for path ACKs
4093 * @param cls closure
4094 * @param message message
4095 * @param peer peer identity this notification is about
4096 * @param atsi performance data
4097 * @param atsi_count number of records in 'atsi'
4099 * @return GNUNET_OK to keep the connection open,
4100 * GNUNET_SYSERR to close it (signal serious error)
4103 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4104 const struct GNUNET_MessageHeader *message,
4105 const struct GNUNET_ATS_Information *atsi,
4106 unsigned int atsi_count)
4108 struct GNUNET_MESH_PathACK *msg;
4109 struct GNUNET_PeerIdentity id;
4110 struct MeshPeerInfo *peer_info;
4111 struct MeshPeerPath *p;
4112 struct MeshTunnel *t;
4114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4115 GNUNET_i2s (&my_full_id));
4116 msg = (struct GNUNET_MESH_PathACK *) message;
4117 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4120 /* TODO notify that we don't know the tunnel */
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4122 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4126 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4128 peer_info = peer_info_get (&msg->peer_id);
4129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4130 GNUNET_i2s (&msg->peer_id));
4131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4134 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4137 "connect_by_string completed, stopping search\n");
4138 regex_cancel_search (t->regex_ctx);
4139 t->regex_ctx = NULL;
4142 /* Add paths to peers? */
4143 p = tree_get_path_to_peer (t->tree, peer_info->id);
4146 path_add_to_peers (p, GNUNET_YES);
4154 /* Message for us? */
4155 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4158 if (NULL == t->owner)
4160 GNUNET_break_op (0);
4163 if (NULL != t->dht_get_type)
4165 GNUNET_DHT_get_stop (t->dht_get_type);
4166 t->dht_get_type = NULL;
4168 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4170 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4171 send_client_peer_connected (t, peer_info->id);
4176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4177 " not for us, retransmitting...\n");
4178 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4179 peer_info = peer_info_get (&msg->oid);
4180 if (NULL == peer_info)
4182 /* If we know the tunnel, we should DEFINITELY know the peer */
4186 send_message (message, &id, t);
4192 * Functions to handle messages from core
4194 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4195 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4196 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4197 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4198 sizeof (struct GNUNET_MESH_PathBroken)},
4199 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
4200 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4201 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4202 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4203 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4204 sizeof (struct GNUNET_MESH_PathACK)},
4210 /******************************************************************************/
4211 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4212 /******************************************************************************/
4215 * deregister_app: iterator for removing each application registered by a client
4217 * @param cls closure
4218 * @param key the hash of the application id (used to access the hashmap)
4219 * @param value the value stored at the key (client)
4221 * @return GNUNET_OK on success
4224 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
4226 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
4227 GNUNET_break (GNUNET_YES ==
4228 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
4234 * notify_client_connection_failure: notify a client that the connection to the
4235 * requested remote peer is not possible (for instance, no route found)
4236 * Function called when the socket is ready to queue more data. "buf" will be
4237 * NULL and "size" zero if the socket was closed for writing in the meantime.
4239 * @param cls closure
4240 * @param size number of bytes available in buf
4241 * @param buf where the callee should write the message
4242 * @return number of bytes written to buf
4245 notify_client_connection_failure (void *cls, size_t size, void *buf)
4248 struct MeshPeerInfo *peer_info;
4249 struct GNUNET_MESH_PeerControl *msg;
4250 struct GNUNET_PeerIdentity id;
4252 if (0 == size && NULL == buf)
4254 // TODO retry? cancel?
4258 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4259 peer_info = (struct MeshPeerInfo *) cls;
4260 msg = (struct GNUNET_MESH_PeerControl *) buf;
4261 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4262 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4263 // msg->tunnel_id = htonl(peer_info->t->tid);
4264 GNUNET_PEER_resolve (peer_info->id, &id);
4265 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4273 * Send keepalive packets for a peer
4275 * @param cls Closure (tunnel for which to send the keepalive).
4276 * @param tc Notification context.
4278 * TODO: implement explicit multicast keepalive?
4281 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4283 struct MeshTunnel *t = cls;
4284 struct GNUNET_MessageHeader *payload;
4285 struct GNUNET_MESH_Multicast *msg;
4287 sizeof (struct GNUNET_MESH_Multicast) +
4288 sizeof (struct GNUNET_MessageHeader);
4291 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4295 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4298 "sending keepalive for tunnel %d\n", t->id.tid);
4300 msg = (struct GNUNET_MESH_Multicast *) cbuf;
4301 msg->header.size = htons (size);
4302 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4303 msg->oid = my_full_id;
4304 msg->tid = htonl (t->id.tid);
4305 msg->ttl = htonl (DEFAULT_TTL);
4306 msg->mid = htonl (t->mid + 1);
4308 payload = (struct GNUNET_MessageHeader *) &msg[1];
4309 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
4310 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4311 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
4313 t->path_refresh_task =
4314 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
4320 * Function to process paths received for a new peer addition. The recorded
4321 * paths form the initial tunnel, which can be optimized later.
4322 * Called on each result obtained for the DHT search.
4324 * @param cls closure
4325 * @param exp when will this value expire
4326 * @param key key of the result
4327 * @param get_path path of the get request
4328 * @param get_path_length lenght of get_path
4329 * @param put_path path of the put request
4330 * @param put_path_length length of the put_path
4331 * @param type type of the result
4332 * @param size number of bytes in data
4333 * @param data pointer to the result data
4335 * TODO: re-issue the request after certain time? cancel after X results?
4338 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4339 const struct GNUNET_HashCode * key,
4340 const struct GNUNET_PeerIdentity *get_path,
4341 unsigned int get_path_length,
4342 const struct GNUNET_PeerIdentity *put_path,
4343 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4344 size_t size, const void *data)
4346 struct MeshPathInfo *path_info = cls;
4347 struct MeshPeerPath *p;
4348 struct GNUNET_PeerIdentity pi;
4351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4352 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4355 p = path_build_from_dht (get_path, get_path_length, put_path,
4357 path_add_to_peers (p, GNUNET_NO);
4359 for (i = 0; i < path_info->peer->ntunnels; i++)
4361 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4362 peer_info_connect (path_info->peer, path_info->t);
4370 * Function to process paths received for a new peer addition. The recorded
4371 * paths form the initial tunnel, which can be optimized later.
4372 * Called on each result obtained for the DHT search.
4374 * @param cls closure
4375 * @param exp when will this value expire
4376 * @param key key of the result
4377 * @param get_path path of the get request
4378 * @param get_path_length lenght of get_path
4379 * @param put_path path of the put request
4380 * @param put_path_length length of the put_path
4381 * @param type type of the result
4382 * @param size number of bytes in data
4383 * @param data pointer to the result data
4386 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4387 const struct GNUNET_HashCode * key,
4388 const struct GNUNET_PeerIdentity *get_path,
4389 unsigned int get_path_length,
4390 const struct GNUNET_PeerIdentity *put_path,
4391 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4392 size_t size, const void *data)
4394 const struct PBlock *pb = data;
4395 const struct GNUNET_PeerIdentity *pi = &pb->id;
4396 struct MeshTunnel *t = cls;
4397 struct MeshPeerInfo *peer_info;
4398 struct MeshPeerPath *p;
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
4401 if (size != sizeof (struct PBlock))
4403 GNUNET_break_op (0);
4406 if (ntohl(pb->type) != t->type)
4408 GNUNET_break_op (0);
4411 GNUNET_assert (NULL != t->owner);
4412 peer_info = peer_info_get (pi);
4413 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
4415 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
4417 p = path_build_from_dht (get_path, get_path_length, put_path,
4419 path_add_to_peers (p, GNUNET_NO);
4421 tunnel_add_peer (t, peer_info);
4422 peer_info_connect (peer_info, t);
4427 * Function called if the connect attempt to a peer found via
4428 * connect_by_string times out. Try to connect to another peer, if any.
4429 * Otherwise try to reconnect to the same peer.
4431 * @param cls Closure (info about regex search).
4432 * @param tc TaskContext.
4435 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4437 struct MeshRegexSearchInfo *info = cls;
4438 struct MeshPeerInfo *peer_info;
4442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
4443 info->timeout = GNUNET_SCHEDULER_NO_TASK;
4444 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
4452 if (0 < info->n_peers)
4454 // Select next peer, put current in that spot.
4455 id = info->peers[info->i_peer];
4456 info->peers[info->i_peer] = info->peer;
4457 info->i_peer = (info->i_peer + 1) % info->n_peers;
4461 // Try to connect to same peer again.
4464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
4466 peer_info = peer_info_get_short(id);
4467 tunnel_add_peer (info->t, peer_info);
4469 tunnel_delete_peer (info->t, old);
4470 peer_info_connect (peer_info, info->t);
4471 info->timeout = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
4472 ®ex_connect_timeout,
4474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
4479 * Function to process DHT string to regex matching.
4480 * Called on each result obtained for the DHT search.
4482 * @param cls closure (search context)
4483 * @param exp when will this value expire
4484 * @param key key of the result
4485 * @param get_path path of the get request (not used)
4486 * @param get_path_length lenght of get_path (not used)
4487 * @param put_path path of the put request (not used)
4488 * @param put_path_length length of the put_path (not used)
4489 * @param type type of the result
4490 * @param size number of bytes in data
4491 * @param data pointer to the result data
4494 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4495 const struct GNUNET_HashCode * key,
4496 const struct GNUNET_PeerIdentity *get_path,
4497 unsigned int get_path_length,
4498 const struct GNUNET_PeerIdentity *put_path,
4499 unsigned int put_path_length,
4500 enum GNUNET_BLOCK_Type type,
4501 size_t size, const void *data)
4503 const struct MeshRegexAccept *block = data;
4504 struct MeshRegexSearchContext *ctx = cls;
4505 struct MeshRegexSearchInfo *info = ctx->info;
4506 struct MeshPeerPath *p;
4507 struct MeshPeerInfo *peer_info;
4509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
4510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
4512 peer_info = peer_info_get(&block->id);
4513 p = path_build_from_dht (get_path, get_path_length, put_path,
4515 path_add_to_peers (p, GNUNET_NO);
4518 tunnel_add_peer (info->t, peer_info);
4519 peer_info_connect (peer_info, info->t);
4520 if (0 == info->peer)
4522 info->peer = peer_info->id;
4526 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
4529 info->timeout = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
4530 ®ex_connect_timeout,
4538 * Function to process DHT string to regex matching..
4539 * Called on each result obtained for the DHT search.
4541 * @param cls closure (search context)
4542 * @param exp when will this value expire
4543 * @param key key of the result
4544 * @param get_path path of the get request (not used)
4545 * @param get_path_length lenght of get_path (not used)
4546 * @param put_path path of the put request (not used)
4547 * @param put_path_length length of the put_path (not used)
4548 * @param type type of the result
4549 * @param size number of bytes in data
4550 * @param data pointer to the result data
4552 * TODO: re-issue the request after certain time? cancel after X results?
4555 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4556 const struct GNUNET_HashCode * key,
4557 const struct GNUNET_PeerIdentity *get_path,
4558 unsigned int get_path_length,
4559 const struct GNUNET_PeerIdentity *put_path,
4560 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4561 size_t size, const void *data)
4563 const struct MeshRegexBlock *block = data;
4564 struct MeshRegexSearchContext *ctx = cls;
4565 struct MeshRegexSearchInfo *info = ctx->info;
4570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4571 "DHT GET STRING RETURNED RESULTS\n");
4572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4573 " key: %s\n", GNUNET_h2s (key));
4575 copy = GNUNET_malloc (size);
4576 memcpy (copy, data, size);
4577 GNUNET_break (GNUNET_OK ==
4578 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
4579 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4580 len = ntohl (block->n_proof);
4581 proof = GNUNET_malloc (len + 1);
4582 memcpy (proof, &block[1], len);
4584 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
4586 GNUNET_break_op (0);
4589 len = strlen (info->description);
4590 if (len == ctx->position) // String processed
4592 if (GNUNET_YES == ntohl (block->accepting))
4594 regex_find_path(key, ctx);
4598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
4599 // FIXME REGEX this block not successful, wait for more? start timeout?
4603 GNUNET_break (GNUNET_OK ==
4604 GNUNET_MESH_regex_block_iterate (block, size,
4605 ®ex_edge_iterator, ctx));
4609 /******************************************************************************/
4610 /********************* MESH LOCAL HANDLES **************************/
4611 /******************************************************************************/
4615 * Handler for client disconnection
4617 * @param cls closure
4618 * @param client identification of the client; NULL
4619 * for the last call when the server is destroyed
4622 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4624 struct MeshClient *c;
4625 struct MeshClient *next;
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4637 if (c->handle != client)
4639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4645 GNUNET_SERVER_client_drop (c->handle);
4646 c->shutting_down = GNUNET_YES;
4647 GNUNET_assert (NULL != c->own_tunnels);
4648 GNUNET_assert (NULL != c->incoming_tunnels);
4649 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4650 &tunnel_destroy_iterator, c);
4651 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4652 &tunnel_destroy_iterator, c);
4653 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4654 &tunnel_destroy_iterator, c);
4655 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4656 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4657 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4659 /* deregister clients applications */
4660 if (NULL != c->apps)
4662 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
4663 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
4665 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
4666 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
4668 GNUNET_SCHEDULER_cancel (announce_applications_task);
4669 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4671 if (NULL != c->types)
4672 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4673 for (i = 0; i < c->n_regex; i++)
4675 GNUNET_free (c->regexes[i]);
4677 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
4678 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
4680 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
4681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
4691 * Handler for new clients
4693 * @param cls closure
4694 * @param client identification of the client
4695 * @param message the actual message, which includes messages the client wants
4698 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4699 const struct GNUNET_MessageHeader *message)
4701 struct GNUNET_MESH_ClientConnect *cc_msg;
4702 struct MeshClient *c;
4703 GNUNET_MESH_ApplicationType *a;
4710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4711 /* Check data sanity */
4712 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4713 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4714 ntypes = ntohs (cc_msg->types);
4715 napps = ntohs (cc_msg->applications);
4717 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
4720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4724 /* Create new client structure */
4725 c = GNUNET_malloc (sizeof (struct MeshClient));
4726 c->id = next_client_id++;
4727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4729 GNUNET_SERVER_client_keep (client);
4730 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
4733 GNUNET_MESH_ApplicationType at;
4734 struct GNUNET_HashCode hc;
4736 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
4737 for (i = 0; i < napps; i++)
4740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
4741 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
4742 /* store in clients hashmap */
4743 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
4744 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4745 /* store in global hashmap, for announcements */
4746 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
4747 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4749 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
4750 announce_applications_task =
4751 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
4757 struct GNUNET_HashCode hc;
4759 t = (uint16_t *) & a[napps];
4760 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
4761 for (i = 0; i < ntypes; i++)
4764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4765 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
4767 /* store in clients hashmap */
4768 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4769 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4770 /* store in global hashmap */
4771 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4772 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4776 " client has %u+%u subscriptions\n", napps, ntypes);
4778 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
4779 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4780 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4781 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4782 GNUNET_SERVER_notification_context_add (nc, client);
4784 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4790 * Handler for clients announcing available services by a regular expression.
4792 * @param cls closure
4793 * @param client identification of the client
4794 * @param message the actual message, which includes messages the client wants
4797 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
4798 const struct GNUNET_MessageHeader *message)
4800 struct MeshClient *c;
4804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
4806 /* Sanity check for client registration */
4807 if (NULL == (c = client_get (client)))
4810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4815 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
4816 regex = GNUNET_malloc (len + 1);
4817 memcpy (regex, &message[1], len);
4819 GNUNET_array_append (c->regexes, c->n_regex, regex);
4820 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
4822 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
4828 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
4834 * Handler for requests of new tunnels
4836 * @param cls closure
4837 * @param client identification of the client
4838 * @param message the actual message
4841 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4842 const struct GNUNET_MessageHeader *message)
4844 struct GNUNET_MESH_TunnelMessage *t_msg;
4845 struct MeshTunnel *t;
4846 struct MeshClient *c;
4848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4850 /* Sanity check for client registration */
4851 if (NULL == (c = client_get (client)))
4854 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4859 /* Message sanity check */
4860 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4863 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4867 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4868 /* Sanity check for tunnel numbering */
4869 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4875 /* Sanity check for duplicate tunnel IDs */
4876 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
4879 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4883 while (NULL != tunnel_get_by_pi (myid, next_tid))
4884 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4885 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
4886 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4888 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4889 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
4891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
4892 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4898 * Handler for requests of deleting tunnels
4900 * @param cls closure
4901 * @param client identification of the client
4902 * @param message the actual message
4905 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4906 const struct GNUNET_MessageHeader *message)
4908 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4909 struct MeshClient *c;
4910 struct MeshTunnel *t;
4911 MESH_TunnelNumber tid;
4913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4914 "Got a DESTROY TUNNEL from client!\n");
4916 /* Sanity check for client registration */
4917 if (NULL == (c = client_get (client)))
4920 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4923 /* Message sanity check */
4924 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4931 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4933 /* Retrieve tunnel */
4934 tid = ntohl (tunnel_msg->tunnel_id);
4935 t = tunnel_get_by_local_id(c, tid);
4939 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4940 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4943 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4945 client_ignore_tunnel (c, t);
4947 // TODO: when to destroy incoming tunnel?
4948 if (t->nclients == 0)
4950 GNUNET_assert (GNUNET_YES ==
4951 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4953 GNUNET_assert (GNUNET_YES ==
4954 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4955 &my_full_id.hashPubKey,
4959 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4962 send_client_tunnel_disconnect(t, c);
4963 client_delete_tunnel(c, t);
4965 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4967 tunnel_send_destroy (t);
4969 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4975 * Handler for connection requests to new peers
4977 * @param cls closure
4978 * @param client identification of the client
4979 * @param message the actual message (PeerControl)
4982 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4983 const struct GNUNET_MessageHeader *message)
4985 struct GNUNET_MESH_PeerControl *peer_msg;
4986 struct MeshPeerInfo *peer_info;
4987 struct MeshClient *c;
4988 struct MeshTunnel *t;
4989 MESH_TunnelNumber tid;
4991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4992 /* Sanity check for client registration */
4993 if (NULL == (c = client_get (client)))
4996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5000 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5001 /* Sanity check for message size */
5002 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5005 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5009 /* Tunnel exists? */
5010 tid = ntohl (peer_msg->tunnel_id);
5011 t = tunnel_get_by_local_id (c, tid);
5015 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5019 /* Does client own tunnel? */
5020 if (t->owner->handle != client)
5023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
5027 GNUNET_i2s (&peer_msg->peer));
5028 peer_info = peer_info_get (&peer_msg->peer);
5030 tunnel_add_peer (t, peer_info);
5031 peer_info_connect (peer_info, t);
5033 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5039 * Handler for disconnection requests of peers in a tunnel
5041 * @param cls closure
5042 * @param client identification of the client
5043 * @param message the actual message (PeerControl)
5046 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
5047 const struct GNUNET_MessageHeader *message)
5049 struct GNUNET_MESH_PeerControl *peer_msg;
5050 struct MeshPeerInfo *peer_info;
5051 struct MeshClient *c;
5052 struct MeshTunnel *t;
5053 MESH_TunnelNumber tid;
5055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
5056 /* Sanity check for client registration */
5057 if (NULL == (c = client_get (client)))
5060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5063 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5064 /* Sanity check for message size */
5065 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5072 /* Tunnel exists? */
5073 tid = ntohl (peer_msg->tunnel_id);
5074 t = tunnel_get_by_local_id (c, tid);
5078 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5083 /* Does client own tunnel? */
5084 if (t->owner->handle != client)
5087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
5092 GNUNET_i2s (&peer_msg->peer));
5093 /* Is the peer in the tunnel? */
5095 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
5096 if (NULL == peer_info)
5099 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5103 /* Ok, delete peer from tunnel */
5104 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
5105 &peer_msg->peer.hashPubKey);
5107 send_destroy_path (t, peer_info->id);
5108 tunnel_delete_peer (t, peer_info->id);
5109 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5114 * Handler for blacklist requests of peers in a tunnel
5116 * @param cls closure
5117 * @param client identification of the client
5118 * @param message the actual message (PeerControl)
5121 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
5122 const struct GNUNET_MessageHeader *message)
5124 struct GNUNET_MESH_PeerControl *peer_msg;
5125 struct MeshClient *c;
5126 struct MeshTunnel *t;
5127 MESH_TunnelNumber tid;
5129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
5130 /* Sanity check for client registration */
5131 if (NULL == (c = client_get (client)))
5134 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5137 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5139 /* Sanity check for message size */
5140 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5147 /* Tunnel exists? */
5148 tid = ntohl (peer_msg->tunnel_id);
5149 t = tunnel_get_by_local_id (c, tid);
5153 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5158 GNUNET_array_append(t->blacklisted, t->nblacklisted,
5159 GNUNET_PEER_intern(&peer_msg->peer));
5164 * Handler for unblacklist requests of peers in a tunnel
5166 * @param cls closure
5167 * @param client identification of the client
5168 * @param message the actual message (PeerControl)
5171 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
5172 const struct GNUNET_MessageHeader *message)
5174 struct GNUNET_MESH_PeerControl *peer_msg;
5175 struct MeshClient *c;
5176 struct MeshTunnel *t;
5177 MESH_TunnelNumber tid;
5181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
5182 /* Sanity check for client registration */
5183 if (NULL == (c = client_get (client)))
5186 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5189 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5191 /* Sanity check for message size */
5192 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5195 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5199 /* Tunnel exists? */
5200 tid = ntohl (peer_msg->tunnel_id);
5201 t = tunnel_get_by_local_id (c, tid);
5205 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5210 /* if peer is not known, complain */
5211 pid = GNUNET_PEER_search (&peer_msg->peer);
5218 /* search and remove from list */
5219 for (i = 0; i < t->nblacklisted; i++)
5221 if (t->blacklisted[i] == pid)
5223 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
5224 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
5229 /* if peer hasn't been blacklisted, complain */
5235 * Handler for connection requests to new peers by type
5237 * @param cls closure
5238 * @param client identification of the client
5239 * @param message the actual message (ConnectPeerByType)
5242 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
5243 const struct GNUNET_MessageHeader *message)
5245 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
5246 struct MeshClient *c;
5247 struct MeshTunnel *t;
5248 struct GNUNET_HashCode hash;
5249 MESH_TunnelNumber tid;
5251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
5252 /* Sanity check for client registration */
5253 if (NULL == (c = client_get (client)))
5256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5260 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
5261 /* Sanity check for message size */
5262 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
5263 ntohs (connect_msg->header.size))
5266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5270 /* Tunnel exists? */
5271 tid = ntohl (connect_msg->tunnel_id);
5272 t = tunnel_get_by_local_id (c, tid);
5276 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5280 /* Does client own tunnel? */
5281 if (t->owner->handle != client)
5284 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5288 /* Do WE have the service? */
5289 t->type = ntohl (connect_msg->type);
5290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
5291 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
5292 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
5295 /* Yes! Fast forward, add ourselves to the tunnel and send the
5296 * good news to the client, and alert the destination client of
5297 * an incoming tunnel.
5299 * FIXME send a path create to self, avoid code duplication
5301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
5302 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
5303 peer_info_get (&my_full_id),
5304 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
5307 send_client_peer_connected (t, myid);
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
5309 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5311 t->local_tid_dest = next_local_tid++;
5312 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5313 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5318 /* Ok, lets find a peer offering the service */
5319 if (NULL != t->dht_get_type)
5321 GNUNET_DHT_get_stop (t->dht_get_type);
5323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
5324 GNUNET_h2s (&hash));
5326 GNUNET_DHT_get_start (dht_handle,
5327 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
5329 DHT_REPLICATION_LEVEL,
5330 GNUNET_DHT_RO_RECORD_ROUTE |
5331 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5333 &dht_get_type_handler, t);
5335 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5341 * Handler for connection requests to new peers by a string service description.
5343 * @param cls closure
5344 * @param client identification of the client
5345 * @param message the actual message, which includes messages the client wants
5348 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
5349 const struct GNUNET_MessageHeader *message)
5351 struct GNUNET_MESH_ConnectPeerByString *msg;
5352 struct MeshRegexSearchContext *ctx;
5353 struct MeshRegexSearchInfo *info;
5354 struct GNUNET_DHT_GetHandle *get_h;
5355 struct GNUNET_HashCode key;
5356 struct MeshTunnel *t;
5357 struct MeshClient *c;
5358 MESH_TunnelNumber tid;
5362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5363 "Connect by string started\n");
5364 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
5365 size = htons (message->size);
5367 /* Sanity check for client registration */
5368 if (NULL == (c = client_get (client)))
5371 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5375 /* Message size sanity check */
5376 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
5379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5383 /* Tunnel exists? */
5384 tid = ntohl (msg->tunnel_id);
5385 t = tunnel_get_by_local_id (c, tid);
5389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5393 /* Does client own tunnel? */
5394 if (t->owner->handle != client)
5397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5402 " on tunnel %s [%u]\n",
5403 GNUNET_i2s(&my_full_id),
5406 /* Only one connect_by_string allowed at the same time! */
5407 /* FIXME: allow more, return handle at api level to cancel, document */
5408 if (NULL != t->regex_ctx)
5411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5415 /* Find string itself */
5416 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
5417 string = (const char *) &msg[1];
5419 /* Initialize context */
5420 size = GNUNET_REGEX_get_first_key(string, len, &key);
5421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5422 " consumed %u bits out of %u\n", size, len);
5423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5424 " looking for %s\n", GNUNET_h2s (&key));
5426 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
5428 info->description = GNUNET_malloc (len + 1);
5429 memcpy (info->description, string, len);
5430 info->description[len] = '\0';
5431 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
5432 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
5435 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
5436 ctx->position = size;
5440 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
5442 /* Start search in DHT */
5443 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
5444 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
5445 &key, /* key to search */
5446 DHT_REPLICATION_LEVEL, /* replication level */
5447 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5448 NULL, /* xquery */ // FIXME BLOOMFILTER
5449 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
5450 &dht_get_string_handler, ctx);
5452 GNUNET_break (GNUNET_OK ==
5453 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
5456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
5458 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
5464 * Handler for client traffic directed to one peer
5466 * @param cls closure
5467 * @param client identification of the client
5468 * @param message the actual message
5471 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5472 const struct GNUNET_MessageHeader *message)
5474 struct MeshClient *c;
5475 struct MeshTunnel *t;
5476 struct MeshPeerInfo *pi;
5477 struct GNUNET_MESH_Unicast *data_msg;
5478 MESH_TunnelNumber tid;
5481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5482 "Got a unicast request from a client!\n");
5484 /* Sanity check for client registration */
5485 if (NULL == (c = client_get (client)))
5488 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5491 data_msg = (struct GNUNET_MESH_Unicast *) message;
5492 /* Sanity check for message size */
5493 size = ntohs (message->size);
5494 if (sizeof (struct GNUNET_MESH_Unicast) +
5495 sizeof (struct GNUNET_MessageHeader) > size)
5498 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5502 /* Tunnel exists? */
5503 tid = ntohl (data_msg->tid);
5504 t = tunnel_get_by_local_id (c, tid);
5508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5512 /* Is it a local tunnel? Then, does client own the tunnel? */
5513 if (t->owner->handle != client)
5516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5520 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
5521 &data_msg->destination.hashPubKey);
5522 /* Is the selected peer in the tunnel? */
5526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5530 /* Ok, everything is correct, send the message
5531 * (pretend we got it from a mesh peer)
5534 char buf[ntohs (message->size)] GNUNET_ALIGN;
5535 struct GNUNET_MESH_Unicast *copy;
5537 /* Work around const limitation */
5538 copy = (struct GNUNET_MESH_Unicast *) buf;
5539 memcpy (buf, data_msg, size);
5540 copy->oid = my_full_id;
5541 copy->tid = htonl (t->id.tid);
5542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5543 " calling generic handler...\n");
5544 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
5546 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5552 * Handler for client traffic directed to the origin
5554 * @param cls closure
5555 * @param client identification of the client
5556 * @param message the actual message
5559 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5560 const struct GNUNET_MessageHeader *message)
5562 struct GNUNET_MESH_ToOrigin *data_msg;
5563 struct GNUNET_PeerIdentity id;
5564 struct MeshClient *c;
5565 struct MeshTunnel *t;
5566 MESH_TunnelNumber tid;
5569 /* Sanity check for client registration */
5570 if (NULL == (c = client_get (client)))
5573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5576 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5577 /* Sanity check for message size */
5578 size = ntohs (message->size);
5579 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5580 sizeof (struct GNUNET_MessageHeader) > size)
5583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5587 /* Tunnel exists? */
5588 tid = ntohl (data_msg->tid);
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5590 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
5591 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5597 t = tunnel_get_by_local_id (c, tid);
5601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5605 /* It should be sent by someone who has this as incoming tunnel. */
5606 if (-1 == client_knows_tunnel (c, t))
5609 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5612 GNUNET_PEER_resolve (t->id.oid, &id);
5614 /* Ok, everything is correct, send the message
5615 * (pretend we got it from a mesh peer)
5618 char buf[ntohs (message->size)] GNUNET_ALIGN;
5619 struct GNUNET_MESH_ToOrigin *copy;
5621 /* Work around const limitation */
5622 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5623 memcpy (buf, data_msg, size);
5625 copy->tid = htonl (t->id.tid);
5626 copy->sender = my_full_id;
5627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5628 " calling generic handler...\n");
5629 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
5631 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5637 * Handler for client traffic directed to all peers in a tunnel
5639 * @param cls closure
5640 * @param client identification of the client
5641 * @param message the actual message
5644 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
5645 const struct GNUNET_MessageHeader *message)
5647 struct MeshClient *c;
5648 struct MeshTunnel *t;
5649 struct GNUNET_MESH_Multicast *data_msg;
5650 MESH_TunnelNumber tid;
5652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5653 "Got a multicast request from a client!\n");
5655 /* Sanity check for client registration */
5656 if (NULL == (c = client_get (client)))
5659 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5662 data_msg = (struct GNUNET_MESH_Multicast *) message;
5663 /* Sanity check for message size */
5664 if (sizeof (struct GNUNET_MESH_Multicast) +
5665 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
5668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5672 /* Tunnel exists? */
5673 tid = ntohl (data_msg->tid);
5674 t = tunnel_get_by_local_id (c, tid);
5678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5682 /* Does client own tunnel? */
5683 if (t->owner->handle != client)
5686 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5691 char buf[ntohs (message->size)] GNUNET_ALIGN;
5692 struct GNUNET_MESH_Multicast *copy;
5694 copy = (struct GNUNET_MESH_Multicast *) buf;
5695 memcpy (buf, message, ntohs (message->size));
5696 copy->oid = my_full_id;
5697 copy->tid = htonl (t->id.tid);
5698 copy->ttl = htonl (DEFAULT_TTL);
5699 copy->mid = htonl (t->mid + 1);
5700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5701 " calling generic handler...\n");
5702 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
5705 /* receive done gets called when last copy is sent to a neighbor */
5710 * Functions to handle messages from clients
5712 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5713 {&handle_local_new_client, NULL,
5714 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5715 {&handle_local_announce_regex, NULL,
5716 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
5717 {&handle_local_tunnel_create, NULL,
5718 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5719 sizeof (struct GNUNET_MESH_TunnelMessage)},
5720 {&handle_local_tunnel_destroy, NULL,
5721 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5722 sizeof (struct GNUNET_MESH_TunnelMessage)},
5723 {&handle_local_connect_add, NULL,
5724 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
5725 sizeof (struct GNUNET_MESH_PeerControl)},
5726 {&handle_local_connect_del, NULL,
5727 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
5728 sizeof (struct GNUNET_MESH_PeerControl)},
5729 {&handle_local_blacklist, NULL,
5730 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
5731 sizeof (struct GNUNET_MESH_PeerControl)},
5732 {&handle_local_unblacklist, NULL,
5733 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
5734 sizeof (struct GNUNET_MESH_PeerControl)},
5735 {&handle_local_connect_by_type, NULL,
5736 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
5737 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
5738 {&handle_local_connect_by_string, NULL,
5739 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
5740 {&handle_local_unicast, NULL,
5741 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5742 {&handle_local_to_origin, NULL,
5743 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5744 {&handle_local_multicast, NULL,
5745 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5751 * To be called on core init/fail.
5753 * @param cls service closure
5754 * @param server handle to the server for this service
5755 * @param identity the public identity of this peer
5758 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5759 const struct GNUNET_PeerIdentity *identity)
5761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5762 core_handle = server;
5763 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5767 GNUNET_SCHEDULER_shutdown ();
5773 * Method called whenever a given peer connects.
5775 * @param cls closure
5776 * @param peer peer identity this notification is about
5777 * @param atsi performance data for the connection
5778 * @param atsi_count number of records in 'atsi'
5781 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
5782 const struct GNUNET_ATS_Information *atsi,
5783 unsigned int atsi_count)
5785 struct MeshPeerInfo *peer_info;
5786 struct MeshPeerPath *path;
5788 DEBUG_CONN ("Peer connected\n");
5789 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5790 peer_info = peer_info_get (peer);
5791 if (myid == peer_info->id)
5793 DEBUG_CONN (" (self)\n");
5798 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5800 path = path_new (2);
5801 path->peers[0] = myid;
5802 path->peers[1] = peer_info->id;
5803 GNUNET_PEER_change_rc (myid, 1);
5804 GNUNET_PEER_change_rc (peer_info->id, 1);
5805 peer_info_add_path (peer_info, path, GNUNET_YES);
5810 * Method called whenever a peer disconnects.
5812 * @param cls closure
5813 * @param peer peer identity this notification is about
5816 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5818 struct MeshPeerInfo *pi;
5819 struct MeshPeerQueue *q;
5820 struct MeshPeerQueue *n;
5822 DEBUG_CONN ("Peer disconnected\n");
5823 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5835 /* try to reroute this traffic instead */
5836 queue_destroy(q, GNUNET_YES);
5840 peer_info_remove_path (pi, pi->id, myid);
5843 DEBUG_CONN (" (self)\n");
5849 /******************************************************************************/
5850 /************************ MAIN FUNCTIONS ****************************/
5851 /******************************************************************************/
5854 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5856 * @param cls closure
5857 * @param key current key code
5858 * @param value value in the hash map
5859 * @return GNUNET_YES if we should continue to iterate,
5863 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5865 struct MeshTunnel *t = value;
5872 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5874 * @param cls closure
5875 * @param key current key code
5876 * @param value value in the hash map
5877 * @return GNUNET_YES if we should continue to iterate,
5881 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5883 struct MeshPeerInfo *p = value;
5884 struct MeshPeerQueue *q;
5885 struct MeshPeerQueue *n;
5893 queue_destroy(q, GNUNET_YES);
5897 peer_info_destroy (p);
5902 * Task run during shutdown.
5908 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5912 if (core_handle != NULL)
5914 GNUNET_CORE_disconnect (core_handle);
5917 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5918 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5919 if (dht_handle != NULL)
5921 GNUNET_DHT_disconnect (dht_handle);
5926 GNUNET_SERVER_notification_context_destroy (nc);
5929 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5931 GNUNET_SCHEDULER_cancel (announce_id_task);
5932 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5938 * Process mesh requests.
5940 * @param cls closure
5941 * @param server the initialized server
5942 * @param c configuration to use
5945 run (void *cls, struct GNUNET_SERVER_Handle *server,
5946 const struct GNUNET_CONFIGURATION_Handle *c)
5948 struct MeshPeerInfo *peer;
5949 struct MeshPeerPath *p;
5952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5953 server_handle = server;
5954 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5955 NULL, /* Closure passed to MESH functions */
5956 &core_init, /* Call core_init once connected */
5957 &core_connect, /* Handle connects */
5958 &core_disconnect, /* remove peers on disconnects */
5959 NULL, /* Don't notify about all incoming messages */
5960 GNUNET_NO, /* For header only in notification */
5961 NULL, /* Don't notify about all outbound messages */
5962 GNUNET_NO, /* For header-only out notification */
5963 core_handlers); /* Register these handlers */
5965 if (core_handle == NULL)
5968 GNUNET_SCHEDULER_shutdown ();
5973 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
5976 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5978 ("Mesh service is lacking key configuration settings. Exiting.\n"));
5979 GNUNET_SCHEDULER_shutdown ();
5982 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
5983 GNUNET_free (keyfile);
5984 if (my_private_key == NULL)
5986 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5987 _("Mesh service could not access hostkey. Exiting.\n"));
5988 GNUNET_SCHEDULER_shutdown ();
5991 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
5992 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5993 &my_full_id.hashPubKey);
5994 myid = GNUNET_PEER_intern (&my_full_id);
5996 // transport_handle = GNUNET_TRANSPORT_connect(c,
6003 dht_handle = GNUNET_DHT_connect (c, 64);
6004 if (dht_handle == NULL)
6010 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6012 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6013 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6014 peers = GNUNET_CONTAINER_multihashmap_create (32);
6015 applications = GNUNET_CONTAINER_multihashmap_create (32);
6016 types = GNUNET_CONTAINER_multihashmap_create (32);
6018 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6019 nc = GNUNET_SERVER_notification_context_create (server_handle,
6021 GNUNET_SERVER_disconnect_notify (server_handle,
6022 &handle_local_client_disconnect, NULL);
6026 clients_tail = NULL;
6029 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6030 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6032 /* Create a peer_info for the local peer */
6033 peer = peer_info_get (&my_full_id);
6036 GNUNET_PEER_change_rc (myid, 1);
6037 peer_info_add_path (peer, p, GNUNET_YES);
6039 /* Scheduled the task to clean up when shutdown is called */
6040 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
6047 * The main function for the mesh service.
6049 * @param argc number of arguments from the command line
6050 * @param argv command line arguments
6051 * @return 0 ok, 1 on error
6054 main (int argc, char *const *argv)
6058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6061 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");