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 DEFAULT_TTL 64
74 #define DHT_REPLICATION_LEVEL 10
78 #define MESH_BLOOM_SIZE 128
80 #define MESH_DEBUG_DHT GNUNET_YES
81 #define MESH_DEBUG_CONNECTION GNUNET_NO
83 #if MESH_DEBUG_CONNECTION
84 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
86 #define DEBUG_CONN(...)
90 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
92 #define DEBUG_DHT(...)
95 /******************************************************************************/
96 /************************ DATA STRUCTURES ****************************/
97 /******************************************************************************/
99 /** FWD declaration */
104 * Struct representing a piece of data being sent to other peers
108 /** Tunnel it belongs to. */
109 struct MeshTunnel *t;
111 /** In case of a multicast, task to allow a client to send more data if
112 * some neighbor is too slow. */
113 GNUNET_SCHEDULER_TaskIdentifier *task;
115 /** How many remaining neighbors we need to send this to. */
116 unsigned int *reference_counter;
118 /** Size of the data. */
127 * Struct containing info about a queued transmission to this peer
134 struct MeshPeerQueue *next;
139 struct MeshPeerQueue *prev;
142 * Peer this transmission is directed to.
144 struct MeshPeerInfo *peer;
147 * Tunnel this message belongs to.
149 struct MeshTunnel *tunnel;
152 * Pointer to info stucture used as cls.
162 * Size of the message
169 * Struct containing all info possibly needed to build a package when called
172 struct MeshTransmissionDescriptor
174 /** ID of the tunnel this packet travels in */
175 struct MESH_TunnelID *origin;
177 /** Who was this message being sent to */
178 struct MeshPeerInfo *peer;
180 /** Ultimate destination of the packet */
181 GNUNET_PEER_Id destination;
183 /** Data descriptor */
184 struct MeshData* mesh_data;
189 * Struct containing all information regarding a given peer
199 * Last time we heard from this peer
201 struct GNUNET_TIME_Absolute last_contact;
204 * Number of attempts to reconnect so far
206 int n_reconnect_attempts;
209 * Paths to reach the peer, ordered by ascending hop count
211 struct MeshPeerPath *path_head;
214 * Paths to reach the peer, ordered by ascending hop count
216 struct MeshPeerPath *path_tail;
219 * Handle to stop the DHT search for a path to this peer
221 struct GNUNET_DHT_GetHandle *dhtget;
224 * Closure given to the DHT GET
226 struct MeshPathInfo *dhtgetcls;
229 * Array of tunnels this peer participates in
230 * (most probably a small amount, therefore not a hashmap)
231 * When the path to the peer changes, notify these tunnels to let them
232 * re-adjust their path trees.
234 struct MeshTunnel **tunnels;
237 * Number of tunnels this peers participates in
239 unsigned int ntunnels;
242 * Transmission queue to core DLL head
244 struct MeshPeerQueue *queue_head;
247 * Transmission queue to core DLL tail
249 struct MeshPeerQueue *queue_tail;
252 * How many messages are in the queue to this peer.
254 unsigned int queue_n;
257 * Handle to for queued transmissions
259 struct GNUNET_CORE_TransmitHandle *core_transmit;
264 * Globally unique tunnel identification (owner + number)
265 * DO NOT USE OVER THE NETWORK
270 * Node that owns the tunnel
275 * Tunnel number to differentiate all the tunnels owned by the node oid
276 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
278 MESH_TunnelNumber tid;
282 struct MeshClient; /* FWD declaration */
285 * Struct containing all information regarding a tunnel
286 * For an intermediate node the improtant info used will be:
287 * - id Tunnel unique identification
288 * - paths[0] To know where to send it next
289 * - metainfo: ready, speeds, accounting
296 struct MESH_TunnelID id;
299 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
301 MESH_TunnelNumber local_tid;
304 * Local tunnel number for local destination clients (incoming number)
305 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
308 MESH_TunnelNumber local_tid_dest;
311 * Global count ID of the last *multicast* packet seen/sent.
316 * Local count ID of the last packet seen/sent.
321 * SKIP value for this tunnel.
326 * How many messages are in the queue.
328 unsigned int queue_n;
331 * How many messages do we accept in the queue.
333 unsigned int queue_max;
336 * Last time the tunnel was used
338 struct GNUNET_TIME_Absolute timestamp;
341 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
342 * containing peers added by id or by type, not intermediate peers.
344 struct GNUNET_CONTAINER_MultiHashMap *peers;
347 * Number of peers that are connected and potentially ready to receive data
349 unsigned int peers_ready;
352 * Number of peers that have been added to the tunnel
354 unsigned int peers_total;
357 * Client owner of the tunnel, if any
359 struct MeshClient *owner;
362 * Clients that have been informed about the tunnel, if any
364 struct MeshClient **clients;
367 * Number of elements in clients
369 unsigned int nclients;
372 * Clients that have requested to leave the tunnel
374 struct MeshClient **ignore;
377 * Number of elements in clients
379 unsigned int nignore;
384 GNUNET_PEER_Id *blacklisted;
387 * Number of elements in blacklisted
389 unsigned int nblacklisted;
392 * Bloomfilter (for peer identities) to stop circular routes
394 char bloomfilter[MESH_BLOOM_SIZE];
399 struct MeshTunnelTree *tree;
402 * Application type we are looking for in this tunnel
404 GNUNET_MESH_ApplicationType type;
407 * Used to search peers offering a service
409 struct GNUNET_DHT_GetHandle *dht_get_type;
412 * Context of the regex search for a connect_by_string
414 struct MeshRegexSearchContext *regex_ctx;
417 * Task to keep the used paths alive
419 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
422 * Task to destroy the tunnel after timeout
424 * FIXME: merge the two? a tunnel will have either
425 * a path refresh OR a timeout, never both!
427 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
432 * Info needed to work with tunnel paths and peers
439 struct MeshTunnel *t;
442 * Neighbouring peer to whom we send the packet to
444 struct MeshPeerInfo *peer;
449 struct MeshPeerPath *path;
454 * Struct containing information about a client of the service
461 struct MeshClient *next;
466 struct MeshClient *prev;
469 * Tunnels that belong to this client, indexed by local id
471 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
474 * Tunnels this client has accepted, indexed by incoming local id
476 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
479 * Tunnels this client has rejected, indexed by incoming local id
481 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
483 * Handle to communicate with the client
485 struct GNUNET_SERVER_Client *handle;
488 * Applications that this client has claimed to provide
490 struct GNUNET_CONTAINER_MultiHashMap *apps;
493 * Messages that this client has declared interest in
495 struct GNUNET_CONTAINER_MultiHashMap *types;
498 * Whether the client is active or shutting down (don't send confirmations
499 * to a client that is shutting down.
504 * ID of the client, mainly for debug messages
509 * Regular expressions describing the services offered by this client.
511 char **regexes; // FIXME add timeout? API to remove a regex?
514 * Number of regular expressions in regexes.
516 unsigned int n_regex;
519 * Task to refresh all regular expresions in the DHT.
521 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
527 * Struct to keep information of searches of services described by a regex
528 * using a user-provided string service description.
530 struct MeshRegexSearchInfo
533 * Which tunnel is this for
535 struct MeshTunnel *t;
538 * User provided description of the searched service.
543 * Part of the description already consumed by the search.
550 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
553 * Results from running DHT GETs.
555 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
558 * Contexts, for each running DHT GET. Free all on end of search.
560 struct MeshRegexSearchContext **copies;
563 * Number of contexts (branches/steps in search).
565 unsigned int n_copies;
570 * Struct to keep state of running searches that have consumed a part of
573 struct MeshRegexSearchContext
576 * Part of the description already consumed by
577 * this particular search branch.
582 * Information about the search.
584 struct MeshRegexSearchInfo *info;
588 /******************************************************************************/
589 /************************ DEBUG FUNCTIONS ****************************/
590 /******************************************************************************/
594 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
595 * @param cls string to print
596 * @param success GNUNET_OK if the PUT was transmitted,
597 * GNUNET_NO on timeout,
598 * GNUNET_SYSERR on disconnect from service
599 * after the PUT message was transmitted
600 * (so we don't know if it was received or not)
605 mesh_debug (void *cls, int success)
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
615 /******************************************************************************/
616 /*********************** GLOBAL VARIABLES ****************************/
617 /******************************************************************************/
622 static struct MeshClient *clients;
623 static struct MeshClient *clients_tail;
626 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
628 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
631 * Tunnels incoming, indexed by MESH_TunnelNumber
632 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
634 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
637 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
639 static struct GNUNET_CONTAINER_MultiHashMap *peers;
642 * Handle to communicate with transport
644 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
647 * Handle to communicate with core
649 static struct GNUNET_CORE_Handle *core_handle;
654 static struct GNUNET_DHT_Handle *dht_handle;
659 static struct GNUNET_SERVER_Handle *server_handle;
662 * Notification context, to send messages to local clients
664 static struct GNUNET_SERVER_NotificationContext *nc;
667 * Local peer own ID (memory efficient handle)
669 static GNUNET_PEER_Id myid;
672 * Local peer own ID (full value)
674 static struct GNUNET_PeerIdentity my_full_id;
679 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
684 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
687 * Tunnel ID for the next created tunnel (global tunnel number)
689 static MESH_TunnelNumber next_tid;
692 * Tunnel ID for the next incoming tunnel (local tunnel number)
694 static MESH_TunnelNumber next_local_tid;
697 * All application types provided by this peer
699 static struct GNUNET_CONTAINER_MultiHashMap *applications;
702 * All message types clients of this peer are interested in
704 static struct GNUNET_CONTAINER_MultiHashMap *types;
707 * Task to periodically announce provided applications
709 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
712 * Task to periodically announce itself in the network
714 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
717 * Next ID to assign to a client
719 unsigned int next_client_id;
723 /******************************************************************************/
724 /************************ ITERATORS ****************************/
725 /******************************************************************************/
727 /* FIXME move iterators here */
729 * Iterator over hash map entries to cancel DHT GET requests after a
730 * successful connect_by_string.
732 * @param cls Closure (unused).
733 * @param key Current key code (unused).
734 * @param value Value in the hash map (get handle).
735 * @return GNUNET_YES if we should continue to iterate,
739 regex_cancel_dht_get (void *cls,
740 const struct GNUNET_HashCode * key,
743 struct GNUNET_DHT_GetHandle *h = value;
745 GNUNET_DHT_get_stop (h);
751 * Iterator over hash map entries to free MeshRegexBlocks stored during the
752 * search for connect_by_string.
754 * @param cls Closure (unused).
755 * @param key Current key code (unused).
756 * @param value MeshRegexBlock in the hash map.
757 * @return GNUNET_YES if we should continue to iterate,
761 regex_free_result (void *cls,
762 const struct GNUNET_HashCode * key,
772 * Regex callback iterator to store own service description in the DHT.
774 * @param cls closure.
775 * @param key hash for current state.
776 * @param proof proof for current state.
777 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
778 * @param num_edges number of edges leaving current state.
779 * @param edges edges leaving current state.
782 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
783 int accepting, unsigned int num_edges,
784 const struct GNUNET_REGEX_Edge *edges)
786 struct MeshRegexBlock *block;
787 struct MeshRegexEdge *block_edge;
788 enum GNUNET_DHT_RouteOption opt;
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 " regex dht put for state %s\n",
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
806 if (GNUNET_YES == accepting)
808 struct MeshRegexAccept block;
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 " state %s is accepting, putting own id\n",
813 size = sizeof (block);
815 block.id = my_full_id;
817 GNUNET_DHT_put(dht_handle, key,
818 DHT_REPLICATION_LEVEL,
819 opt | GNUNET_DHT_RO_RECORD_ROUTE,
820 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
823 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
829 size = sizeof (struct MeshRegexBlock) + len;
830 block = GNUNET_malloc (size);
833 block->n_proof = htonl (len);
834 block->n_edges = htonl (num_edges);
835 block->accepting = htonl (accepting);
837 /* Store the proof at the end of the block. */
838 aux = (char *) &block[1];
839 memcpy (aux, proof, len);
842 /* Store each edge in a variable length MeshEdge struct at the
843 * very end of the MeshRegexBlock structure.
845 for (i = 0; i < num_edges; i++)
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 " edge %s towards %s\n",
850 GNUNET_h2s(&edges[i].destination));
852 /* aux points at the end of the last block */
853 len = strlen (edges[i].label);
854 size += sizeof (struct MeshRegexEdge) + len;
855 // Calculate offset FIXME is this ok? use size instead?
856 offset = aux - (char *) block;
857 block = GNUNET_realloc (block, size);
858 aux = &((char *) block)[offset];
859 block_edge = (struct MeshRegexEdge *) aux;
860 block_edge->key = edges[i].destination;
861 block_edge->n_token = htonl (len);
862 aux = (char *) &block_edge[1];
863 memcpy (aux, edges[i].label, len);
867 GNUNET_DHT_put(dht_handle, key,
868 DHT_REPLICATION_LEVEL,
870 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
872 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
881 * Store the regular expression describing a local service into the DHT.
883 * @param regex The regular expresion.
886 regex_put (const char *regex)
888 struct GNUNET_REGEX_Automaton *dfa;
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
891 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
892 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
893 GNUNET_REGEX_automaton_destroy (dfa);
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
899 /******************************************************************************/
900 /************************ PERIODIC FUNCTIONS ****************************/
901 /******************************************************************************/
904 * Announce iterator over for each application provided by the peer
907 * @param key current key code
908 * @param value value in the hash map
909 * @return GNUNET_YES if we should continue to
914 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
917 struct MeshClient *c;
919 block.id = my_full_id;
920 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
921 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
927 block.type = htonl (block.type);
928 /* FIXME are hashes in multihash map equal on all aquitectures? */
929 /* FIXME: keep return value of 'put' to possibly cancel!? */
930 GNUNET_DHT_put (dht_handle, key,
931 DHT_REPLICATION_LEVEL,
932 GNUNET_DHT_RO_RECORD_ROUTE |
933 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
934 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
936 (const char *) &block,
937 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
939 APP_ANNOUNCE_TIME, NULL, NULL);
945 * Periodically announce what applications are provided by local clients
949 * @param tc task context
952 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
954 struct MeshClient *c = cls;
957 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
959 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
963 DEBUG_DHT ("Starting PUT for regex\n");
965 for (i = 0; i < c->n_regex; i++)
967 regex_put (c->regexes[i]);
969 c->regex_announce_task =
970 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_regex, cls);
971 DEBUG_DHT ("Finished PUT for regex\n");
978 * Periodically announce what applications are provided by local clients
982 * @param tc task context
985 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
987 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
989 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
993 DEBUG_DHT ("Starting PUT for apps\n");
995 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
997 announce_applications_task =
998 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
1000 DEBUG_DHT ("Finished PUT for apps\n");
1007 * Periodically announce self id in the DHT
1009 * @param cls closure
1010 * @param tc task context
1013 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1015 struct PBlock block;
1017 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1019 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1023 * - Set data expiration in function of X
1024 * - Adapt X to churn
1026 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1028 block.id = my_full_id;
1029 block.type = htonl (0);
1030 GNUNET_DHT_put (dht_handle, /* DHT handle */
1031 &my_full_id.hashPubKey, /* Key to use */
1032 DHT_REPLICATION_LEVEL, /* Replication level */
1033 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1034 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1035 sizeof (block), /* Size of the data */
1036 (const char *) &block, /* Data itself */
1037 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1038 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1039 NULL, /* Continuation */
1040 NULL); /* Continuation closure */
1042 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
1047 * Function to process paths received for a new peer addition. The recorded
1048 * paths form the initial tunnel, which can be optimized later.
1049 * Called on each result obtained for the DHT search.
1051 * @param cls closure
1052 * @param exp when will this value expire
1053 * @param key key of the result
1054 * @param type type of the result
1055 * @param size number of bytes in data
1056 * @param data pointer to the result data
1059 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1060 const struct GNUNET_HashCode * key,
1061 const struct GNUNET_PeerIdentity *get_path,
1062 unsigned int get_path_length,
1063 const struct GNUNET_PeerIdentity *put_path,
1064 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1065 size_t size, const void *data);
1068 /******************************************************************************/
1069 /****************** GENERAL HELPER FUNCTIONS ************************/
1070 /******************************************************************************/
1073 * Cancel an ongoing regex search in the DHT and free all resources.
1075 * @param ctx The search context.
1078 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1080 struct MeshRegexSearchInfo *info = ctx->info;
1083 GNUNET_free (info->description);
1084 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1085 ®ex_cancel_dht_get, NULL);
1086 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1087 ®ex_free_result, NULL);
1088 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1089 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1090 info->t->regex_ctx = NULL;
1091 for (i = 0; i < info->n_copies; i++)
1093 GNUNET_free (info->copies[i]);
1099 * Search for a tunnel by global ID using full PeerIdentities
1101 * @param oid owner of the tunnel
1102 * @param tid global tunnel number
1104 * @return tunnel handler, NULL if doesn't exist
1106 static struct MeshTunnel *
1107 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1111 * Delete an active client from the tunnel.
1117 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1120 * Notify a tunnel that a connection has broken that affects at least
1121 * some of its peers.
1123 * @param t Tunnel affected.
1124 * @param p1 Peer that got disconnected from p2.
1125 * @param p2 Peer that got disconnected from p1.
1127 * @return Short ID of the peer disconnected (either p1 or p2).
1128 * 0 if the tunnel remained unaffected.
1130 static GNUNET_PEER_Id
1131 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1136 * Check if client has registered with the service and has not disconnected
1138 * @param client the client to check
1140 * @return non-NULL if client exists in the global DLL
1142 static struct MeshClient *
1143 client_get (struct GNUNET_SERVER_Client *client)
1145 struct MeshClient *c;
1150 if (c->handle == client)
1159 * Checks if a given client has subscribed to certain message type
1161 * @param message_type Type of message to check
1162 * @param c Client to check
1164 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1169 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1171 struct GNUNET_HashCode hc;
1173 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1174 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1179 * Allow a client to send more data after transmitting a multicast message
1180 * which some neighbor has not yet accepted altough a reasonable time has
1183 * @param cls Closure (DataDescriptor containing the task identifier)
1184 * @param tc Task Context
1186 * FIXME reference counter cshould be just int
1189 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1191 struct MeshData *mdata = cls;
1193 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
1195 GNUNET_assert (NULL != mdata->reference_counter);
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1198 *(mdata->reference_counter));
1199 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1200 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1205 * Check whether client wants traffic from a tunnel.
1207 * @param c Client to check.
1208 * @param t Tunnel to be found.
1210 * @return GNUNET_YES if client knows tunnel.
1212 * TODO look in client hashmap
1215 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1219 for (i = 0; i < t->nclients; i++)
1220 if (t->clients[i] == c)
1227 * Check whether client has been informed about a tunnel.
1229 * @param c Client to check.
1230 * @param t Tunnel to be found.
1232 * @return GNUNET_YES if client knows tunnel.
1234 * TODO look in client hashmap
1237 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1241 for (i = 0; i < t->nignore; i++)
1242 if (t->ignore[i] == c)
1244 return client_wants_tunnel(c, t);
1249 * Marks a client as uninterested in traffic from the tunnel, updating both
1250 * client and tunnel to reflect this.
1252 * @param c Client that doesn't want traffic anymore.
1253 * @param t Tunnel which should be ignored.
1255 * FIXME when to delete an incoming tunnel?
1258 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1260 struct GNUNET_HashCode hash;
1262 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1263 GNUNET_break (GNUNET_YES ==
1264 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1266 GNUNET_break (GNUNET_YES ==
1267 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1268 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1269 tunnel_delete_active_client (t, c);
1270 GNUNET_array_append (t->ignore, t->nignore, c);
1275 * Deletes a tunnel from a client (either owner or destination). To be used on
1276 * tunnel destroy, otherwise, use client_ignore_tunnel.
1278 * @param c Client whose tunnel to delete.
1279 * @param t Tunnel which should be deleted.
1282 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1284 struct GNUNET_HashCode hash;
1288 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1289 GNUNET_assert (GNUNET_YES ==
1290 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1296 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1298 GNUNET_assert (GNUNET_YES ==
1299 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1303 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1312 * Send the message to all clients that have subscribed to its type
1314 * @param msg Pointer to the message itself
1315 * @param payload Pointer to the payload of the message.
1316 * @return number of clients this message was sent to
1319 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1320 const struct GNUNET_MessageHeader *payload)
1322 struct GNUNET_PeerIdentity *oid;
1323 struct MeshClient *c;
1324 struct MeshTunnel *t;
1325 MESH_TunnelNumber *tid;
1328 char cbuf[htons (msg->size)];
1330 type = ntohs (payload->type);
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1334 memcpy (cbuf, msg, sizeof (cbuf));
1335 switch (htons (msg->type))
1337 struct GNUNET_MESH_Unicast *uc;
1338 struct GNUNET_MESH_Multicast *mc;
1339 struct GNUNET_MESH_ToOrigin *to;
1341 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1342 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1346 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1347 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1351 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1352 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1360 t = tunnel_get (oid, ntohl (*tid));
1367 for (count = 0, c = clients; c != NULL; c = c->next)
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1370 if (client_is_subscribed (type, c))
1372 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1376 *tid = htonl (t->local_tid);
1380 if (GNUNET_NO == client_knows_tunnel (c, t))
1382 /* This client doesn't know the tunnel */
1383 struct GNUNET_MESH_TunnelNotification tmsg;
1384 struct GNUNET_HashCode hash;
1386 tmsg.header.size = htons (sizeof (tmsg));
1387 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1388 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1389 tmsg.tunnel_id = htonl (t->local_tid_dest);
1390 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1391 &tmsg.header, GNUNET_NO);
1392 GNUNET_array_append (t->clients, t->nclients, c);
1393 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1395 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1396 c->incoming_tunnels, &hash, t,
1397 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1399 *tid = htonl (t->local_tid_dest);
1402 /* Check if the client wants to get traffic from the tunnel */
1403 if (GNUNET_NO == client_wants_tunnel(c, t))
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1407 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1408 (struct GNUNET_MessageHeader
1409 *) cbuf, GNUNET_YES);
1417 * Notify the client that owns the tunnel that a peer has connected to it
1418 * (the requested path to it has been confirmed).
1420 * @param t Tunnel whose owner to notify
1421 * @param id Short id of the peer that has connected
1424 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1426 struct GNUNET_MESH_PeerControl pc;
1428 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1429 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1430 pc.tunnel_id = htonl (t->local_tid);
1431 GNUNET_PEER_resolve (id, &pc.peer);
1432 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1438 * Notify all clients (not depending on registration status) that the incoming
1439 * tunnel is no longer valid.
1441 * @param t Tunnel that was destroyed.
1444 send_clients_tunnel_destroy (struct MeshTunnel *t)
1446 struct GNUNET_MESH_TunnelMessage msg;
1448 msg.header.size = htons (sizeof (msg));
1449 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1450 msg.tunnel_id = htonl (t->local_tid_dest);
1451 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1456 * Notify clients of tunnel disconnections, if needed.
1457 * In case the origin disconnects, the destination clients get a tunnel destroy
1458 * notification. If the last destination disconnects (only one remaining client
1459 * in tunnel), the origin gets a (local ID) peer disconnected.
1460 * Note that the function must be called BEFORE removing the client from
1463 * @param t Tunnel that was destroyed.
1464 * @param c Client that disconnected.
1467 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1473 struct GNUNET_MESH_TunnelMessage msg;
1475 msg.header.size = htons (sizeof (msg));
1476 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1477 msg.tunnel_id = htonl (t->local_tid_dest);
1478 for (i = 0; i < t->nclients; i++)
1479 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1480 &msg.header, GNUNET_NO);
1482 // FIXME when to disconnect an incoming tunnel?
1483 else if (1 == t->nclients && NULL != t->owner)
1485 struct GNUNET_MESH_PeerControl msg;
1487 msg.header.size = htons (sizeof (msg));
1488 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1489 msg.tunnel_id = htonl (t->local_tid);
1490 msg.peer = my_full_id;
1491 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1492 &msg.header, GNUNET_NO);
1498 * Decrements the reference counter and frees all resources if needed
1500 * @param mesh_data Data Descriptor used in a multicast message.
1501 * Freed no longer needed (last message).
1504 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1506 /* Make sure it's a multicast packet */
1507 GNUNET_assert (NULL != mesh_data->reference_counter);
1509 if (0 == --(*(mesh_data->reference_counter)))
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1512 if (NULL != mesh_data->task)
1514 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1516 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1518 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1520 GNUNET_free (mesh_data->task);
1522 GNUNET_free (mesh_data->reference_counter);
1523 GNUNET_free (mesh_data->data);
1524 GNUNET_free (mesh_data);
1530 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1531 * and insert it in the appropiate structures if the peer is not known yet.
1533 * @param peer Full identity of the peer.
1535 * @return Existing or newly created peer info.
1537 static struct MeshPeerInfo *
1538 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1540 struct MeshPeerInfo *peer_info;
1542 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1543 if (NULL == peer_info)
1546 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1547 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1548 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1549 peer_info->id = GNUNET_PEER_intern (peer);
1557 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1558 * and insert it in the appropiate structures if the peer is not known yet.
1560 * @param peer Short identity of the peer.
1562 * @return Existing or newly created peer info.
1564 static struct MeshPeerInfo *
1565 peer_info_get_short (const GNUNET_PEER_Id peer)
1567 struct GNUNET_PeerIdentity id;
1569 GNUNET_PEER_resolve (peer, &id);
1570 return peer_info_get (&id);
1575 * Iterator to remove the tunnel from the list of tunnels a peer participates
1578 * @param cls Closure (tunnel info)
1579 * @param key GNUNET_PeerIdentity of the peer (unused)
1580 * @param value PeerInfo of the peer
1582 * @return always GNUNET_YES, to keep iterating
1585 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1587 struct MeshTunnel *t = cls;
1588 struct MeshPeerInfo *peer = value;
1591 for (i = 0; i < peer->ntunnels; i++)
1594 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1597 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1598 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1607 * Queue and pass message to core when possible.
1609 * @param cls Closure (type dependant).
1610 * @param type Type of the message.
1611 * @param size Size of the message.
1612 * @param dst Neighbor to send message to.
1613 * @param t Tunnel this message belongs to.
1616 queue_add (void *cls, uint16_t type, size_t size,
1617 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1620 * Core callback to write a pre-constructed data packet to core buffer
1622 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1623 * @param size Number of bytes available in buf.
1624 * @param buf Where the to write the message.
1626 * @return number of bytes written to buf
1629 send_core_data_raw (void *cls, size_t size, void *buf)
1631 struct MeshTransmissionDescriptor *info = cls;
1632 struct GNUNET_MessageHeader *msg;
1635 GNUNET_assert (NULL != info);
1636 GNUNET_assert (NULL != info->mesh_data);
1637 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1638 total_size = ntohs (msg->size);
1640 if (total_size > size)
1645 memcpy (buf, msg, total_size);
1646 GNUNET_free (info->mesh_data);
1653 * Sends an already built unicast message to a peer, properly registrating
1654 * all used resources.
1656 * @param message Message to send. Function makes a copy of it.
1657 * @param peer Short ID of the neighbor whom to send the message.
1658 * @param t Tunnel on which this message is transmitted.
1661 send_message (const struct GNUNET_MessageHeader *message,
1662 const struct GNUNET_PeerIdentity *peer,
1663 struct MeshTunnel *t)
1665 struct MeshTransmissionDescriptor *info;
1666 struct MeshPeerInfo *neighbor;
1667 struct MeshPeerPath *p;
1670 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1672 size = ntohs (message->size);
1673 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1674 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1675 info->mesh_data->data = GNUNET_malloc (size);
1676 memcpy (info->mesh_data->data, message, size);
1677 info->mesh_data->data_len = size;
1678 neighbor = peer_info_get (peer);
1679 for (p = neighbor->path_head; NULL != p; p = p->next)
1689 GNUNET_free (info->mesh_data->data);
1690 GNUNET_free (info->mesh_data);
1694 info->peer = neighbor;
1696 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
1704 * Sends a CREATE PATH message for a path to a peer, properly registrating
1705 * all used resources.
1707 * @param peer PeerInfo of the final peer for whom this path is being created.
1708 * @param p Path itself.
1709 * @param t Tunnel for which the path is created.
1712 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1713 struct MeshTunnel *t)
1715 struct GNUNET_PeerIdentity id;
1716 struct MeshPathInfo *path_info;
1717 struct MeshPeerInfo *neighbor;
1723 p = tree_get_path_to_peer (t->tree, peer->id);
1730 for (i = 0; i < p->length; i++)
1732 if (p->peers[i] == myid)
1735 if (i >= p->length - 1)
1741 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1743 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1744 path_info->path = p;
1746 neighbor = peer_info_get (&id);
1747 path_info->peer = neighbor;
1748 queue_add (path_info,
1749 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1750 sizeof (struct GNUNET_MESH_ManipulatePath) +
1751 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1758 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1760 * @param t Tunnel whose path to destroy.
1761 * @param destination Short ID of the peer to whom the path to destroy.
1764 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1766 struct MeshPeerPath *p;
1769 p = tree_get_path_to_peer (t->tree, destination);
1775 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1776 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1778 struct GNUNET_MESH_ManipulatePath *msg;
1779 struct GNUNET_PeerIdentity *pi;
1783 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1784 msg->header.size = htons (size);
1785 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1786 msg->tid = htonl (t->id.tid);
1787 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1788 for (i = 0; i < p->length; i++)
1790 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1792 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1799 * Try to establish a new connection to this peer.
1800 * Use the best path for the given tunnel.
1801 * If the peer doesn't have any path to it yet, try to get one.
1802 * If the peer already has some path, send a CREATE PATH towards it.
1804 * @param peer PeerInfo of the peer.
1805 * @param t Tunnel for which to create the path, if possible.
1808 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1810 struct MeshPeerPath *p;
1811 struct MeshPathInfo *path_info;
1813 if (NULL != peer->path_head)
1815 p = tree_get_path_to_peer (t->tree, peer->id);
1822 // FIXME always send create path to self
1825 send_create_path (peer, p, t);
1829 struct GNUNET_HashCode hash;
1832 send_client_peer_connected (t, myid);
1833 t->local_tid_dest = next_local_tid++;
1834 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1837 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1838 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1845 else if (NULL == peer->dhtget)
1847 struct GNUNET_PeerIdentity id;
1849 GNUNET_PEER_resolve (peer->id, &id);
1850 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1851 path_info->peer = peer;
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1854 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1855 peer->dhtgetcls = path_info;
1856 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1857 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1858 &id.hashPubKey, /* key to search */
1859 DHT_REPLICATION_LEVEL, /* replication level */
1860 GNUNET_DHT_RO_RECORD_ROUTE |
1861 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1862 NULL, /* xquery */ // FIXME BLOOMFILTER
1863 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1864 &dht_get_id_handler, path_info);
1866 /* Otherwise, there is no path but the DHT get is already started. */
1871 * Task to delay the connection of a peer
1873 * @param cls Closure (path info with tunnel and peer to connect).
1874 * Will be free'd on exection.
1875 * @param tc TaskContext
1878 peer_info_connect_task (void *cls,
1879 const struct GNUNET_SCHEDULER_TaskContext *tc)
1881 struct MeshPathInfo *path_info = cls;
1883 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1888 peer_info_connect (path_info->peer, path_info->t);
1894 * Destroy the peer_info and free any allocated resources linked to it
1896 * @param pi The peer_info to destroy.
1898 * @return GNUNET_OK on success
1901 peer_info_destroy (struct MeshPeerInfo *pi)
1903 struct GNUNET_PeerIdentity id;
1904 struct MeshPeerPath *p;
1905 struct MeshPeerPath *nextp;
1907 GNUNET_PEER_resolve (pi->id, &id);
1908 GNUNET_PEER_change_rc (pi->id, -1);
1911 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1914 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1915 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1917 if (NULL != pi->dhtget)
1919 GNUNET_DHT_get_stop (pi->dhtget);
1920 GNUNET_free (pi->dhtgetcls);
1926 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1936 * Remove all paths that rely on a direct connection between p1 and p2
1937 * from the peer itself and notify all tunnels about it.
1939 * @param peer PeerInfo of affected peer.
1940 * @param p1 GNUNET_PEER_Id of one peer.
1941 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1944 * TODO: optimize (see below)
1947 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1950 struct MeshPeerPath *p;
1951 struct MeshPeerPath *aux;
1952 struct MeshPeerInfo *peer_d;
1954 unsigned int destroyed;
1960 p = peer->path_head;
1964 for (i = 0; i < (p->length - 1); i++)
1966 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1967 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1969 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1980 for (i = 0; i < peer->ntunnels; i++)
1982 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1986 * Problem: one or more peers have been deleted from the tunnel tree.
1987 * We don't know who they are to try to add them again.
1988 * We need to try to find a new path for each of the disconnected peers.
1989 * Some of them might already have a path to reach them that does not
1990 * involve p1 and p2. Adding all anew might render in a better tree than
1991 * the trivial immediate fix.
1993 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1994 * its children will be reachable trough him.
1996 peer_d = peer_info_get_short (d);
1999 for (p = peer_d->path_head; NULL != p; p = p->next)
2001 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2009 /* No callback, as peer will be already disconnected and a connection
2010 * scheduled by tunnel_notify_connection_broken.
2012 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2016 peer_info_connect (peer_d, peer->tunnels[i]);
2023 * Add the path to the peer and update the path used to reach it in case this
2026 * @param peer_info Destination peer to add the path to.
2027 * @param path New path to add. Last peer must be the peer in arg 1.
2028 * Path will be either used of freed if already known.
2029 * @param trusted Do we trust that this path is real?
2032 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2035 struct MeshPeerPath *aux;
2039 if ((NULL == peer_info) || (NULL == path))
2042 path_destroy (path);
2045 if (path->peers[path->length - 1] != peer_info->id)
2048 path_destroy (path);
2051 if (path->length <= 2 && GNUNET_NO == trusted)
2053 /* Only allow CORE to tell us about direct paths */
2054 path_destroy (path);
2057 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2058 for (l = 1; l < path->length; l++)
2060 if (path->peers[l] == myid)
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2063 for (l2 = 0; l2 < path->length - l; l2++)
2065 path->peers[l2] = path->peers[l + l2];
2070 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2075 struct GNUNET_PeerIdentity id;
2077 GNUNET_PEER_resolve (peer_info->id, &id);
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2079 path->length, GNUNET_i2s (&id));
2082 l = path_get_length (path);
2089 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2090 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2092 l2 = path_get_length (aux);
2095 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2096 peer_info->path_tail, aux, path);
2101 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2103 path_destroy (path);
2108 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2115 * Add the path to the origin peer and update the path used to reach it in case
2116 * this is the shortest.
2117 * The path is given in peer_info -> destination, therefore we turn the path
2118 * upside down first.
2120 * @param peer_info Peer to add the path to, being the origin of the path.
2121 * @param path New path to add after being inversed.
2122 * @param trusted Do we trust that this path is real?
2125 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2126 struct MeshPeerPath *path, int trusted)
2129 peer_info_add_path (peer_info, path, trusted);
2134 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2135 * to obtain a local peer -> destination path and interning the peer ids.
2137 * @return Newly allocated and created path
2139 static struct MeshPeerPath *
2140 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2141 unsigned int get_path_length,
2142 const struct GNUNET_PeerIdentity *put_path,
2143 unsigned int put_path_length)
2145 struct MeshPeerPath *p;
2151 GNUNET_PEER_change_rc (myid, 1);
2152 i = get_path_length;
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2154 for (i--; i >= 0; i--)
2156 id = GNUNET_PEER_intern (&get_path[i]);
2157 if (p->length > 0 && id == p->peers[p->length - 1])
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2160 GNUNET_PEER_change_rc (id, -1);
2164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2165 GNUNET_i2s (&get_path[i]));
2167 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2168 p->peers[p->length - 1] = id;
2171 i = put_path_length;
2172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2173 for (i--; i >= 0; i--)
2175 id = GNUNET_PEER_intern (&put_path[i]);
2178 /* PUT path went through us, so discard the path up until now and start
2179 * from here to get a much shorter (and loop-free) path.
2184 if (p->length > 0 && id == p->peers[p->length - 1])
2186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2187 GNUNET_PEER_change_rc (id, -1);
2191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2192 GNUNET_i2s (&put_path[i]));
2194 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2195 p->peers[p->length - 1] = id;
2199 if (get_path_length > 0)
2200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2201 GNUNET_i2s (&get_path[0]));
2202 if (put_path_length > 0)
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2204 GNUNET_i2s (&put_path[0]));
2205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2207 for (i = 0; i < p->length; i++)
2209 struct GNUNET_PeerIdentity peer_id;
2211 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2213 GNUNET_i2s (&peer_id));
2221 * Adds a path to the peer_infos of all the peers in the path
2223 * @param p Path to process.
2224 * @param confirmed Whether we know if the path works or not. FIXME use
2227 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2231 /* TODO: invert and add */
2232 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2233 for (i++; i < p->length; i++)
2235 struct MeshPeerInfo *aux;
2236 struct MeshPeerPath *copy;
2238 aux = peer_info_get_short (p->peers[i]);
2239 copy = path_duplicate (p);
2240 copy->length = i + 1;
2241 peer_info_add_path (aux, copy, GNUNET_NO);
2247 * Send keepalive packets for a peer
2249 * @param cls Closure (tunnel for which to send the keepalive).
2250 * @param tc Notification context.
2252 * TODO: implement explicit multicast keepalive?
2255 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2259 * Search for a tunnel among the incoming tunnels
2261 * @param tid the local id of the tunnel
2263 * @return tunnel handler, NULL if doesn't exist
2265 static struct MeshTunnel *
2266 tunnel_get_incoming (MESH_TunnelNumber tid)
2268 struct GNUNET_HashCode hash;
2270 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2271 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2272 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2277 * Search for a tunnel among the tunnels for a client
2279 * @param c the client whose tunnels to search in
2280 * @param tid the local id of the tunnel
2282 * @return tunnel handler, NULL if doesn't exist
2284 static struct MeshTunnel *
2285 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2287 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2289 return tunnel_get_incoming (tid);
2293 struct GNUNET_HashCode hash;
2295 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2296 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2302 * Search for a tunnel by global ID using PEER_ID
2304 * @param pi owner of the tunnel
2305 * @param tid global tunnel number
2307 * @return tunnel handler, NULL if doesn't exist
2309 static struct MeshTunnel *
2310 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2312 struct MESH_TunnelID id;
2313 struct GNUNET_HashCode hash;
2318 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2319 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2324 * Search for a tunnel by global ID using full PeerIdentities
2326 * @param oid owner of the tunnel
2327 * @param tid global tunnel number
2329 * @return tunnel handler, NULL if doesn't exist
2331 static struct MeshTunnel *
2332 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2334 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2339 * Delete an active client from the tunnel.
2345 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2349 for (i = 0; i < t->nclients; i++)
2351 if (t->clients[i] == c)
2353 t->clients[i] = t->clients[t->nclients - 1];
2354 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2362 * Delete an ignored client from the tunnel.
2368 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2372 for (i = 0; i < t->nignore; i++)
2374 if (t->ignore[i] == c)
2376 t->ignore[i] = t->ignore[t->nignore - 1];
2377 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2385 * Delete a client from the tunnel. It should be only done on
2386 * client disconnection, otherwise use client_ignore_tunnel.
2392 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2394 tunnel_delete_ignored_client (t, c);
2395 tunnel_delete_active_client (t, c);
2400 * Callback used to notify a client owner of a tunnel that a peer has
2401 * disconnected, most likely because of a path change.
2403 * @param cls Closure (tunnel this notification is about).
2404 * @param peer_id Short ID of disconnected peer.
2407 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2409 struct MeshTunnel *t = cls;
2410 struct MeshPeerInfo *peer;
2411 struct MeshPathInfo *path_info;
2413 if (NULL != t->owner && NULL != nc)
2415 struct GNUNET_MESH_PeerControl msg;
2417 msg.header.size = htons (sizeof (msg));
2418 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2419 msg.tunnel_id = htonl (t->local_tid);
2420 GNUNET_PEER_resolve (peer_id, &msg.peer);
2421 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2422 &msg.header, GNUNET_NO);
2424 peer = peer_info_get_short (peer_id);
2425 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2426 path_info->peer = peer;
2428 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2433 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2434 * needed rescources.
2435 * If peer already exists, reevaluate shortest path and change if different.
2437 * @param t Tunnel we want to add a new peer to
2438 * @param peer PeerInfo of the peer being added
2442 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2444 struct GNUNET_PeerIdentity id;
2445 struct MeshPeerPath *best_p;
2446 struct MeshPeerPath *p;
2447 unsigned int best_cost;
2450 GNUNET_PEER_resolve (peer->id, &id);
2452 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2455 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2456 GNUNET_assert (GNUNET_OK ==
2457 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2459 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2462 if (NULL != (p = peer->path_head))
2465 best_cost = tree_get_path_cost (t->tree, p);
2468 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2475 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2476 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2477 t->path_refresh_task =
2478 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2482 /* Start a DHT get */
2483 peer_info_connect (peer, t);
2488 * Add a path to a tunnel which we don't own, just to remember the next hop.
2489 * If destination node was already in the tunnel, the first hop information
2490 * will be replaced with the new path.
2492 * @param t Tunnel we want to add a new peer to
2493 * @param p Path to add
2494 * @param own_pos Position of local node in path.
2498 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2499 unsigned int own_pos)
2501 struct GNUNET_PeerIdentity id;
2503 GNUNET_assert (0 != own_pos);
2504 tree_add_path (t->tree, p, NULL, NULL);
2505 if (own_pos < p->length - 1)
2507 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2508 tree_update_first_hops (t->tree, myid, &id);
2514 * Notifies a tunnel that a connection has broken that affects at least
2515 * some of its peers. Sends a notification towards the root of the tree.
2516 * In case the peer is the owner of the tree, notifies the client that owns
2517 * the tunnel and tries to reconnect.
2519 * @param t Tunnel affected.
2520 * @param p1 Peer that got disconnected from p2.
2521 * @param p2 Peer that got disconnected from p1.
2523 * @return Short ID of the peer disconnected (either p1 or p2).
2524 * 0 if the tunnel remained unaffected.
2526 static GNUNET_PEER_Id
2527 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2533 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2535 if (myid != p1 && myid != p2)
2541 if (tree_get_predecessor (t->tree) != 0)
2543 /* We are the peer still connected, notify owner of the disconnection. */
2544 struct GNUNET_MESH_PathBroken msg;
2545 struct GNUNET_PeerIdentity neighbor;
2547 msg.header.size = htons (sizeof (msg));
2548 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2549 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2550 msg.tid = htonl (t->id.tid);
2551 msg.peer1 = my_full_id;
2552 GNUNET_PEER_resolve (pid, &msg.peer2);
2553 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2554 send_message (&msg.header, &neighbor, t);
2562 * Send a multicast packet to a neighbor.
2564 * @param cls Closure (Info about the multicast packet)
2565 * @param neighbor_id Short ID of the neighbor to send the packet to.
2568 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2570 struct MeshData *mdata = cls;
2571 struct MeshTransmissionDescriptor *info;
2572 struct GNUNET_PeerIdentity neighbor;
2574 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2576 info->mesh_data = mdata;
2577 (*(mdata->reference_counter)) ++;
2578 info->destination = neighbor_id;
2579 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2581 GNUNET_i2s (&neighbor));
2582 info->peer = peer_info_get (&neighbor);
2583 GNUNET_assert (NULL != info->peer);
2585 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2586 info->mesh_data->data_len,
2592 * Send a message in a tunnel in multicast, sending a copy to each child node
2593 * down the local one in the tunnel tree.
2595 * @param t Tunnel in which to send the data.
2596 * @param msg Message to be sent.
2597 * @param internal Has the service generated this message?
2600 tunnel_send_multicast (struct MeshTunnel *t,
2601 const struct GNUNET_MessageHeader *msg,
2604 struct MeshData *mdata;
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 " sending a multicast packet...\n");
2608 mdata = GNUNET_malloc (sizeof (struct MeshData));
2609 mdata->data_len = ntohs (msg->size);
2610 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2612 mdata->data = GNUNET_malloc (mdata->data_len);
2613 memcpy (mdata->data, msg, mdata->data_len);
2614 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2616 struct GNUNET_MESH_Multicast *mcast;
2618 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2619 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2621 ntohl (mcast->ttl));
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2627 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2628 && GNUNET_NO == internal)
2630 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2632 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2638 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2639 if (*(mdata->reference_counter) == 0)
2641 GNUNET_free (mdata->data);
2642 GNUNET_free (mdata->reference_counter);
2643 if (NULL != mdata->task)
2645 GNUNET_SCHEDULER_cancel(*(mdata->task));
2646 GNUNET_free (mdata->task);
2647 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2649 // FIXME change order?
2650 GNUNET_free (mdata);
2652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2653 " sending a multicast packet done\n");
2659 * Send a message to all peers in this tunnel that the tunnel is no longer
2662 * @param t The tunnel whose peers to notify.
2665 tunnel_send_destroy (struct MeshTunnel *t)
2667 struct GNUNET_MESH_TunnelDestroy msg;
2669 msg.header.size = htons (sizeof (msg));
2670 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2671 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2672 msg.tid = htonl (t->id.tid);
2673 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2679 * Destroy the tunnel and free any allocated resources linked to it.
2681 * @param t the tunnel to destroy
2683 * @return GNUNET_OK on success
2686 tunnel_destroy (struct MeshTunnel *t)
2688 struct MeshClient *c;
2689 struct GNUNET_HashCode hash;
2700 struct GNUNET_PeerIdentity id;
2702 GNUNET_PEER_resolve (t->id.oid, &id);
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2704 GNUNET_i2s (&id), t->id.tid);
2706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2710 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2711 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2716 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2719 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2723 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2724 for (i = 0; i < t->nclients; i++)
2728 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2733 for (i = 0; i < t->nignore; i++)
2737 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2742 if (t->nclients > 0)
2745 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2749 GNUNET_free (t->clients);
2751 if (NULL != t->peers)
2753 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2755 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2758 tree_destroy (t->tree);
2759 if (NULL != t->regex_ctx)
2760 regex_cancel_search (t->regex_ctx);
2761 if (NULL != t->dht_get_type)
2762 GNUNET_DHT_get_stop (t->dht_get_type);
2763 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2764 GNUNET_SCHEDULER_cancel (t->timeout_task);
2765 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2766 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2773 * Create a new tunnel
2775 * @param owner Who is the owner of the tunnel (short ID).
2776 * @param tid Tunnel Number of the tunnel.
2777 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2778 * @param local Tunnel Number for the tunnel, for the client point of view.
2781 static struct MeshTunnel *
2782 tunnel_new (GNUNET_PEER_Id owner,
2783 MESH_TunnelNumber tid,
2784 struct MeshClient *client,
2785 MESH_TunnelNumber local)
2787 struct MeshTunnel *t;
2788 struct GNUNET_HashCode hash;
2790 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2793 t->queue_max = 1000; // FIXME API parameter
2794 t->tree = tree_new (owner);
2796 t->local_tid = local;
2798 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2800 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2801 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2806 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2812 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2814 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2815 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2819 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2829 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2830 * that are no longer needed, as well as nodes of no longer reachable peers.
2831 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2833 * @param t Tunnel from which to remove the path.
2834 * @param peer Short id of the peer which should be removed.
2837 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2839 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2845 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2846 * client when the client disconnects. If the client is not the owner, the
2847 * owner will get notified if no more clients are in the tunnel and the client
2848 * get removed from the tunnel's list.
2850 * @param cls closure (client that is disconnecting)
2851 * @param key the hash of the local tunnel id (used to access the hashmap)
2852 * @param value the value stored at the key (tunnel to destroy)
2854 * @return GNUNET_OK on success
2857 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
2859 struct MeshTunnel *t = value;
2860 struct MeshClient *c = cls;
2863 send_client_tunnel_disconnect(t, c);
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2867 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2868 tunnel_delete_client(t, c);
2869 client_delete_tunnel(c, t);
2872 tunnel_send_destroy(t);
2873 r = tunnel_destroy (t);
2879 * Timeout function, destroys tunnel if called
2881 * @param cls Closure (tunnel to destroy).
2882 * @param tc TaskContext
2885 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2887 struct MeshTunnel *t = cls;
2889 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2891 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2896 * Resets the tunnel timeout. Starts it if no timeout was running.
2898 * @param t Tunnel whose timeout to reset.
2901 tunnel_reset_timeout (struct MeshTunnel *t)
2903 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2904 GNUNET_SCHEDULER_cancel (t->timeout_task);
2906 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2907 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2911 /******************************************************************************/
2912 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2913 /******************************************************************************/
2916 * Function to send a create path packet to a peer.
2918 * @param cls closure
2919 * @param size number of bytes available in buf
2920 * @param buf where the callee should write the message
2921 * @return number of bytes written to buf
2924 send_core_path_create (void *cls, size_t size, void *buf)
2926 struct MeshPathInfo *info = cls;
2927 struct GNUNET_MESH_ManipulatePath *msg;
2928 struct GNUNET_PeerIdentity *peer_ptr;
2929 struct MeshTunnel *t = info->t;
2930 struct MeshPeerPath *p = info->path;
2934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2936 sizeof (struct GNUNET_MESH_ManipulatePath) +
2937 p->length * sizeof (struct GNUNET_PeerIdentity);
2939 if (size < size_needed || NULL == buf)
2944 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2945 msg->header.size = htons (size_needed);
2946 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2947 msg->tid = ntohl (t->id.tid);
2949 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2950 for (i = 0; i < p->length; i++)
2952 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2959 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2965 * Fill the core buffer
2967 * @param cls closure (data itself)
2968 * @param size number of bytes available in buf
2969 * @param buf where the callee should write the message
2971 * @return number of bytes written to buf
2974 send_core_data_multicast (void *cls, size_t size, void *buf)
2976 struct MeshTransmissionDescriptor *info = cls;
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2980 GNUNET_assert (NULL != info);
2981 GNUNET_assert (NULL != info->peer);
2982 total_size = info->mesh_data->data_len;
2983 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2985 if (total_size > size)
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2991 memcpy (buf, info->mesh_data->data, total_size);
2994 struct GNUNET_MESH_Multicast *mc;
2995 struct GNUNET_MessageHeader *mh;
2998 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3000 mc = (struct GNUNET_MESH_Multicast *) mh;
3001 mh = (struct GNUNET_MessageHeader *) &mc[1];
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3003 " multicast, payload type %u\n", ntohs (mh->type));
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3005 " multicast, payload size %u\n", ntohs (mh->size));
3009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3014 data_descriptor_decrement_multicast (info->mesh_data);
3015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3023 * Creates a path ack message in buf and frees all unused resources.
3025 * @param cls closure (MeshTransmissionDescriptor)
3026 * @param size number of bytes available in buf
3027 * @param buf where the callee should write the message
3028 * @return number of bytes written to buf
3031 send_core_path_ack (void *cls, size_t size, void *buf)
3033 struct MeshTransmissionDescriptor *info = cls;
3034 struct GNUNET_MESH_PathACK *msg = buf;
3036 GNUNET_assert (NULL != info);
3037 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3042 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3043 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3044 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3045 msg->tid = htonl (info->origin->tid);
3046 msg->peer_id = my_full_id;
3049 /* TODO add signature */
3051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3052 return sizeof (struct GNUNET_MESH_PathACK);
3057 * Free a transmission that was already queued with all resources
3058 * associated to the request.
3060 * @param queue Queue handler to cancel.
3061 * @param clear_cls Is it necessary to free associated cls?
3064 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3066 struct MeshTransmissionDescriptor *dd;
3067 struct MeshPathInfo *path_info;
3069 if (GNUNET_YES == clear_cls)
3071 switch (queue->type)
3073 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3074 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
3077 data_descriptor_decrement_multicast (dd->mesh_data);
3079 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3081 path_info = queue->cls;
3082 path_destroy (path_info->path);
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
3088 GNUNET_free_non_null (queue->cls);
3090 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3091 queue->peer->queue_tail,
3093 GNUNET_free (queue);
3098 * Core callback to write a queued packet to core buffer
3100 * @param cls Closure (peer info).
3101 * @param size Number of bytes available in buf.
3102 * @param buf Where the to write the message.
3104 * @return number of bytes written to buf
3107 queue_send (void *cls, size_t size, void *buf)
3109 struct MeshPeerInfo *peer = cls;
3110 struct MeshPeerQueue *queue;
3113 peer->core_transmit = NULL;
3114 queue = peer->queue_head;
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3118 /* If queue is empty, send should have been cancelled */
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3126 /* Check if buffer size is enough for the message */
3127 if (queue->size > size)
3129 struct GNUNET_PeerIdentity id;
3131 GNUNET_PEER_resolve (peer->id, &id);
3132 peer->core_transmit =
3133 GNUNET_CORE_notify_transmit_ready(core_handle,
3136 GNUNET_TIME_UNIT_FOREVER_REL,
3143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3146 switch (queue->type)
3148 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
3150 data_size = send_core_data_raw (queue->cls, size, buf);
3152 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
3154 data_size = send_core_data_multicast(queue->cls, size, buf);
3156 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3158 data_size = send_core_path_create(queue->cls, size, buf);
3160 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3162 data_size = send_core_path_ack(queue->cls, size, buf);
3166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
3169 queue->tunnel->queue_n--;
3171 /* Free queue, but cls was freed by send_core_* */
3172 queue_destroy(queue, GNUNET_NO);
3174 /* If more data in queue, send next */
3175 if (NULL != peer->queue_head)
3177 struct GNUNET_PeerIdentity id;
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3180 GNUNET_PEER_resolve (peer->id, &id);
3181 peer->core_transmit =
3182 GNUNET_CORE_notify_transmit_ready(core_handle,
3185 GNUNET_TIME_UNIT_FOREVER_REL,
3187 peer->queue_head->size,
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3197 * Queue and pass message to core when possible.
3199 * @param cls Closure (type dependant).
3200 * @param type Type of the message.
3201 * @param size Size of the message.
3202 * @param dst Neighbor to send message to.
3203 * @param t Tunnel this message belongs to.
3206 queue_add (void *cls, uint16_t type, size_t size,
3207 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3209 struct MeshPeerQueue *queue;
3211 if (t->queue_n >= t->queue_max)
3213 if (NULL == t->owner)
3214 GNUNET_break_op(0); // TODO: kill connection?
3217 return; // Drop message
3220 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3226 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3227 if (NULL == dst->core_transmit)
3229 struct GNUNET_PeerIdentity id;
3231 GNUNET_PEER_resolve (dst->id, &id);
3232 dst->core_transmit =
3233 GNUNET_CORE_notify_transmit_ready(core_handle,
3236 GNUNET_TIME_UNIT_FOREVER_REL,
3245 /******************************************************************************/
3246 /******************** MESH NETWORK HANDLERS **************************/
3247 /******************************************************************************/
3251 * Core handler for path creation
3253 * @param cls closure
3254 * @param message message
3255 * @param peer peer identity this notification is about
3256 * @param atsi performance data
3257 * @param atsi_count number of records in 'atsi'
3259 * @return GNUNET_OK to keep the connection open,
3260 * GNUNET_SYSERR to close it (signal serious error)
3263 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3264 const struct GNUNET_MessageHeader *message,
3265 const struct GNUNET_ATS_Information *atsi,
3266 unsigned int atsi_count)
3268 unsigned int own_pos;
3271 MESH_TunnelNumber tid;
3272 struct GNUNET_MESH_ManipulatePath *msg;
3273 struct GNUNET_PeerIdentity *pi;
3274 struct GNUNET_HashCode hash;
3275 struct MeshPeerPath *path;
3276 struct MeshPeerInfo *dest_peer_info;
3277 struct MeshPeerInfo *orig_peer_info;
3278 struct MeshTunnel *t;
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3281 "Received a path create msg [%s]\n",
3282 GNUNET_i2s (&my_full_id));
3283 size = ntohs (message->size);
3284 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3286 GNUNET_break_op (0);
3290 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3291 if (size % sizeof (struct GNUNET_PeerIdentity))
3293 GNUNET_break_op (0);
3296 size /= sizeof (struct GNUNET_PeerIdentity);
3299 GNUNET_break_op (0);
3302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3303 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3305 tid = ntohl (msg->tid);
3306 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3308 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3309 t = tunnel_get (pi, tid);
3310 if (NULL == t) // FIXME only for INCOMING tunnels?
3312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3313 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3315 while (NULL != tunnel_get_incoming (next_local_tid))
3316 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3317 t->local_tid_dest = next_local_tid++;
3318 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3320 tunnel_reset_timeout (t);
3321 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3323 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3324 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3332 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3333 if (NULL == dest_peer_info)
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3336 " Creating PeerInfo for destination.\n");
3337 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3338 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3339 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3343 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3344 if (NULL == orig_peer_info)
3346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3347 " Creating PeerInfo for origin.\n");
3348 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3349 orig_peer_info->id = GNUNET_PEER_intern (pi);
3350 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3351 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3354 path = path_new (size);
3356 for (i = 0; i < size; i++)
3358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3359 GNUNET_i2s (&pi[i]));
3360 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3361 if (path->peers[i] == myid)
3364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3367 /* cannot be self, must be 'not found' */
3368 /* create path: self not found in path through self */
3369 GNUNET_break_op (0);
3370 path_destroy (path);
3371 /* FIXME error. destroy tunnel? leave for timeout? */
3374 path_add_to_peers (path, GNUNET_NO);
3375 tunnel_add_path (t, path, own_pos);
3376 if (own_pos == size - 1)
3378 /* It is for us! Send ack. */
3379 struct MeshTransmissionDescriptor *info;
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3382 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3383 if (NULL == t->peers)
3385 /* New tunnel! Notify clients on data. */
3386 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
3388 GNUNET_break (GNUNET_OK ==
3389 GNUNET_CONTAINER_multihashmap_put (t->peers,
3390 &my_full_id.hashPubKey,
3393 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
3394 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3395 info->origin = &t->id;
3396 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3397 GNUNET_assert (NULL != info->peer);
3399 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3400 sizeof (struct GNUNET_MESH_PathACK),
3406 struct MeshPeerPath *path2;
3408 /* It's for somebody else! Retransmit. */
3409 path2 = path_duplicate (path);
3410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3411 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3412 path2 = path_duplicate (path);
3413 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3414 send_create_path (dest_peer_info, path, t);
3421 * Core handler for path destruction
3423 * @param cls closure
3424 * @param message message
3425 * @param peer peer identity this notification is about
3426 * @param atsi performance data
3427 * @param atsi_count number of records in 'atsi'
3429 * @return GNUNET_OK to keep the connection open,
3430 * GNUNET_SYSERR to close it (signal serious error)
3433 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3434 const struct GNUNET_MessageHeader *message,
3435 const struct GNUNET_ATS_Information *atsi,
3436 unsigned int atsi_count)
3438 struct GNUNET_MESH_ManipulatePath *msg;
3439 struct GNUNET_PeerIdentity *pi;
3440 struct MeshPeerPath *path;
3441 struct MeshTunnel *t;
3442 unsigned int own_pos;
3446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3447 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3448 size = ntohs (message->size);
3449 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3451 GNUNET_break_op (0);
3455 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3456 if (size % sizeof (struct GNUNET_PeerIdentity))
3458 GNUNET_break_op (0);
3461 size /= sizeof (struct GNUNET_PeerIdentity);
3464 GNUNET_break_op (0);
3467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3469 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3470 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3472 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3474 t = tunnel_get (pi, ntohl (msg->tid));
3477 /* TODO notify back: we don't know this tunnel */
3478 GNUNET_break_op (0);
3481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3482 path = path_new (size);
3484 for (i = 0; i < size; i++)
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3487 GNUNET_i2s (&pi[i]));
3488 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3489 if (path->peers[i] == myid)
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3493 if (own_pos < path->length - 1)
3494 send_message (message, &pi[own_pos + 1], t);
3496 send_client_tunnel_disconnect(t, NULL);
3498 tunnel_delete_peer (t, path->peers[path->length - 1]);
3499 path_destroy (path);
3505 * Core handler for notifications of broken paths
3507 * @param cls closure
3508 * @param message message
3509 * @param peer peer identity this notification is about
3510 * @param atsi performance data
3511 * @param atsi_count number of records in 'atsi'
3513 * @return GNUNET_OK to keep the connection open,
3514 * GNUNET_SYSERR to close it (signal serious error)
3517 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3518 const struct GNUNET_MessageHeader *message,
3519 const struct GNUNET_ATS_Information *atsi,
3520 unsigned int atsi_count)
3522 struct GNUNET_MESH_PathBroken *msg;
3523 struct MeshTunnel *t;
3525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3526 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3527 msg = (struct GNUNET_MESH_PathBroken *) message;
3528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3529 GNUNET_i2s (&msg->peer1));
3530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3531 GNUNET_i2s (&msg->peer2));
3532 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3535 GNUNET_break_op (0);
3538 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3539 GNUNET_PEER_search (&msg->peer2));
3546 * Core handler for tunnel destruction
3548 * @param cls closure
3549 * @param message message
3550 * @param peer peer identity this notification is about
3551 * @param atsi performance data
3552 * @param atsi_count number of records in 'atsi'
3554 * @return GNUNET_OK to keep the connection open,
3555 * GNUNET_SYSERR to close it (signal serious error)
3558 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3559 const struct GNUNET_MessageHeader *message,
3560 const struct GNUNET_ATS_Information *atsi,
3561 unsigned int atsi_count)
3563 struct GNUNET_MESH_TunnelDestroy *msg;
3564 struct MeshTunnel *t;
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3568 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3570 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3571 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3574 /* Probably already got the message from another path,
3575 * destroyed the tunnel and retransmitted to children.
3580 if (t->id.oid == myid)
3582 GNUNET_break_op (0);
3585 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3587 /* Tunnel was incoming, notify clients */
3588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3589 t->local_tid, t->local_tid_dest);
3590 send_clients_tunnel_destroy (t);
3592 tunnel_send_destroy (t);
3599 * Core handler for mesh network traffic going from the origin to a peer
3601 * @param cls closure
3602 * @param peer peer identity this notification is about
3603 * @param message message
3604 * @param atsi performance data
3605 * @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_data_unicast (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 struct GNUNET_MESH_Unicast *msg;
3616 struct MeshTunnel *t;
3620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3622 size = ntohs (message->size);
3624 sizeof (struct GNUNET_MESH_Unicast) +
3625 sizeof (struct GNUNET_MessageHeader))
3630 msg = (struct GNUNET_MESH_Unicast *) message;
3631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3632 ntohs (msg[1].header.type));
3633 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3636 /* TODO notify back: we don't know this tunnel */
3637 GNUNET_break_op (0);
3640 tunnel_reset_timeout (t);
3641 pid = GNUNET_PEER_search (&msg->destination);
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3645 " it's for us! sending to clients...\n");
3646 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3650 " not for us, retransmitting...\n");
3651 send_message (message, tree_get_first_hop (t->tree, pid), t);
3657 * Core handler for mesh network traffic going from the origin to all peers
3659 * @param cls closure
3660 * @param message message
3661 * @param peer peer identity this notification is about
3662 * @param atsi performance data
3663 * @param atsi_count number of records in 'atsi'
3664 * @return GNUNET_OK to keep the connection open,
3665 * GNUNET_SYSERR to close it (signal serious error)
3667 * TODO: Check who we got this from, to validate route.
3670 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3671 const struct GNUNET_MessageHeader *message,
3672 const struct GNUNET_ATS_Information *atsi,
3673 unsigned int atsi_count)
3675 struct GNUNET_MESH_Multicast *msg;
3676 struct MeshTunnel *t;
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3681 size = ntohs (message->size);
3682 if (sizeof (struct GNUNET_MESH_Multicast) +
3683 sizeof (struct GNUNET_MessageHeader) > size)
3685 GNUNET_break_op (0);
3688 msg = (struct GNUNET_MESH_Multicast *) message;
3689 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3693 /* TODO notify that we dont know that tunnel */
3694 GNUNET_break_op (0);
3697 if (t->mid == ntohl (msg->mid))
3699 /* FIXME: already seen this packet, log dropping */
3700 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3701 " Already seen mid %u, DROPPING!\n", t->mid);
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3707 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3709 t->mid = ntohl (msg->mid);
3710 tunnel_reset_timeout (t);
3712 /* Transmit to locally interested clients */
3713 if (NULL != t->peers &&
3714 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3716 send_subscribed_clients (message, &msg[1].header);
3718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3719 if (ntohl (msg->ttl) == 0)
3721 /* FIXME: ttl is 0, log dropping */
3722 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3725 tunnel_send_multicast (t, message, GNUNET_NO);
3731 * Core handler for mesh network traffic toward the owner of a tunnel
3733 * @param cls closure
3734 * @param message message
3735 * @param peer peer identity this notification is about
3736 * @param atsi performance data
3737 * @param atsi_count number of records in 'atsi'
3739 * @return GNUNET_OK to keep the connection open,
3740 * GNUNET_SYSERR to close it (signal serious error)
3743 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3744 const struct GNUNET_MessageHeader *message,
3745 const struct GNUNET_ATS_Information *atsi,
3746 unsigned int atsi_count)
3748 struct GNUNET_MESH_ToOrigin *msg;
3749 struct GNUNET_PeerIdentity id;
3750 struct MeshPeerInfo *peer_info;
3751 struct MeshTunnel *t;
3754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3756 size = ntohs (message->size);
3757 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3758 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3760 GNUNET_break_op (0);
3763 msg = (struct GNUNET_MESH_ToOrigin *) message;
3764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3765 ntohs (msg[1].header.type));
3766 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3770 /* TODO notify that we dont know this tunnel (whom)? */
3771 GNUNET_break_op (0);
3775 if (t->id.oid == myid)
3778 struct GNUNET_MESH_ToOrigin *copy;
3780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3781 " it's for us! sending to clients...\n");
3782 if (NULL == t->owner)
3784 /* got data packet for ownerless tunnel */
3785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3786 GNUNET_break_op (0);
3789 /* TODO signature verification */
3790 memcpy (cbuf, message, size);
3791 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3792 copy->tid = htonl (t->local_tid);
3793 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3794 ©->header, GNUNET_YES);
3797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3798 " not for us, retransmitting...\n");
3800 peer_info = peer_info_get (&msg->oid);
3801 if (NULL == peer_info)
3803 /* unknown origin of tunnel */
3807 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3808 send_message (message, &id, t);
3815 * Core handler for path ACKs
3817 * @param cls closure
3818 * @param message message
3819 * @param peer peer identity this notification is about
3820 * @param atsi performance data
3821 * @param atsi_count number of records in 'atsi'
3823 * @return GNUNET_OK to keep the connection open,
3824 * GNUNET_SYSERR to close it (signal serious error)
3827 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3828 const struct GNUNET_MessageHeader *message,
3829 const struct GNUNET_ATS_Information *atsi,
3830 unsigned int atsi_count)
3832 struct GNUNET_MESH_PathACK *msg;
3833 struct GNUNET_PeerIdentity id;
3834 struct MeshPeerInfo *peer_info;
3835 struct MeshPeerPath *p;
3836 struct MeshTunnel *t;
3838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3839 GNUNET_i2s (&my_full_id));
3840 msg = (struct GNUNET_MESH_PathACK *) message;
3841 t = tunnel_get (&msg->oid, msg->tid);
3844 /* TODO notify that we don't know the tunnel */
3848 peer_info = peer_info_get (&msg->peer_id);
3850 /* Add paths to peers? */
3851 p = tree_get_path_to_peer (t->tree, peer_info->id);
3854 path_add_to_peers (p, GNUNET_YES);
3862 /* Message for us? */
3863 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3866 if (NULL == t->owner)
3868 GNUNET_break_op (0);
3871 if (NULL != t->dht_get_type)
3873 GNUNET_DHT_get_stop (t->dht_get_type);
3874 t->dht_get_type = NULL;
3876 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3878 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3879 send_client_peer_connected (t, peer_info->id);
3884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3885 " not for us, retransmitting...\n");
3886 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3887 peer_info = peer_info_get (&msg->oid);
3888 if (NULL == peer_info)
3890 /* If we know the tunnel, we should DEFINITELY know the peer */
3894 send_message (message, &id, t);
3900 * Functions to handle messages from core
3902 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3903 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3904 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3905 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3906 sizeof (struct GNUNET_MESH_PathBroken)},
3907 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3908 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3909 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3910 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3911 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3912 sizeof (struct GNUNET_MESH_PathACK)},
3918 /******************************************************************************/
3919 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3920 /******************************************************************************/
3923 * deregister_app: iterator for removing each application registered by a client
3925 * @param cls closure
3926 * @param key the hash of the application id (used to access the hashmap)
3927 * @param value the value stored at the key (client)
3929 * @return GNUNET_OK on success
3932 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
3934 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
3935 GNUNET_break (GNUNET_YES ==
3936 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
3942 * notify_client_connection_failure: notify a client that the connection to the
3943 * requested remote peer is not possible (for instance, no route found)
3944 * Function called when the socket is ready to queue more data. "buf" will be
3945 * NULL and "size" zero if the socket was closed for writing in the meantime.
3947 * @param cls closure
3948 * @param size number of bytes available in buf
3949 * @param buf where the callee should write the message
3950 * @return number of bytes written to buf
3953 notify_client_connection_failure (void *cls, size_t size, void *buf)
3956 struct MeshPeerInfo *peer_info;
3957 struct GNUNET_MESH_PeerControl *msg;
3958 struct GNUNET_PeerIdentity id;
3960 if (0 == size && NULL == buf)
3962 // TODO retry? cancel?
3966 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3967 peer_info = (struct MeshPeerInfo *) cls;
3968 msg = (struct GNUNET_MESH_PeerControl *) buf;
3969 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3970 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3971 // msg->tunnel_id = htonl(peer_info->t->tid);
3972 GNUNET_PEER_resolve (peer_info->id, &id);
3973 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3981 * Send keepalive packets for a peer
3983 * @param cls Closure (tunnel for which to send the keepalive).
3984 * @param tc Notification context.
3986 * TODO: implement explicit multicast keepalive?
3989 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3991 struct MeshTunnel *t = cls;
3992 struct GNUNET_MessageHeader *payload;
3993 struct GNUNET_MESH_Multicast *msg;
3995 sizeof (struct GNUNET_MESH_Multicast) +
3996 sizeof (struct GNUNET_MessageHeader);
3999 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4003 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4006 "sending keepalive for tunnel %d\n", t->id.tid);
4008 msg = (struct GNUNET_MESH_Multicast *) cbuf;
4009 msg->header.size = htons (size);
4010 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4011 msg->oid = my_full_id;
4012 msg->tid = htonl (t->id.tid);
4013 msg->ttl = htonl (DEFAULT_TTL);
4014 msg->mid = htonl (t->mid + 1);
4016 payload = (struct GNUNET_MessageHeader *) &msg[1];
4017 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
4018 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4019 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
4021 t->path_refresh_task =
4022 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
4028 * Function to process paths received for a new peer addition. The recorded
4029 * paths form the initial tunnel, which can be optimized later.
4030 * Called on each result obtained for the DHT search.
4032 * @param cls closure
4033 * @param exp when will this value expire
4034 * @param key key of the result
4035 * @param get_path path of the get request
4036 * @param get_path_length lenght of get_path
4037 * @param put_path path of the put request
4038 * @param put_path_length length of the put_path
4039 * @param type type of the result
4040 * @param size number of bytes in data
4041 * @param data pointer to the result data
4043 * TODO: re-issue the request after certain time? cancel after X results?
4046 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4047 const struct GNUNET_HashCode * key,
4048 const struct GNUNET_PeerIdentity *get_path,
4049 unsigned int get_path_length,
4050 const struct GNUNET_PeerIdentity *put_path,
4051 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4052 size_t size, const void *data)
4054 struct MeshPathInfo *path_info = cls;
4055 struct MeshPeerPath *p;
4056 struct GNUNET_PeerIdentity pi;
4059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4060 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4063 p = path_build_from_dht (get_path, get_path_length, put_path,
4065 path_add_to_peers (p, GNUNET_NO);
4067 for (i = 0; i < path_info->peer->ntunnels; i++)
4069 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4070 peer_info_connect (path_info->peer, path_info->t);
4078 * Function to process paths received for a new peer addition. The recorded
4079 * paths form the initial tunnel, which can be optimized later.
4080 * Called on each result obtained for the DHT search.
4082 * @param cls closure
4083 * @param exp when will this value expire
4084 * @param key key of the result
4085 * @param get_path path of the get request
4086 * @param get_path_length lenght of get_path
4087 * @param put_path path of the put request
4088 * @param put_path_length length of the put_path
4089 * @param type type of the result
4090 * @param size number of bytes in data
4091 * @param data pointer to the result data
4094 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4095 const struct GNUNET_HashCode * key,
4096 const struct GNUNET_PeerIdentity *get_path,
4097 unsigned int get_path_length,
4098 const struct GNUNET_PeerIdentity *put_path,
4099 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4100 size_t size, const void *data)
4102 const struct PBlock *pb = data;
4103 const struct GNUNET_PeerIdentity *pi = &pb->id;
4104 struct MeshTunnel *t = cls;
4105 struct MeshPeerInfo *peer_info;
4106 struct MeshPeerPath *p;
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
4109 if (size != sizeof (struct PBlock))
4111 GNUNET_break_op (0);
4114 if (ntohl(pb->type) != t->type)
4116 GNUNET_break_op (0);
4119 GNUNET_assert (NULL != t->owner);
4120 peer_info = peer_info_get (pi);
4121 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
4123 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
4125 p = path_build_from_dht (get_path, get_path_length, put_path,
4127 path_add_to_peers (p, GNUNET_NO);
4129 tunnel_add_peer (t, peer_info);
4130 peer_info_connect (peer_info, t);
4134 * Function to process DHT string to regex matching.
4135 * Called on each result obtained for the DHT search.
4137 * @param cls closure (search context)
4138 * @param exp when will this value expire
4139 * @param key key of the result
4140 * @param get_path path of the get request (not used)
4141 * @param get_path_length lenght of get_path (not used)
4142 * @param put_path path of the put request (not used)
4143 * @param put_path_length length of the put_path (not used)
4144 * @param type type of the result
4145 * @param size number of bytes in data
4146 * @param data pointer to the result data
4148 * TODO: re-issue the request after certain time? cancel after X results?
4151 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4152 const struct GNUNET_HashCode * key,
4153 const struct GNUNET_PeerIdentity *get_path,
4154 unsigned int get_path_length,
4155 const struct GNUNET_PeerIdentity *put_path,
4156 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4157 size_t size, const void *data);
4160 * Iterator over edges in a regex block retrieved from the DHT.
4162 * @param cls Closure.
4163 * @param token Token that follows to next state.
4164 * @param len Lenght of token.
4165 * @param key Hash of next state.
4167 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
4170 regex_edge_iterator (void *cls,
4173 const struct GNUNET_HashCode *key);
4175 * Iterator over hash map entries.
4177 * @param cls closure
4178 * @param key current key code
4179 * @param value value in the hash map
4180 * @return GNUNET_YES if we should continue to
4185 regex_result_iterator (void *cls,
4186 const struct GNUNET_HashCode * key,
4189 struct MeshRegexBlock *block = value;
4190 struct MeshRegexSearchContext *ctx = cls;
4192 // block was checked when stored, no need to check again
4193 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
4194 ®ex_edge_iterator, ctx);
4200 * Iterator over edges in a regex block retrieved from the DHT.
4202 * @param cls Closure (context of the search).
4203 * @param token Token that follows to next state.
4204 * @param len Lenght of token.
4205 * @param key Hash of next state.
4207 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
4210 regex_edge_iterator (void *cls,
4213 const struct GNUNET_HashCode *key)
4215 struct MeshRegexSearchContext *ctx = cls;
4216 struct MeshRegexSearchContext *new_ctx;
4217 struct MeshRegexSearchInfo *info = ctx->info;
4218 struct GNUNET_DHT_GetHandle *get_h;
4222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
4223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
4224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
4225 current = &info->description[ctx->position];
4226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
4227 current_len = strlen (info->description) - ctx->position;
4228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
4229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
4230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
4231 if (len > current_len)
4233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
4234 return GNUNET_YES; // Token too long, wont match
4236 if (0 != strncmp (current, token, len))
4238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
4239 return GNUNET_YES; // Token doesn't match
4241 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
4242 new_ctx->info = info;
4243 new_ctx->position += ctx->position + len;
4244 GNUNET_array_append (info->copies, info->n_copies, new_ctx);
4246 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
4248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
4249 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
4250 ®ex_result_iterator,
4252 return GNUNET_YES; // We are already looking for it
4254 /* Start search in DHT */
4256 GNUNET_DHT_get_start (dht_handle, /* handle */
4257 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
4258 key, /* key to search */
4259 DHT_REPLICATION_LEVEL, /* replication level */
4260 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
4261 NULL, /* xquery */ // FIXME BLOOMFILTER
4262 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
4263 &dht_get_string_handler, new_ctx);
4265 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
4266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
4277 * Function to process DHT string to regex matching.
4278 * Called on each result obtained for the DHT search.
4280 * @param cls closure (search context)
4281 * @param exp when will this value expire
4282 * @param key key of the result
4283 * @param get_path path of the get request (not used)
4284 * @param get_path_length lenght of get_path (not used)
4285 * @param put_path path of the put request (not used)
4286 * @param put_path_length length of the put_path (not used)
4287 * @param type type of the result
4288 * @param size number of bytes in data
4289 * @param data pointer to the result data
4292 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4293 const struct GNUNET_HashCode * key,
4294 const struct GNUNET_PeerIdentity *get_path,
4295 unsigned int get_path_length,
4296 const struct GNUNET_PeerIdentity *put_path,
4297 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4298 size_t size, const void *data)
4300 const struct MeshRegexAccept *block = data;
4301 struct MeshRegexSearchContext *ctx = cls;
4302 struct MeshRegexSearchInfo *info = ctx->info;
4303 struct MeshPeerPath *p;
4304 struct MeshPeerInfo *peer_info;
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4308 peer_info = peer_info_get(&block->id);
4309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
4311 p = path_build_from_dht (get_path, get_path_length, put_path,
4313 path_add_to_peers (p, GNUNET_NO);
4316 tunnel_add_peer (info->t, peer_info);
4317 peer_info_connect (peer_info, info->t);
4319 // FIXME REGEX cancel only AFTER successful connection (received ACK)
4320 regex_cancel_search (ctx);
4327 * Function to process DHT string to regex matching..
4328 * Called on each result obtained for the DHT search.
4330 * @param cls closure (search context)
4331 * @param exp when will this value expire
4332 * @param key key of the result
4333 * @param get_path path of the get request (not used)
4334 * @param get_path_length lenght of get_path (not used)
4335 * @param put_path path of the put request (not used)
4336 * @param put_path_length length of the put_path (not used)
4337 * @param type type of the result
4338 * @param size number of bytes in data
4339 * @param data pointer to the result data
4341 * TODO: re-issue the request after certain time? cancel after X results?
4344 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4345 const struct GNUNET_HashCode * key,
4346 const struct GNUNET_PeerIdentity *get_path,
4347 unsigned int get_path_length,
4348 const struct GNUNET_PeerIdentity *put_path,
4349 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4350 size_t size, const void *data)
4352 const struct MeshRegexBlock *block = data;
4353 struct MeshRegexSearchContext *ctx = cls;
4354 struct MeshRegexSearchInfo *info = ctx->info;
4359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4360 "DHT GET STRING RETURNED RESULTS\n");
4361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4362 " key: %s\n", GNUNET_h2s (key));
4364 copy = GNUNET_malloc (size);
4365 memcpy (copy, data, size);
4366 GNUNET_break (GNUNET_OK ==
4367 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
4368 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4369 len = ntohl (block->n_proof);
4370 proof = GNUNET_malloc (len + 1);
4371 memcpy (proof, &block[1], len);
4373 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
4375 GNUNET_break_op (0);
4378 len = strlen (info->description);
4379 if (len == ctx->position) // String processed
4381 if (GNUNET_YES == ntohl (block->accepting))
4383 struct GNUNET_DHT_GetHandle *get_h;
4384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
4385 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
4386 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
4387 key, /* key to search */
4388 DHT_REPLICATION_LEVEL, /* replication level */
4389 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
4390 GNUNET_DHT_RO_RECORD_ROUTE,
4391 NULL, /* xquery */ // FIXME BLOOMFILTER
4392 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
4393 &dht_get_string_accept_handler, ctx);
4394 GNUNET_break (GNUNET_OK ==
4395 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
4398 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
4403 // FIXME REGEX this block not successful, wait for more? start timeout?
4407 GNUNET_break (GNUNET_OK ==
4408 GNUNET_MESH_regex_block_iterate (block, size,
4409 ®ex_edge_iterator, ctx));
4413 /******************************************************************************/
4414 /********************* MESH LOCAL HANDLES **************************/
4415 /******************************************************************************/
4419 * Handler for client disconnection
4421 * @param cls closure
4422 * @param client identification of the client; NULL
4423 * for the last call when the server is destroyed
4426 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4428 struct MeshClient *c;
4429 struct MeshClient *next;
4432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4441 if (c->handle != client)
4443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4449 GNUNET_SERVER_client_drop (c->handle);
4450 c->shutting_down = GNUNET_YES;
4451 GNUNET_assert (NULL != c->own_tunnels);
4452 GNUNET_assert (NULL != c->incoming_tunnels);
4453 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4454 &tunnel_destroy_iterator, c);
4455 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4456 &tunnel_destroy_iterator, c);
4457 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4458 &tunnel_destroy_iterator, c);
4459 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4460 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4461 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4463 /* deregister clients applications */
4464 if (NULL != c->apps)
4466 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
4467 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
4469 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
4470 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
4472 GNUNET_SCHEDULER_cancel (announce_applications_task);
4473 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4475 if (NULL != c->types)
4476 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4477 for (i = 0; i < c->n_regex; i++)
4479 GNUNET_free (c->regexes[i]);
4481 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
4482 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
4484 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
4485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
4495 * Handler for new clients
4497 * @param cls closure
4498 * @param client identification of the client
4499 * @param message the actual message, which includes messages the client wants
4502 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4503 const struct GNUNET_MessageHeader *message)
4505 struct GNUNET_MESH_ClientConnect *cc_msg;
4506 struct MeshClient *c;
4507 GNUNET_MESH_ApplicationType *a;
4514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4515 /* Check data sanity */
4516 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4517 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4518 ntypes = ntohs (cc_msg->types);
4519 napps = ntohs (cc_msg->applications);
4521 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
4524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4528 /* Create new client structure */
4529 c = GNUNET_malloc (sizeof (struct MeshClient));
4530 c->id = next_client_id++;
4531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4533 GNUNET_SERVER_client_keep (client);
4534 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
4537 GNUNET_MESH_ApplicationType at;
4538 struct GNUNET_HashCode hc;
4540 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
4541 for (i = 0; i < napps; i++)
4544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
4545 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
4546 /* store in clients hashmap */
4547 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
4548 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4549 /* store in global hashmap, for announcements */
4550 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
4551 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4553 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
4554 announce_applications_task =
4555 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
4561 struct GNUNET_HashCode hc;
4563 t = (uint16_t *) & a[napps];
4564 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
4565 for (i = 0; i < ntypes; i++)
4568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4569 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
4571 /* store in clients hashmap */
4572 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4573 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4574 /* store in global hashmap */
4575 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4576 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4580 " client has %u+%u subscriptions\n", napps, ntypes);
4582 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
4583 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4584 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4585 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4586 GNUNET_SERVER_notification_context_add (nc, client);
4588 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4594 * Handler for clients announcing available services by a regular expression.
4596 * @param cls closure
4597 * @param client identification of the client
4598 * @param message the actual message, which includes messages the client wants
4601 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
4602 const struct GNUNET_MessageHeader *message)
4604 struct MeshClient *c;
4608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
4610 /* Sanity check for client registration */
4611 if (NULL == (c = client_get (client)))
4614 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4619 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
4620 regex = GNUNET_malloc (len + 1);
4621 memcpy (regex, &message[1], len);
4623 GNUNET_array_append (c->regexes, c->n_regex, regex);
4624 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
4626 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
4632 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
4638 * Handler for requests of new tunnels
4640 * @param cls closure
4641 * @param client identification of the client
4642 * @param message the actual message
4645 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4646 const struct GNUNET_MessageHeader *message)
4648 struct GNUNET_MESH_TunnelMessage *t_msg;
4649 struct MeshTunnel *t;
4650 struct MeshClient *c;
4652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4654 /* Sanity check for client registration */
4655 if (NULL == (c = client_get (client)))
4658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4663 /* Message sanity check */
4664 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4667 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4671 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4672 /* Sanity check for tunnel numbering */
4673 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4676 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4679 /* Sanity check for duplicate tunnel IDs */
4680 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
4683 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4687 while (NULL != tunnel_get_by_pi (myid, next_tid))
4688 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4689 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
4690 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4692 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4693 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
4695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
4696 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4702 * Handler for requests of deleting tunnels
4704 * @param cls closure
4705 * @param client identification of the client
4706 * @param message the actual message
4709 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4710 const struct GNUNET_MessageHeader *message)
4712 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4713 struct MeshClient *c;
4714 struct MeshTunnel *t;
4715 MESH_TunnelNumber tid;
4717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4718 "Got a DESTROY TUNNEL from client!\n");
4720 /* Sanity check for client registration */
4721 if (NULL == (c = client_get (client)))
4724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4727 /* Message sanity check */
4728 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4735 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4737 /* Retrieve tunnel */
4738 tid = ntohl (tunnel_msg->tunnel_id);
4739 t = tunnel_get_by_local_id(c, tid);
4743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4747 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4749 client_ignore_tunnel (c, t);
4751 // TODO: when to destroy incoming tunnel?
4752 if (t->nclients == 0)
4754 GNUNET_assert (GNUNET_YES ==
4755 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4757 GNUNET_assert (GNUNET_YES ==
4758 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4759 &my_full_id.hashPubKey,
4763 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4766 send_client_tunnel_disconnect(t, c);
4767 client_delete_tunnel(c, t);
4769 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4771 tunnel_send_destroy (t);
4773 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4779 * Handler for connection requests to new peers
4781 * @param cls closure
4782 * @param client identification of the client
4783 * @param message the actual message (PeerControl)
4786 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4787 const struct GNUNET_MessageHeader *message)
4789 struct GNUNET_MESH_PeerControl *peer_msg;
4790 struct MeshPeerInfo *peer_info;
4791 struct MeshClient *c;
4792 struct MeshTunnel *t;
4793 MESH_TunnelNumber tid;
4795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4796 /* Sanity check for client registration */
4797 if (NULL == (c = client_get (client)))
4800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4804 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4805 /* Sanity check for message size */
4806 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4813 /* Tunnel exists? */
4814 tid = ntohl (peer_msg->tunnel_id);
4815 t = tunnel_get_by_local_id (c, tid);
4819 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4823 /* Does client own tunnel? */
4824 if (t->owner->handle != client)
4827 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4831 GNUNET_i2s (&peer_msg->peer));
4832 peer_info = peer_info_get (&peer_msg->peer);
4834 tunnel_add_peer (t, peer_info);
4835 peer_info_connect (peer_info, t);
4837 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4843 * Handler for disconnection requests of peers in a tunnel
4845 * @param cls closure
4846 * @param client identification of the client
4847 * @param message the actual message (PeerControl)
4850 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4851 const struct GNUNET_MessageHeader *message)
4853 struct GNUNET_MESH_PeerControl *peer_msg;
4854 struct MeshPeerInfo *peer_info;
4855 struct MeshClient *c;
4856 struct MeshTunnel *t;
4857 MESH_TunnelNumber tid;
4859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4860 /* Sanity check for client registration */
4861 if (NULL == (c = client_get (client)))
4864 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4867 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4868 /* Sanity check for message size */
4869 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4876 /* Tunnel exists? */
4877 tid = ntohl (peer_msg->tunnel_id);
4878 t = tunnel_get_by_local_id (c, tid);
4882 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4887 /* Does client own tunnel? */
4888 if (t->owner->handle != client)
4891 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4896 GNUNET_i2s (&peer_msg->peer));
4897 /* Is the peer in the tunnel? */
4899 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4900 if (NULL == peer_info)
4903 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4907 /* Ok, delete peer from tunnel */
4908 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4909 &peer_msg->peer.hashPubKey);
4911 send_destroy_path (t, peer_info->id);
4912 tunnel_delete_peer (t, peer_info->id);
4913 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4918 * Handler for blacklist requests of peers in a tunnel
4920 * @param cls closure
4921 * @param client identification of the client
4922 * @param message the actual message (PeerControl)
4925 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
4926 const struct GNUNET_MessageHeader *message)
4928 struct GNUNET_MESH_PeerControl *peer_msg;
4929 struct MeshClient *c;
4930 struct MeshTunnel *t;
4931 MESH_TunnelNumber tid;
4933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
4934 /* Sanity check for client registration */
4935 if (NULL == (c = client_get (client)))
4938 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4941 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4943 /* Sanity check for message size */
4944 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4947 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4951 /* Tunnel exists? */
4952 tid = ntohl (peer_msg->tunnel_id);
4953 t = tunnel_get_by_local_id (c, tid);
4957 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4962 GNUNET_array_append(t->blacklisted, t->nblacklisted,
4963 GNUNET_PEER_intern(&peer_msg->peer));
4968 * Handler for unblacklist requests of peers in a tunnel
4970 * @param cls closure
4971 * @param client identification of the client
4972 * @param message the actual message (PeerControl)
4975 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
4976 const struct GNUNET_MessageHeader *message)
4978 struct GNUNET_MESH_PeerControl *peer_msg;
4979 struct MeshClient *c;
4980 struct MeshTunnel *t;
4981 MESH_TunnelNumber tid;
4985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
4986 /* Sanity check for client registration */
4987 if (NULL == (c = client_get (client)))
4990 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4993 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4995 /* Sanity check for message size */
4996 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4999 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5003 /* Tunnel exists? */
5004 tid = ntohl (peer_msg->tunnel_id);
5005 t = tunnel_get_by_local_id (c, tid);
5009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5014 /* if peer is not known, complain */
5015 pid = GNUNET_PEER_search (&peer_msg->peer);
5022 /* search and remove from list */
5023 for (i = 0; i < t->nblacklisted; i++)
5025 if (t->blacklisted[i] == pid)
5027 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
5028 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
5033 /* if peer hasn't been blacklisted, complain */
5039 * Handler for connection requests to new peers by type
5041 * @param cls closure
5042 * @param client identification of the client
5043 * @param message the actual message (ConnectPeerByType)
5046 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
5047 const struct GNUNET_MessageHeader *message)
5049 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
5050 struct MeshClient *c;
5051 struct MeshTunnel *t;
5052 struct GNUNET_HashCode hash;
5053 MESH_TunnelNumber tid;
5055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
5056 /* Sanity check for client registration */
5057 if (NULL == (c = client_get (client)))
5060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5064 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
5065 /* Sanity check for message size */
5066 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
5067 ntohs (connect_msg->header.size))
5070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5074 /* Tunnel exists? */
5075 tid = ntohl (connect_msg->tunnel_id);
5076 t = tunnel_get_by_local_id (c, tid);
5080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5084 /* Does client own tunnel? */
5085 if (t->owner->handle != client)
5088 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5092 /* Do WE have the service? */
5093 t->type = ntohl (connect_msg->type);
5094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
5095 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
5096 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
5099 /* Yes! Fast forward, add ourselves to the tunnel and send the
5100 * good news to the client, and alert the destination client of
5101 * an incoming tunnel.
5103 * FIXME send a path create to self, avoid code duplication
5105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
5106 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
5107 peer_info_get (&my_full_id),
5108 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
5111 send_client_peer_connected (t, myid);
5112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
5113 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5115 t->local_tid_dest = next_local_tid++;
5116 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5117 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5118 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5122 /* Ok, lets find a peer offering the service */
5123 if (NULL != t->dht_get_type)
5125 GNUNET_DHT_get_stop (t->dht_get_type);
5127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
5128 GNUNET_h2s (&hash));
5130 GNUNET_DHT_get_start (dht_handle,
5131 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
5133 DHT_REPLICATION_LEVEL,
5134 GNUNET_DHT_RO_RECORD_ROUTE |
5135 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5137 &dht_get_type_handler, t);
5139 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5145 * Handler for connection requests to new peers by a string service description.
5147 * @param cls closure
5148 * @param client identification of the client
5149 * @param message the actual message, which includes messages the client wants
5152 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
5153 const struct GNUNET_MessageHeader *message)
5155 struct GNUNET_MESH_ConnectPeerByString *msg;
5156 struct MeshRegexSearchContext *ctx;
5157 struct MeshRegexSearchInfo *info;
5158 struct GNUNET_DHT_GetHandle *get_h;
5159 struct GNUNET_HashCode key;
5160 struct MeshTunnel *t;
5161 struct MeshClient *c;
5162 MESH_TunnelNumber tid;
5166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5167 "Connect by string started\n");
5168 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
5169 size = htons (message->size);
5171 /* Sanity check for client registration */
5172 if (NULL == (c = client_get (client)))
5175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5179 /* Message size sanity check */
5180 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
5183 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5187 /* Tunnel exists? */
5188 tid = ntohl (msg->tunnel_id);
5189 t = tunnel_get_by_local_id (c, tid);
5193 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5197 /* Does client own tunnel? */
5198 if (t->owner->handle != client)
5201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5205 /* Only one connect_by_string allowed at the same time! */
5206 /* FIXME: allow more, return handle at api level to cancel, document */
5207 if (NULL != t->regex_ctx)
5210 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5214 /* Find string itself */
5215 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
5216 string = (const char *) &msg[1];
5218 /* Initialize context */
5219 size = GNUNET_REGEX_get_first_key(string, len, &key);
5220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5221 " consumed %u bits out of %u\n", size, len);
5222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5223 " looking for %s\n", GNUNET_h2s (&key));
5225 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
5227 info->description = GNUNET_malloc (len + 1);
5228 memcpy (info->description, string, len);
5229 info->description[len] = '\0';
5230 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
5231 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
5234 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
5235 ctx->position = size;
5239 GNUNET_array_append (info->copies, info->n_copies, ctx);
5241 /* Start search in DHT */
5242 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
5243 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
5244 &key, /* key to search */
5245 DHT_REPLICATION_LEVEL, /* replication level */
5246 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5247 NULL, /* xquery */ // FIXME BLOOMFILTER
5248 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
5249 &dht_get_string_handler, ctx);
5251 GNUNET_break (GNUNET_OK ==
5252 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
5255 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
5257 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
5263 * Handler for client traffic directed to one peer
5265 * @param cls closure
5266 * @param client identification of the client
5267 * @param message the actual message
5270 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5271 const struct GNUNET_MessageHeader *message)
5273 struct MeshClient *c;
5274 struct MeshTunnel *t;
5275 struct MeshPeerInfo *pi;
5276 struct GNUNET_MESH_Unicast *data_msg;
5277 MESH_TunnelNumber tid;
5280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5281 "Got a unicast request from a client!\n");
5283 /* Sanity check for client registration */
5284 if (NULL == (c = client_get (client)))
5287 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5290 data_msg = (struct GNUNET_MESH_Unicast *) message;
5291 /* Sanity check for message size */
5292 size = ntohs (message->size);
5293 if (sizeof (struct GNUNET_MESH_Unicast) +
5294 sizeof (struct GNUNET_MessageHeader) > size)
5297 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5301 /* Tunnel exists? */
5302 tid = ntohl (data_msg->tid);
5303 t = tunnel_get_by_local_id (c, tid);
5307 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5311 /* Is it a local tunnel? Then, does client own the tunnel? */
5312 if (t->owner->handle != client)
5315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5319 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
5320 &data_msg->destination.hashPubKey);
5321 /* Is the selected peer in the tunnel? */
5325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5329 /* Ok, everything is correct, send the message
5330 * (pretend we got it from a mesh peer)
5333 char buf[ntohs (message->size)] GNUNET_ALIGN;
5334 struct GNUNET_MESH_Unicast *copy;
5336 /* Work around const limitation */
5337 copy = (struct GNUNET_MESH_Unicast *) buf;
5338 memcpy (buf, data_msg, size);
5339 copy->oid = my_full_id;
5340 copy->tid = htonl (t->id.tid);
5341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5342 " calling generic handler...\n");
5343 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
5345 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5351 * Handler for client traffic directed to the origin
5353 * @param cls closure
5354 * @param client identification of the client
5355 * @param message the actual message
5358 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5359 const struct GNUNET_MessageHeader *message)
5361 struct GNUNET_MESH_ToOrigin *data_msg;
5362 struct GNUNET_PeerIdentity id;
5363 struct MeshClient *c;
5364 struct MeshTunnel *t;
5365 MESH_TunnelNumber tid;
5368 /* Sanity check for client registration */
5369 if (NULL == (c = client_get (client)))
5372 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5375 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5376 /* Sanity check for message size */
5377 size = ntohs (message->size);
5378 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5379 sizeof (struct GNUNET_MessageHeader) > size)
5382 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5386 /* Tunnel exists? */
5387 tid = ntohl (data_msg->tid);
5388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5389 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
5390 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5396 t = tunnel_get_by_local_id (c, tid);
5400 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5404 /* It should be sent by someone who has this as incoming tunnel. */
5405 if (-1 == client_knows_tunnel (c, t))
5408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5411 GNUNET_PEER_resolve (t->id.oid, &id);
5413 /* Ok, everything is correct, send the message
5414 * (pretend we got it from a mesh peer)
5417 char buf[ntohs (message->size)] GNUNET_ALIGN;
5418 struct GNUNET_MESH_ToOrigin *copy;
5420 /* Work around const limitation */
5421 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5422 memcpy (buf, data_msg, size);
5424 copy->tid = htonl (t->id.tid);
5425 copy->sender = my_full_id;
5426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5427 " calling generic handler...\n");
5428 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
5430 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5436 * Handler for client traffic directed to all peers in a tunnel
5438 * @param cls closure
5439 * @param client identification of the client
5440 * @param message the actual message
5443 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
5444 const struct GNUNET_MessageHeader *message)
5446 struct MeshClient *c;
5447 struct MeshTunnel *t;
5448 struct GNUNET_MESH_Multicast *data_msg;
5449 MESH_TunnelNumber tid;
5451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5452 "Got a multicast request from a client!\n");
5454 /* Sanity check for client registration */
5455 if (NULL == (c = client_get (client)))
5458 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5461 data_msg = (struct GNUNET_MESH_Multicast *) message;
5462 /* Sanity check for message size */
5463 if (sizeof (struct GNUNET_MESH_Multicast) +
5464 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
5467 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5471 /* Tunnel exists? */
5472 tid = ntohl (data_msg->tid);
5473 t = tunnel_get_by_local_id (c, tid);
5477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5481 /* Does client own tunnel? */
5482 if (t->owner->handle != client)
5485 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5490 char buf[ntohs (message->size)] GNUNET_ALIGN;
5491 struct GNUNET_MESH_Multicast *copy;
5493 copy = (struct GNUNET_MESH_Multicast *) buf;
5494 memcpy (buf, message, ntohs (message->size));
5495 copy->oid = my_full_id;
5496 copy->tid = htonl (t->id.tid);
5497 copy->ttl = htonl (DEFAULT_TTL);
5498 copy->mid = htonl (t->mid + 1);
5499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5500 " calling generic handler...\n");
5501 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
5504 /* receive done gets called when last copy is sent to a neighbor */
5509 * Functions to handle messages from clients
5511 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5512 {&handle_local_new_client, NULL,
5513 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5514 {&handle_local_announce_regex, NULL,
5515 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
5516 {&handle_local_tunnel_create, NULL,
5517 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5518 sizeof (struct GNUNET_MESH_TunnelMessage)},
5519 {&handle_local_tunnel_destroy, NULL,
5520 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5521 sizeof (struct GNUNET_MESH_TunnelMessage)},
5522 {&handle_local_connect_add, NULL,
5523 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
5524 sizeof (struct GNUNET_MESH_PeerControl)},
5525 {&handle_local_connect_del, NULL,
5526 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
5527 sizeof (struct GNUNET_MESH_PeerControl)},
5528 {&handle_local_blacklist, NULL,
5529 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
5530 sizeof (struct GNUNET_MESH_PeerControl)},
5531 {&handle_local_unblacklist, NULL,
5532 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
5533 sizeof (struct GNUNET_MESH_PeerControl)},
5534 {&handle_local_connect_by_type, NULL,
5535 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
5536 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
5537 {&handle_local_connect_by_string, NULL,
5538 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
5539 {&handle_local_unicast, NULL,
5540 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5541 {&handle_local_to_origin, NULL,
5542 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5543 {&handle_local_multicast, NULL,
5544 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5550 * To be called on core init/fail.
5552 * @param cls service closure
5553 * @param server handle to the server for this service
5554 * @param identity the public identity of this peer
5557 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5558 const struct GNUNET_PeerIdentity *identity)
5560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5561 core_handle = server;
5562 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5565 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5566 GNUNET_SCHEDULER_shutdown ();
5572 * Method called whenever a given peer connects.
5574 * @param cls closure
5575 * @param peer peer identity this notification is about
5576 * @param atsi performance data for the connection
5577 * @param atsi_count number of records in 'atsi'
5580 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
5581 const struct GNUNET_ATS_Information *atsi,
5582 unsigned int atsi_count)
5584 struct MeshPeerInfo *peer_info;
5585 struct MeshPeerPath *path;
5587 DEBUG_CONN ("Peer connected\n");
5588 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5589 peer_info = peer_info_get (peer);
5590 if (myid == peer_info->id)
5592 DEBUG_CONN (" (self)\n");
5597 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5599 path = path_new (2);
5600 path->peers[0] = myid;
5601 path->peers[1] = peer_info->id;
5602 GNUNET_PEER_change_rc (myid, 1);
5603 GNUNET_PEER_change_rc (peer_info->id, 1);
5604 peer_info_add_path (peer_info, path, GNUNET_YES);
5609 * Method called whenever a peer disconnects.
5611 * @param cls closure
5612 * @param peer peer identity this notification is about
5615 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5617 struct MeshPeerInfo *pi;
5618 struct MeshPeerQueue *q;
5619 struct MeshPeerQueue *n;
5621 DEBUG_CONN ("Peer disconnected\n");
5622 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5634 /* try to reroute this traffic instead */
5635 queue_destroy(q, GNUNET_YES);
5639 peer_info_remove_path (pi, pi->id, myid);
5642 DEBUG_CONN (" (self)\n");
5648 /******************************************************************************/
5649 /************************ MAIN FUNCTIONS ****************************/
5650 /******************************************************************************/
5653 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5655 * @param cls closure
5656 * @param key current key code
5657 * @param value value in the hash map
5658 * @return GNUNET_YES if we should continue to iterate,
5662 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5664 struct MeshTunnel *t = value;
5671 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5673 * @param cls closure
5674 * @param key current key code
5675 * @param value value in the hash map
5676 * @return GNUNET_YES if we should continue to iterate,
5680 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5682 struct MeshPeerInfo *p = value;
5683 struct MeshPeerQueue *q;
5684 struct MeshPeerQueue *n;
5692 queue_destroy(q, GNUNET_YES);
5696 peer_info_destroy (p);
5701 * Task run during shutdown.
5707 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5711 if (core_handle != NULL)
5713 GNUNET_CORE_disconnect (core_handle);
5716 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5717 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5718 if (dht_handle != NULL)
5720 GNUNET_DHT_disconnect (dht_handle);
5725 GNUNET_SERVER_notification_context_destroy (nc);
5728 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5730 GNUNET_SCHEDULER_cancel (announce_id_task);
5731 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5737 * Process mesh requests.
5739 * @param cls closure
5740 * @param server the initialized server
5741 * @param c configuration to use
5744 run (void *cls, struct GNUNET_SERVER_Handle *server,
5745 const struct GNUNET_CONFIGURATION_Handle *c)
5747 struct MeshPeerInfo *peer;
5748 struct MeshPeerPath *p;
5751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5752 server_handle = server;
5753 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5754 NULL, /* Closure passed to MESH functions */
5755 &core_init, /* Call core_init once connected */
5756 &core_connect, /* Handle connects */
5757 &core_disconnect, /* remove peers on disconnects */
5758 NULL, /* Don't notify about all incoming messages */
5759 GNUNET_NO, /* For header only in notification */
5760 NULL, /* Don't notify about all outbound messages */
5761 GNUNET_NO, /* For header-only out notification */
5762 core_handlers); /* Register these handlers */
5764 if (core_handle == NULL)
5767 GNUNET_SCHEDULER_shutdown ();
5772 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
5775 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5777 ("Mesh service is lacking key configuration settings. Exiting.\n"));
5778 GNUNET_SCHEDULER_shutdown ();
5781 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
5782 GNUNET_free (keyfile);
5783 if (my_private_key == NULL)
5785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5786 _("Mesh service could not access hostkey. Exiting.\n"));
5787 GNUNET_SCHEDULER_shutdown ();
5790 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
5791 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5792 &my_full_id.hashPubKey);
5793 myid = GNUNET_PEER_intern (&my_full_id);
5795 // transport_handle = GNUNET_TRANSPORT_connect(c,
5802 dht_handle = GNUNET_DHT_connect (c, 64);
5803 if (dht_handle == NULL)
5809 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5811 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5812 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5813 peers = GNUNET_CONTAINER_multihashmap_create (32);
5814 applications = GNUNET_CONTAINER_multihashmap_create (32);
5815 types = GNUNET_CONTAINER_multihashmap_create (32);
5817 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5818 nc = GNUNET_SERVER_notification_context_create (server_handle,
5820 GNUNET_SERVER_disconnect_notify (server_handle,
5821 &handle_local_client_disconnect, NULL);
5825 clients_tail = NULL;
5828 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5829 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5831 /* Create a peer_info for the local peer */
5832 peer = peer_info_get (&my_full_id);
5835 GNUNET_PEER_change_rc (myid, 1);
5836 peer_info_add_path (peer, p, GNUNET_YES);
5838 /* Scheduled the task to clean up when shutdown is called */
5839 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
5846 * The main function for the mesh service.
5848 * @param argc number of arguments from the command line
5849 * @param argv command line arguments
5850 * @return 0 ok, 1 on error
5853 main (int argc, char *const *argv)
5857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5860 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");