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 * Is the speed on the tunnel limited to the slowest peer?
344 * Is buffering allowed in the tunnel?
349 * Flag to signal the destruction of the tunnel.
350 * If this is set GNUNET_YES the tunnel will be destroyed
351 * when the queue is empty.
356 * Last time the tunnel was used
358 struct GNUNET_TIME_Absolute timestamp;
361 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
362 * containing peers added by id or by type, not intermediate peers.
364 struct GNUNET_CONTAINER_MultiHashMap *peers;
367 * Number of peers that are connected and potentially ready to receive data
369 unsigned int peers_ready;
372 * Number of peers that have been added to the tunnel
374 unsigned int peers_total;
377 * Client owner of the tunnel, if any
379 struct MeshClient *owner;
382 * Clients that have been informed about the tunnel, if any
384 struct MeshClient **clients;
387 * Number of elements in clients
389 unsigned int nclients;
392 * Clients that have requested to leave the tunnel
394 struct MeshClient **ignore;
397 * Number of elements in clients
399 unsigned int nignore;
404 GNUNET_PEER_Id *blacklisted;
407 * Number of elements in blacklisted
409 unsigned int nblacklisted;
412 * Bloomfilter (for peer identities) to stop circular routes
414 char bloomfilter[MESH_BLOOM_SIZE];
419 struct MeshTunnelTree *tree;
422 * Application type we are looking for in this tunnel
424 GNUNET_MESH_ApplicationType type;
427 * Used to search peers offering a service
429 struct GNUNET_DHT_GetHandle *dht_get_type;
432 * Initial context of the regex search for a connect_by_string
434 struct MeshRegexSearchContext *regex_ctx;
437 * Task to keep the used paths alive
439 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
442 * Task to destroy the tunnel after timeout
444 * FIXME: merge the two? a tunnel will have either
445 * a path refresh OR a timeout, never both!
447 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
452 * Info needed to work with tunnel paths and peers
459 struct MeshTunnel *t;
462 * Neighbouring peer to whom we send the packet to
464 struct MeshPeerInfo *peer;
469 struct MeshPeerPath *path;
474 * Struct containing information about a client of the service
481 struct MeshClient *next;
486 struct MeshClient *prev;
489 * Tunnels that belong to this client, indexed by local id
491 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
494 * Tunnels this client has accepted, indexed by incoming local id
496 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
499 * Tunnels this client has rejected, indexed by incoming local id
501 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
503 * Handle to communicate with the client
505 struct GNUNET_SERVER_Client *handle;
508 * Applications that this client has claimed to provide
510 struct GNUNET_CONTAINER_MultiHashMap *apps;
513 * Messages that this client has declared interest in
515 struct GNUNET_CONTAINER_MultiHashMap *types;
518 * Whether the client is active or shutting down (don't send confirmations
519 * to a client that is shutting down.
524 * ID of the client, mainly for debug messages
529 * Regular expressions describing the services offered by this client.
531 char **regexes; // FIXME add timeout? API to remove a regex?
534 * Number of regular expressions in regexes.
536 unsigned int n_regex;
539 * Task to refresh all regular expresions in the DHT.
541 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
547 * Struct to keep information of searches of services described by a regex
548 * using a user-provided string service description.
550 struct MeshRegexSearchInfo
553 * Which tunnel is this for
555 struct MeshTunnel *t;
558 * User provided description of the searched service.
563 * Part of the description already consumed by the search.
570 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
573 * Results from running DHT GETs.
575 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
578 * Contexts, for each running DHT GET. Free all on end of search.
580 struct MeshRegexSearchContext **contexts;
583 * Number of contexts (branches/steps in search).
585 unsigned int n_contexts;
588 * Peer that is connecting via connect_by_string. When connected, free ctx.
593 * Other peers that are found but not yet being connected to.
595 GNUNET_PEER_Id *peers;
598 * Number of elements in peers.
600 unsigned int n_peers;
603 * Next peer to try to connect to.
608 * Timeout for a connect attempt.
609 * When reached, try to connect to a different peer, if any. If not,
610 * try the same peer again.
612 GNUNET_SCHEDULER_TaskIdentifier timeout;
617 * Struct to keep state of running searches that have consumed a part of
620 struct MeshRegexSearchContext
623 * Part of the description already consumed by
624 * this particular search branch.
629 * Information about the search.
631 struct MeshRegexSearchInfo *info;
635 /******************************************************************************/
636 /************************ DEBUG FUNCTIONS ****************************/
637 /******************************************************************************/
641 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
642 * @param cls string to print
643 * @param success GNUNET_OK if the PUT was transmitted,
644 * GNUNET_NO on timeout,
645 * GNUNET_SYSERR on disconnect from service
646 * after the PUT message was transmitted
647 * (so we don't know if it was received or not)
652 mesh_debug (void *cls, int success)
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
662 /******************************************************************************/
663 /*********************** GLOBAL VARIABLES ****************************/
664 /******************************************************************************/
669 static struct MeshClient *clients;
670 static struct MeshClient *clients_tail;
673 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
675 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
678 * Tunnels incoming, indexed by MESH_TunnelNumber
679 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
681 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
684 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
686 static struct GNUNET_CONTAINER_MultiHashMap *peers;
689 * Handle to communicate with transport
691 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
694 * Handle to communicate with core
696 static struct GNUNET_CORE_Handle *core_handle;
701 static struct GNUNET_DHT_Handle *dht_handle;
706 static struct GNUNET_SERVER_Handle *server_handle;
709 * Notification context, to send messages to local clients
711 static struct GNUNET_SERVER_NotificationContext *nc;
714 * Local peer own ID (memory efficient handle)
716 static GNUNET_PEER_Id myid;
719 * Local peer own ID (full value)
721 static struct GNUNET_PeerIdentity my_full_id;
726 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
731 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
734 * Tunnel ID for the next created tunnel (global tunnel number)
736 static MESH_TunnelNumber next_tid;
739 * Tunnel ID for the next incoming tunnel (local tunnel number)
741 static MESH_TunnelNumber next_local_tid;
744 * All application types provided by this peer
746 static struct GNUNET_CONTAINER_MultiHashMap *applications;
749 * All message types clients of this peer are interested in
751 static struct GNUNET_CONTAINER_MultiHashMap *types;
754 * Task to periodically announce provided applications
756 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
759 * Task to periodically announce itself in the network
761 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
764 * Next ID to assign to a client
766 unsigned int next_client_id;
769 /******************************************************************************/
770 /*********************** DECLARATIONS **************************/
771 /******************************************************************************/
773 /* FIXME move declarations here */
776 * Function to process paths received for a new peer addition. The recorded
777 * paths form the initial tunnel, which can be optimized later.
778 * Called on each result obtained for the DHT search.
781 * @param exp when will this value expire
782 * @param key key of the result
783 * @param type type of the result
784 * @param size number of bytes in data
785 * @param data pointer to the result data
788 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
789 const struct GNUNET_HashCode * key,
790 const struct GNUNET_PeerIdentity *get_path,
791 unsigned int get_path_length,
792 const struct GNUNET_PeerIdentity *put_path,
793 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
794 size_t size, const void *data);
798 * Function to process DHT string to regex matching.
799 * Called on each result obtained for the DHT search.
801 * @param cls closure (search context)
802 * @param exp when will this value expire
803 * @param key key of the result
804 * @param get_path path of the get request (not used)
805 * @param get_path_length lenght of get_path (not used)
806 * @param put_path path of the put request (not used)
807 * @param put_path_length length of the put_path (not used)
808 * @param type type of the result
809 * @param size number of bytes in data
810 * @param data pointer to the result data
812 * TODO: re-issue the request after certain time? cancel after X results?
815 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
816 const struct GNUNET_HashCode * key,
817 const struct GNUNET_PeerIdentity *get_path,
818 unsigned int get_path_length,
819 const struct GNUNET_PeerIdentity *put_path,
820 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
821 size_t size, const void *data);
825 * Function to process DHT string to regex matching.
826 * Called on each result obtained for the DHT search.
828 * @param cls closure (search context)
829 * @param exp when will this value expire
830 * @param key key of the result
831 * @param get_path path of the get request (not used)
832 * @param get_path_length lenght of get_path (not used)
833 * @param put_path path of the put request (not used)
834 * @param put_path_length length of the put_path (not used)
835 * @param type type of the result
836 * @param size number of bytes in data
837 * @param data pointer to the result data
840 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
841 const struct GNUNET_HashCode * key,
842 const struct GNUNET_PeerIdentity *get_path,
843 unsigned int get_path_length,
844 const struct GNUNET_PeerIdentity *put_path,
845 unsigned int put_path_length,
846 enum GNUNET_BLOCK_Type type,
847 size_t size, const void *data);
851 * Iterator over edges in a regex block retrieved from the DHT.
853 * @param cls Closure.
854 * @param token Token that follows to next state.
855 * @param len Lenght of token.
856 * @param key Hash of next state.
858 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
861 regex_edge_iterator (void *cls,
864 const struct GNUNET_HashCode *key);
868 * Find a path to a peer that offers a regex servcie compatible
869 * with a given string.
871 * @param key The key of the accepting state.
872 * @param ctx Context containing info about the string, tunnel, etc.
875 regex_find_path (const struct GNUNET_HashCode *key,
876 struct MeshRegexSearchContext *ctx);
880 * Queue and pass message to core when possible.
882 * @param cls Closure (type dependant).
883 * @param type Type of the message.
884 * @param size Size of the message.
885 * @param dst Neighbor to send message to.
886 * @param t Tunnel this message belongs to.
889 queue_add (void *cls, uint16_t type, size_t size,
890 struct MeshPeerInfo *dst, struct MeshTunnel *t);
893 * Free a transmission that was already queued with all resources
894 * associated to the request.
896 * @param queue Queue handler to cancel.
897 * @param clear_cls Is it necessary to free associated cls?
900 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
902 /******************************************************************************/
903 /************************ ITERATORS ****************************/
904 /******************************************************************************/
906 /* FIXME move iterators here */
909 * Iterator over found existing mesh regex blocks that match an ongoing search.
912 * @param key current key code
913 * @param value value in the hash map
914 * @return GNUNET_YES if we should continue to iterate,
918 regex_result_iterator (void *cls,
919 const struct GNUNET_HashCode * key,
922 struct MeshRegexBlock *block = value;
923 struct MeshRegexSearchContext *ctx = cls;
925 if (GNUNET_YES == ntohl(block->accepting) &&
926 ctx->position == strlen (ctx->info->description))
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
929 regex_find_path (key, ctx);
930 return GNUNET_YES; // We found an accept state!
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
935 ctx->position, strlen(ctx->info->description),
936 ntohl(block->accepting));
939 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
940 ®ex_edge_iterator, ctx);
946 * Iterator over edges in a regex block retrieved from the DHT.
948 * @param cls Closure (context of the search).
949 * @param token Token that follows to next state.
950 * @param len Lenght of token.
951 * @param key Hash of next state.
953 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
956 regex_edge_iterator (void *cls,
959 const struct GNUNET_HashCode *key)
961 struct MeshRegexSearchContext *ctx = cls;
962 struct MeshRegexSearchContext *new_ctx;
963 struct MeshRegexSearchInfo *info = ctx->info;
964 struct GNUNET_DHT_GetHandle *get_h;
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
971 current = &info->description[ctx->position];
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
973 current_len = strlen (info->description) - ctx->position;
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
978 if (len > current_len)
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
981 return GNUNET_YES; // Token too long, wont match
983 if (0 != strncmp (current, token, len))
985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
986 return GNUNET_YES; // Token doesn't match
988 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
989 new_ctx->info = info;
990 new_ctx->position = ctx->position + len;
991 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
993 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
996 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
997 ®ex_result_iterator,
999 return GNUNET_YES; // We are already looking for it
1001 /* Start search in DHT */
1003 GNUNET_DHT_get_start (dht_handle, /* handle */
1004 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1005 key, /* key to search */
1006 DHT_REPLICATION_LEVEL, /* replication level */
1007 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1008 NULL, /* xquery */ // FIXME BLOOMFILTER
1009 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1010 &dht_get_string_handler, new_ctx);
1012 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1013 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1023 * Iterator over hash map entries to cancel DHT GET requests after a
1024 * successful connect_by_string.
1026 * @param cls Closure (unused).
1027 * @param key Current key code (unused).
1028 * @param value Value in the hash map (get handle).
1029 * @return GNUNET_YES if we should continue to iterate,
1033 regex_cancel_dht_get (void *cls,
1034 const struct GNUNET_HashCode * key,
1037 struct GNUNET_DHT_GetHandle *h = value;
1039 GNUNET_DHT_get_stop (h);
1045 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1046 * search for connect_by_string.
1048 * @param cls Closure (unused).
1049 * @param key Current key code (unused).
1050 * @param value MeshRegexBlock in the hash map.
1051 * @return GNUNET_YES if we should continue to iterate,
1055 regex_free_result (void *cls,
1056 const struct GNUNET_HashCode * key,
1060 GNUNET_free (value);
1066 * Regex callback iterator to store own service description in the DHT.
1068 * @param cls closure.
1069 * @param key hash for current state.
1070 * @param proof proof for current state.
1071 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1072 * @param num_edges number of edges leaving current state.
1073 * @param edges edges leaving current state.
1076 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1077 int accepting, unsigned int num_edges,
1078 const struct GNUNET_REGEX_Edge *edges)
1080 struct MeshRegexBlock *block;
1081 struct MeshRegexEdge *block_edge;
1082 enum GNUNET_DHT_RouteOption opt;
1086 unsigned int offset;
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 " regex dht put for state %s\n",
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1099 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1100 if (GNUNET_YES == accepting)
1102 struct MeshRegexAccept block;
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1105 " state %s is accepting, putting own id\n",
1107 size = sizeof (block);
1109 block.id = my_full_id;
1111 GNUNET_DHT_put(dht_handle, key,
1112 DHT_REPLICATION_LEVEL,
1113 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1114 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1117 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1122 len = strlen(proof);
1123 size = sizeof (struct MeshRegexBlock) + len;
1124 block = GNUNET_malloc (size);
1127 block->n_proof = htonl (len);
1128 block->n_edges = htonl (num_edges);
1129 block->accepting = htonl (accepting);
1131 /* Store the proof at the end of the block. */
1132 aux = (char *) &block[1];
1133 memcpy (aux, proof, len);
1136 /* Store each edge in a variable length MeshEdge struct at the
1137 * very end of the MeshRegexBlock structure.
1139 for (i = 0; i < num_edges; i++)
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 " edge %s towards %s\n",
1144 GNUNET_h2s(&edges[i].destination));
1146 /* aux points at the end of the last block */
1147 len = strlen (edges[i].label);
1148 size += sizeof (struct MeshRegexEdge) + len;
1149 // Calculate offset FIXME is this ok? use size instead?
1150 offset = aux - (char *) block;
1151 block = GNUNET_realloc (block, size);
1152 aux = &((char *) block)[offset];
1153 block_edge = (struct MeshRegexEdge *) aux;
1154 block_edge->key = edges[i].destination;
1155 block_edge->n_token = htonl (len);
1156 aux = (char *) &block_edge[1];
1157 memcpy (aux, edges[i].label, len);
1161 GNUNET_DHT_put(dht_handle, key,
1162 DHT_REPLICATION_LEVEL,
1164 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1166 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1170 GNUNET_free (block);
1175 * Store the regular expression describing a local service into the DHT.
1177 * @param regex The regular expresion.
1180 regex_put (const char *regex)
1182 struct GNUNET_REGEX_Automaton *dfa;
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1185 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1186 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1187 GNUNET_REGEX_automaton_destroy (dfa);
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1193 * Find a path to a peer that offers a regex servcie compatible
1194 * with a given string.
1196 * @param key The key of the accepting state.
1197 * @param ctx Context containing info about the string, tunnel, etc.
1200 regex_find_path (const struct GNUNET_HashCode *key,
1201 struct MeshRegexSearchContext *ctx)
1203 struct GNUNET_DHT_GetHandle *get_h;
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1206 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1207 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1208 key, /* key to search */
1209 DHT_REPLICATION_LEVEL, /* replication level */
1210 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1211 GNUNET_DHT_RO_RECORD_ROUTE,
1212 NULL, /* xquery */ // FIXME BLOOMFILTER
1213 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1214 &dht_get_string_accept_handler, ctx);
1215 GNUNET_break (GNUNET_OK ==
1216 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1223 /******************************************************************************/
1224 /************************ PERIODIC FUNCTIONS ****************************/
1225 /******************************************************************************/
1228 * Announce iterator over for each application provided by the peer
1230 * @param cls closure
1231 * @param key current key code
1232 * @param value value in the hash map
1233 * @return GNUNET_YES if we should continue to
1238 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1240 struct PBlock block;
1241 struct MeshClient *c;
1243 block.id = my_full_id;
1244 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1245 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1246 if (0 == block.type)
1251 block.type = htonl (block.type);
1252 /* FIXME are hashes in multihash map equal on all aquitectures? */
1253 /* FIXME: keep return value of 'put' to possibly cancel!? */
1254 GNUNET_DHT_put (dht_handle, key,
1255 DHT_REPLICATION_LEVEL,
1256 GNUNET_DHT_RO_RECORD_ROUTE |
1257 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1258 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1260 (const char *) &block,
1261 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1263 APP_ANNOUNCE_TIME, NULL, NULL);
1269 * Periodically announce what applications are provided by local clients
1272 * @param cls closure
1273 * @param tc task context
1276 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1278 struct MeshClient *c = cls;
1281 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1283 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1287 DEBUG_DHT ("Starting PUT for regex\n");
1289 for (i = 0; i < c->n_regex; i++)
1291 regex_put (c->regexes[i]);
1293 c->regex_announce_task =
1294 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_regex, cls);
1295 DEBUG_DHT ("Finished PUT for regex\n");
1302 * Periodically announce what applications are provided by local clients
1305 * @param cls closure
1306 * @param tc task context
1309 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1311 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1313 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1317 DEBUG_DHT ("Starting PUT for apps\n");
1319 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1321 announce_applications_task =
1322 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
1324 DEBUG_DHT ("Finished PUT for apps\n");
1331 * Periodically announce self id in the DHT
1333 * @param cls closure
1334 * @param tc task context
1337 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1339 struct PBlock block;
1341 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1343 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1347 * - Set data expiration in function of X
1348 * - Adapt X to churn
1350 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1352 block.id = my_full_id;
1353 block.type = htonl (0);
1354 GNUNET_DHT_put (dht_handle, /* DHT handle */
1355 &my_full_id.hashPubKey, /* Key to use */
1356 DHT_REPLICATION_LEVEL, /* Replication level */
1357 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1358 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1359 sizeof (block), /* Size of the data */
1360 (const char *) &block, /* Data itself */
1361 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1362 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1363 NULL, /* Continuation */
1364 NULL); /* Continuation closure */
1366 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
1370 /******************************************************************************/
1371 /****************** GENERAL HELPER FUNCTIONS ************************/
1372 /******************************************************************************/
1375 * Cancel an ongoing regex search in the DHT and free all resources.
1377 * @param ctx The search context.
1380 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1382 struct MeshRegexSearchInfo *info = ctx->info;
1385 GNUNET_free (info->description);
1386 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1387 ®ex_cancel_dht_get, NULL);
1388 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1389 ®ex_free_result, NULL);
1390 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1391 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1392 info->t->regex_ctx = NULL;
1393 for (i = 0; i < info->n_contexts; i++)
1395 GNUNET_free (info->contexts[i]);
1397 if (0 < info->n_contexts)
1398 GNUNET_free (info->contexts);
1399 if (0 < info->n_peers)
1400 GNUNET_free (info->peers);
1401 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1403 GNUNET_SCHEDULER_cancel(info->timeout);
1409 * Search for a tunnel by global ID using full PeerIdentities
1411 * @param oid owner of the tunnel
1412 * @param tid global tunnel number
1414 * @return tunnel handler, NULL if doesn't exist
1416 static struct MeshTunnel *
1417 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1421 * Delete an active client from the tunnel.
1427 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1430 * Notify a tunnel that a connection has broken that affects at least
1431 * some of its peers.
1433 * @param t Tunnel affected.
1434 * @param p1 Peer that got disconnected from p2.
1435 * @param p2 Peer that got disconnected from p1.
1437 * @return Short ID of the peer disconnected (either p1 or p2).
1438 * 0 if the tunnel remained unaffected.
1440 static GNUNET_PEER_Id
1441 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1446 * Check if client has registered with the service and has not disconnected
1448 * @param client the client to check
1450 * @return non-NULL if client exists in the global DLL
1452 static struct MeshClient *
1453 client_get (struct GNUNET_SERVER_Client *client)
1455 struct MeshClient *c;
1460 if (c->handle == client)
1469 * Checks if a given client has subscribed to certain message type
1471 * @param message_type Type of message to check
1472 * @param c Client to check
1474 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1479 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1481 struct GNUNET_HashCode hc;
1483 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1484 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1489 * Allow a client to send more data after transmitting a multicast message
1490 * which some neighbor has not yet accepted altough a reasonable time has
1493 * @param cls Closure (DataDescriptor containing the task identifier)
1494 * @param tc Task Context
1496 * FIXME reference counter cshould be just int
1499 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1501 struct MeshData *mdata = cls;
1503 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1505 GNUNET_assert (NULL != mdata->reference_counter);
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1508 *(mdata->reference_counter));
1509 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1510 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1515 * Check whether client wants traffic from a tunnel.
1517 * @param c Client to check.
1518 * @param t Tunnel to be found.
1520 * @return GNUNET_YES if client knows tunnel.
1522 * TODO look in client hashmap
1525 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1529 for (i = 0; i < t->nclients; i++)
1530 if (t->clients[i] == c)
1537 * Check whether client has been informed about a tunnel.
1539 * @param c Client to check.
1540 * @param t Tunnel to be found.
1542 * @return GNUNET_YES if client knows tunnel.
1544 * TODO look in client hashmap
1547 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1551 for (i = 0; i < t->nignore; i++)
1552 if (t->ignore[i] == c)
1554 return client_wants_tunnel(c, t);
1559 * Marks a client as uninterested in traffic from the tunnel, updating both
1560 * client and tunnel to reflect this.
1562 * @param c Client that doesn't want traffic anymore.
1563 * @param t Tunnel which should be ignored.
1565 * FIXME when to delete an incoming tunnel?
1568 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1570 struct GNUNET_HashCode hash;
1572 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1573 GNUNET_break (GNUNET_YES ==
1574 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1576 GNUNET_break (GNUNET_YES ==
1577 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1578 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1579 tunnel_delete_active_client (t, c);
1580 GNUNET_array_append (t->ignore, t->nignore, c);
1585 * Deletes a tunnel from a client (either owner or destination). To be used on
1586 * tunnel destroy, otherwise, use client_ignore_tunnel.
1588 * @param c Client whose tunnel to delete.
1589 * @param t Tunnel which should be deleted.
1592 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1594 struct GNUNET_HashCode hash;
1598 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1599 GNUNET_assert (GNUNET_YES ==
1600 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1606 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1608 GNUNET_assert (GNUNET_YES ==
1609 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1613 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1622 * Send the message to all clients that have subscribed to its type
1624 * @param msg Pointer to the message itself
1625 * @param payload Pointer to the payload of the message.
1626 * @return number of clients this message was sent to
1629 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1630 const struct GNUNET_MessageHeader *payload)
1632 struct GNUNET_PeerIdentity *oid;
1633 struct MeshClient *c;
1634 struct MeshTunnel *t;
1635 MESH_TunnelNumber *tid;
1638 char cbuf[htons (msg->size)];
1640 type = ntohs (payload->type);
1641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1644 memcpy (cbuf, msg, sizeof (cbuf));
1645 switch (htons (msg->type))
1647 struct GNUNET_MESH_Unicast *uc;
1648 struct GNUNET_MESH_Multicast *mc;
1649 struct GNUNET_MESH_ToOrigin *to;
1651 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1652 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1656 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1657 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1661 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1662 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1670 t = tunnel_get (oid, ntohl (*tid));
1677 for (count = 0, c = clients; c != NULL; c = c->next)
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1680 if (client_is_subscribed (type, c))
1682 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1686 *tid = htonl (t->local_tid);
1690 if (GNUNET_NO == client_knows_tunnel (c, t))
1692 /* This client doesn't know the tunnel */
1693 struct GNUNET_MESH_TunnelNotification tmsg;
1694 struct GNUNET_HashCode hash;
1696 tmsg.header.size = htons (sizeof (tmsg));
1697 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1698 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1699 tmsg.tunnel_id = htonl (t->local_tid_dest);
1700 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1701 &tmsg.header, GNUNET_NO);
1702 GNUNET_array_append (t->clients, t->nclients, c);
1703 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1705 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1706 c->incoming_tunnels, &hash, t,
1707 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1709 *tid = htonl (t->local_tid_dest);
1712 /* Check if the client wants to get traffic from the tunnel */
1713 if (GNUNET_NO == client_wants_tunnel(c, t))
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1717 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1718 (struct GNUNET_MessageHeader
1719 *) cbuf, GNUNET_YES);
1727 * Notify the client that owns the tunnel that a peer has connected to it
1728 * (the requested path to it has been confirmed).
1730 * @param t Tunnel whose owner to notify
1731 * @param id Short id of the peer that has connected
1734 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1736 struct GNUNET_MESH_PeerControl pc;
1738 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1739 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1740 pc.tunnel_id = htonl (t->local_tid);
1741 GNUNET_PEER_resolve (id, &pc.peer);
1742 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1748 * Notify all clients (not depending on registration status) that the incoming
1749 * tunnel is no longer valid.
1751 * @param t Tunnel that was destroyed.
1754 send_clients_tunnel_destroy (struct MeshTunnel *t)
1756 struct GNUNET_MESH_TunnelMessage msg;
1758 msg.header.size = htons (sizeof (msg));
1759 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1760 msg.tunnel_id = htonl (t->local_tid_dest);
1761 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1766 * Notify clients of tunnel disconnections, if needed.
1767 * In case the origin disconnects, the destination clients get a tunnel destroy
1768 * notification. If the last destination disconnects (only one remaining client
1769 * in tunnel), the origin gets a (local ID) peer disconnected.
1770 * Note that the function must be called BEFORE removing the client from
1773 * @param t Tunnel that was destroyed.
1774 * @param c Client that disconnected.
1777 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1783 struct GNUNET_MESH_TunnelMessage msg;
1785 msg.header.size = htons (sizeof (msg));
1786 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1787 msg.tunnel_id = htonl (t->local_tid_dest);
1788 for (i = 0; i < t->nclients; i++)
1789 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1790 &msg.header, GNUNET_NO);
1792 // FIXME when to disconnect an incoming tunnel?
1793 else if (1 == t->nclients && NULL != t->owner)
1795 struct GNUNET_MESH_PeerControl msg;
1797 msg.header.size = htons (sizeof (msg));
1798 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1799 msg.tunnel_id = htonl (t->local_tid);
1800 msg.peer = my_full_id;
1801 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1802 &msg.header, GNUNET_NO);
1808 * Decrements the reference counter and frees all resources if needed
1810 * @param mesh_data Data Descriptor used in a multicast message.
1811 * Freed no longer needed (last message).
1814 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1816 /* Make sure it's a multicast packet */
1817 GNUNET_assert (NULL != mesh_data->reference_counter);
1819 if (0 == --(*(mesh_data->reference_counter)))
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1822 if (NULL != mesh_data->task)
1824 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1826 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1828 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1830 GNUNET_free (mesh_data->task);
1832 GNUNET_free (mesh_data->reference_counter);
1833 GNUNET_free (mesh_data->data);
1834 GNUNET_free (mesh_data);
1840 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1841 * and insert it in the appropiate structures if the peer is not known yet.
1843 * @param peer Full identity of the peer.
1845 * @return Existing or newly created peer info.
1847 static struct MeshPeerInfo *
1848 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1850 struct MeshPeerInfo *peer_info;
1852 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1853 if (NULL == peer_info)
1856 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1857 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1858 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1859 peer_info->id = GNUNET_PEER_intern (peer);
1867 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1868 * and insert it in the appropiate structures if the peer is not known yet.
1870 * @param peer Short identity of the peer.
1872 * @return Existing or newly created peer info.
1874 static struct MeshPeerInfo *
1875 peer_info_get_short (const GNUNET_PEER_Id peer)
1877 struct GNUNET_PeerIdentity id;
1879 GNUNET_PEER_resolve (peer, &id);
1880 return peer_info_get (&id);
1885 * Iterator to remove the tunnel from the list of tunnels a peer participates
1888 * @param cls Closure (tunnel info)
1889 * @param key GNUNET_PeerIdentity of the peer (unused)
1890 * @param value PeerInfo of the peer
1892 * @return always GNUNET_YES, to keep iterating
1895 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1897 struct MeshTunnel *t = cls;
1898 struct MeshPeerInfo *peer = value;
1901 for (i = 0; i < peer->ntunnels; i++)
1904 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1907 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1908 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1917 * Core callback to write a pre-constructed data packet to core buffer
1919 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1920 * @param size Number of bytes available in buf.
1921 * @param buf Where the to write the message.
1923 * @return number of bytes written to buf
1926 send_core_data_raw (void *cls, size_t size, void *buf)
1928 struct MeshTransmissionDescriptor *info = cls;
1929 struct GNUNET_MessageHeader *msg;
1932 GNUNET_assert (NULL != info);
1933 GNUNET_assert (NULL != info->mesh_data);
1934 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1935 total_size = ntohs (msg->size);
1937 if (total_size > size)
1942 memcpy (buf, msg, total_size);
1943 data_descriptor_decrement_rc (info->mesh_data);
1950 * Sends an already built unicast message to a peer, properly registrating
1951 * all used resources.
1953 * @param message Message to send. Function makes a copy of it.
1954 * @param peer Short ID of the neighbor whom to send the message.
1955 * @param t Tunnel on which this message is transmitted.
1958 send_message (const struct GNUNET_MessageHeader *message,
1959 const struct GNUNET_PeerIdentity *peer,
1960 struct MeshTunnel *t)
1962 struct MeshTransmissionDescriptor *info;
1963 struct MeshPeerInfo *neighbor;
1964 struct MeshPeerPath *p;
1967 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1969 size = ntohs (message->size);
1970 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1971 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1972 info->mesh_data->data = GNUNET_malloc (size);
1973 memcpy (info->mesh_data->data, message, size);
1974 info->mesh_data->data_len = size;
1975 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
1976 *info->mesh_data->reference_counter = 1;
1977 neighbor = peer_info_get (peer);
1978 for (p = neighbor->path_head; NULL != p; p = p->next)
1987 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1988 GNUNET_free (info->mesh_data->data);
1989 GNUNET_free (info->mesh_data);
1993 info->peer = neighbor;
1995 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
2003 * Sends a CREATE PATH message for a path to a peer, properly registrating
2004 * all used resources.
2006 * @param peer PeerInfo of the final peer for whom this path is being created.
2007 * @param p Path itself.
2008 * @param t Tunnel for which the path is created.
2011 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2012 struct MeshTunnel *t)
2014 struct GNUNET_PeerIdentity id;
2015 struct MeshPathInfo *path_info;
2016 struct MeshPeerInfo *neighbor;
2022 p = tree_get_path_to_peer (t->tree, peer->id);
2029 for (i = 0; i < p->length; i++)
2031 if (p->peers[i] == myid)
2034 if (i >= p->length - 1)
2040 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2042 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2043 path_info->path = p;
2045 neighbor = peer_info_get (&id);
2046 path_info->peer = neighbor;
2047 queue_add (path_info,
2048 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2049 sizeof (struct GNUNET_MESH_ManipulatePath) +
2050 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2057 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2059 * @param t Tunnel whose path to destroy.
2060 * @param destination Short ID of the peer to whom the path to destroy.
2063 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2065 struct MeshPeerPath *p;
2068 p = tree_get_path_to_peer (t->tree, destination);
2074 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2075 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2077 struct GNUNET_MESH_ManipulatePath *msg;
2078 struct GNUNET_PeerIdentity *pi;
2082 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2083 msg->header.size = htons (size);
2084 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2085 msg->tid = htonl (t->id.tid);
2086 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2087 for (i = 0; i < p->length; i++)
2089 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2091 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2098 * Try to establish a new connection to this peer.
2099 * Use the best path for the given tunnel.
2100 * If the peer doesn't have any path to it yet, try to get one.
2101 * If the peer already has some path, send a CREATE PATH towards it.
2103 * @param peer PeerInfo of the peer.
2104 * @param t Tunnel for which to create the path, if possible.
2107 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2109 struct MeshPeerPath *p;
2110 struct MeshPathInfo *path_info;
2112 if (NULL != peer->path_head)
2114 p = tree_get_path_to_peer (t->tree, peer->id);
2121 // FIXME always send create path to self
2124 send_create_path (peer, p, t);
2128 struct GNUNET_HashCode hash;
2131 send_client_peer_connected (t, myid);
2132 t->local_tid_dest = next_local_tid++;
2133 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2136 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2137 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2144 else if (NULL == peer->dhtget)
2146 struct GNUNET_PeerIdentity id;
2148 GNUNET_PEER_resolve (peer->id, &id);
2149 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2150 path_info->peer = peer;
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2153 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2154 peer->dhtgetcls = path_info;
2155 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2156 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2157 &id.hashPubKey, /* key to search */
2158 DHT_REPLICATION_LEVEL, /* replication level */
2159 GNUNET_DHT_RO_RECORD_ROUTE |
2160 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2161 NULL, /* xquery */ // FIXME BLOOMFILTER
2162 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2163 &dht_get_id_handler, path_info);
2165 /* Otherwise, there is no path but the DHT get is already started. */
2170 * Task to delay the connection of a peer
2172 * @param cls Closure (path info with tunnel and peer to connect).
2173 * Will be free'd on exection.
2174 * @param tc TaskContext
2177 peer_info_connect_task (void *cls,
2178 const struct GNUNET_SCHEDULER_TaskContext *tc)
2180 struct MeshPathInfo *path_info = cls;
2182 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2187 peer_info_connect (path_info->peer, path_info->t);
2193 * Destroy the peer_info and free any allocated resources linked to it
2195 * @param pi The peer_info to destroy.
2197 * @return GNUNET_OK on success
2200 peer_info_destroy (struct MeshPeerInfo *pi)
2202 struct GNUNET_PeerIdentity id;
2203 struct MeshPeerPath *p;
2204 struct MeshPeerPath *nextp;
2206 GNUNET_PEER_resolve (pi->id, &id);
2207 GNUNET_PEER_change_rc (pi->id, -1);
2210 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2213 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2214 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2216 if (NULL != pi->dhtget)
2218 GNUNET_DHT_get_stop (pi->dhtget);
2219 GNUNET_free (pi->dhtgetcls);
2225 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2235 * Remove all paths that rely on a direct connection between p1 and p2
2236 * from the peer itself and notify all tunnels about it.
2238 * @param peer PeerInfo of affected peer.
2239 * @param p1 GNUNET_PEER_Id of one peer.
2240 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2243 * TODO: optimize (see below)
2246 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2249 struct MeshPeerPath *p;
2250 struct MeshPeerPath *aux;
2251 struct MeshPeerInfo *peer_d;
2253 unsigned int destroyed;
2258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2260 p = peer->path_head;
2264 for (i = 0; i < (p->length - 1); i++)
2266 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2267 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2269 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2280 for (i = 0; i < peer->ntunnels; i++)
2282 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2286 * Problem: one or more peers have been deleted from the tunnel tree.
2287 * We don't know who they are to try to add them again.
2288 * We need to try to find a new path for each of the disconnected peers.
2289 * Some of them might already have a path to reach them that does not
2290 * involve p1 and p2. Adding all anew might render in a better tree than
2291 * the trivial immediate fix.
2293 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2294 * its children will be reachable trough him.
2296 peer_d = peer_info_get_short (d);
2299 for (p = peer_d->path_head; NULL != p; p = p->next)
2301 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2309 /* No callback, as peer will be already disconnected and a connection
2310 * scheduled by tunnel_notify_connection_broken.
2312 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2316 peer_info_connect (peer_d, peer->tunnels[i]);
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2324 * Add the path to the peer and update the path used to reach it in case this
2327 * @param peer_info Destination peer to add the path to.
2328 * @param path New path to add. Last peer must be the peer in arg 1.
2329 * Path will be either used of freed if already known.
2330 * @param trusted Do we trust that this path is real?
2333 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2336 struct MeshPeerPath *aux;
2340 if ((NULL == peer_info) || (NULL == path))
2343 path_destroy (path);
2346 if (path->peers[path->length - 1] != peer_info->id)
2349 path_destroy (path);
2352 if (path->length <= 2 && GNUNET_NO == trusted)
2354 /* Only allow CORE to tell us about direct paths */
2355 path_destroy (path);
2358 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2359 for (l = 1; l < path->length; l++)
2361 if (path->peers[l] == myid)
2363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2364 for (l2 = 0; l2 < path->length - l; l2++)
2366 path->peers[l2] = path->peers[l + l2];
2371 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2376 struct GNUNET_PeerIdentity id;
2378 GNUNET_PEER_resolve (peer_info->id, &id);
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2380 path->length, GNUNET_i2s (&id));
2383 l = path_get_length (path);
2390 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2391 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2393 l2 = path_get_length (aux);
2396 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2397 peer_info->path_tail, aux, path);
2402 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2404 path_destroy (path);
2409 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2416 * Add the path to the origin peer and update the path used to reach it in case
2417 * this is the shortest.
2418 * The path is given in peer_info -> destination, therefore we turn the path
2419 * upside down first.
2421 * @param peer_info Peer to add the path to, being the origin of the path.
2422 * @param path New path to add after being inversed.
2423 * @param trusted Do we trust that this path is real?
2426 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2427 struct MeshPeerPath *path, int trusted)
2430 peer_info_add_path (peer_info, path, trusted);
2435 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2436 * to obtain a local peer -> destination path and interning the peer ids.
2438 * @return Newly allocated and created path
2440 static struct MeshPeerPath *
2441 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2442 unsigned int get_path_length,
2443 const struct GNUNET_PeerIdentity *put_path,
2444 unsigned int put_path_length)
2446 struct MeshPeerPath *p;
2452 GNUNET_PEER_change_rc (myid, 1);
2453 i = get_path_length;
2454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2455 for (i--; i >= 0; i--)
2457 id = GNUNET_PEER_intern (&get_path[i]);
2458 if (p->length > 0 && id == p->peers[p->length - 1])
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2461 GNUNET_PEER_change_rc (id, -1);
2465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2466 GNUNET_i2s (&get_path[i]));
2468 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2469 p->peers[p->length - 1] = id;
2472 i = put_path_length;
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2474 for (i--; i >= 0; i--)
2476 id = GNUNET_PEER_intern (&put_path[i]);
2479 /* PUT path went through us, so discard the path up until now and start
2480 * from here to get a much shorter (and loop-free) path.
2485 if (p->length > 0 && id == p->peers[p->length - 1])
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2488 GNUNET_PEER_change_rc (id, -1);
2492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2493 GNUNET_i2s (&put_path[i]));
2495 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2496 p->peers[p->length - 1] = id;
2500 if (get_path_length > 0)
2501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2502 GNUNET_i2s (&get_path[0]));
2503 if (put_path_length > 0)
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2505 GNUNET_i2s (&put_path[0]));
2506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2508 for (i = 0; i < p->length; i++)
2510 struct GNUNET_PeerIdentity peer_id;
2512 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2514 GNUNET_i2s (&peer_id));
2522 * Adds a path to the peer_infos of all the peers in the path
2524 * @param p Path to process.
2525 * @param confirmed Whether we know if the path works or not. FIXME use
2528 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2532 /* TODO: invert and add */
2533 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2534 for (i++; i < p->length; i++)
2536 struct MeshPeerInfo *aux;
2537 struct MeshPeerPath *copy;
2539 aux = peer_info_get_short (p->peers[i]);
2540 copy = path_duplicate (p);
2541 copy->length = i + 1;
2542 peer_info_add_path (aux, copy, GNUNET_NO);
2548 * Send keepalive packets for a peer
2550 * @param cls Closure (tunnel for which to send the keepalive).
2551 * @param tc Notification context.
2553 * TODO: implement explicit multicast keepalive?
2556 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2560 * Search for a tunnel among the incoming tunnels
2562 * @param tid the local id of the tunnel
2564 * @return tunnel handler, NULL if doesn't exist
2566 static struct MeshTunnel *
2567 tunnel_get_incoming (MESH_TunnelNumber tid)
2569 struct GNUNET_HashCode hash;
2571 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2572 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2573 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2578 * Search for a tunnel among the tunnels for a client
2580 * @param c the client whose tunnels to search in
2581 * @param tid the local id of the tunnel
2583 * @return tunnel handler, NULL if doesn't exist
2585 static struct MeshTunnel *
2586 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2588 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2590 return tunnel_get_incoming (tid);
2594 struct GNUNET_HashCode hash;
2596 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2597 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2603 * Search for a tunnel by global ID using PEER_ID
2605 * @param pi owner of the tunnel
2606 * @param tid global tunnel number
2608 * @return tunnel handler, NULL if doesn't exist
2610 static struct MeshTunnel *
2611 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2613 struct MESH_TunnelID id;
2614 struct GNUNET_HashCode hash;
2619 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2620 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2625 * Search for a tunnel by global ID using full PeerIdentities
2627 * @param oid owner of the tunnel
2628 * @param tid global tunnel number
2630 * @return tunnel handler, NULL if doesn't exist
2632 static struct MeshTunnel *
2633 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2635 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2640 * Delete an active client from the tunnel.
2646 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2650 for (i = 0; i < t->nclients; i++)
2652 if (t->clients[i] == c)
2654 t->clients[i] = t->clients[t->nclients - 1];
2655 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2663 * Delete an ignored client from the tunnel.
2669 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2673 for (i = 0; i < t->nignore; i++)
2675 if (t->ignore[i] == c)
2677 t->ignore[i] = t->ignore[t->nignore - 1];
2678 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2686 * Delete a client from the tunnel. It should be only done on
2687 * client disconnection, otherwise use client_ignore_tunnel.
2693 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2695 tunnel_delete_ignored_client (t, c);
2696 tunnel_delete_active_client (t, c);
2701 * Callback used to notify a client owner of a tunnel that a peer has
2702 * disconnected, most likely because of a path change.
2704 * @param cls Closure (tunnel this notification is about).
2705 * @param peer_id Short ID of disconnected peer.
2708 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2710 struct MeshTunnel *t = cls;
2711 struct MeshPeerInfo *peer;
2712 struct MeshPathInfo *path_info;
2714 if (NULL != t->owner && NULL != nc)
2716 struct GNUNET_MESH_PeerControl msg;
2718 msg.header.size = htons (sizeof (msg));
2719 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2720 msg.tunnel_id = htonl (t->local_tid);
2721 GNUNET_PEER_resolve (peer_id, &msg.peer);
2722 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2723 &msg.header, GNUNET_NO);
2725 peer = peer_info_get_short (peer_id);
2726 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2727 path_info->peer = peer;
2729 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2734 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2735 * needed rescources.
2736 * If peer already exists, reevaluate shortest path and change if different.
2738 * @param t Tunnel we want to add a new peer to
2739 * @param peer PeerInfo of the peer being added
2743 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2745 struct GNUNET_PeerIdentity id;
2746 struct MeshPeerPath *best_p;
2747 struct MeshPeerPath *p;
2748 unsigned int best_cost;
2751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2752 GNUNET_PEER_resolve (peer->id, &id);
2754 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2757 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2758 GNUNET_assert (GNUNET_OK ==
2759 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2761 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2764 if (NULL != (p = peer->path_head))
2767 best_cost = tree_get_path_cost (t->tree, p);
2770 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2777 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2778 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2779 t->path_refresh_task =
2780 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2784 /* Start a DHT get */
2785 peer_info_connect (peer, t);
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2791 * Add a path to a tunnel which we don't own, just to remember the next hop.
2792 * If destination node was already in the tunnel, the first hop information
2793 * will be replaced with the new path.
2795 * @param t Tunnel we want to add a new peer to
2796 * @param p Path to add
2797 * @param own_pos Position of local node in path.
2801 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2802 unsigned int own_pos)
2804 struct GNUNET_PeerIdentity id;
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2807 GNUNET_assert (0 != own_pos);
2808 tree_add_path (t->tree, p, NULL, NULL);
2809 if (own_pos < p->length - 1)
2811 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2812 tree_update_first_hops (t->tree, myid, &id);
2814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2819 * Notifies a tunnel that a connection has broken that affects at least
2820 * some of its peers. Sends a notification towards the root of the tree.
2821 * In case the peer is the owner of the tree, notifies the client that owns
2822 * the tunnel and tries to reconnect.
2824 * @param t Tunnel affected.
2825 * @param p1 Peer that got disconnected from p2.
2826 * @param p2 Peer that got disconnected from p1.
2828 * @return Short ID of the peer disconnected (either p1 or p2).
2829 * 0 if the tunnel remained unaffected.
2831 static GNUNET_PEER_Id
2832 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2838 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2840 if (myid != p1 && myid != p2)
2846 if (tree_get_predecessor (t->tree) != 0)
2848 /* We are the peer still connected, notify owner of the disconnection. */
2849 struct GNUNET_MESH_PathBroken msg;
2850 struct GNUNET_PeerIdentity neighbor;
2852 msg.header.size = htons (sizeof (msg));
2853 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2854 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2855 msg.tid = htonl (t->id.tid);
2856 msg.peer1 = my_full_id;
2857 GNUNET_PEER_resolve (pid, &msg.peer2);
2858 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2859 send_message (&msg.header, &neighbor, t);
2867 * Send a multicast packet to a neighbor.
2869 * @param cls Closure (Info about the multicast packet)
2870 * @param neighbor_id Short ID of the neighbor to send the packet to.
2873 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2875 struct MeshData *mdata = cls;
2876 struct MeshTransmissionDescriptor *info;
2877 struct GNUNET_PeerIdentity neighbor;
2879 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2881 info->mesh_data = mdata;
2882 (*(mdata->reference_counter)) ++;
2883 info->destination = neighbor_id;
2884 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2886 GNUNET_i2s (&neighbor));
2887 info->peer = peer_info_get (&neighbor);
2888 GNUNET_assert (NULL != info->peer);
2890 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2891 info->mesh_data->data_len,
2897 * Send a message in a tunnel in multicast, sending a copy to each child node
2898 * down the local one in the tunnel tree.
2900 * @param t Tunnel in which to send the data.
2901 * @param msg Message to be sent.
2902 * @param internal Has the service generated this message?
2905 tunnel_send_multicast (struct MeshTunnel *t,
2906 const struct GNUNET_MessageHeader *msg,
2909 struct MeshData *mdata;
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2912 " sending a multicast packet...\n");
2913 mdata = GNUNET_malloc (sizeof (struct MeshData));
2914 mdata->data_len = ntohs (msg->size);
2915 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2917 mdata->data = GNUNET_malloc (mdata->data_len);
2918 memcpy (mdata->data, msg, mdata->data_len);
2919 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2921 struct GNUNET_MESH_Multicast *mcast;
2923 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2924 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2926 ntohl (mcast->ttl));
2930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2932 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2933 && GNUNET_NO == internal)
2935 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2937 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2943 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2944 if (*(mdata->reference_counter) == 0)
2946 GNUNET_free (mdata->data);
2947 GNUNET_free (mdata->reference_counter);
2948 if (NULL != mdata->task)
2950 GNUNET_SCHEDULER_cancel(*(mdata->task));
2951 GNUNET_free (mdata->task);
2952 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2954 // FIXME change order?
2955 GNUNET_free (mdata);
2957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2958 " sending a multicast packet done\n");
2964 * Send a message to all peers in this tunnel that the tunnel is no longer
2967 * @param t The tunnel whose peers to notify.
2970 tunnel_send_destroy (struct MeshTunnel *t)
2972 struct GNUNET_MESH_TunnelDestroy msg;
2974 msg.header.size = htons (sizeof (msg));
2975 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2976 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2977 msg.tid = htonl (t->id.tid);
2978 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2982 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
2984 * @param cls Closure (Tunnel which to cancel).
2985 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
2988 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
2990 struct MeshTunnel *t = cls;
2991 struct MeshPeerInfo *peer_info;
2992 struct MeshPeerQueue *pq;
2993 struct MeshPeerQueue *next;
2995 peer_info = peer_info_get_short (neighbor_id);
2996 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2999 if (pq->tunnel == t)
3001 queue_destroy (pq, GNUNET_YES);
3004 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3006 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3007 peer_info->core_transmit = NULL;
3012 * Destroy the tunnel and free any allocated resources linked to it.
3014 * @param t the tunnel to destroy
3016 * @return GNUNET_OK on success
3019 tunnel_destroy (struct MeshTunnel *t)
3021 struct MeshClient *c;
3022 struct GNUNET_HashCode hash;
3029 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3035 struct GNUNET_PeerIdentity id;
3037 GNUNET_PEER_resolve (t->id.oid, &id);
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3039 GNUNET_i2s (&id), t->id.tid);
3041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3045 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3046 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3051 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3054 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3058 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3059 for (i = 0; i < t->nclients; i++)
3063 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3068 for (i = 0; i < t->nignore; i++)
3072 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3077 if (t->nclients > 0)
3080 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3084 GNUNET_free (t->clients);
3086 if (NULL != t->peers)
3088 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3090 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3093 tree_destroy (t->tree);
3095 if (NULL != t->regex_ctx)
3096 regex_cancel_search (t->regex_ctx);
3097 if (NULL != t->dht_get_type)
3098 GNUNET_DHT_get_stop (t->dht_get_type);
3099 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3100 GNUNET_SCHEDULER_cancel (t->timeout_task);
3101 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3102 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3110 * Create a new tunnel
3112 * @param owner Who is the owner of the tunnel (short ID).
3113 * @param tid Tunnel Number of the tunnel.
3114 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3115 * @param local Tunnel Number for the tunnel, for the client point of view.
3118 static struct MeshTunnel *
3119 tunnel_new (GNUNET_PEER_Id owner,
3120 MESH_TunnelNumber tid,
3121 struct MeshClient *client,
3122 MESH_TunnelNumber local)
3124 struct MeshTunnel *t;
3125 struct GNUNET_HashCode hash;
3127 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3130 t->queue_max = 1000; // FIXME API parameter
3131 t->tree = tree_new (owner);
3133 t->local_tid = local;
3135 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3137 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3138 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3143 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3149 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3151 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3152 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3156 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3166 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3167 * that are no longer needed, as well as nodes of no longer reachable peers.
3168 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3170 * @param t Tunnel from which to remove the path.
3171 * @param peer Short id of the peer which should be removed.
3174 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3176 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3182 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3183 * client when the client disconnects. If the client is not the owner, the
3184 * owner will get notified if no more clients are in the tunnel and the client
3185 * get removed from the tunnel's list.
3187 * @param cls closure (client that is disconnecting)
3188 * @param key the hash of the local tunnel id (used to access the hashmap)
3189 * @param value the value stored at the key (tunnel to destroy)
3191 * @return GNUNET_OK on success
3194 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3196 struct MeshTunnel *t = value;
3197 struct MeshClient *c = cls;
3200 send_client_tunnel_disconnect(t, c);
3203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3204 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3205 tunnel_delete_client(t, c);
3206 client_delete_tunnel(c, t);
3209 tunnel_send_destroy(t);
3210 r = tunnel_destroy (t);
3216 * Timeout function, destroys tunnel if called
3218 * @param cls Closure (tunnel to destroy).
3219 * @param tc TaskContext
3222 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3224 struct MeshTunnel *t = cls;
3226 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3228 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3233 * Resets the tunnel timeout. Starts it if no timeout was running.
3235 * @param t Tunnel whose timeout to reset.
3238 tunnel_reset_timeout (struct MeshTunnel *t)
3240 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3241 GNUNET_SCHEDULER_cancel (t->timeout_task);
3243 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3244 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
3248 /******************************************************************************/
3249 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3250 /******************************************************************************/
3253 * Function to send a create path packet to a peer.
3255 * @param cls closure
3256 * @param size number of bytes available in buf
3257 * @param buf where the callee should write the message
3258 * @return number of bytes written to buf
3261 send_core_path_create (void *cls, size_t size, void *buf)
3263 struct MeshPathInfo *info = cls;
3264 struct GNUNET_MESH_ManipulatePath *msg;
3265 struct GNUNET_PeerIdentity *peer_ptr;
3266 struct MeshTunnel *t = info->t;
3267 struct MeshPeerPath *p = info->path;
3271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3273 sizeof (struct GNUNET_MESH_ManipulatePath) +
3274 p->length * sizeof (struct GNUNET_PeerIdentity);
3276 if (size < size_needed || NULL == buf)
3281 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3282 msg->header.size = htons (size_needed);
3283 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3284 msg->tid = ntohl (t->id.tid);
3286 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3287 for (i = 0; i < p->length; i++)
3289 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3296 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3302 * Fill the core buffer
3304 * @param cls closure (data itself)
3305 * @param size number of bytes available in buf
3306 * @param buf where the callee should write the message
3308 * @return number of bytes written to buf
3311 send_core_data_multicast (void *cls, size_t size, void *buf)
3313 struct MeshTransmissionDescriptor *info = cls;
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3317 GNUNET_assert (NULL != info);
3318 GNUNET_assert (NULL != info->peer);
3319 total_size = info->mesh_data->data_len;
3320 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3322 if (total_size > size)
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3328 memcpy (buf, info->mesh_data->data, total_size);
3331 struct GNUNET_MESH_Multicast *mc;
3332 struct GNUNET_MessageHeader *mh;
3335 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3337 mc = (struct GNUNET_MESH_Multicast *) mh;
3338 mh = (struct GNUNET_MessageHeader *) &mc[1];
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3340 " multicast, payload type %u\n", ntohs (mh->type));
3341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3342 " multicast, payload size %u\n", ntohs (mh->size));
3346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3351 data_descriptor_decrement_rc (info->mesh_data);
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3360 * Creates a path ack message in buf and frees all unused resources.
3362 * @param cls closure (MeshTransmissionDescriptor)
3363 * @param size number of bytes available in buf
3364 * @param buf where the callee should write the message
3365 * @return number of bytes written to buf
3368 send_core_path_ack (void *cls, size_t size, void *buf)
3370 struct MeshTransmissionDescriptor *info = cls;
3371 struct GNUNET_MESH_PathACK *msg = buf;
3373 GNUNET_assert (NULL != info);
3374 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3379 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3380 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3381 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3382 msg->tid = htonl (info->origin->tid);
3383 msg->peer_id = my_full_id;
3386 /* TODO add signature */
3388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3389 return sizeof (struct GNUNET_MESH_PathACK);
3394 * Free a transmission that was already queued with all resources
3395 * associated to the request.
3397 * @param queue Queue handler to cancel.
3398 * @param clear_cls Is it necessary to free associated cls?
3401 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3403 struct MeshTransmissionDescriptor *dd;
3404 struct MeshPathInfo *path_info;
3406 if (GNUNET_YES == clear_cls)
3408 switch (queue->type)
3410 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3411 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3412 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
3415 data_descriptor_decrement_rc (dd->mesh_data);
3417 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3419 path_info = queue->cls;
3420 path_destroy (path_info->path);
3424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
3426 GNUNET_free_non_null (queue->cls);
3428 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3429 queue->peer->queue_tail,
3431 GNUNET_free (queue);
3436 * Core callback to write a queued packet to core buffer
3438 * @param cls Closure (peer info).
3439 * @param size Number of bytes available in buf.
3440 * @param buf Where the to write the message.
3442 * @return number of bytes written to buf
3445 queue_send (void *cls, size_t size, void *buf)
3447 struct MeshPeerInfo *peer = cls;
3448 struct MeshPeerQueue *queue;
3449 struct MeshTunnel *t;
3452 peer->core_transmit = NULL;
3453 queue = peer->queue_head;
3455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3457 /* If queue is empty, send should have been cancelled */
3463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3465 /* Check if buffer size is enough for the message */
3466 if (queue->size > size)
3468 struct GNUNET_PeerIdentity id;
3470 GNUNET_PEER_resolve (peer->id, &id);
3471 peer->core_transmit =
3472 GNUNET_CORE_notify_transmit_ready(core_handle,
3475 GNUNET_TIME_UNIT_FOREVER_REL,
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3485 switch (queue->type)
3487 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
3489 data_size = send_core_data_raw (queue->cls, size, buf);
3491 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
3493 data_size = send_core_data_multicast(queue->cls, size, buf);
3495 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3497 data_size = send_core_path_create(queue->cls, size, buf);
3499 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3501 data_size = send_core_path_ack(queue->cls, size, buf);
3505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
3512 /* Free queue, but cls was freed by send_core_* */
3513 queue_destroy(queue, GNUNET_NO);
3515 if (GNUNET_YES == t->destroy && 0 == t->queue_n)
3517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3521 /* If more data in queue, send next */
3522 if (NULL != peer->queue_head)
3524 struct GNUNET_PeerIdentity id;
3526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3527 GNUNET_PEER_resolve (peer->id, &id);
3528 peer->core_transmit =
3529 GNUNET_CORE_notify_transmit_ready(core_handle,
3532 GNUNET_TIME_UNIT_FOREVER_REL,
3534 peer->queue_head->size,
3538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3544 * Queue and pass message to core when possible.
3546 * @param cls Closure (type dependant).
3547 * @param type Type of the message.
3548 * @param size Size of the message.
3549 * @param dst Neighbor to send message to.
3550 * @param t Tunnel this message belongs to.
3553 queue_add (void *cls, uint16_t type, size_t size,
3554 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3556 struct MeshPeerQueue *queue;
3558 if (t->queue_n >= t->queue_max)
3560 if (NULL == t->owner)
3561 GNUNET_break_op(0); // TODO: kill connection?
3564 return; // Drop message
3567 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3573 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3574 if (NULL == dst->core_transmit)
3576 struct GNUNET_PeerIdentity id;
3578 GNUNET_PEER_resolve (dst->id, &id);
3579 dst->core_transmit =
3580 GNUNET_CORE_notify_transmit_ready(core_handle,
3583 GNUNET_TIME_UNIT_FOREVER_REL,
3592 /******************************************************************************/
3593 /******************** MESH NETWORK HANDLERS **************************/
3594 /******************************************************************************/
3598 * Core handler for path creation
3600 * @param cls closure
3601 * @param message message
3602 * @param peer peer identity this notification is about
3603 * @param atsi performance data
3604 * @param atsi_count number of records in 'atsi'
3606 * @return GNUNET_OK to keep the connection open,
3607 * GNUNET_SYSERR to close it (signal serious error)
3610 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3611 const struct GNUNET_MessageHeader *message,
3612 const struct GNUNET_ATS_Information *atsi,
3613 unsigned int atsi_count)
3615 unsigned int own_pos;
3618 MESH_TunnelNumber tid;
3619 struct GNUNET_MESH_ManipulatePath *msg;
3620 struct GNUNET_PeerIdentity *pi;
3621 struct GNUNET_HashCode hash;
3622 struct MeshPeerPath *path;
3623 struct MeshPeerInfo *dest_peer_info;
3624 struct MeshPeerInfo *orig_peer_info;
3625 struct MeshTunnel *t;
3627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3628 "Received a path create msg [%s]\n",
3629 GNUNET_i2s (&my_full_id));
3630 size = ntohs (message->size);
3631 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3633 GNUNET_break_op (0);
3637 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3638 if (size % sizeof (struct GNUNET_PeerIdentity))
3640 GNUNET_break_op (0);
3643 size /= sizeof (struct GNUNET_PeerIdentity);
3646 GNUNET_break_op (0);
3649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3650 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3652 tid = ntohl (msg->tid);
3653 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3655 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3656 t = tunnel_get (pi, tid);
3657 if (NULL == t) // FIXME only for INCOMING tunnels?
3659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3660 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3662 while (NULL != tunnel_get_incoming (next_local_tid))
3663 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3664 t->local_tid_dest = next_local_tid++;
3665 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3667 tunnel_reset_timeout (t);
3668 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3670 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3671 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3679 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3680 if (NULL == dest_peer_info)
3682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3683 " Creating PeerInfo for destination.\n");
3684 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3685 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3686 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3688 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3690 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3691 if (NULL == orig_peer_info)
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3694 " Creating PeerInfo for origin.\n");
3695 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3696 orig_peer_info->id = GNUNET_PEER_intern (pi);
3697 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3698 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3701 path = path_new (size);
3703 for (i = 0; i < size; i++)
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3706 GNUNET_i2s (&pi[i]));
3707 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3708 if (path->peers[i] == myid)
3711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3714 /* cannot be self, must be 'not found' */
3715 /* create path: self not found in path through self */
3716 GNUNET_break_op (0);
3717 path_destroy (path);
3718 /* FIXME error. destroy tunnel? leave for timeout? */
3721 path_add_to_peers (path, GNUNET_NO);
3722 tunnel_add_path (t, path, own_pos);
3723 if (own_pos == size - 1)
3725 /* It is for us! Send ack. */
3726 struct MeshTransmissionDescriptor *info;
3728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3729 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3730 if (NULL == t->peers)
3732 /* New tunnel! Notify clients on data. */
3733 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
3735 GNUNET_break (GNUNET_OK ==
3736 GNUNET_CONTAINER_multihashmap_put (t->peers,
3737 &my_full_id.hashPubKey,
3740 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
3741 // FIXME URGENT (GNUNET_NO?)
3742 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3743 info->origin = &t->id;
3744 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3745 GNUNET_assert (NULL != info->peer);
3747 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3748 sizeof (struct GNUNET_MESH_PathACK),
3754 struct MeshPeerPath *path2;
3756 /* It's for somebody else! Retransmit. */
3757 path2 = path_duplicate (path);
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3759 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3760 path2 = path_duplicate (path);
3761 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3762 send_create_path (dest_peer_info, path, t);
3769 * Core handler for path destruction
3771 * @param cls closure
3772 * @param message message
3773 * @param peer peer identity this notification is about
3774 * @param atsi performance data
3775 * @param atsi_count number of records in 'atsi'
3777 * @return GNUNET_OK to keep the connection open,
3778 * GNUNET_SYSERR to close it (signal serious error)
3781 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3782 const struct GNUNET_MessageHeader *message,
3783 const struct GNUNET_ATS_Information *atsi,
3784 unsigned int atsi_count)
3786 struct GNUNET_MESH_ManipulatePath *msg;
3787 struct GNUNET_PeerIdentity *pi;
3788 struct MeshPeerPath *path;
3789 struct MeshTunnel *t;
3790 unsigned int own_pos;
3794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3795 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3796 size = ntohs (message->size);
3797 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3799 GNUNET_break_op (0);
3803 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3804 if (size % sizeof (struct GNUNET_PeerIdentity))
3806 GNUNET_break_op (0);
3809 size /= sizeof (struct GNUNET_PeerIdentity);
3812 GNUNET_break_op (0);
3815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3817 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3818 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3820 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3822 t = tunnel_get (pi, ntohl (msg->tid));
3825 /* TODO notify back: we don't know this tunnel */
3826 GNUNET_break_op (0);
3829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3830 path = path_new (size);
3832 for (i = 0; i < size; i++)
3834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3835 GNUNET_i2s (&pi[i]));
3836 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3837 if (path->peers[i] == myid)
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3841 if (own_pos < path->length - 1)
3842 send_message (message, &pi[own_pos + 1], t);
3844 send_client_tunnel_disconnect(t, NULL);
3846 tunnel_delete_peer (t, path->peers[path->length - 1]);
3847 path_destroy (path);
3853 * Core handler for notifications of broken paths
3855 * @param cls closure
3856 * @param message message
3857 * @param peer peer identity this notification is about
3858 * @param atsi performance data
3859 * @param atsi_count number of records in 'atsi'
3861 * @return GNUNET_OK to keep the connection open,
3862 * GNUNET_SYSERR to close it (signal serious error)
3865 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3866 const struct GNUNET_MessageHeader *message,
3867 const struct GNUNET_ATS_Information *atsi,
3868 unsigned int atsi_count)
3870 struct GNUNET_MESH_PathBroken *msg;
3871 struct MeshTunnel *t;
3873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3874 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3875 msg = (struct GNUNET_MESH_PathBroken *) message;
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3877 GNUNET_i2s (&msg->peer1));
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3879 GNUNET_i2s (&msg->peer2));
3880 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3883 GNUNET_break_op (0);
3886 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3887 GNUNET_PEER_search (&msg->peer2));
3894 * Core handler for tunnel destruction
3896 * @param cls closure
3897 * @param message message
3898 * @param peer peer identity this notification is about
3899 * @param atsi performance data
3900 * @param atsi_count number of records in 'atsi'
3902 * @return GNUNET_OK to keep the connection open,
3903 * GNUNET_SYSERR to close it (signal serious error)
3906 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3907 const struct GNUNET_MessageHeader *message,
3908 const struct GNUNET_ATS_Information *atsi,
3909 unsigned int atsi_count)
3911 struct GNUNET_MESH_TunnelDestroy *msg;
3912 struct MeshTunnel *t;
3914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3915 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3916 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3918 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3919 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3922 /* Probably already got the message from another path,
3923 * destroyed the tunnel and retransmitted to children.
3928 if (t->id.oid == myid)
3930 GNUNET_break_op (0);
3933 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3935 /* Tunnel was incoming, notify clients */
3936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3937 t->local_tid, t->local_tid_dest);
3938 send_clients_tunnel_destroy (t);
3940 tunnel_send_destroy (t);
3941 t->destroy = GNUNET_YES;
3942 // TODO: add timeout to destroy the tunnel anyway
3948 * Core handler for mesh network traffic going from the origin to a peer
3950 * @param cls closure
3951 * @param peer peer identity this notification is about
3952 * @param message message
3953 * @param atsi performance data
3954 * @param atsi_count number of records in 'atsi'
3955 * @return GNUNET_OK to keep the connection open,
3956 * GNUNET_SYSERR to close it (signal serious error)
3959 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3960 const struct GNUNET_MessageHeader *message,
3961 const struct GNUNET_ATS_Information *atsi,
3962 unsigned int atsi_count)
3964 struct GNUNET_MESH_Unicast *msg;
3965 struct MeshTunnel *t;
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3971 size = ntohs (message->size);
3973 sizeof (struct GNUNET_MESH_Unicast) +
3974 sizeof (struct GNUNET_MessageHeader))
3979 msg = (struct GNUNET_MESH_Unicast *) message;
3980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3981 ntohs (msg[1].header.type));
3982 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3985 /* TODO notify back: we don't know this tunnel */
3986 GNUNET_break_op (0);
3989 tunnel_reset_timeout (t);
3990 pid = GNUNET_PEER_search (&msg->destination);
3993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3994 " it's for us! sending to clients...\n");
3995 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3999 " not for us, retransmitting...\n");
4000 send_message (message, tree_get_first_hop (t->tree, pid), t);
4006 * Core handler for mesh network traffic going from the origin to all peers
4008 * @param cls closure
4009 * @param message message
4010 * @param peer peer identity this notification is about
4011 * @param atsi performance data
4012 * @param atsi_count number of records in 'atsi'
4013 * @return GNUNET_OK to keep the connection open,
4014 * GNUNET_SYSERR to close it (signal serious error)
4016 * TODO: Check who we got this from, to validate route.
4019 handle_mesh_data_multicast (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_Multicast *msg;
4025 struct MeshTunnel *t;
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
4030 size = ntohs (message->size);
4031 if (sizeof (struct GNUNET_MESH_Multicast) +
4032 sizeof (struct GNUNET_MessageHeader) > size)
4034 GNUNET_break_op (0);
4037 msg = (struct GNUNET_MESH_Multicast *) message;
4038 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4042 /* TODO notify that we dont know that tunnel */
4043 GNUNET_break_op (0);
4046 if (t->mid == ntohl (msg->mid))
4048 /* FIXME: already seen this packet, log dropping */
4049 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4050 " Already seen mid %u, DROPPING!\n", t->mid);
4055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4056 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
4058 t->mid = ntohl (msg->mid);
4059 tunnel_reset_timeout (t);
4061 /* Transmit to locally interested clients */
4062 if (NULL != t->peers &&
4063 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
4065 send_subscribed_clients (message, &msg[1].header);
4067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
4068 if (ntohl (msg->ttl) == 0)
4070 /* FIXME: ttl is 0, log dropping */
4071 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4074 tunnel_send_multicast (t, message, GNUNET_NO);
4080 * Core handler for mesh network traffic toward the owner of a tunnel
4082 * @param cls closure
4083 * @param message message
4084 * @param peer peer identity this notification is about
4085 * @param atsi performance data
4086 * @param atsi_count number of records in 'atsi'
4088 * @return GNUNET_OK to keep the connection open,
4089 * GNUNET_SYSERR to close it (signal serious error)
4092 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4093 const struct GNUNET_MessageHeader *message,
4094 const struct GNUNET_ATS_Information *atsi,
4095 unsigned int atsi_count)
4097 struct GNUNET_MESH_ToOrigin *msg;
4098 struct GNUNET_PeerIdentity id;
4099 struct MeshPeerInfo *peer_info;
4100 struct MeshTunnel *t;
4103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4105 size = ntohs (message->size);
4106 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4107 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4109 GNUNET_break_op (0);
4112 msg = (struct GNUNET_MESH_ToOrigin *) message;
4113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4114 ntohs (msg[1].header.type));
4115 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4119 /* TODO notify that we dont know this tunnel (whom)? */
4120 GNUNET_break_op (0);
4124 if (t->id.oid == myid)
4127 struct GNUNET_MESH_ToOrigin *copy;
4129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4130 " it's for us! sending to clients...\n");
4131 if (NULL == t->owner)
4133 /* got data packet for ownerless tunnel */
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4135 GNUNET_break_op (0);
4138 /* TODO signature verification */
4139 memcpy (cbuf, message, size);
4140 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4141 copy->tid = htonl (t->local_tid);
4142 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4143 ©->header, GNUNET_YES);
4146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4147 " not for us, retransmitting...\n");
4149 peer_info = peer_info_get (&msg->oid);
4150 if (NULL == peer_info)
4152 /* unknown origin of tunnel */
4156 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4157 send_message (message, &id, t);
4164 * Core handler for path ACKs
4166 * @param cls closure
4167 * @param message message
4168 * @param peer peer identity this notification is about
4169 * @param atsi performance data
4170 * @param atsi_count number of records in 'atsi'
4172 * @return GNUNET_OK to keep the connection open,
4173 * GNUNET_SYSERR to close it (signal serious error)
4176 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4177 const struct GNUNET_MessageHeader *message,
4178 const struct GNUNET_ATS_Information *atsi,
4179 unsigned int atsi_count)
4181 struct GNUNET_MESH_PathACK *msg;
4182 struct GNUNET_PeerIdentity id;
4183 struct MeshPeerInfo *peer_info;
4184 struct MeshPeerPath *p;
4185 struct MeshTunnel *t;
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4188 GNUNET_i2s (&my_full_id));
4189 msg = (struct GNUNET_MESH_PathACK *) message;
4190 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4193 /* TODO notify that we don't know the tunnel */
4194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4195 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4199 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4201 peer_info = peer_info_get (&msg->peer_id);
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4203 GNUNET_i2s (&msg->peer_id));
4204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4207 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
4209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4210 "connect_by_string completed, stopping search\n");
4211 regex_cancel_search (t->regex_ctx);
4212 t->regex_ctx = NULL;
4215 /* Add paths to peers? */
4216 p = tree_get_path_to_peer (t->tree, peer_info->id);
4219 path_add_to_peers (p, GNUNET_YES);
4227 /* Message for us? */
4228 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4231 if (NULL == t->owner)
4233 GNUNET_break_op (0);
4236 if (NULL != t->dht_get_type)
4238 GNUNET_DHT_get_stop (t->dht_get_type);
4239 t->dht_get_type = NULL;
4241 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4243 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4244 send_client_peer_connected (t, peer_info->id);
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4250 " not for us, retransmitting...\n");
4251 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4252 peer_info = peer_info_get (&msg->oid);
4253 if (NULL == peer_info)
4255 /* If we know the tunnel, we should DEFINITELY know the peer */
4259 send_message (message, &id, t);
4265 * Functions to handle messages from core
4267 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4268 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4269 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4270 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4271 sizeof (struct GNUNET_MESH_PathBroken)},
4272 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
4273 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4274 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4275 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4276 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4277 sizeof (struct GNUNET_MESH_PathACK)},
4283 /******************************************************************************/
4284 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4285 /******************************************************************************/
4288 * deregister_app: iterator for removing each application registered by a client
4290 * @param cls closure
4291 * @param key the hash of the application id (used to access the hashmap)
4292 * @param value the value stored at the key (client)
4294 * @return GNUNET_OK on success
4297 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
4299 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
4300 GNUNET_break (GNUNET_YES ==
4301 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
4307 * notify_client_connection_failure: notify a client that the connection to the
4308 * requested remote peer is not possible (for instance, no route found)
4309 * Function called when the socket is ready to queue more data. "buf" will be
4310 * NULL and "size" zero if the socket was closed for writing in the meantime.
4312 * @param cls closure
4313 * @param size number of bytes available in buf
4314 * @param buf where the callee should write the message
4315 * @return number of bytes written to buf
4318 notify_client_connection_failure (void *cls, size_t size, void *buf)
4321 struct MeshPeerInfo *peer_info;
4322 struct GNUNET_MESH_PeerControl *msg;
4323 struct GNUNET_PeerIdentity id;
4325 if (0 == size && NULL == buf)
4327 // TODO retry? cancel?
4331 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4332 peer_info = (struct MeshPeerInfo *) cls;
4333 msg = (struct GNUNET_MESH_PeerControl *) buf;
4334 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4335 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4336 // msg->tunnel_id = htonl(peer_info->t->tid);
4337 GNUNET_PEER_resolve (peer_info->id, &id);
4338 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4346 * Send keepalive packets for a peer
4348 * @param cls Closure (tunnel for which to send the keepalive).
4349 * @param tc Notification context.
4351 * TODO: implement explicit multicast keepalive?
4354 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4356 struct MeshTunnel *t = cls;
4357 struct GNUNET_MessageHeader *payload;
4358 struct GNUNET_MESH_Multicast *msg;
4360 sizeof (struct GNUNET_MESH_Multicast) +
4361 sizeof (struct GNUNET_MessageHeader);
4364 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4368 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4371 "sending keepalive for tunnel %d\n", t->id.tid);
4373 msg = (struct GNUNET_MESH_Multicast *) cbuf;
4374 msg->header.size = htons (size);
4375 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4376 msg->oid = my_full_id;
4377 msg->tid = htonl (t->id.tid);
4378 msg->ttl = htonl (DEFAULT_TTL);
4379 msg->mid = htonl (t->mid + 1);
4381 payload = (struct GNUNET_MessageHeader *) &msg[1];
4382 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
4383 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4384 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
4386 t->path_refresh_task =
4387 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
4393 * Function to process paths received for a new peer addition. The recorded
4394 * paths form the initial tunnel, which can be optimized later.
4395 * Called on each result obtained for the DHT search.
4397 * @param cls closure
4398 * @param exp when will this value expire
4399 * @param key key of the result
4400 * @param get_path path of the get request
4401 * @param get_path_length lenght of get_path
4402 * @param put_path path of the put request
4403 * @param put_path_length length of the put_path
4404 * @param type type of the result
4405 * @param size number of bytes in data
4406 * @param data pointer to the result data
4408 * TODO: re-issue the request after certain time? cancel after X results?
4411 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4412 const struct GNUNET_HashCode * key,
4413 const struct GNUNET_PeerIdentity *get_path,
4414 unsigned int get_path_length,
4415 const struct GNUNET_PeerIdentity *put_path,
4416 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4417 size_t size, const void *data)
4419 struct MeshPathInfo *path_info = cls;
4420 struct MeshPeerPath *p;
4421 struct GNUNET_PeerIdentity pi;
4424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4425 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4428 p = path_build_from_dht (get_path, get_path_length, put_path,
4430 path_add_to_peers (p, GNUNET_NO);
4432 for (i = 0; i < path_info->peer->ntunnels; i++)
4434 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4435 peer_info_connect (path_info->peer, path_info->t);
4443 * Function to process paths received for a new peer addition. The recorded
4444 * paths form the initial tunnel, which can be optimized later.
4445 * Called on each result obtained for the DHT search.
4447 * @param cls closure
4448 * @param exp when will this value expire
4449 * @param key key of the result
4450 * @param get_path path of the get request
4451 * @param get_path_length lenght of get_path
4452 * @param put_path path of the put request
4453 * @param put_path_length length of the put_path
4454 * @param type type of the result
4455 * @param size number of bytes in data
4456 * @param data pointer to the result data
4459 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4460 const struct GNUNET_HashCode * key,
4461 const struct GNUNET_PeerIdentity *get_path,
4462 unsigned int get_path_length,
4463 const struct GNUNET_PeerIdentity *put_path,
4464 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4465 size_t size, const void *data)
4467 const struct PBlock *pb = data;
4468 const struct GNUNET_PeerIdentity *pi = &pb->id;
4469 struct MeshTunnel *t = cls;
4470 struct MeshPeerInfo *peer_info;
4471 struct MeshPeerPath *p;
4473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
4474 if (size != sizeof (struct PBlock))
4476 GNUNET_break_op (0);
4479 if (ntohl(pb->type) != t->type)
4481 GNUNET_break_op (0);
4484 GNUNET_assert (NULL != t->owner);
4485 peer_info = peer_info_get (pi);
4486 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
4488 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
4490 p = path_build_from_dht (get_path, get_path_length, put_path,
4492 path_add_to_peers (p, GNUNET_NO);
4494 tunnel_add_peer (t, peer_info);
4495 peer_info_connect (peer_info, t);
4500 * Function called if the connect attempt to a peer found via
4501 * connect_by_string times out. Try to connect to another peer, if any.
4502 * Otherwise try to reconnect to the same peer.
4504 * @param cls Closure (info about regex search).
4505 * @param tc TaskContext.
4508 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4510 struct MeshRegexSearchInfo *info = cls;
4511 struct MeshPeerInfo *peer_info;
4515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
4516 info->timeout = GNUNET_SCHEDULER_NO_TASK;
4517 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
4525 if (0 < info->n_peers)
4527 // Select next peer, put current in that spot.
4528 id = info->peers[info->i_peer];
4529 info->peers[info->i_peer] = info->peer;
4530 info->i_peer = (info->i_peer + 1) % info->n_peers;
4534 // Try to connect to same peer again.
4537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
4539 peer_info = peer_info_get_short(id);
4540 tunnel_add_peer (info->t, peer_info);
4542 tunnel_delete_peer (info->t, old);
4543 peer_info_connect (peer_info, info->t);
4544 info->timeout = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
4545 ®ex_connect_timeout,
4547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
4552 * Function to process DHT string to regex matching.
4553 * Called on each result obtained for the DHT search.
4555 * @param cls closure (search context)
4556 * @param exp when will this value expire
4557 * @param key key of the result
4558 * @param get_path path of the get request (not used)
4559 * @param get_path_length lenght of get_path (not used)
4560 * @param put_path path of the put request (not used)
4561 * @param put_path_length length of the put_path (not used)
4562 * @param type type of the result
4563 * @param size number of bytes in data
4564 * @param data pointer to the result data
4567 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4568 const struct GNUNET_HashCode * key,
4569 const struct GNUNET_PeerIdentity *get_path,
4570 unsigned int get_path_length,
4571 const struct GNUNET_PeerIdentity *put_path,
4572 unsigned int put_path_length,
4573 enum GNUNET_BLOCK_Type type,
4574 size_t size, const void *data)
4576 const struct MeshRegexAccept *block = data;
4577 struct MeshRegexSearchContext *ctx = cls;
4578 struct MeshRegexSearchInfo *info = ctx->info;
4579 struct MeshPeerPath *p;
4580 struct MeshPeerInfo *peer_info;
4582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
4583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
4585 peer_info = peer_info_get(&block->id);
4586 p = path_build_from_dht (get_path, get_path_length, put_path,
4588 path_add_to_peers (p, GNUNET_NO);
4591 tunnel_add_peer (info->t, peer_info);
4592 peer_info_connect (peer_info, info->t);
4593 if (0 == info->peer)
4595 info->peer = peer_info->id;
4599 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
4602 info->timeout = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
4603 ®ex_connect_timeout,
4611 * Function to process DHT string to regex matching..
4612 * Called on each result obtained for the DHT search.
4614 * @param cls closure (search context)
4615 * @param exp when will this value expire
4616 * @param key key of the result
4617 * @param get_path path of the get request (not used)
4618 * @param get_path_length lenght of get_path (not used)
4619 * @param put_path path of the put request (not used)
4620 * @param put_path_length length of the put_path (not used)
4621 * @param type type of the result
4622 * @param size number of bytes in data
4623 * @param data pointer to the result data
4625 * TODO: re-issue the request after certain time? cancel after X results?
4628 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4629 const struct GNUNET_HashCode * key,
4630 const struct GNUNET_PeerIdentity *get_path,
4631 unsigned int get_path_length,
4632 const struct GNUNET_PeerIdentity *put_path,
4633 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4634 size_t size, const void *data)
4636 const struct MeshRegexBlock *block = data;
4637 struct MeshRegexSearchContext *ctx = cls;
4638 struct MeshRegexSearchInfo *info = ctx->info;
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4643 "DHT GET STRING RETURNED RESULTS\n");
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4645 " key: %s\n", GNUNET_h2s (key));
4647 copy = GNUNET_malloc (size);
4648 memcpy (copy, data, size);
4649 GNUNET_break (GNUNET_OK ==
4650 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
4651 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4652 len = ntohl (block->n_proof);
4654 char proof[len + 1];
4656 memcpy (proof, &block[1], len);
4658 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
4660 GNUNET_break_op (0);
4664 len = strlen (info->description);
4665 if (len == ctx->position) // String processed
4667 if (GNUNET_YES == ntohl (block->accepting))
4669 regex_find_path(key, ctx);
4673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
4674 // FIXME REGEX this block not successful, wait for more? start timeout?
4678 GNUNET_break (GNUNET_OK ==
4679 GNUNET_MESH_regex_block_iterate (block, size,
4680 ®ex_edge_iterator, ctx));
4684 /******************************************************************************/
4685 /********************* MESH LOCAL HANDLES **************************/
4686 /******************************************************************************/
4690 * Handler for client disconnection
4692 * @param cls closure
4693 * @param client identification of the client; NULL
4694 * for the last call when the server is destroyed
4697 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4699 struct MeshClient *c;
4700 struct MeshClient *next;
4703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4712 if (c->handle != client)
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4720 GNUNET_SERVER_client_drop (c->handle);
4721 c->shutting_down = GNUNET_YES;
4722 GNUNET_assert (NULL != c->own_tunnels);
4723 GNUNET_assert (NULL != c->incoming_tunnels);
4724 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4725 &tunnel_destroy_iterator, c);
4726 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4727 &tunnel_destroy_iterator, c);
4728 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4729 &tunnel_destroy_iterator, c);
4730 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4731 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4732 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4734 /* deregister clients applications */
4735 if (NULL != c->apps)
4737 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
4738 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
4740 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
4741 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
4743 GNUNET_SCHEDULER_cancel (announce_applications_task);
4744 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4746 if (NULL != c->types)
4747 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4748 for (i = 0; i < c->n_regex; i++)
4750 GNUNET_free (c->regexes[i]);
4752 GNUNET_free_non_null (c->regexes);
4753 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
4754 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
4756 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
4757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
4767 * Handler for new clients
4769 * @param cls closure
4770 * @param client identification of the client
4771 * @param message the actual message, which includes messages the client wants
4774 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4775 const struct GNUNET_MessageHeader *message)
4777 struct GNUNET_MESH_ClientConnect *cc_msg;
4778 struct MeshClient *c;
4779 GNUNET_MESH_ApplicationType *a;
4786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4787 /* Check data sanity */
4788 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4789 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4790 ntypes = ntohs (cc_msg->types);
4791 napps = ntohs (cc_msg->applications);
4793 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
4796 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4800 /* Create new client structure */
4801 c = GNUNET_malloc (sizeof (struct MeshClient));
4802 c->id = next_client_id++;
4803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4805 GNUNET_SERVER_client_keep (client);
4806 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
4809 GNUNET_MESH_ApplicationType at;
4810 struct GNUNET_HashCode hc;
4812 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
4813 for (i = 0; i < napps; i++)
4816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
4817 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
4818 /* store in clients hashmap */
4819 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
4820 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4821 /* store in global hashmap, for announcements */
4822 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
4823 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4825 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
4826 announce_applications_task =
4827 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
4833 struct GNUNET_HashCode hc;
4835 t = (uint16_t *) & a[napps];
4836 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
4837 for (i = 0; i < ntypes; i++)
4840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4841 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
4843 /* store in clients hashmap */
4844 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4845 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4846 /* store in global hashmap */
4847 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4848 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4852 " client has %u+%u subscriptions\n", napps, ntypes);
4854 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
4855 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4856 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4857 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4858 GNUNET_SERVER_notification_context_add (nc, client);
4860 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4866 * Handler for clients announcing available services by a regular expression.
4868 * @param cls closure
4869 * @param client identification of the client
4870 * @param message the actual message, which includes messages the client wants
4873 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
4874 const struct GNUNET_MessageHeader *message)
4876 struct MeshClient *c;
4880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
4882 /* Sanity check for client registration */
4883 if (NULL == (c = client_get (client)))
4886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4891 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
4892 regex = GNUNET_malloc (len + 1);
4893 memcpy (regex, &message[1], len);
4895 GNUNET_array_append (c->regexes, c->n_regex, regex);
4896 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
4898 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
4904 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
4910 * Handler for requests of new tunnels
4912 * @param cls closure
4913 * @param client identification of the client
4914 * @param message the actual message
4917 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4918 const struct GNUNET_MessageHeader *message)
4920 struct GNUNET_MESH_TunnelMessage *t_msg;
4921 struct MeshTunnel *t;
4922 struct MeshClient *c;
4924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4926 /* Sanity check for client registration */
4927 if (NULL == (c = client_get (client)))
4930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4935 /* Message sanity check */
4936 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4943 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4944 /* Sanity check for tunnel numbering */
4945 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4948 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4951 /* Sanity check for duplicate tunnel IDs */
4952 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
4955 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4959 while (NULL != tunnel_get_by_pi (myid, next_tid))
4960 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4961 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
4962 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4964 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4965 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
4968 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4974 * Handler for requests of deleting tunnels
4976 * @param cls closure
4977 * @param client identification of the client
4978 * @param message the actual message
4981 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4982 const struct GNUNET_MessageHeader *message)
4984 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4985 struct MeshClient *c;
4986 struct MeshTunnel *t;
4987 MESH_TunnelNumber tid;
4989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4990 "Got a DESTROY TUNNEL from client!\n");
4992 /* Sanity check for client registration */
4993 if (NULL == (c = client_get (client)))
4996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4999 /* Message sanity check */
5000 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5007 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5009 /* Retrieve tunnel */
5010 tid = ntohl (tunnel_msg->tunnel_id);
5011 t = tunnel_get_by_local_id(c, tid);
5015 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5016 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5019 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5021 client_ignore_tunnel (c, t);
5023 // TODO: when to destroy incoming tunnel?
5024 if (t->nclients == 0)
5026 GNUNET_assert (GNUNET_YES ==
5027 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
5029 GNUNET_assert (GNUNET_YES ==
5030 GNUNET_CONTAINER_multihashmap_remove (t->peers,
5031 &my_full_id.hashPubKey,
5035 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5038 send_client_tunnel_disconnect(t, c);
5039 client_delete_tunnel(c, t);
5041 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5043 tunnel_send_destroy (t);
5044 t->destroy = GNUNET_YES;
5045 // The tunnel will be destroyed when the last message is transmitted.
5046 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5052 * Handler for requests of seeting tunnel's speed.
5054 * @param cls Closure (unused).
5055 * @param client Identification of the client.
5056 * @param message The actual message.
5059 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
5060 const struct GNUNET_MessageHeader *message)
5062 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5063 struct MeshClient *c;
5064 struct MeshTunnel *t;
5065 MESH_TunnelNumber tid;
5067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5068 "Got a SPEED request from client!\n");
5070 /* Sanity check for client registration */
5071 if (NULL == (c = client_get (client)))
5074 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5079 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5081 /* Retrieve tunnel */
5082 tid = ntohl (tunnel_msg->tunnel_id);
5083 t = tunnel_get_by_local_id(c, tid);
5087 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5088 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5092 switch (ntohs(message->type))
5094 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
5095 t->speed_min = GNUNET_YES;
5097 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
5098 t->speed_min = GNUNET_NO;
5107 * Handler for connection requests to new peers
5109 * @param cls closure
5110 * @param client identification of the client
5111 * @param message the actual message (PeerControl)
5114 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
5115 const struct GNUNET_MessageHeader *message)
5117 struct GNUNET_MESH_PeerControl *peer_msg;
5118 struct MeshPeerInfo *peer_info;
5119 struct MeshClient *c;
5120 struct MeshTunnel *t;
5121 MESH_TunnelNumber tid;
5123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
5124 /* Sanity check for client registration */
5125 if (NULL == (c = client_get (client)))
5128 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5132 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5133 /* Sanity check for message size */
5134 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5137 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5141 /* Tunnel exists? */
5142 tid = ntohl (peer_msg->tunnel_id);
5143 t = tunnel_get_by_local_id (c, tid);
5147 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5151 /* Does client own tunnel? */
5152 if (t->owner->handle != client)
5155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
5159 GNUNET_i2s (&peer_msg->peer));
5160 peer_info = peer_info_get (&peer_msg->peer);
5162 tunnel_add_peer (t, peer_info);
5163 peer_info_connect (peer_info, t);
5165 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5171 * Handler for disconnection requests of peers in a tunnel
5173 * @param cls closure
5174 * @param client identification of the client
5175 * @param message the actual message (PeerControl)
5178 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
5179 const struct GNUNET_MessageHeader *message)
5181 struct GNUNET_MESH_PeerControl *peer_msg;
5182 struct MeshPeerInfo *peer_info;
5183 struct MeshClient *c;
5184 struct MeshTunnel *t;
5185 MESH_TunnelNumber tid;
5187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
5188 /* Sanity check for client registration */
5189 if (NULL == (c = client_get (client)))
5192 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5195 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5196 /* Sanity check for message size */
5197 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5200 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5204 /* Tunnel exists? */
5205 tid = ntohl (peer_msg->tunnel_id);
5206 t = tunnel_get_by_local_id (c, tid);
5210 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5215 /* Does client own tunnel? */
5216 if (t->owner->handle != client)
5219 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
5224 GNUNET_i2s (&peer_msg->peer));
5225 /* Is the peer in the tunnel? */
5227 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
5228 if (NULL == peer_info)
5231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5235 /* Ok, delete peer from tunnel */
5236 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
5237 &peer_msg->peer.hashPubKey);
5239 send_destroy_path (t, peer_info->id);
5240 tunnel_delete_peer (t, peer_info->id);
5241 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5246 * Handler for blacklist requests of peers in a tunnel
5248 * @param cls closure
5249 * @param client identification of the client
5250 * @param message the actual message (PeerControl)
5253 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
5254 const struct GNUNET_MessageHeader *message)
5256 struct GNUNET_MESH_PeerControl *peer_msg;
5257 struct MeshClient *c;
5258 struct MeshTunnel *t;
5259 MESH_TunnelNumber tid;
5261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
5262 /* Sanity check for client registration */
5263 if (NULL == (c = client_get (client)))
5266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5269 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5271 /* Sanity check for message size */
5272 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5275 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5279 /* Tunnel exists? */
5280 tid = ntohl (peer_msg->tunnel_id);
5281 t = tunnel_get_by_local_id (c, tid);
5285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5290 GNUNET_array_append(t->blacklisted, t->nblacklisted,
5291 GNUNET_PEER_intern(&peer_msg->peer));
5296 * Handler for unblacklist requests of peers in a tunnel
5298 * @param cls closure
5299 * @param client identification of the client
5300 * @param message the actual message (PeerControl)
5303 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
5304 const struct GNUNET_MessageHeader *message)
5306 struct GNUNET_MESH_PeerControl *peer_msg;
5307 struct MeshClient *c;
5308 struct MeshTunnel *t;
5309 MESH_TunnelNumber tid;
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
5314 /* Sanity check for client registration */
5315 if (NULL == (c = client_get (client)))
5318 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5321 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5323 /* Sanity check for message size */
5324 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5327 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5331 /* Tunnel exists? */
5332 tid = ntohl (peer_msg->tunnel_id);
5333 t = tunnel_get_by_local_id (c, tid);
5337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5342 /* if peer is not known, complain */
5343 pid = GNUNET_PEER_search (&peer_msg->peer);
5350 /* search and remove from list */
5351 for (i = 0; i < t->nblacklisted; i++)
5353 if (t->blacklisted[i] == pid)
5355 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
5356 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
5361 /* if peer hasn't been blacklisted, complain */
5367 * Handler for connection requests to new peers by type
5369 * @param cls closure
5370 * @param client identification of the client
5371 * @param message the actual message (ConnectPeerByType)
5374 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
5375 const struct GNUNET_MessageHeader *message)
5377 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
5378 struct MeshClient *c;
5379 struct MeshTunnel *t;
5380 struct GNUNET_HashCode hash;
5381 MESH_TunnelNumber tid;
5383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
5384 /* Sanity check for client registration */
5385 if (NULL == (c = client_get (client)))
5388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5392 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
5393 /* Sanity check for message size */
5394 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
5395 ntohs (connect_msg->header.size))
5398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5402 /* Tunnel exists? */
5403 tid = ntohl (connect_msg->tunnel_id);
5404 t = tunnel_get_by_local_id (c, tid);
5408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5412 /* Does client own tunnel? */
5413 if (t->owner->handle != client)
5416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5420 /* Do WE have the service? */
5421 t->type = ntohl (connect_msg->type);
5422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
5423 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
5424 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
5427 /* Yes! Fast forward, add ourselves to the tunnel and send the
5428 * good news to the client, and alert the destination client of
5429 * an incoming tunnel.
5431 * FIXME send a path create to self, avoid code duplication
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
5434 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
5435 peer_info_get (&my_full_id),
5436 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
5439 send_client_peer_connected (t, myid);
5440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
5441 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5443 t->local_tid_dest = next_local_tid++;
5444 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5445 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5446 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5450 /* Ok, lets find a peer offering the service */
5451 if (NULL != t->dht_get_type)
5453 GNUNET_DHT_get_stop (t->dht_get_type);
5455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
5456 GNUNET_h2s (&hash));
5458 GNUNET_DHT_get_start (dht_handle,
5459 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
5461 DHT_REPLICATION_LEVEL,
5462 GNUNET_DHT_RO_RECORD_ROUTE |
5463 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5465 &dht_get_type_handler, t);
5467 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5473 * Handler for connection requests to new peers by a string service description.
5475 * @param cls closure
5476 * @param client identification of the client
5477 * @param message the actual message, which includes messages the client wants
5480 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
5481 const struct GNUNET_MessageHeader *message)
5483 struct GNUNET_MESH_ConnectPeerByString *msg;
5484 struct MeshRegexSearchContext *ctx;
5485 struct MeshRegexSearchInfo *info;
5486 struct GNUNET_DHT_GetHandle *get_h;
5487 struct GNUNET_HashCode key;
5488 struct MeshTunnel *t;
5489 struct MeshClient *c;
5490 MESH_TunnelNumber tid;
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5495 "Connect by string started\n");
5496 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
5497 size = htons (message->size);
5499 /* Sanity check for client registration */
5500 if (NULL == (c = client_get (client)))
5503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5507 /* Message size sanity check */
5508 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
5511 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5515 /* Tunnel exists? */
5516 tid = ntohl (msg->tunnel_id);
5517 t = tunnel_get_by_local_id (c, tid);
5521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5525 /* Does client own tunnel? */
5526 if (t->owner->handle != client)
5529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5534 " on tunnel %s [%u]\n",
5535 GNUNET_i2s(&my_full_id),
5538 /* Only one connect_by_string allowed at the same time! */
5539 /* FIXME: allow more, return handle at api level to cancel, document */
5540 if (NULL != t->regex_ctx)
5543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5547 /* Find string itself */
5548 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
5549 string = (const char *) &msg[1];
5551 /* Initialize context */
5552 size = GNUNET_REGEX_get_first_key(string, len, &key);
5553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5554 " consumed %u bits out of %u\n", size, len);
5555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5556 " looking for %s\n", GNUNET_h2s (&key));
5558 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
5560 info->description = GNUNET_malloc (len + 1);
5561 memcpy (info->description, string, len);
5562 info->description[len] = '\0';
5563 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
5564 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
5565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
5567 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
5568 ctx->position = size;
5572 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
5574 /* Start search in DHT */
5575 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
5576 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
5577 &key, /* key to search */
5578 DHT_REPLICATION_LEVEL, /* replication level */
5579 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5580 NULL, /* xquery */ // FIXME BLOOMFILTER
5581 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
5582 &dht_get_string_handler, ctx);
5584 GNUNET_break (GNUNET_OK ==
5585 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
5588 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
5590 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
5596 * Handler for client traffic directed to one peer
5598 * @param cls closure
5599 * @param client identification of the client
5600 * @param message the actual message
5603 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5604 const struct GNUNET_MessageHeader *message)
5606 struct MeshClient *c;
5607 struct MeshTunnel *t;
5608 struct MeshPeerInfo *pi;
5609 struct GNUNET_MESH_Unicast *data_msg;
5610 MESH_TunnelNumber tid;
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5614 "Got a unicast request from a client!\n");
5616 /* Sanity check for client registration */
5617 if (NULL == (c = client_get (client)))
5620 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5623 data_msg = (struct GNUNET_MESH_Unicast *) message;
5624 /* Sanity check for message size */
5625 size = ntohs (message->size);
5626 if (sizeof (struct GNUNET_MESH_Unicast) +
5627 sizeof (struct GNUNET_MessageHeader) > size)
5630 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5634 /* Tunnel exists? */
5635 tid = ntohl (data_msg->tid);
5636 t = tunnel_get_by_local_id (c, tid);
5640 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5644 /* Is it a local tunnel? Then, does client own the tunnel? */
5645 if (t->owner->handle != client)
5648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5652 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
5653 &data_msg->destination.hashPubKey);
5654 /* Is the selected peer in the tunnel? */
5658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5662 /* Ok, everything is correct, send the message
5663 * (pretend we got it from a mesh peer)
5666 char buf[ntohs (message->size)] GNUNET_ALIGN;
5667 struct GNUNET_MESH_Unicast *copy;
5669 /* Work around const limitation */
5670 copy = (struct GNUNET_MESH_Unicast *) buf;
5671 memcpy (buf, data_msg, size);
5672 copy->oid = my_full_id;
5673 copy->tid = htonl (t->id.tid);
5674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5675 " calling generic handler...\n");
5676 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
5678 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5684 * Handler for client traffic directed to the origin
5686 * @param cls closure
5687 * @param client identification of the client
5688 * @param message the actual message
5691 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5692 const struct GNUNET_MessageHeader *message)
5694 struct GNUNET_MESH_ToOrigin *data_msg;
5695 struct GNUNET_PeerIdentity id;
5696 struct MeshClient *c;
5697 struct MeshTunnel *t;
5698 MESH_TunnelNumber tid;
5701 /* Sanity check for client registration */
5702 if (NULL == (c = client_get (client)))
5705 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5708 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5709 /* Sanity check for message size */
5710 size = ntohs (message->size);
5711 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5712 sizeof (struct GNUNET_MessageHeader) > size)
5715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5719 /* Tunnel exists? */
5720 tid = ntohl (data_msg->tid);
5721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5722 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
5723 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5726 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5729 t = tunnel_get_by_local_id (c, tid);
5733 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5737 /* It should be sent by someone who has this as incoming tunnel. */
5738 if (-1 == client_knows_tunnel (c, t))
5741 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5744 GNUNET_PEER_resolve (t->id.oid, &id);
5746 /* Ok, everything is correct, send the message
5747 * (pretend we got it from a mesh peer)
5750 char buf[ntohs (message->size)] GNUNET_ALIGN;
5751 struct GNUNET_MESH_ToOrigin *copy;
5753 /* Work around const limitation */
5754 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5755 memcpy (buf, data_msg, size);
5757 copy->tid = htonl (t->id.tid);
5758 copy->sender = my_full_id;
5759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5760 " calling generic handler...\n");
5761 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
5763 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5769 * Handler for client traffic directed to all peers in a tunnel
5771 * @param cls closure
5772 * @param client identification of the client
5773 * @param message the actual message
5776 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
5777 const struct GNUNET_MessageHeader *message)
5779 struct MeshClient *c;
5780 struct MeshTunnel *t;
5781 struct GNUNET_MESH_Multicast *data_msg;
5782 MESH_TunnelNumber tid;
5784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5785 "Got a multicast request from a client!\n");
5787 /* Sanity check for client registration */
5788 if (NULL == (c = client_get (client)))
5791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5794 data_msg = (struct GNUNET_MESH_Multicast *) message;
5795 /* Sanity check for message size */
5796 if (sizeof (struct GNUNET_MESH_Multicast) +
5797 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
5800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5804 /* Tunnel exists? */
5805 tid = ntohl (data_msg->tid);
5806 t = tunnel_get_by_local_id (c, tid);
5810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5814 /* Does client own tunnel? */
5815 if (t->owner->handle != client)
5818 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5823 char buf[ntohs (message->size)] GNUNET_ALIGN;
5824 struct GNUNET_MESH_Multicast *copy;
5826 copy = (struct GNUNET_MESH_Multicast *) buf;
5827 memcpy (buf, message, ntohs (message->size));
5828 copy->oid = my_full_id;
5829 copy->tid = htonl (t->id.tid);
5830 copy->ttl = htonl (DEFAULT_TTL);
5831 copy->mid = htonl (t->mid + 1);
5832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5833 " calling generic handler...\n");
5834 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
5837 /* receive done gets called when last copy is sent to a neighbor */
5842 * Functions to handle messages from clients
5844 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5845 {&handle_local_new_client, NULL,
5846 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5847 {&handle_local_announce_regex, NULL,
5848 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
5849 {&handle_local_tunnel_create, NULL,
5850 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5851 sizeof (struct GNUNET_MESH_TunnelMessage)},
5852 {&handle_local_tunnel_destroy, NULL,
5853 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5854 sizeof (struct GNUNET_MESH_TunnelMessage)},
5855 {&handle_local_tunnel_speed, NULL,
5856 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
5857 sizeof (struct GNUNET_MESH_TunnelMessage)},
5858 {&handle_local_tunnel_speed, NULL,
5859 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
5860 sizeof (struct GNUNET_MESH_TunnelMessage)},
5861 {&handle_local_connect_add, NULL,
5862 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
5863 sizeof (struct GNUNET_MESH_PeerControl)},
5864 {&handle_local_connect_del, NULL,
5865 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
5866 sizeof (struct GNUNET_MESH_PeerControl)},
5867 {&handle_local_blacklist, NULL,
5868 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
5869 sizeof (struct GNUNET_MESH_PeerControl)},
5870 {&handle_local_unblacklist, NULL,
5871 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
5872 sizeof (struct GNUNET_MESH_PeerControl)},
5873 {&handle_local_connect_by_type, NULL,
5874 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
5875 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
5876 {&handle_local_connect_by_string, NULL,
5877 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
5878 {&handle_local_unicast, NULL,
5879 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5880 {&handle_local_to_origin, NULL,
5881 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5882 {&handle_local_multicast, NULL,
5883 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5889 * To be called on core init/fail.
5891 * @param cls service closure
5892 * @param server handle to the server for this service
5893 * @param identity the public identity of this peer
5896 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5897 const struct GNUNET_PeerIdentity *identity)
5899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5900 core_handle = server;
5901 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5904 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5905 GNUNET_SCHEDULER_shutdown ();
5911 * Method called whenever a given peer connects.
5913 * @param cls closure
5914 * @param peer peer identity this notification is about
5915 * @param atsi performance data for the connection
5916 * @param atsi_count number of records in 'atsi'
5919 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
5920 const struct GNUNET_ATS_Information *atsi,
5921 unsigned int atsi_count)
5923 struct MeshPeerInfo *peer_info;
5924 struct MeshPeerPath *path;
5926 DEBUG_CONN ("Peer connected\n");
5927 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5928 peer_info = peer_info_get (peer);
5929 if (myid == peer_info->id)
5931 DEBUG_CONN (" (self)\n");
5936 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5938 path = path_new (2);
5939 path->peers[0] = myid;
5940 path->peers[1] = peer_info->id;
5941 GNUNET_PEER_change_rc (myid, 1);
5942 GNUNET_PEER_change_rc (peer_info->id, 1);
5943 peer_info_add_path (peer_info, path, GNUNET_YES);
5948 * Method called whenever a peer disconnects.
5950 * @param cls closure
5951 * @param peer peer identity this notification is about
5954 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5956 struct MeshPeerInfo *pi;
5957 struct MeshPeerQueue *q;
5958 struct MeshPeerQueue *n;
5960 DEBUG_CONN ("Peer disconnected\n");
5961 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5973 /* try to reroute this traffic instead */
5974 queue_destroy(q, GNUNET_YES);
5978 peer_info_remove_path (pi, pi->id, myid);
5981 DEBUG_CONN (" (self)\n");
5987 /******************************************************************************/
5988 /************************ MAIN FUNCTIONS ****************************/
5989 /******************************************************************************/
5992 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5994 * @param cls closure
5995 * @param key current key code
5996 * @param value value in the hash map
5997 * @return GNUNET_YES if we should continue to iterate,
6001 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6003 struct MeshTunnel *t = value;
6010 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
6012 * @param cls closure
6013 * @param key current key code
6014 * @param value value in the hash map
6015 * @return GNUNET_YES if we should continue to iterate,
6019 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
6021 struct MeshPeerInfo *p = value;
6022 struct MeshPeerQueue *q;
6023 struct MeshPeerQueue *n;
6031 queue_destroy(q, GNUNET_YES);
6035 peer_info_destroy (p);
6040 * Task run during shutdown.
6046 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6050 if (core_handle != NULL)
6052 GNUNET_CORE_disconnect (core_handle);
6055 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6056 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6057 if (dht_handle != NULL)
6059 GNUNET_DHT_disconnect (dht_handle);
6064 GNUNET_SERVER_notification_context_destroy (nc);
6067 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6069 GNUNET_SCHEDULER_cancel (announce_id_task);
6070 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6076 * Process mesh requests.
6078 * @param cls closure
6079 * @param server the initialized server
6080 * @param c configuration to use
6083 run (void *cls, struct GNUNET_SERVER_Handle *server,
6084 const struct GNUNET_CONFIGURATION_Handle *c)
6086 struct MeshPeerInfo *peer;
6087 struct MeshPeerPath *p;
6090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6091 server_handle = server;
6092 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6093 NULL, /* Closure passed to MESH functions */
6094 &core_init, /* Call core_init once connected */
6095 &core_connect, /* Handle connects */
6096 &core_disconnect, /* remove peers on disconnects */
6097 NULL, /* Don't notify about all incoming messages */
6098 GNUNET_NO, /* For header only in notification */
6099 NULL, /* Don't notify about all outbound messages */
6100 GNUNET_NO, /* For header-only out notification */
6101 core_handlers); /* Register these handlers */
6103 if (core_handle == NULL)
6106 GNUNET_SCHEDULER_shutdown ();
6111 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
6114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6116 ("Mesh service is lacking key configuration settings. Exiting.\n"));
6117 GNUNET_SCHEDULER_shutdown ();
6120 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6121 GNUNET_free (keyfile);
6122 if (my_private_key == NULL)
6124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6125 _("Mesh service could not access hostkey. Exiting.\n"));
6126 GNUNET_SCHEDULER_shutdown ();
6129 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6130 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6131 &my_full_id.hashPubKey);
6132 myid = GNUNET_PEER_intern (&my_full_id);
6134 // transport_handle = GNUNET_TRANSPORT_connect(c,
6141 dht_handle = GNUNET_DHT_connect (c, 64);
6142 if (dht_handle == NULL)
6148 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6150 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6151 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6152 peers = GNUNET_CONTAINER_multihashmap_create (32);
6153 applications = GNUNET_CONTAINER_multihashmap_create (32);
6154 types = GNUNET_CONTAINER_multihashmap_create (32);
6156 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6157 nc = GNUNET_SERVER_notification_context_create (server_handle,
6159 GNUNET_SERVER_disconnect_notify (server_handle,
6160 &handle_local_client_disconnect, NULL);
6164 clients_tail = NULL;
6167 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6168 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6170 /* Create a peer_info for the local peer */
6171 peer = peer_info_get (&my_full_id);
6174 GNUNET_PEER_change_rc (myid, 1);
6175 peer_info_add_path (peer, p, GNUNET_YES);
6177 /* Scheduled the task to clean up when shutdown is called */
6178 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
6185 * The main function for the mesh service.
6187 * @param argc number of arguments from the command line
6188 * @param argv command line arguments
6189 * @return 0 ok, 1 on error
6192 main (int argc, char *const *argv)
6196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6199 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");