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 state of searches of services described by a regex
528 * using a user-provided string service description.
530 struct MeshRegexSearchContext
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;
554 /******************************************************************************/
555 /************************ DEBUG FUNCTIONS ****************************/
556 /******************************************************************************/
560 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
561 * @param cls string to print
562 * @param success GNUNET_OK if the PUT was transmitted,
563 * GNUNET_NO on timeout,
564 * GNUNET_SYSERR on disconnect from service
565 * after the PUT message was transmitted
566 * (so we don't know if it was received or not)
571 mesh_debug (void *cls, int success)
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
581 /******************************************************************************/
582 /*********************** GLOBAL VARIABLES ****************************/
583 /******************************************************************************/
588 static struct MeshClient *clients;
589 static struct MeshClient *clients_tail;
592 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
594 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
597 * Tunnels incoming, indexed by MESH_TunnelNumber
598 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
600 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
603 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
605 static struct GNUNET_CONTAINER_MultiHashMap *peers;
608 * Handle to communicate with transport
610 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
613 * Handle to communicate with core
615 static struct GNUNET_CORE_Handle *core_handle;
620 static struct GNUNET_DHT_Handle *dht_handle;
625 static struct GNUNET_SERVER_Handle *server_handle;
628 * Notification context, to send messages to local clients
630 static struct GNUNET_SERVER_NotificationContext *nc;
633 * Local peer own ID (memory efficient handle)
635 static GNUNET_PEER_Id myid;
638 * Local peer own ID (full value)
640 static struct GNUNET_PeerIdentity my_full_id;
645 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
650 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
653 * Tunnel ID for the next created tunnel (global tunnel number)
655 static MESH_TunnelNumber next_tid;
658 * Tunnel ID for the next incoming tunnel (local tunnel number)
660 static MESH_TunnelNumber next_local_tid;
663 * All application types provided by this peer
665 static struct GNUNET_CONTAINER_MultiHashMap *applications;
668 * All message types clients of this peer are interested in
670 static struct GNUNET_CONTAINER_MultiHashMap *types;
673 * Task to periodically announce provided applications
675 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
678 * Task to periodically announce itself in the network
680 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
683 * Next ID to assign to a client
685 unsigned int next_client_id;
689 /******************************************************************************/
690 /************************ ITERATORS ****************************/
691 /******************************************************************************/
693 /* FIXME move iterators here */
695 * Iterator over hash map entries to cancel DHT GET requests after a
696 * successful connect_by_string.
698 * @param cls closure (unused)
699 * @param key current key code (unused)
700 * @param value value in the hash map (get handle)
701 * @return GNUNET_YES if we should continue to iterate,
705 regex_cancel_dht_get (void *cls,
706 const struct GNUNET_HashCode * key,
709 struct GNUNET_DHT_GetHandle *h = value;
711 GNUNET_DHT_get_stop (h);
716 * Regex callback iterator to store own service description in the DHT.
718 * @param cls closure.
719 * @param key hash for current state.
720 * @param proof proof for current state.
721 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
722 * @param num_edges number of edges leaving current state.
723 * @param edges edges leaving current state.
726 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
727 int accepting, unsigned int num_edges,
728 const struct GNUNET_REGEX_Edge *edges)
730 struct MeshRegexBlock *block;
731 struct MeshRegexEdge *block_edge;
732 enum GNUNET_DHT_RouteOption opt;
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 " regex dht put for state %s\n",
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
750 if (GNUNET_YES == accepting)
752 struct MeshRegexAccept block;
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
755 " state %s is accepting, putting own id\n",
757 size = sizeof (block);
759 block.id = my_full_id;
761 GNUNET_DHT_put(dht_handle, key,
762 DHT_REPLICATION_LEVEL,
763 opt | GNUNET_DHT_RO_RECORD_ROUTE,
764 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
767 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
773 size = sizeof (struct MeshRegexBlock) + len;
774 block = GNUNET_malloc (size);
777 block->n_proof = htonl (len);
778 block->n_edges = htonl (num_edges);
779 block->accepting = htonl (accepting);
781 /* Store the proof at the end of the block. */
782 aux = (char *) &block[1];
783 memcpy (aux, proof, len);
786 /* Store each edge in a variable length MeshEdge struct at the
787 * very end of the MeshRegexBlock structure.
789 for (i = 0; i < num_edges; i++)
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792 " edge %s towards %s\n",
794 GNUNET_h2s(&edges[i].destination));
796 /* aux points at the end of the last block */
797 len = strlen (edges[i].label);
798 size += sizeof (struct MeshRegexEdge) + len;
799 // Calculate offset FIXME is this ok? use size instead?
800 offset = aux - (char *) block;
801 block = GNUNET_realloc (block, size);
802 aux = &((char *) block)[offset];
803 block_edge = (struct MeshRegexEdge *) aux;
804 block_edge->key = edges[i].destination;
805 block_edge->n_token = htonl (len);
806 aux = (char *) &block_edge[1];
807 memcpy (aux, edges[i].label, len);
811 GNUNET_DHT_put(dht_handle, key,
812 DHT_REPLICATION_LEVEL,
814 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
816 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
825 * Store the regular expression describing a local service into the DHT.
827 * @param regex The regular expresion.
830 regex_put (const char *regex)
832 struct GNUNET_REGEX_Automaton *dfa;
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
835 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
836 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
837 GNUNET_REGEX_automaton_destroy (dfa);
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
843 /******************************************************************************/
844 /************************ PERIODIC FUNCTIONS ****************************/
845 /******************************************************************************/
848 * Announce iterator over for each application provided by the peer
851 * @param key current key code
852 * @param value value in the hash map
853 * @return GNUNET_YES if we should continue to
858 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
861 struct MeshClient *c;
863 block.id = my_full_id;
864 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
865 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
871 block.type = htonl (block.type);
872 /* FIXME are hashes in multihash map equal on all aquitectures? */
873 /* FIXME: keep return value of 'put' to possibly cancel!? */
874 GNUNET_DHT_put (dht_handle, key,
875 DHT_REPLICATION_LEVEL,
876 GNUNET_DHT_RO_RECORD_ROUTE |
877 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
878 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
880 (const char *) &block,
881 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
883 APP_ANNOUNCE_TIME, NULL, NULL);
889 * Periodically announce what applications are provided by local clients
893 * @param tc task context
896 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
898 struct MeshClient *c = cls;
901 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
903 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
907 DEBUG_DHT ("Starting PUT for regex\n");
909 for (i = 0; i < c->n_regex; i++)
911 regex_put (c->regexes[i]);
913 c->regex_announce_task =
914 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_regex, cls);
915 DEBUG_DHT ("Finished PUT for regex\n");
922 * Periodically announce what applications are provided by local clients
926 * @param tc task context
929 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
931 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
933 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
937 DEBUG_DHT ("Starting PUT for apps\n");
939 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
941 announce_applications_task =
942 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
944 DEBUG_DHT ("Finished PUT for apps\n");
951 * Periodically announce self id in the DHT
954 * @param tc task context
957 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
961 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
963 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
967 * - Set data expiration in function of X
970 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
972 block.id = my_full_id;
973 block.type = htonl (0);
974 GNUNET_DHT_put (dht_handle, /* DHT handle */
975 &my_full_id.hashPubKey, /* Key to use */
976 DHT_REPLICATION_LEVEL, /* Replication level */
977 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
978 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
979 sizeof (block), /* Size of the data */
980 (const char *) &block, /* Data itself */
981 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
982 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
983 NULL, /* Continuation */
984 NULL); /* Continuation closure */
986 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
991 * Function to process paths received for a new peer addition. The recorded
992 * paths form the initial tunnel, which can be optimized later.
993 * Called on each result obtained for the DHT search.
996 * @param exp when will this value expire
997 * @param key key of the result
998 * @param type type of the result
999 * @param size number of bytes in data
1000 * @param data pointer to the result data
1003 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1004 const struct GNUNET_HashCode * key,
1005 const struct GNUNET_PeerIdentity *get_path,
1006 unsigned int get_path_length,
1007 const struct GNUNET_PeerIdentity *put_path,
1008 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1009 size_t size, const void *data);
1012 /******************************************************************************/
1013 /****************** GENERAL HELPER FUNCTIONS ************************/
1014 /******************************************************************************/
1017 * Cancel an ongoing regex search in the DHT and free all resources.
1019 * @param ctx The search context.
1022 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1024 GNUNET_free (ctx->description);
1025 GNUNET_CONTAINER_multihashmap_iterate (ctx->dht_get_handles,
1026 ®ex_cancel_dht_get, NULL);
1027 GNUNET_CONTAINER_multihashmap_destroy (ctx->dht_get_handles);
1028 ctx->t->regex_ctx = NULL;
1033 * Search for a tunnel by global ID using full PeerIdentities
1035 * @param oid owner of the tunnel
1036 * @param tid global tunnel number
1038 * @return tunnel handler, NULL if doesn't exist
1040 static struct MeshTunnel *
1041 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1045 * Delete an active client from the tunnel.
1051 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1054 * Notify a tunnel that a connection has broken that affects at least
1055 * some of its peers.
1057 * @param t Tunnel affected.
1058 * @param p1 Peer that got disconnected from p2.
1059 * @param p2 Peer that got disconnected from p1.
1061 * @return Short ID of the peer disconnected (either p1 or p2).
1062 * 0 if the tunnel remained unaffected.
1064 static GNUNET_PEER_Id
1065 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1070 * Check if client has registered with the service and has not disconnected
1072 * @param client the client to check
1074 * @return non-NULL if client exists in the global DLL
1076 static struct MeshClient *
1077 client_get (struct GNUNET_SERVER_Client *client)
1079 struct MeshClient *c;
1084 if (c->handle == client)
1093 * Checks if a given client has subscribed to certain message type
1095 * @param message_type Type of message to check
1096 * @param c Client to check
1098 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1103 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1105 struct GNUNET_HashCode hc;
1107 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1108 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1113 * Allow a client to send more data after transmitting a multicast message
1114 * which some neighbor has not yet accepted altough a reasonable time has
1117 * @param cls Closure (DataDescriptor containing the task identifier)
1118 * @param tc Task Context
1120 * FIXME reference counter cshould be just int
1123 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1125 struct MeshData *mdata = cls;
1127 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
1129 GNUNET_assert (NULL != mdata->reference_counter);
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1131 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1132 *(mdata->reference_counter));
1133 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1134 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1139 * Check whether client wants traffic from a tunnel.
1141 * @param c Client to check.
1142 * @param t Tunnel to be found.
1144 * @return GNUNET_YES if client knows tunnel.
1146 * TODO look in client hashmap
1149 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1153 for (i = 0; i < t->nclients; i++)
1154 if (t->clients[i] == c)
1161 * Check whether client has been informed about a tunnel.
1163 * @param c Client to check.
1164 * @param t Tunnel to be found.
1166 * @return GNUNET_YES if client knows tunnel.
1168 * TODO look in client hashmap
1171 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1175 for (i = 0; i < t->nignore; i++)
1176 if (t->ignore[i] == c)
1178 return client_wants_tunnel(c, t);
1183 * Marks a client as uninterested in traffic from the tunnel, updating both
1184 * client and tunnel to reflect this.
1186 * @param c Client that doesn't want traffic anymore.
1187 * @param t Tunnel which should be ignored.
1189 * FIXME when to delete an incoming tunnel?
1192 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1194 struct GNUNET_HashCode hash;
1196 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1197 GNUNET_break (GNUNET_YES ==
1198 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1200 GNUNET_break (GNUNET_YES ==
1201 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1202 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1203 tunnel_delete_active_client (t, c);
1204 GNUNET_array_append (t->ignore, t->nignore, c);
1209 * Deletes a tunnel from a client (either owner or destination). To be used on
1210 * tunnel destroy, otherwise, use client_ignore_tunnel.
1212 * @param c Client whose tunnel to delete.
1213 * @param t Tunnel which should be deleted.
1216 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1218 struct GNUNET_HashCode hash;
1222 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1223 GNUNET_assert (GNUNET_YES ==
1224 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1230 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1232 GNUNET_assert (GNUNET_YES ==
1233 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1237 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1246 * Send the message to all clients that have subscribed to its type
1248 * @param msg Pointer to the message itself
1249 * @param payload Pointer to the payload of the message.
1250 * @return number of clients this message was sent to
1253 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1254 const struct GNUNET_MessageHeader *payload)
1256 struct GNUNET_PeerIdentity *oid;
1257 struct MeshClient *c;
1258 struct MeshTunnel *t;
1259 MESH_TunnelNumber *tid;
1262 char cbuf[htons (msg->size)];
1264 type = ntohs (payload->type);
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1268 memcpy (cbuf, msg, sizeof (cbuf));
1269 switch (htons (msg->type))
1271 struct GNUNET_MESH_Unicast *uc;
1272 struct GNUNET_MESH_Multicast *mc;
1273 struct GNUNET_MESH_ToOrigin *to;
1275 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1276 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1280 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1281 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1285 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1286 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1294 t = tunnel_get (oid, ntohl (*tid));
1301 for (count = 0, c = clients; c != NULL; c = c->next)
1303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1304 if (client_is_subscribed (type, c))
1306 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1310 *tid = htonl (t->local_tid);
1314 if (GNUNET_NO == client_knows_tunnel (c, t))
1316 /* This client doesn't know the tunnel */
1317 struct GNUNET_MESH_TunnelNotification tmsg;
1318 struct GNUNET_HashCode hash;
1320 tmsg.header.size = htons (sizeof (tmsg));
1321 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1322 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1323 tmsg.tunnel_id = htonl (t->local_tid_dest);
1324 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1325 &tmsg.header, GNUNET_NO);
1326 GNUNET_array_append (t->clients, t->nclients, c);
1327 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1329 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1330 c->incoming_tunnels, &hash, t,
1331 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1333 *tid = htonl (t->local_tid_dest);
1336 /* Check if the client wants to get traffic from the tunnel */
1337 if (GNUNET_NO == client_wants_tunnel(c, t))
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1341 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1342 (struct GNUNET_MessageHeader
1343 *) cbuf, GNUNET_YES);
1351 * Notify the client that owns the tunnel that a peer has connected to it
1352 * (the requested path to it has been confirmed).
1354 * @param t Tunnel whose owner to notify
1355 * @param id Short id of the peer that has connected
1358 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1360 struct GNUNET_MESH_PeerControl pc;
1362 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1363 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1364 pc.tunnel_id = htonl (t->local_tid);
1365 GNUNET_PEER_resolve (id, &pc.peer);
1366 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1372 * Notify all clients (not depending on registration status) that the incoming
1373 * tunnel is no longer valid.
1375 * @param t Tunnel that was destroyed.
1378 send_clients_tunnel_destroy (struct MeshTunnel *t)
1380 struct GNUNET_MESH_TunnelMessage msg;
1382 msg.header.size = htons (sizeof (msg));
1383 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1384 msg.tunnel_id = htonl (t->local_tid_dest);
1385 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1390 * Notify clients of tunnel disconnections, if needed.
1391 * In case the origin disconnects, the destination clients get a tunnel destroy
1392 * notification. If the last destination disconnects (only one remaining client
1393 * in tunnel), the origin gets a (local ID) peer disconnected.
1394 * Note that the function must be called BEFORE removing the client from
1397 * @param t Tunnel that was destroyed.
1398 * @param c Client that disconnected.
1401 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1407 struct GNUNET_MESH_TunnelMessage msg;
1409 msg.header.size = htons (sizeof (msg));
1410 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1411 msg.tunnel_id = htonl (t->local_tid_dest);
1412 for (i = 0; i < t->nclients; i++)
1413 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1414 &msg.header, GNUNET_NO);
1416 // FIXME when to disconnect an incoming tunnel?
1417 else if (1 == t->nclients && NULL != t->owner)
1419 struct GNUNET_MESH_PeerControl msg;
1421 msg.header.size = htons (sizeof (msg));
1422 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1423 msg.tunnel_id = htonl (t->local_tid);
1424 msg.peer = my_full_id;
1425 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1426 &msg.header, GNUNET_NO);
1432 * Decrements the reference counter and frees all resources if needed
1434 * @param mesh_data Data Descriptor used in a multicast message.
1435 * Freed no longer needed (last message).
1438 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1440 /* Make sure it's a multicast packet */
1441 GNUNET_assert (NULL != mesh_data->reference_counter);
1443 if (0 == --(*(mesh_data->reference_counter)))
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1446 if (NULL != mesh_data->task)
1448 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1450 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1452 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1454 GNUNET_free (mesh_data->task);
1456 GNUNET_free (mesh_data->reference_counter);
1457 GNUNET_free (mesh_data->data);
1458 GNUNET_free (mesh_data);
1464 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1465 * and insert it in the appropiate structures if the peer is not known yet.
1467 * @param peer Full identity of the peer.
1469 * @return Existing or newly created peer info.
1471 static struct MeshPeerInfo *
1472 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1474 struct MeshPeerInfo *peer_info;
1476 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1477 if (NULL == peer_info)
1480 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1481 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1482 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1483 peer_info->id = GNUNET_PEER_intern (peer);
1491 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1492 * and insert it in the appropiate structures if the peer is not known yet.
1494 * @param peer Short identity of the peer.
1496 * @return Existing or newly created peer info.
1498 static struct MeshPeerInfo *
1499 peer_info_get_short (const GNUNET_PEER_Id peer)
1501 struct GNUNET_PeerIdentity id;
1503 GNUNET_PEER_resolve (peer, &id);
1504 return peer_info_get (&id);
1509 * Iterator to remove the tunnel from the list of tunnels a peer participates
1512 * @param cls Closure (tunnel info)
1513 * @param key GNUNET_PeerIdentity of the peer (unused)
1514 * @param value PeerInfo of the peer
1516 * @return always GNUNET_YES, to keep iterating
1519 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1521 struct MeshTunnel *t = cls;
1522 struct MeshPeerInfo *peer = value;
1525 for (i = 0; i < peer->ntunnels; i++)
1528 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1531 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1532 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1541 * Queue and pass message to core when possible.
1543 * @param cls Closure (type dependant).
1544 * @param type Type of the message.
1545 * @param size Size of the message.
1546 * @param dst Neighbor to send message to.
1547 * @param t Tunnel this message belongs to.
1550 queue_add (void *cls, uint16_t type, size_t size,
1551 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1554 * Core callback to write a pre-constructed data packet to core buffer
1556 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1557 * @param size Number of bytes available in buf.
1558 * @param buf Where the to write the message.
1560 * @return number of bytes written to buf
1563 send_core_data_raw (void *cls, size_t size, void *buf)
1565 struct MeshTransmissionDescriptor *info = cls;
1566 struct GNUNET_MessageHeader *msg;
1569 GNUNET_assert (NULL != info);
1570 GNUNET_assert (NULL != info->mesh_data);
1571 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1572 total_size = ntohs (msg->size);
1574 if (total_size > size)
1579 memcpy (buf, msg, total_size);
1580 GNUNET_free (info->mesh_data);
1587 * Sends an already built unicast message to a peer, properly registrating
1588 * all used resources.
1590 * @param message Message to send. Function makes a copy of it.
1591 * @param peer Short ID of the neighbor whom to send the message.
1592 * @param t Tunnel on which this message is transmitted.
1595 send_message (const struct GNUNET_MessageHeader *message,
1596 const struct GNUNET_PeerIdentity *peer,
1597 struct MeshTunnel *t)
1599 struct MeshTransmissionDescriptor *info;
1600 struct MeshPeerInfo *neighbor;
1601 struct MeshPeerPath *p;
1604 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1606 size = ntohs (message->size);
1607 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1608 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1609 info->mesh_data->data = GNUNET_malloc (size);
1610 memcpy (info->mesh_data->data, message, size);
1611 info->mesh_data->data_len = size;
1612 neighbor = peer_info_get (peer);
1613 for (p = neighbor->path_head; NULL != p; p = p->next)
1623 GNUNET_free (info->mesh_data->data);
1624 GNUNET_free (info->mesh_data);
1628 info->peer = neighbor;
1630 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
1638 * Sends a CREATE PATH message for a path to a peer, properly registrating
1639 * all used resources.
1641 * @param peer PeerInfo of the final peer for whom this path is being created.
1642 * @param p Path itself.
1643 * @param t Tunnel for which the path is created.
1646 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1647 struct MeshTunnel *t)
1649 struct GNUNET_PeerIdentity id;
1650 struct MeshPathInfo *path_info;
1651 struct MeshPeerInfo *neighbor;
1657 p = tree_get_path_to_peer (t->tree, peer->id);
1664 for (i = 0; i < p->length; i++)
1666 if (p->peers[i] == myid)
1669 if (i >= p->length - 1)
1675 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1677 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1678 path_info->path = p;
1680 neighbor = peer_info_get (&id);
1681 path_info->peer = neighbor;
1682 queue_add (path_info,
1683 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1684 sizeof (struct GNUNET_MESH_ManipulatePath) +
1685 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1692 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1694 * @param t Tunnel whose path to destroy.
1695 * @param destination Short ID of the peer to whom the path to destroy.
1698 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1700 struct MeshPeerPath *p;
1703 p = tree_get_path_to_peer (t->tree, destination);
1709 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1710 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1712 struct GNUNET_MESH_ManipulatePath *msg;
1713 struct GNUNET_PeerIdentity *pi;
1717 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1718 msg->header.size = htons (size);
1719 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1720 msg->tid = htonl (t->id.tid);
1721 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1722 for (i = 0; i < p->length; i++)
1724 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1726 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1733 * Try to establish a new connection to this peer.
1734 * Use the best path for the given tunnel.
1735 * If the peer doesn't have any path to it yet, try to get one.
1736 * If the peer already has some path, send a CREATE PATH towards it.
1738 * @param peer PeerInfo of the peer.
1739 * @param t Tunnel for which to create the path, if possible.
1742 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1744 struct MeshPeerPath *p;
1745 struct MeshPathInfo *path_info;
1747 if (NULL != peer->path_head)
1749 p = tree_get_path_to_peer (t->tree, peer->id);
1756 // FIXME always send create path to self
1759 send_create_path (peer, p, t);
1763 struct GNUNET_HashCode hash;
1766 send_client_peer_connected (t, myid);
1767 t->local_tid_dest = next_local_tid++;
1768 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1771 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1772 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1779 else if (NULL == peer->dhtget)
1781 struct GNUNET_PeerIdentity id;
1783 GNUNET_PEER_resolve (peer->id, &id);
1784 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1785 path_info->peer = peer;
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1788 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1789 peer->dhtgetcls = path_info;
1790 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1791 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1792 &id.hashPubKey, /* key to search */
1793 DHT_REPLICATION_LEVEL, /* replication level */
1794 GNUNET_DHT_RO_RECORD_ROUTE |
1795 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1796 NULL, /* xquery */ // FIXME BLOOMFILTER
1797 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1798 &dht_get_id_handler, path_info);
1800 /* Otherwise, there is no path but the DHT get is already started. */
1805 * Task to delay the connection of a peer
1807 * @param cls Closure (path info with tunnel and peer to connect).
1808 * Will be free'd on exection.
1809 * @param tc TaskContext
1812 peer_info_connect_task (void *cls,
1813 const struct GNUNET_SCHEDULER_TaskContext *tc)
1815 struct MeshPathInfo *path_info = cls;
1817 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1822 peer_info_connect (path_info->peer, path_info->t);
1828 * Destroy the peer_info and free any allocated resources linked to it
1830 * @param pi The peer_info to destroy.
1832 * @return GNUNET_OK on success
1835 peer_info_destroy (struct MeshPeerInfo *pi)
1837 struct GNUNET_PeerIdentity id;
1838 struct MeshPeerPath *p;
1839 struct MeshPeerPath *nextp;
1841 GNUNET_PEER_resolve (pi->id, &id);
1842 GNUNET_PEER_change_rc (pi->id, -1);
1845 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1848 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1849 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1851 if (NULL != pi->dhtget)
1853 GNUNET_DHT_get_stop (pi->dhtget);
1854 GNUNET_free (pi->dhtgetcls);
1860 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1870 * Remove all paths that rely on a direct connection between p1 and p2
1871 * from the peer itself and notify all tunnels about it.
1873 * @param peer PeerInfo of affected peer.
1874 * @param p1 GNUNET_PEER_Id of one peer.
1875 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1878 * TODO: optimize (see below)
1881 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1884 struct MeshPeerPath *p;
1885 struct MeshPeerPath *aux;
1886 struct MeshPeerInfo *peer_d;
1888 unsigned int destroyed;
1894 p = peer->path_head;
1898 for (i = 0; i < (p->length - 1); i++)
1900 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1901 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1903 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1914 for (i = 0; i < peer->ntunnels; i++)
1916 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1920 * Problem: one or more peers have been deleted from the tunnel tree.
1921 * We don't know who they are to try to add them again.
1922 * We need to try to find a new path for each of the disconnected peers.
1923 * Some of them might already have a path to reach them that does not
1924 * involve p1 and p2. Adding all anew might render in a better tree than
1925 * the trivial immediate fix.
1927 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1928 * its children will be reachable trough him.
1930 peer_d = peer_info_get_short (d);
1933 for (p = peer_d->path_head; NULL != p; p = p->next)
1935 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1943 /* No callback, as peer will be already disconnected and a connection
1944 * scheduled by tunnel_notify_connection_broken.
1946 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1950 peer_info_connect (peer_d, peer->tunnels[i]);
1957 * Add the path to the peer and update the path used to reach it in case this
1960 * @param peer_info Destination peer to add the path to.
1961 * @param path New path to add. Last peer must be the peer in arg 1.
1962 * Path will be either used of freed if already known.
1963 * @param trusted Do we trust that this path is real?
1966 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1969 struct MeshPeerPath *aux;
1973 if ((NULL == peer_info) || (NULL == path))
1976 path_destroy (path);
1979 if (path->peers[path->length - 1] != peer_info->id)
1982 path_destroy (path);
1985 if (path->length <= 2 && GNUNET_NO == trusted)
1987 /* Only allow CORE to tell us about direct paths */
1988 path_destroy (path);
1991 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1992 for (l = 1; l < path->length; l++)
1994 if (path->peers[l] == myid)
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1997 for (l2 = 0; l2 < path->length - l; l2++)
1999 path->peers[l2] = path->peers[l + l2];
2004 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2009 struct GNUNET_PeerIdentity id;
2011 GNUNET_PEER_resolve (peer_info->id, &id);
2012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2013 path->length, GNUNET_i2s (&id));
2016 l = path_get_length (path);
2023 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2024 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2026 l2 = path_get_length (aux);
2029 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2030 peer_info->path_tail, aux, path);
2035 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2037 path_destroy (path);
2042 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2049 * Add the path to the origin peer and update the path used to reach it in case
2050 * this is the shortest.
2051 * The path is given in peer_info -> destination, therefore we turn the path
2052 * upside down first.
2054 * @param peer_info Peer to add the path to, being the origin of the path.
2055 * @param path New path to add after being inversed.
2056 * @param trusted Do we trust that this path is real?
2059 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2060 struct MeshPeerPath *path, int trusted)
2063 peer_info_add_path (peer_info, path, trusted);
2068 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2069 * to obtain a local peer -> destination path and interning the peer ids.
2071 * @return Newly allocated and created path
2073 static struct MeshPeerPath *
2074 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2075 unsigned int get_path_length,
2076 const struct GNUNET_PeerIdentity *put_path,
2077 unsigned int put_path_length)
2079 struct MeshPeerPath *p;
2085 GNUNET_PEER_change_rc (myid, 1);
2086 i = get_path_length;
2087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2088 for (i--; i >= 0; i--)
2090 id = GNUNET_PEER_intern (&get_path[i]);
2091 if (p->length > 0 && id == p->peers[p->length - 1])
2093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2094 GNUNET_PEER_change_rc (id, -1);
2098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2099 GNUNET_i2s (&get_path[i]));
2101 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2102 p->peers[p->length - 1] = id;
2105 i = put_path_length;
2106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2107 for (i--; i >= 0; i--)
2109 id = GNUNET_PEER_intern (&put_path[i]);
2112 /* PUT path went through us, so discard the path up until now and start
2113 * from here to get a much shorter (and loop-free) path.
2118 if (p->length > 0 && id == p->peers[p->length - 1])
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2121 GNUNET_PEER_change_rc (id, -1);
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2126 GNUNET_i2s (&put_path[i]));
2128 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2129 p->peers[p->length - 1] = id;
2133 if (get_path_length > 0)
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2135 GNUNET_i2s (&get_path[0]));
2136 if (put_path_length > 0)
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2138 GNUNET_i2s (&put_path[0]));
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2141 for (i = 0; i < p->length; i++)
2143 struct GNUNET_PeerIdentity peer_id;
2145 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2147 GNUNET_i2s (&peer_id));
2155 * Adds a path to the peer_infos of all the peers in the path
2157 * @param p Path to process.
2158 * @param confirmed Whether we know if the path works or not. FIXME use
2161 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2165 /* TODO: invert and add */
2166 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2167 for (i++; i < p->length; i++)
2169 struct MeshPeerInfo *aux;
2170 struct MeshPeerPath *copy;
2172 aux = peer_info_get_short (p->peers[i]);
2173 copy = path_duplicate (p);
2174 copy->length = i + 1;
2175 peer_info_add_path (aux, copy, GNUNET_NO);
2181 * Send keepalive packets for a peer
2183 * @param cls Closure (tunnel for which to send the keepalive).
2184 * @param tc Notification context.
2186 * TODO: implement explicit multicast keepalive?
2189 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2193 * Search for a tunnel among the incoming tunnels
2195 * @param tid the local id of the tunnel
2197 * @return tunnel handler, NULL if doesn't exist
2199 static struct MeshTunnel *
2200 tunnel_get_incoming (MESH_TunnelNumber tid)
2202 struct GNUNET_HashCode hash;
2204 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2205 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2206 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2211 * Search for a tunnel among the tunnels for a client
2213 * @param c the client whose tunnels to search in
2214 * @param tid the local id of the tunnel
2216 * @return tunnel handler, NULL if doesn't exist
2218 static struct MeshTunnel *
2219 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2221 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2223 return tunnel_get_incoming (tid);
2227 struct GNUNET_HashCode hash;
2229 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2230 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2236 * Search for a tunnel by global ID using PEER_ID
2238 * @param pi owner of the tunnel
2239 * @param tid global tunnel number
2241 * @return tunnel handler, NULL if doesn't exist
2243 static struct MeshTunnel *
2244 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2246 struct MESH_TunnelID id;
2247 struct GNUNET_HashCode hash;
2252 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2253 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2258 * Search for a tunnel by global ID using full PeerIdentities
2260 * @param oid owner of the tunnel
2261 * @param tid global tunnel number
2263 * @return tunnel handler, NULL if doesn't exist
2265 static struct MeshTunnel *
2266 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2268 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2273 * Delete an active client from the tunnel.
2279 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2283 for (i = 0; i < t->nclients; i++)
2285 if (t->clients[i] == c)
2287 t->clients[i] = t->clients[t->nclients - 1];
2288 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2296 * Delete an ignored client from the tunnel.
2302 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2306 for (i = 0; i < t->nignore; i++)
2308 if (t->ignore[i] == c)
2310 t->ignore[i] = t->ignore[t->nignore - 1];
2311 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2319 * Delete a client from the tunnel. It should be only done on
2320 * client disconnection, otherwise use client_ignore_tunnel.
2326 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2328 tunnel_delete_ignored_client (t, c);
2329 tunnel_delete_active_client (t, c);
2334 * Callback used to notify a client owner of a tunnel that a peer has
2335 * disconnected, most likely because of a path change.
2337 * @param cls Closure (tunnel this notification is about).
2338 * @param peer_id Short ID of disconnected peer.
2341 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2343 struct MeshTunnel *t = cls;
2344 struct MeshPeerInfo *peer;
2345 struct MeshPathInfo *path_info;
2347 if (NULL != t->owner && NULL != nc)
2349 struct GNUNET_MESH_PeerControl msg;
2351 msg.header.size = htons (sizeof (msg));
2352 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2353 msg.tunnel_id = htonl (t->local_tid);
2354 GNUNET_PEER_resolve (peer_id, &msg.peer);
2355 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2356 &msg.header, GNUNET_NO);
2358 peer = peer_info_get_short (peer_id);
2359 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2360 path_info->peer = peer;
2362 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2367 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2368 * needed rescources.
2369 * If peer already exists, reevaluate shortest path and change if different.
2371 * @param t Tunnel we want to add a new peer to
2372 * @param peer PeerInfo of the peer being added
2376 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2378 struct GNUNET_PeerIdentity id;
2379 struct MeshPeerPath *best_p;
2380 struct MeshPeerPath *p;
2381 unsigned int best_cost;
2384 GNUNET_PEER_resolve (peer->id, &id);
2386 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2389 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2390 GNUNET_assert (GNUNET_OK ==
2391 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2393 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2396 if (NULL != (p = peer->path_head))
2399 best_cost = tree_get_path_cost (t->tree, p);
2402 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2409 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2410 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2411 t->path_refresh_task =
2412 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2416 /* Start a DHT get */
2417 peer_info_connect (peer, t);
2422 * Add a path to a tunnel which we don't own, just to remember the next hop.
2423 * If destination node was already in the tunnel, the first hop information
2424 * will be replaced with the new path.
2426 * @param t Tunnel we want to add a new peer to
2427 * @param p Path to add
2428 * @param own_pos Position of local node in path.
2432 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2433 unsigned int own_pos)
2435 struct GNUNET_PeerIdentity id;
2437 GNUNET_assert (0 != own_pos);
2438 tree_add_path (t->tree, p, NULL, NULL);
2439 if (own_pos < p->length - 1)
2441 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2442 tree_update_first_hops (t->tree, myid, &id);
2448 * Notifies a tunnel that a connection has broken that affects at least
2449 * some of its peers. Sends a notification towards the root of the tree.
2450 * In case the peer is the owner of the tree, notifies the client that owns
2451 * the tunnel and tries to reconnect.
2453 * @param t Tunnel affected.
2454 * @param p1 Peer that got disconnected from p2.
2455 * @param p2 Peer that got disconnected from p1.
2457 * @return Short ID of the peer disconnected (either p1 or p2).
2458 * 0 if the tunnel remained unaffected.
2460 static GNUNET_PEER_Id
2461 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2467 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2469 if (myid != p1 && myid != p2)
2475 if (tree_get_predecessor (t->tree) != 0)
2477 /* We are the peer still connected, notify owner of the disconnection. */
2478 struct GNUNET_MESH_PathBroken msg;
2479 struct GNUNET_PeerIdentity neighbor;
2481 msg.header.size = htons (sizeof (msg));
2482 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2483 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2484 msg.tid = htonl (t->id.tid);
2485 msg.peer1 = my_full_id;
2486 GNUNET_PEER_resolve (pid, &msg.peer2);
2487 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2488 send_message (&msg.header, &neighbor, t);
2496 * Send a multicast packet to a neighbor.
2498 * @param cls Closure (Info about the multicast packet)
2499 * @param neighbor_id Short ID of the neighbor to send the packet to.
2502 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2504 struct MeshData *mdata = cls;
2505 struct MeshTransmissionDescriptor *info;
2506 struct GNUNET_PeerIdentity neighbor;
2508 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2510 info->mesh_data = mdata;
2511 (*(mdata->reference_counter)) ++;
2512 info->destination = neighbor_id;
2513 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2515 GNUNET_i2s (&neighbor));
2516 info->peer = peer_info_get (&neighbor);
2517 GNUNET_assert (NULL != info->peer);
2519 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2520 info->mesh_data->data_len,
2526 * Send a message in a tunnel in multicast, sending a copy to each child node
2527 * down the local one in the tunnel tree.
2529 * @param t Tunnel in which to send the data.
2530 * @param msg Message to be sent.
2531 * @param internal Has the service generated this message?
2534 tunnel_send_multicast (struct MeshTunnel *t,
2535 const struct GNUNET_MessageHeader *msg,
2538 struct MeshData *mdata;
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 " sending a multicast packet...\n");
2542 mdata = GNUNET_malloc (sizeof (struct MeshData));
2543 mdata->data_len = ntohs (msg->size);
2544 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2546 mdata->data = GNUNET_malloc (mdata->data_len);
2547 memcpy (mdata->data, msg, mdata->data_len);
2548 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2550 struct GNUNET_MESH_Multicast *mcast;
2552 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2553 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2555 ntohl (mcast->ttl));
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2561 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2562 && GNUNET_NO == internal)
2564 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2566 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2572 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2573 if (*(mdata->reference_counter) == 0)
2575 GNUNET_free (mdata->data);
2576 GNUNET_free (mdata->reference_counter);
2577 if (NULL != mdata->task)
2579 GNUNET_SCHEDULER_cancel(*(mdata->task));
2580 GNUNET_free (mdata->task);
2581 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2583 // FIXME change order?
2584 GNUNET_free (mdata);
2586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2587 " sending a multicast packet done\n");
2593 * Send a message to all peers in this tunnel that the tunnel is no longer
2596 * @param t The tunnel whose peers to notify.
2599 tunnel_send_destroy (struct MeshTunnel *t)
2601 struct GNUNET_MESH_TunnelDestroy msg;
2603 msg.header.size = htons (sizeof (msg));
2604 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2605 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2606 msg.tid = htonl (t->id.tid);
2607 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2613 * Destroy the tunnel and free any allocated resources linked to it.
2615 * @param t the tunnel to destroy
2617 * @return GNUNET_OK on success
2620 tunnel_destroy (struct MeshTunnel *t)
2622 struct MeshClient *c;
2623 struct GNUNET_HashCode hash;
2634 struct GNUNET_PeerIdentity id;
2636 GNUNET_PEER_resolve (t->id.oid, &id);
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2638 GNUNET_i2s (&id), t->id.tid);
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2644 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2645 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2650 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2653 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2657 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2658 for (i = 0; i < t->nclients; i++)
2662 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2667 for (i = 0; i < t->nignore; i++)
2671 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2676 if (t->nclients > 0)
2679 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2683 GNUNET_free (t->clients);
2685 if (NULL != t->peers)
2687 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2689 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2692 tree_destroy (t->tree);
2693 if (NULL != t->regex_ctx)
2694 regex_cancel_search (t->regex_ctx);
2695 if (NULL != t->dht_get_type)
2696 GNUNET_DHT_get_stop (t->dht_get_type);
2697 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2698 GNUNET_SCHEDULER_cancel (t->timeout_task);
2699 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2700 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2707 * Create a new tunnel
2709 * @param owner Who is the owner of the tunnel (short ID).
2710 * @param tid Tunnel Number of the tunnel.
2711 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2712 * @param local Tunnel Number for the tunnel, for the client point of view.
2715 static struct MeshTunnel *
2716 tunnel_new (GNUNET_PEER_Id owner,
2717 MESH_TunnelNumber tid,
2718 struct MeshClient *client,
2719 MESH_TunnelNumber local)
2721 struct MeshTunnel *t;
2722 struct GNUNET_HashCode hash;
2724 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2727 t->queue_max = 1000; // FIXME API parameter
2728 t->tree = tree_new (owner);
2730 t->local_tid = local;
2732 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2734 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2735 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2740 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2746 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2748 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2749 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2753 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2763 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2764 * that are no longer needed, as well as nodes of no longer reachable peers.
2765 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2767 * @param t Tunnel from which to remove the path.
2768 * @param peer Short id of the peer which should be removed.
2771 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2773 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2779 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2780 * client when the client disconnects. If the client is not the owner, the
2781 * owner will get notified if no more clients are in the tunnel and the client
2782 * get removed from the tunnel's list.
2784 * @param cls closure (client that is disconnecting)
2785 * @param key the hash of the local tunnel id (used to access the hashmap)
2786 * @param value the value stored at the key (tunnel to destroy)
2788 * @return GNUNET_OK on success
2791 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
2793 struct MeshTunnel *t = value;
2794 struct MeshClient *c = cls;
2797 send_client_tunnel_disconnect(t, c);
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2801 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2802 tunnel_delete_client(t, c);
2803 client_delete_tunnel(c, t);
2806 tunnel_send_destroy(t);
2807 r = tunnel_destroy (t);
2813 * Timeout function, destroys tunnel if called
2815 * @param cls Closure (tunnel to destroy).
2816 * @param tc TaskContext
2819 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2821 struct MeshTunnel *t = cls;
2823 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2825 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2830 * Resets the tunnel timeout. Starts it if no timeout was running.
2832 * @param t Tunnel whose timeout to reset.
2835 tunnel_reset_timeout (struct MeshTunnel *t)
2837 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2838 GNUNET_SCHEDULER_cancel (t->timeout_task);
2840 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2841 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2845 /******************************************************************************/
2846 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2847 /******************************************************************************/
2850 * Function to send a create path packet to a peer.
2852 * @param cls closure
2853 * @param size number of bytes available in buf
2854 * @param buf where the callee should write the message
2855 * @return number of bytes written to buf
2858 send_core_path_create (void *cls, size_t size, void *buf)
2860 struct MeshPathInfo *info = cls;
2861 struct GNUNET_MESH_ManipulatePath *msg;
2862 struct GNUNET_PeerIdentity *peer_ptr;
2863 struct MeshTunnel *t = info->t;
2864 struct MeshPeerPath *p = info->path;
2868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2870 sizeof (struct GNUNET_MESH_ManipulatePath) +
2871 p->length * sizeof (struct GNUNET_PeerIdentity);
2873 if (size < size_needed || NULL == buf)
2878 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2879 msg->header.size = htons (size_needed);
2880 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2881 msg->tid = ntohl (t->id.tid);
2883 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2884 for (i = 0; i < p->length; i++)
2886 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2893 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2899 * Fill the core buffer
2901 * @param cls closure (data itself)
2902 * @param size number of bytes available in buf
2903 * @param buf where the callee should write the message
2905 * @return number of bytes written to buf
2908 send_core_data_multicast (void *cls, size_t size, void *buf)
2910 struct MeshTransmissionDescriptor *info = cls;
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2914 GNUNET_assert (NULL != info);
2915 GNUNET_assert (NULL != info->peer);
2916 total_size = info->mesh_data->data_len;
2917 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2919 if (total_size > size)
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2925 memcpy (buf, info->mesh_data->data, total_size);
2928 struct GNUNET_MESH_Multicast *mc;
2929 struct GNUNET_MessageHeader *mh;
2932 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2934 mc = (struct GNUNET_MESH_Multicast *) mh;
2935 mh = (struct GNUNET_MessageHeader *) &mc[1];
2936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2937 " multicast, payload type %u\n", ntohs (mh->type));
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2939 " multicast, payload size %u\n", ntohs (mh->size));
2943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2948 data_descriptor_decrement_multicast (info->mesh_data);
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2957 * Creates a path ack message in buf and frees all unused resources.
2959 * @param cls closure (MeshTransmissionDescriptor)
2960 * @param size number of bytes available in buf
2961 * @param buf where the callee should write the message
2962 * @return number of bytes written to buf
2965 send_core_path_ack (void *cls, size_t size, void *buf)
2967 struct MeshTransmissionDescriptor *info = cls;
2968 struct GNUNET_MESH_PathACK *msg = buf;
2970 GNUNET_assert (NULL != info);
2971 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2976 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2977 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2978 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2979 msg->tid = htonl (info->origin->tid);
2980 msg->peer_id = my_full_id;
2983 /* TODO add signature */
2985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2986 return sizeof (struct GNUNET_MESH_PathACK);
2991 * Free a transmission that was already queued with all resources
2992 * associated to the request.
2994 * @param queue Queue handler to cancel.
2995 * @param clear_cls Is it necessary to free associated cls?
2998 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3000 struct MeshTransmissionDescriptor *dd;
3001 struct MeshPathInfo *path_info;
3003 if (GNUNET_YES == clear_cls)
3005 switch (queue->type)
3007 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3008 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
3011 data_descriptor_decrement_multicast (dd->mesh_data);
3013 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3015 path_info = queue->cls;
3016 path_destroy (path_info->path);
3020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
3022 GNUNET_free_non_null (queue->cls);
3024 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3025 queue->peer->queue_tail,
3027 GNUNET_free (queue);
3032 * Core callback to write a queued packet to core buffer
3034 * @param cls Closure (peer info).
3035 * @param size Number of bytes available in buf.
3036 * @param buf Where the to write the message.
3038 * @return number of bytes written to buf
3041 queue_send (void *cls, size_t size, void *buf)
3043 struct MeshPeerInfo *peer = cls;
3044 struct MeshPeerQueue *queue;
3047 peer->core_transmit = NULL;
3048 queue = peer->queue_head;
3050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3053 /* If queue is empty, send should have been cancelled */
3059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3061 /* Check if buffer size is enough for the message */
3062 if (queue->size > size)
3064 struct GNUNET_PeerIdentity id;
3066 GNUNET_PEER_resolve (peer->id, &id);
3067 peer->core_transmit =
3068 GNUNET_CORE_notify_transmit_ready(core_handle,
3071 GNUNET_TIME_UNIT_FOREVER_REL,
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3081 switch (queue->type)
3083 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
3085 data_size = send_core_data_raw (queue->cls, size, buf);
3087 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
3089 data_size = send_core_data_multicast(queue->cls, size, buf);
3091 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3093 data_size = send_core_path_create(queue->cls, size, buf);
3095 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3097 data_size = send_core_path_ack(queue->cls, size, buf);
3101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
3104 queue->tunnel->queue_n--;
3106 /* Free queue, but cls was freed by send_core_* */
3107 queue_destroy(queue, GNUNET_NO);
3109 /* If more data in queue, send next */
3110 if (NULL != peer->queue_head)
3112 struct GNUNET_PeerIdentity id;
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3115 GNUNET_PEER_resolve (peer->id, &id);
3116 peer->core_transmit =
3117 GNUNET_CORE_notify_transmit_ready(core_handle,
3120 GNUNET_TIME_UNIT_FOREVER_REL,
3122 peer->queue_head->size,
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3132 * Queue and pass message to core when possible.
3134 * @param cls Closure (type dependant).
3135 * @param type Type of the message.
3136 * @param size Size of the message.
3137 * @param dst Neighbor to send message to.
3138 * @param t Tunnel this message belongs to.
3141 queue_add (void *cls, uint16_t type, size_t size,
3142 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3144 struct MeshPeerQueue *queue;
3146 if (t->queue_n >= t->queue_max)
3148 if (NULL == t->owner)
3149 GNUNET_break_op(0); // TODO: kill connection?
3152 return; // Drop message
3155 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3161 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3162 if (NULL == dst->core_transmit)
3164 struct GNUNET_PeerIdentity id;
3166 GNUNET_PEER_resolve (dst->id, &id);
3167 dst->core_transmit =
3168 GNUNET_CORE_notify_transmit_ready(core_handle,
3171 GNUNET_TIME_UNIT_FOREVER_REL,
3180 /******************************************************************************/
3181 /******************** MESH NETWORK HANDLERS **************************/
3182 /******************************************************************************/
3186 * Core handler for path creation
3188 * @param cls closure
3189 * @param message message
3190 * @param peer peer identity this notification is about
3191 * @param atsi performance data
3192 * @param atsi_count number of records in 'atsi'
3194 * @return GNUNET_OK to keep the connection open,
3195 * GNUNET_SYSERR to close it (signal serious error)
3198 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3199 const struct GNUNET_MessageHeader *message,
3200 const struct GNUNET_ATS_Information *atsi,
3201 unsigned int atsi_count)
3203 unsigned int own_pos;
3206 MESH_TunnelNumber tid;
3207 struct GNUNET_MESH_ManipulatePath *msg;
3208 struct GNUNET_PeerIdentity *pi;
3209 struct GNUNET_HashCode hash;
3210 struct MeshPeerPath *path;
3211 struct MeshPeerInfo *dest_peer_info;
3212 struct MeshPeerInfo *orig_peer_info;
3213 struct MeshTunnel *t;
3215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3216 "Received a path create msg [%s]\n",
3217 GNUNET_i2s (&my_full_id));
3218 size = ntohs (message->size);
3219 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3221 GNUNET_break_op (0);
3225 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3226 if (size % sizeof (struct GNUNET_PeerIdentity))
3228 GNUNET_break_op (0);
3231 size /= sizeof (struct GNUNET_PeerIdentity);
3234 GNUNET_break_op (0);
3237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3238 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3240 tid = ntohl (msg->tid);
3241 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3243 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3244 t = tunnel_get (pi, tid);
3245 if (NULL == t) // FIXME only for INCOMING tunnels?
3247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3248 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3250 while (NULL != tunnel_get_incoming (next_local_tid))
3251 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3252 t->local_tid_dest = next_local_tid++;
3253 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3255 tunnel_reset_timeout (t);
3256 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3258 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3259 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3267 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3268 if (NULL == dest_peer_info)
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3271 " Creating PeerInfo for destination.\n");
3272 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3273 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3274 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3276 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3278 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3279 if (NULL == orig_peer_info)
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3282 " Creating PeerInfo for origin.\n");
3283 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3284 orig_peer_info->id = GNUNET_PEER_intern (pi);
3285 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3286 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3289 path = path_new (size);
3291 for (i = 0; i < size; i++)
3293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3294 GNUNET_i2s (&pi[i]));
3295 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3296 if (path->peers[i] == myid)
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3302 /* cannot be self, must be 'not found' */
3303 /* create path: self not found in path through self */
3304 GNUNET_break_op (0);
3305 path_destroy (path);
3306 /* FIXME error. destroy tunnel? leave for timeout? */
3309 path_add_to_peers (path, GNUNET_NO);
3310 tunnel_add_path (t, path, own_pos);
3311 if (own_pos == size - 1)
3313 /* It is for us! Send ack. */
3314 struct MeshTransmissionDescriptor *info;
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3317 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3318 if (NULL == t->peers)
3320 /* New tunnel! Notify clients on data. */
3321 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
3323 GNUNET_break (GNUNET_OK ==
3324 GNUNET_CONTAINER_multihashmap_put (t->peers,
3325 &my_full_id.hashPubKey,
3328 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
3329 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3330 info->origin = &t->id;
3331 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3332 GNUNET_assert (NULL != info->peer);
3334 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3335 sizeof (struct GNUNET_MESH_PathACK),
3341 struct MeshPeerPath *path2;
3343 /* It's for somebody else! Retransmit. */
3344 path2 = path_duplicate (path);
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3346 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3347 path2 = path_duplicate (path);
3348 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3349 send_create_path (dest_peer_info, path, t);
3356 * Core handler for path destruction
3358 * @param cls closure
3359 * @param message message
3360 * @param peer peer identity this notification is about
3361 * @param atsi performance data
3362 * @param atsi_count number of records in 'atsi'
3364 * @return GNUNET_OK to keep the connection open,
3365 * GNUNET_SYSERR to close it (signal serious error)
3368 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3369 const struct GNUNET_MessageHeader *message,
3370 const struct GNUNET_ATS_Information *atsi,
3371 unsigned int atsi_count)
3373 struct GNUNET_MESH_ManipulatePath *msg;
3374 struct GNUNET_PeerIdentity *pi;
3375 struct MeshPeerPath *path;
3376 struct MeshTunnel *t;
3377 unsigned int own_pos;
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3382 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3383 size = ntohs (message->size);
3384 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3386 GNUNET_break_op (0);
3390 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3391 if (size % sizeof (struct GNUNET_PeerIdentity))
3393 GNUNET_break_op (0);
3396 size /= sizeof (struct GNUNET_PeerIdentity);
3399 GNUNET_break_op (0);
3402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3404 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3405 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3407 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3409 t = tunnel_get (pi, ntohl (msg->tid));
3412 /* TODO notify back: we don't know this tunnel */
3413 GNUNET_break_op (0);
3416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3417 path = path_new (size);
3419 for (i = 0; i < size; i++)
3421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3422 GNUNET_i2s (&pi[i]));
3423 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3424 if (path->peers[i] == myid)
3427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3428 if (own_pos < path->length - 1)
3429 send_message (message, &pi[own_pos + 1], t);
3431 send_client_tunnel_disconnect(t, NULL);
3433 tunnel_delete_peer (t, path->peers[path->length - 1]);
3434 path_destroy (path);
3440 * Core handler for notifications of broken paths
3442 * @param cls closure
3443 * @param message message
3444 * @param peer peer identity this notification is about
3445 * @param atsi performance data
3446 * @param atsi_count number of records in 'atsi'
3448 * @return GNUNET_OK to keep the connection open,
3449 * GNUNET_SYSERR to close it (signal serious error)
3452 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3453 const struct GNUNET_MessageHeader *message,
3454 const struct GNUNET_ATS_Information *atsi,
3455 unsigned int atsi_count)
3457 struct GNUNET_MESH_PathBroken *msg;
3458 struct MeshTunnel *t;
3460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3461 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3462 msg = (struct GNUNET_MESH_PathBroken *) message;
3463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3464 GNUNET_i2s (&msg->peer1));
3465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3466 GNUNET_i2s (&msg->peer2));
3467 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3470 GNUNET_break_op (0);
3473 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3474 GNUNET_PEER_search (&msg->peer2));
3481 * Core handler for tunnel destruction
3483 * @param cls closure
3484 * @param message message
3485 * @param peer peer identity this notification is about
3486 * @param atsi performance data
3487 * @param atsi_count number of records in 'atsi'
3489 * @return GNUNET_OK to keep the connection open,
3490 * GNUNET_SYSERR to close it (signal serious error)
3493 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3494 const struct GNUNET_MessageHeader *message,
3495 const struct GNUNET_ATS_Information *atsi,
3496 unsigned int atsi_count)
3498 struct GNUNET_MESH_TunnelDestroy *msg;
3499 struct MeshTunnel *t;
3501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3502 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3503 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3505 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3506 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3509 /* Probably already got the message from another path,
3510 * destroyed the tunnel and retransmitted to children.
3515 if (t->id.oid == myid)
3517 GNUNET_break_op (0);
3520 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3522 /* Tunnel was incoming, notify clients */
3523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3524 t->local_tid, t->local_tid_dest);
3525 send_clients_tunnel_destroy (t);
3527 tunnel_send_destroy (t);
3534 * Core handler for mesh network traffic going from the origin to a peer
3536 * @param cls closure
3537 * @param peer peer identity this notification is about
3538 * @param message message
3539 * @param atsi performance data
3540 * @param atsi_count number of records in 'atsi'
3541 * @return GNUNET_OK to keep the connection open,
3542 * GNUNET_SYSERR to close it (signal serious error)
3545 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3546 const struct GNUNET_MessageHeader *message,
3547 const struct GNUNET_ATS_Information *atsi,
3548 unsigned int atsi_count)
3550 struct GNUNET_MESH_Unicast *msg;
3551 struct MeshTunnel *t;
3555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3557 size = ntohs (message->size);
3559 sizeof (struct GNUNET_MESH_Unicast) +
3560 sizeof (struct GNUNET_MessageHeader))
3565 msg = (struct GNUNET_MESH_Unicast *) message;
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3567 ntohs (msg[1].header.type));
3568 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3571 /* TODO notify back: we don't know this tunnel */
3572 GNUNET_break_op (0);
3575 tunnel_reset_timeout (t);
3576 pid = GNUNET_PEER_search (&msg->destination);
3579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3580 " it's for us! sending to clients...\n");
3581 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3585 " not for us, retransmitting...\n");
3586 send_message (message, tree_get_first_hop (t->tree, pid), t);
3592 * Core handler for mesh network traffic going from the origin to all peers
3594 * @param cls closure
3595 * @param message message
3596 * @param peer peer identity this notification is about
3597 * @param atsi performance data
3598 * @param atsi_count number of records in 'atsi'
3599 * @return GNUNET_OK to keep the connection open,
3600 * GNUNET_SYSERR to close it (signal serious error)
3602 * TODO: Check who we got this from, to validate route.
3605 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3606 const struct GNUNET_MessageHeader *message,
3607 const struct GNUNET_ATS_Information *atsi,
3608 unsigned int atsi_count)
3610 struct GNUNET_MESH_Multicast *msg;
3611 struct MeshTunnel *t;
3614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3616 size = ntohs (message->size);
3617 if (sizeof (struct GNUNET_MESH_Multicast) +
3618 sizeof (struct GNUNET_MessageHeader) > size)
3620 GNUNET_break_op (0);
3623 msg = (struct GNUNET_MESH_Multicast *) message;
3624 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3628 /* TODO notify that we dont know that tunnel */
3629 GNUNET_break_op (0);
3632 if (t->mid == ntohl (msg->mid))
3634 /* FIXME: already seen this packet, log dropping */
3635 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3636 " Already seen mid %u, DROPPING!\n", t->mid);
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3642 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3644 t->mid = ntohl (msg->mid);
3645 tunnel_reset_timeout (t);
3647 /* Transmit to locally interested clients */
3648 if (NULL != t->peers &&
3649 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3651 send_subscribed_clients (message, &msg[1].header);
3653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3654 if (ntohl (msg->ttl) == 0)
3656 /* FIXME: ttl is 0, log dropping */
3657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3660 tunnel_send_multicast (t, message, GNUNET_NO);
3666 * Core handler for mesh network traffic toward the owner of a tunnel
3668 * @param cls closure
3669 * @param message message
3670 * @param peer peer identity this notification is about
3671 * @param atsi performance data
3672 * @param atsi_count number of records in 'atsi'
3674 * @return GNUNET_OK to keep the connection open,
3675 * GNUNET_SYSERR to close it (signal serious error)
3678 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3679 const struct GNUNET_MessageHeader *message,
3680 const struct GNUNET_ATS_Information *atsi,
3681 unsigned int atsi_count)
3683 struct GNUNET_MESH_ToOrigin *msg;
3684 struct GNUNET_PeerIdentity id;
3685 struct MeshPeerInfo *peer_info;
3686 struct MeshTunnel *t;
3689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3691 size = ntohs (message->size);
3692 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3693 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3695 GNUNET_break_op (0);
3698 msg = (struct GNUNET_MESH_ToOrigin *) message;
3699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3700 ntohs (msg[1].header.type));
3701 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3705 /* TODO notify that we dont know this tunnel (whom)? */
3706 GNUNET_break_op (0);
3710 if (t->id.oid == myid)
3713 struct GNUNET_MESH_ToOrigin *copy;
3715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3716 " it's for us! sending to clients...\n");
3717 if (NULL == t->owner)
3719 /* got data packet for ownerless tunnel */
3720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3721 GNUNET_break_op (0);
3724 /* TODO signature verification */
3725 memcpy (cbuf, message, size);
3726 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3727 copy->tid = htonl (t->local_tid);
3728 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3729 ©->header, GNUNET_YES);
3732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3733 " not for us, retransmitting...\n");
3735 peer_info = peer_info_get (&msg->oid);
3736 if (NULL == peer_info)
3738 /* unknown origin of tunnel */
3742 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3743 send_message (message, &id, t);
3750 * Core handler for path ACKs
3752 * @param cls closure
3753 * @param message message
3754 * @param peer peer identity this notification is about
3755 * @param atsi performance data
3756 * @param atsi_count number of records in 'atsi'
3758 * @return GNUNET_OK to keep the connection open,
3759 * GNUNET_SYSERR to close it (signal serious error)
3762 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3763 const struct GNUNET_MessageHeader *message,
3764 const struct GNUNET_ATS_Information *atsi,
3765 unsigned int atsi_count)
3767 struct GNUNET_MESH_PathACK *msg;
3768 struct GNUNET_PeerIdentity id;
3769 struct MeshPeerInfo *peer_info;
3770 struct MeshPeerPath *p;
3771 struct MeshTunnel *t;
3773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3774 GNUNET_i2s (&my_full_id));
3775 msg = (struct GNUNET_MESH_PathACK *) message;
3776 t = tunnel_get (&msg->oid, msg->tid);
3779 /* TODO notify that we don't know the tunnel */
3783 peer_info = peer_info_get (&msg->peer_id);
3785 /* Add paths to peers? */
3786 p = tree_get_path_to_peer (t->tree, peer_info->id);
3789 path_add_to_peers (p, GNUNET_YES);
3797 /* Message for us? */
3798 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3801 if (NULL == t->owner)
3803 GNUNET_break_op (0);
3806 if (NULL != t->dht_get_type)
3808 GNUNET_DHT_get_stop (t->dht_get_type);
3809 t->dht_get_type = NULL;
3811 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3813 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3814 send_client_peer_connected (t, peer_info->id);
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3820 " not for us, retransmitting...\n");
3821 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3822 peer_info = peer_info_get (&msg->oid);
3823 if (NULL == peer_info)
3825 /* If we know the tunnel, we should DEFINITELY know the peer */
3829 send_message (message, &id, t);
3835 * Functions to handle messages from core
3837 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3838 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3839 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3840 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3841 sizeof (struct GNUNET_MESH_PathBroken)},
3842 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3843 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3844 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3845 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3846 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3847 sizeof (struct GNUNET_MESH_PathACK)},
3853 /******************************************************************************/
3854 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3855 /******************************************************************************/
3858 * deregister_app: iterator for removing each application registered by a client
3860 * @param cls closure
3861 * @param key the hash of the application id (used to access the hashmap)
3862 * @param value the value stored at the key (client)
3864 * @return GNUNET_OK on success
3867 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
3869 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
3870 GNUNET_break (GNUNET_YES ==
3871 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
3877 * notify_client_connection_failure: notify a client that the connection to the
3878 * requested remote peer is not possible (for instance, no route found)
3879 * Function called when the socket is ready to queue more data. "buf" will be
3880 * NULL and "size" zero if the socket was closed for writing in the meantime.
3882 * @param cls closure
3883 * @param size number of bytes available in buf
3884 * @param buf where the callee should write the message
3885 * @return number of bytes written to buf
3888 notify_client_connection_failure (void *cls, size_t size, void *buf)
3891 struct MeshPeerInfo *peer_info;
3892 struct GNUNET_MESH_PeerControl *msg;
3893 struct GNUNET_PeerIdentity id;
3895 if (0 == size && NULL == buf)
3897 // TODO retry? cancel?
3901 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3902 peer_info = (struct MeshPeerInfo *) cls;
3903 msg = (struct GNUNET_MESH_PeerControl *) buf;
3904 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3905 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3906 // msg->tunnel_id = htonl(peer_info->t->tid);
3907 GNUNET_PEER_resolve (peer_info->id, &id);
3908 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3916 * Send keepalive packets for a peer
3918 * @param cls Closure (tunnel for which to send the keepalive).
3919 * @param tc Notification context.
3921 * TODO: implement explicit multicast keepalive?
3924 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3926 struct MeshTunnel *t = cls;
3927 struct GNUNET_MessageHeader *payload;
3928 struct GNUNET_MESH_Multicast *msg;
3930 sizeof (struct GNUNET_MESH_Multicast) +
3931 sizeof (struct GNUNET_MessageHeader);
3934 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3938 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3941 "sending keepalive for tunnel %d\n", t->id.tid);
3943 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3944 msg->header.size = htons (size);
3945 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3946 msg->oid = my_full_id;
3947 msg->tid = htonl (t->id.tid);
3948 msg->ttl = htonl (DEFAULT_TTL);
3949 msg->mid = htonl (t->mid + 1);
3951 payload = (struct GNUNET_MessageHeader *) &msg[1];
3952 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3953 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3954 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3956 t->path_refresh_task =
3957 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3963 * Function to process paths received for a new peer addition. The recorded
3964 * paths form the initial tunnel, which can be optimized later.
3965 * Called on each result obtained for the DHT search.
3967 * @param cls closure
3968 * @param exp when will this value expire
3969 * @param key key of the result
3970 * @param get_path path of the get request
3971 * @param get_path_length lenght of get_path
3972 * @param put_path path of the put request
3973 * @param put_path_length length of the put_path
3974 * @param type type of the result
3975 * @param size number of bytes in data
3976 * @param data pointer to the result data
3978 * TODO: re-issue the request after certain time? cancel after X results?
3981 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3982 const struct GNUNET_HashCode * key,
3983 const struct GNUNET_PeerIdentity *get_path,
3984 unsigned int get_path_length,
3985 const struct GNUNET_PeerIdentity *put_path,
3986 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3987 size_t size, const void *data)
3989 struct MeshPathInfo *path_info = cls;
3990 struct MeshPeerPath *p;
3991 struct GNUNET_PeerIdentity pi;
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3995 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3998 p = path_build_from_dht (get_path, get_path_length, put_path,
4000 path_add_to_peers (p, GNUNET_NO);
4002 for (i = 0; i < path_info->peer->ntunnels; i++)
4004 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4005 peer_info_connect (path_info->peer, path_info->t);
4013 * Function to process paths received for a new peer addition. The recorded
4014 * paths form the initial tunnel, which can be optimized later.
4015 * Called on each result obtained for the DHT search.
4017 * @param cls closure
4018 * @param exp when will this value expire
4019 * @param key key of the result
4020 * @param get_path path of the get request
4021 * @param get_path_length lenght of get_path
4022 * @param put_path path of the put request
4023 * @param put_path_length length of the put_path
4024 * @param type type of the result
4025 * @param size number of bytes in data
4026 * @param data pointer to the result data
4029 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4030 const struct GNUNET_HashCode * key,
4031 const struct GNUNET_PeerIdentity *get_path,
4032 unsigned int get_path_length,
4033 const struct GNUNET_PeerIdentity *put_path,
4034 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4035 size_t size, const void *data)
4037 const struct PBlock *pb = data;
4038 const struct GNUNET_PeerIdentity *pi = &pb->id;
4039 struct MeshTunnel *t = cls;
4040 struct MeshPeerInfo *peer_info;
4041 struct MeshPeerPath *p;
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
4044 if (size != sizeof (struct PBlock))
4046 GNUNET_break_op (0);
4049 if (ntohl(pb->type) != t->type)
4051 GNUNET_break_op (0);
4054 GNUNET_assert (NULL != t->owner);
4055 peer_info = peer_info_get (pi);
4056 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
4058 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
4060 p = path_build_from_dht (get_path, get_path_length, put_path,
4062 path_add_to_peers (p, GNUNET_NO);
4064 tunnel_add_peer (t, peer_info);
4065 peer_info_connect (peer_info, t);
4069 * Function to process DHT string to regex matching..
4070 * Called on each result obtained for the DHT search.
4072 * @param cls closure (search context)
4073 * @param exp when will this value expire
4074 * @param key key of the result
4075 * @param get_path path of the get request (not used)
4076 * @param get_path_length lenght of get_path (not used)
4077 * @param put_path path of the put request (not used)
4078 * @param put_path_length length of the put_path (not used)
4079 * @param type type of the result
4080 * @param size number of bytes in data
4081 * @param data pointer to the result data
4083 * TODO: re-issue the request after certain time? cancel after X results?
4086 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4087 const struct GNUNET_HashCode * key,
4088 const struct GNUNET_PeerIdentity *get_path,
4089 unsigned int get_path_length,
4090 const struct GNUNET_PeerIdentity *put_path,
4091 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4092 size_t size, const void *data);
4095 * Iterator over edges in a block.
4097 * @param cls Closure.
4098 * @param token Token that follows to next state.
4099 * @param len Lenght of token.
4100 * @param key Hash of next state.
4102 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
4105 regex_edge_iterator (void *cls,
4108 const struct GNUNET_HashCode *key)
4110 struct MeshRegexSearchContext *ctx = cls;
4111 struct GNUNET_DHT_GetHandle *get_h;
4115 current = &ctx->description[ctx->position];
4116 current_len = strlen (ctx->description) - ctx->position;
4117 if (len > current_len)
4118 return GNUNET_YES; // Token too long, wont match
4119 if (0 != strncmp (current, token, len))
4120 return GNUNET_YES; // Token doesn't match
4122 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(ctx->dht_get_handles,
4125 return GNUNET_YES; // We are already looking for it
4127 /* Start search in DHT */
4129 GNUNET_DHT_get_start (dht_handle, /* handle */
4130 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
4131 key, /* key to search */
4132 DHT_REPLICATION_LEVEL, /* replication level */
4133 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
4134 NULL, /* xquery */ // FIXME BLOOMFILTER
4135 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
4136 &dht_get_string_handler, ctx);
4138 GNUNET_CONTAINER_multihashmap_put(ctx->dht_get_handles, key, get_h,
4139 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4149 * Function to process DHT string to regex matching..
4150 * Called on each result obtained for the DHT search.
4152 * @param cls closure (search context)
4153 * @param exp when will this value expire
4154 * @param key key of the result
4155 * @param get_path path of the get request (not used)
4156 * @param get_path_length lenght of get_path (not used)
4157 * @param put_path path of the put request (not used)
4158 * @param put_path_length length of the put_path (not used)
4159 * @param type type of the result
4160 * @param size number of bytes in data
4161 * @param data pointer to the result data
4164 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4165 const struct GNUNET_HashCode * key,
4166 const struct GNUNET_PeerIdentity *get_path,
4167 unsigned int get_path_length,
4168 const struct GNUNET_PeerIdentity *put_path,
4169 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4170 size_t size, const void *data)
4172 const struct MeshRegexAccept *block = data;
4173 struct MeshRegexSearchContext *ctx = cls;
4174 struct MeshPeerPath *p;
4175 struct MeshPeerInfo *peer_info;
4177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4179 peer_info = peer_info_get(&block->id);
4180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", ctx->description);
4182 p = path_build_from_dht (get_path, get_path_length, put_path,
4184 path_add_to_peers (p, GNUNET_NO);
4187 tunnel_add_peer (ctx->t, peer_info);
4188 peer_info_connect (peer_info, ctx->t);
4190 // FIXME cancel only AFTER successful connection (received ACK)
4191 regex_cancel_search (ctx);
4198 * Function to process DHT string to regex matching..
4199 * Called on each result obtained for the DHT search.
4201 * @param cls closure (search context)
4202 * @param exp when will this value expire
4203 * @param key key of the result
4204 * @param get_path path of the get request (not used)
4205 * @param get_path_length lenght of get_path (not used)
4206 * @param put_path path of the put request (not used)
4207 * @param put_path_length length of the put_path (not used)
4208 * @param type type of the result
4209 * @param size number of bytes in data
4210 * @param data pointer to the result data
4212 * TODO: re-issue the request after certain time? cancel after X results?
4215 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4216 const struct GNUNET_HashCode * key,
4217 const struct GNUNET_PeerIdentity *get_path,
4218 unsigned int get_path_length,
4219 const struct GNUNET_PeerIdentity *put_path,
4220 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4221 size_t size, const void *data)
4223 const struct MeshRegexBlock *block = data;
4224 struct MeshRegexSearchContext *ctx = cls;
4228 // FIXME: does proof have to be NULL terminated?
4229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4230 "*******************************\n");
4231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4232 "DHT GET STRING RETURNED RESULTS\n");
4233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4234 " key: %s\n", GNUNET_h2s (key));
4235 proof = (char *) &block[1];
4236 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
4238 GNUNET_break_op (0);
4241 len = strlen (ctx->description);
4242 if (len == ctx->position) // String processed
4244 if (GNUNET_YES == ntohl (block->accepting))
4246 struct GNUNET_DHT_GetHandle *get_h;
4247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
4248 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
4249 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
4250 key, /* key to search */
4251 DHT_REPLICATION_LEVEL, /* replication level */
4252 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
4253 GNUNET_DHT_RO_RECORD_ROUTE,
4254 NULL, /* xquery */ // FIXME BLOOMFILTER
4255 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
4256 &dht_get_string_accept_handler, ctx);
4257 GNUNET_break (GNUNET_OK ==
4258 GNUNET_CONTAINER_multihashmap_put(ctx->dht_get_handles, key, get_h,
4259 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4263 // FIXME this block not successful, wait for more? start timeout?
4267 GNUNET_MESH_regex_block_iterate (block, size, ®ex_edge_iterator, ctx);
4271 /******************************************************************************/
4272 /********************* MESH LOCAL HANDLES **************************/
4273 /******************************************************************************/
4277 * Handler for client disconnection
4279 * @param cls closure
4280 * @param client identification of the client; NULL
4281 * for the last call when the server is destroyed
4284 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4286 struct MeshClient *c;
4287 struct MeshClient *next;
4290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4299 if (c->handle != client)
4301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4307 GNUNET_SERVER_client_drop (c->handle);
4308 c->shutting_down = GNUNET_YES;
4309 GNUNET_assert (NULL != c->own_tunnels);
4310 GNUNET_assert (NULL != c->incoming_tunnels);
4311 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4312 &tunnel_destroy_iterator, c);
4313 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4314 &tunnel_destroy_iterator, c);
4315 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4316 &tunnel_destroy_iterator, c);
4317 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4318 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4319 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4321 /* deregister clients applications */
4322 if (NULL != c->apps)
4324 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
4325 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
4327 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
4328 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
4330 GNUNET_SCHEDULER_cancel (announce_applications_task);
4331 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4333 if (NULL != c->types)
4334 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4335 for (i = 0; i < c->n_regex; i++)
4337 GNUNET_free (c->regexes[i]);
4339 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
4340 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
4342 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
4353 * Handler for new clients
4355 * @param cls closure
4356 * @param client identification of the client
4357 * @param message the actual message, which includes messages the client wants
4360 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4361 const struct GNUNET_MessageHeader *message)
4363 struct GNUNET_MESH_ClientConnect *cc_msg;
4364 struct MeshClient *c;
4365 GNUNET_MESH_ApplicationType *a;
4372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4373 /* Check data sanity */
4374 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4375 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4376 ntypes = ntohs (cc_msg->types);
4377 napps = ntohs (cc_msg->applications);
4379 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
4382 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4386 /* Create new client structure */
4387 c = GNUNET_malloc (sizeof (struct MeshClient));
4388 c->id = next_client_id++;
4389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4391 GNUNET_SERVER_client_keep (client);
4392 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
4395 GNUNET_MESH_ApplicationType at;
4396 struct GNUNET_HashCode hc;
4398 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
4399 for (i = 0; i < napps; i++)
4402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
4403 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
4404 /* store in clients hashmap */
4405 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
4406 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4407 /* store in global hashmap, for announcements */
4408 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
4409 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4411 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
4412 announce_applications_task =
4413 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
4419 struct GNUNET_HashCode hc;
4421 t = (uint16_t *) & a[napps];
4422 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
4423 for (i = 0; i < ntypes; i++)
4426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4427 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
4429 /* store in clients hashmap */
4430 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4431 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4432 /* store in global hashmap */
4433 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4434 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4438 " client has %u+%u subscriptions\n", napps, ntypes);
4440 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
4441 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4442 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4443 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4444 GNUNET_SERVER_notification_context_add (nc, client);
4446 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4452 * Handler for clients announcing available services by a regular expression.
4454 * @param cls closure
4455 * @param client identification of the client
4456 * @param message the actual message, which includes messages the client wants
4459 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
4460 const struct GNUNET_MessageHeader *message)
4462 struct MeshClient *c;
4466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
4468 /* Sanity check for client registration */
4469 if (NULL == (c = client_get (client)))
4472 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4477 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
4478 regex = GNUNET_malloc (len + 1);
4479 memcpy (regex, &message[1], len);
4481 GNUNET_array_append (c->regexes, c->n_regex, regex);
4482 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
4484 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
4490 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
4496 * Handler for requests of new tunnels
4498 * @param cls closure
4499 * @param client identification of the client
4500 * @param message the actual message
4503 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4504 const struct GNUNET_MessageHeader *message)
4506 struct GNUNET_MESH_TunnelMessage *t_msg;
4507 struct MeshTunnel *t;
4508 struct MeshClient *c;
4510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4512 /* Sanity check for client registration */
4513 if (NULL == (c = client_get (client)))
4516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4521 /* Message sanity check */
4522 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4529 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4530 /* Sanity check for tunnel numbering */
4531 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4534 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4537 /* Sanity check for duplicate tunnel IDs */
4538 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
4541 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4545 while (NULL != tunnel_get_by_pi (myid, next_tid))
4546 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4547 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
4548 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4550 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4551 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
4553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
4554 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4560 * Handler for requests of deleting tunnels
4562 * @param cls closure
4563 * @param client identification of the client
4564 * @param message the actual message
4567 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4568 const struct GNUNET_MessageHeader *message)
4570 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4571 struct MeshClient *c;
4572 struct MeshTunnel *t;
4573 MESH_TunnelNumber tid;
4575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4576 "Got a DESTROY TUNNEL from client!\n");
4578 /* Sanity check for client registration */
4579 if (NULL == (c = client_get (client)))
4582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4585 /* Message sanity check */
4586 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4589 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4593 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4595 /* Retrieve tunnel */
4596 tid = ntohl (tunnel_msg->tunnel_id);
4597 t = tunnel_get_by_local_id(c, tid);
4601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4602 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4605 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4607 client_ignore_tunnel (c, t);
4609 // TODO: when to destroy incoming tunnel?
4610 if (t->nclients == 0)
4612 GNUNET_assert (GNUNET_YES ==
4613 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4615 GNUNET_assert (GNUNET_YES ==
4616 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4617 &my_full_id.hashPubKey,
4621 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4624 send_client_tunnel_disconnect(t, c);
4625 client_delete_tunnel(c, t);
4627 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4629 tunnel_send_destroy (t);
4631 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4637 * Handler for connection requests to new peers
4639 * @param cls closure
4640 * @param client identification of the client
4641 * @param message the actual message (PeerControl)
4644 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4645 const struct GNUNET_MessageHeader *message)
4647 struct GNUNET_MESH_PeerControl *peer_msg;
4648 struct MeshPeerInfo *peer_info;
4649 struct MeshClient *c;
4650 struct MeshTunnel *t;
4651 MESH_TunnelNumber tid;
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4654 /* Sanity check for client registration */
4655 if (NULL == (c = client_get (client)))
4658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4662 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4663 /* Sanity check for message size */
4664 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4667 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4671 /* Tunnel exists? */
4672 tid = ntohl (peer_msg->tunnel_id);
4673 t = tunnel_get_by_local_id (c, tid);
4677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4681 /* Does client own tunnel? */
4682 if (t->owner->handle != client)
4685 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4689 GNUNET_i2s (&peer_msg->peer));
4690 peer_info = peer_info_get (&peer_msg->peer);
4692 tunnel_add_peer (t, peer_info);
4693 peer_info_connect (peer_info, t);
4695 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4701 * Handler for disconnection requests of peers in a tunnel
4703 * @param cls closure
4704 * @param client identification of the client
4705 * @param message the actual message (PeerControl)
4708 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4709 const struct GNUNET_MessageHeader *message)
4711 struct GNUNET_MESH_PeerControl *peer_msg;
4712 struct MeshPeerInfo *peer_info;
4713 struct MeshClient *c;
4714 struct MeshTunnel *t;
4715 MESH_TunnelNumber tid;
4717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4718 /* Sanity check for client registration */
4719 if (NULL == (c = client_get (client)))
4722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4725 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4726 /* Sanity check for message size */
4727 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4730 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4734 /* Tunnel exists? */
4735 tid = ntohl (peer_msg->tunnel_id);
4736 t = tunnel_get_by_local_id (c, tid);
4740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4745 /* Does client own tunnel? */
4746 if (t->owner->handle != client)
4749 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4754 GNUNET_i2s (&peer_msg->peer));
4755 /* Is the peer in the tunnel? */
4757 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4758 if (NULL == peer_info)
4761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4765 /* Ok, delete peer from tunnel */
4766 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4767 &peer_msg->peer.hashPubKey);
4769 send_destroy_path (t, peer_info->id);
4770 tunnel_delete_peer (t, peer_info->id);
4771 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4776 * Handler for blacklist requests of peers in a tunnel
4778 * @param cls closure
4779 * @param client identification of the client
4780 * @param message the actual message (PeerControl)
4783 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
4784 const struct GNUNET_MessageHeader *message)
4786 struct GNUNET_MESH_PeerControl *peer_msg;
4787 struct MeshClient *c;
4788 struct MeshTunnel *t;
4789 MESH_TunnelNumber tid;
4791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
4792 /* Sanity check for client registration */
4793 if (NULL == (c = client_get (client)))
4796 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4799 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4801 /* Sanity check for message size */
4802 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4805 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4809 /* Tunnel exists? */
4810 tid = ntohl (peer_msg->tunnel_id);
4811 t = tunnel_get_by_local_id (c, tid);
4815 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4820 GNUNET_array_append(t->blacklisted, t->nblacklisted,
4821 GNUNET_PEER_intern(&peer_msg->peer));
4826 * Handler for unblacklist requests of peers in a tunnel
4828 * @param cls closure
4829 * @param client identification of the client
4830 * @param message the actual message (PeerControl)
4833 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
4834 const struct GNUNET_MessageHeader *message)
4836 struct GNUNET_MESH_PeerControl *peer_msg;
4837 struct MeshClient *c;
4838 struct MeshTunnel *t;
4839 MESH_TunnelNumber tid;
4843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
4844 /* Sanity check for client registration */
4845 if (NULL == (c = client_get (client)))
4848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4851 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4853 /* Sanity check for message size */
4854 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4861 /* Tunnel exists? */
4862 tid = ntohl (peer_msg->tunnel_id);
4863 t = tunnel_get_by_local_id (c, tid);
4867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4872 /* if peer is not known, complain */
4873 pid = GNUNET_PEER_search (&peer_msg->peer);
4880 /* search and remove from list */
4881 for (i = 0; i < t->nblacklisted; i++)
4883 if (t->blacklisted[i] == pid)
4885 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
4886 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
4891 /* if peer hasn't been blacklisted, complain */
4897 * Handler for connection requests to new peers by type
4899 * @param cls closure
4900 * @param client identification of the client
4901 * @param message the actual message (ConnectPeerByType)
4904 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4905 const struct GNUNET_MessageHeader *message)
4907 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4908 struct MeshClient *c;
4909 struct MeshTunnel *t;
4910 struct GNUNET_HashCode hash;
4911 MESH_TunnelNumber tid;
4913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4914 /* Sanity check for client registration */
4915 if (NULL == (c = client_get (client)))
4918 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4922 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4923 /* Sanity check for message size */
4924 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4925 ntohs (connect_msg->header.size))
4928 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4932 /* Tunnel exists? */
4933 tid = ntohl (connect_msg->tunnel_id);
4934 t = tunnel_get_by_local_id (c, tid);
4938 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4942 /* Does client own tunnel? */
4943 if (t->owner->handle != client)
4946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4950 /* Do WE have the service? */
4951 t->type = ntohl (connect_msg->type);
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4953 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4954 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4957 /* Yes! Fast forward, add ourselves to the tunnel and send the
4958 * good news to the client, and alert the destination client of
4959 * an incoming tunnel.
4961 * FIXME send a path create to self, avoid code duplication
4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4964 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4965 peer_info_get (&my_full_id),
4966 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4969 send_client_peer_connected (t, myid);
4970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4971 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4973 t->local_tid_dest = next_local_tid++;
4974 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4975 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4976 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4980 /* Ok, lets find a peer offering the service */
4981 if (NULL != t->dht_get_type)
4983 GNUNET_DHT_get_stop (t->dht_get_type);
4985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4986 GNUNET_h2s (&hash));
4988 GNUNET_DHT_get_start (dht_handle,
4989 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
4991 DHT_REPLICATION_LEVEL,
4992 GNUNET_DHT_RO_RECORD_ROUTE |
4993 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
4995 &dht_get_type_handler, t);
4997 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5003 * Handler for connection requests to new peers by a string service description.
5005 * @param cls closure
5006 * @param client identification of the client
5007 * @param message the actual message, which includes messages the client wants
5010 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
5011 const struct GNUNET_MessageHeader *message)
5013 struct GNUNET_MESH_ConnectPeerByString *msg;
5014 struct MeshRegexSearchContext *ctx;
5015 struct GNUNET_DHT_GetHandle *get_h;
5016 struct GNUNET_HashCode key;
5017 struct MeshTunnel *t;
5018 struct MeshClient *c;
5019 MESH_TunnelNumber tid;
5023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5024 "************************************************************\n");
5025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5026 "Connect by string started\n");
5027 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
5028 size = htons (message->size);
5030 /* Sanity check for client registration */
5031 if (NULL == (c = client_get (client)))
5034 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5038 /* Message size sanity check */
5039 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
5042 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5046 /* Tunnel exists? */
5047 tid = ntohl (msg->tunnel_id);
5048 t = tunnel_get_by_local_id (c, tid);
5052 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5056 /* Does client own tunnel? */
5057 if (t->owner->handle != client)
5060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5064 /* Only one connect_by_string allowed at the same time! */
5065 /* FIXME: allow more, return handle at api level to cancel, document */
5066 if (NULL != t->regex_ctx)
5069 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5073 /* Find string itself */
5074 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
5075 string = (const char *) &msg[1];
5077 /* Initialize context */
5078 size = GNUNET_REGEX_get_first_key(string, len, &key);
5079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5080 "************************************************************\n");
5081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5082 " consumed %u bits out of %u\n", size, len);
5083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5084 " looking for %s\n", GNUNET_h2s (&key));
5086 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
5087 ctx->position = size;
5090 ctx->description = GNUNET_malloc (len + 1);
5091 memcpy (ctx->description, string, len);
5092 ctx->description[len] = '\0';
5093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", ctx->description);
5095 ctx->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
5097 /* Start search in DHT */
5098 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
5099 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
5100 &key, /* key to search */
5101 DHT_REPLICATION_LEVEL, /* replication level */
5102 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5103 NULL, /* xquery */ // FIXME BLOOMFILTER
5104 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
5105 &dht_get_string_handler, ctx);
5107 GNUNET_break (GNUNET_OK ==
5108 GNUNET_CONTAINER_multihashmap_put(ctx->dht_get_handles,
5111 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
5113 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
5119 * Handler for client traffic directed to one peer
5121 * @param cls closure
5122 * @param client identification of the client
5123 * @param message the actual message
5126 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5127 const struct GNUNET_MessageHeader *message)
5129 struct MeshClient *c;
5130 struct MeshTunnel *t;
5131 struct MeshPeerInfo *pi;
5132 struct GNUNET_MESH_Unicast *data_msg;
5133 MESH_TunnelNumber tid;
5136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5137 "Got a unicast request from a client!\n");
5139 /* Sanity check for client registration */
5140 if (NULL == (c = client_get (client)))
5143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5146 data_msg = (struct GNUNET_MESH_Unicast *) message;
5147 /* Sanity check for message size */
5148 size = ntohs (message->size);
5149 if (sizeof (struct GNUNET_MESH_Unicast) +
5150 sizeof (struct GNUNET_MessageHeader) > size)
5153 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5157 /* Tunnel exists? */
5158 tid = ntohl (data_msg->tid);
5159 t = tunnel_get_by_local_id (c, tid);
5163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5167 /* Is it a local tunnel? Then, does client own the tunnel? */
5168 if (t->owner->handle != client)
5171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5175 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
5176 &data_msg->destination.hashPubKey);
5177 /* Is the selected peer in the tunnel? */
5181 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5185 /* Ok, everything is correct, send the message
5186 * (pretend we got it from a mesh peer)
5189 char buf[ntohs (message->size)] GNUNET_ALIGN;
5190 struct GNUNET_MESH_Unicast *copy;
5192 /* Work around const limitation */
5193 copy = (struct GNUNET_MESH_Unicast *) buf;
5194 memcpy (buf, data_msg, size);
5195 copy->oid = my_full_id;
5196 copy->tid = htonl (t->id.tid);
5197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5198 " calling generic handler...\n");
5199 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
5201 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5207 * Handler for client traffic directed to the origin
5209 * @param cls closure
5210 * @param client identification of the client
5211 * @param message the actual message
5214 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5215 const struct GNUNET_MessageHeader *message)
5217 struct GNUNET_MESH_ToOrigin *data_msg;
5218 struct GNUNET_PeerIdentity id;
5219 struct MeshClient *c;
5220 struct MeshTunnel *t;
5221 MESH_TunnelNumber tid;
5224 /* Sanity check for client registration */
5225 if (NULL == (c = client_get (client)))
5228 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5231 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5232 /* Sanity check for message size */
5233 size = ntohs (message->size);
5234 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5235 sizeof (struct GNUNET_MessageHeader) > size)
5238 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5242 /* Tunnel exists? */
5243 tid = ntohl (data_msg->tid);
5244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5245 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
5246 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5249 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5252 t = tunnel_get_by_local_id (c, tid);
5256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5260 /* It should be sent by someone who has this as incoming tunnel. */
5261 if (-1 == client_knows_tunnel (c, t))
5264 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5267 GNUNET_PEER_resolve (t->id.oid, &id);
5269 /* Ok, everything is correct, send the message
5270 * (pretend we got it from a mesh peer)
5273 char buf[ntohs (message->size)] GNUNET_ALIGN;
5274 struct GNUNET_MESH_ToOrigin *copy;
5276 /* Work around const limitation */
5277 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5278 memcpy (buf, data_msg, size);
5280 copy->tid = htonl (t->id.tid);
5281 copy->sender = my_full_id;
5282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5283 " calling generic handler...\n");
5284 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
5286 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5292 * Handler for client traffic directed to all peers in a tunnel
5294 * @param cls closure
5295 * @param client identification of the client
5296 * @param message the actual message
5299 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
5300 const struct GNUNET_MessageHeader *message)
5302 struct MeshClient *c;
5303 struct MeshTunnel *t;
5304 struct GNUNET_MESH_Multicast *data_msg;
5305 MESH_TunnelNumber tid;
5307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5308 "Got a multicast request from a client!\n");
5310 /* Sanity check for client registration */
5311 if (NULL == (c = client_get (client)))
5314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5317 data_msg = (struct GNUNET_MESH_Multicast *) message;
5318 /* Sanity check for message size */
5319 if (sizeof (struct GNUNET_MESH_Multicast) +
5320 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
5323 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5327 /* Tunnel exists? */
5328 tid = ntohl (data_msg->tid);
5329 t = tunnel_get_by_local_id (c, tid);
5333 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5337 /* Does client own tunnel? */
5338 if (t->owner->handle != client)
5341 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5346 char buf[ntohs (message->size)] GNUNET_ALIGN;
5347 struct GNUNET_MESH_Multicast *copy;
5349 copy = (struct GNUNET_MESH_Multicast *) buf;
5350 memcpy (buf, message, ntohs (message->size));
5351 copy->oid = my_full_id;
5352 copy->tid = htonl (t->id.tid);
5353 copy->ttl = htonl (DEFAULT_TTL);
5354 copy->mid = htonl (t->mid + 1);
5355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5356 " calling generic handler...\n");
5357 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
5360 /* receive done gets called when last copy is sent to a neighbor */
5365 * Functions to handle messages from clients
5367 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5368 {&handle_local_new_client, NULL,
5369 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5370 {&handle_local_announce_regex, NULL,
5371 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
5372 {&handle_local_tunnel_create, NULL,
5373 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5374 sizeof (struct GNUNET_MESH_TunnelMessage)},
5375 {&handle_local_tunnel_destroy, NULL,
5376 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5377 sizeof (struct GNUNET_MESH_TunnelMessage)},
5378 {&handle_local_connect_add, NULL,
5379 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
5380 sizeof (struct GNUNET_MESH_PeerControl)},
5381 {&handle_local_connect_del, NULL,
5382 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
5383 sizeof (struct GNUNET_MESH_PeerControl)},
5384 {&handle_local_blacklist, NULL,
5385 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
5386 sizeof (struct GNUNET_MESH_PeerControl)},
5387 {&handle_local_unblacklist, NULL,
5388 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
5389 sizeof (struct GNUNET_MESH_PeerControl)},
5390 {&handle_local_connect_by_type, NULL,
5391 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
5392 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
5393 {&handle_local_connect_by_string, NULL,
5394 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
5395 {&handle_local_unicast, NULL,
5396 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5397 {&handle_local_to_origin, NULL,
5398 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5399 {&handle_local_multicast, NULL,
5400 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5406 * To be called on core init/fail.
5408 * @param cls service closure
5409 * @param server handle to the server for this service
5410 * @param identity the public identity of this peer
5413 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5414 const struct GNUNET_PeerIdentity *identity)
5416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5417 core_handle = server;
5418 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5422 GNUNET_SCHEDULER_shutdown ();
5428 * Method called whenever a given peer connects.
5430 * @param cls closure
5431 * @param peer peer identity this notification is about
5432 * @param atsi performance data for the connection
5433 * @param atsi_count number of records in 'atsi'
5436 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
5437 const struct GNUNET_ATS_Information *atsi,
5438 unsigned int atsi_count)
5440 struct MeshPeerInfo *peer_info;
5441 struct MeshPeerPath *path;
5443 DEBUG_CONN ("Peer connected\n");
5444 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5445 peer_info = peer_info_get (peer);
5446 if (myid == peer_info->id)
5448 DEBUG_CONN (" (self)\n");
5453 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5455 path = path_new (2);
5456 path->peers[0] = myid;
5457 path->peers[1] = peer_info->id;
5458 GNUNET_PEER_change_rc (myid, 1);
5459 GNUNET_PEER_change_rc (peer_info->id, 1);
5460 peer_info_add_path (peer_info, path, GNUNET_YES);
5465 * Method called whenever a peer disconnects.
5467 * @param cls closure
5468 * @param peer peer identity this notification is about
5471 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5473 struct MeshPeerInfo *pi;
5474 struct MeshPeerQueue *q;
5475 struct MeshPeerQueue *n;
5477 DEBUG_CONN ("Peer disconnected\n");
5478 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5490 /* try to reroute this traffic instead */
5491 queue_destroy(q, GNUNET_YES);
5495 peer_info_remove_path (pi, pi->id, myid);
5498 DEBUG_CONN (" (self)\n");
5504 /******************************************************************************/
5505 /************************ MAIN FUNCTIONS ****************************/
5506 /******************************************************************************/
5509 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5511 * @param cls closure
5512 * @param key current key code
5513 * @param value value in the hash map
5514 * @return GNUNET_YES if we should continue to iterate,
5518 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5520 struct MeshTunnel *t = value;
5527 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5529 * @param cls closure
5530 * @param key current key code
5531 * @param value value in the hash map
5532 * @return GNUNET_YES if we should continue to iterate,
5536 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5538 struct MeshPeerInfo *p = value;
5539 struct MeshPeerQueue *q;
5540 struct MeshPeerQueue *n;
5548 queue_destroy(q, GNUNET_YES);
5552 peer_info_destroy (p);
5557 * Task run during shutdown.
5563 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5567 if (core_handle != NULL)
5569 GNUNET_CORE_disconnect (core_handle);
5572 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5573 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5574 if (dht_handle != NULL)
5576 GNUNET_DHT_disconnect (dht_handle);
5581 GNUNET_SERVER_notification_context_destroy (nc);
5584 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5586 GNUNET_SCHEDULER_cancel (announce_id_task);
5587 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5593 * Process mesh requests.
5595 * @param cls closure
5596 * @param server the initialized server
5597 * @param c configuration to use
5600 run (void *cls, struct GNUNET_SERVER_Handle *server,
5601 const struct GNUNET_CONFIGURATION_Handle *c)
5603 struct MeshPeerInfo *peer;
5604 struct MeshPeerPath *p;
5607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5608 server_handle = server;
5609 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5610 NULL, /* Closure passed to MESH functions */
5611 &core_init, /* Call core_init once connected */
5612 &core_connect, /* Handle connects */
5613 &core_disconnect, /* remove peers on disconnects */
5614 NULL, /* Don't notify about all incoming messages */
5615 GNUNET_NO, /* For header only in notification */
5616 NULL, /* Don't notify about all outbound messages */
5617 GNUNET_NO, /* For header-only out notification */
5618 core_handlers); /* Register these handlers */
5620 if (core_handle == NULL)
5623 GNUNET_SCHEDULER_shutdown ();
5628 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
5631 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5633 ("Mesh service is lacking key configuration settings. Exiting.\n"));
5634 GNUNET_SCHEDULER_shutdown ();
5637 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
5638 GNUNET_free (keyfile);
5639 if (my_private_key == NULL)
5641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5642 _("Mesh service could not access hostkey. Exiting.\n"));
5643 GNUNET_SCHEDULER_shutdown ();
5646 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
5647 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5648 &my_full_id.hashPubKey);
5649 myid = GNUNET_PEER_intern (&my_full_id);
5651 // transport_handle = GNUNET_TRANSPORT_connect(c,
5658 dht_handle = GNUNET_DHT_connect (c, 64);
5659 if (dht_handle == NULL)
5665 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5667 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5668 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5669 peers = GNUNET_CONTAINER_multihashmap_create (32);
5670 applications = GNUNET_CONTAINER_multihashmap_create (32);
5671 types = GNUNET_CONTAINER_multihashmap_create (32);
5673 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5674 nc = GNUNET_SERVER_notification_context_create (server_handle,
5676 GNUNET_SERVER_disconnect_notify (server_handle,
5677 &handle_local_client_disconnect, NULL);
5681 clients_tail = NULL;
5684 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5685 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5687 /* Create a peer_info for the local peer */
5688 peer = peer_info_get (&my_full_id);
5691 GNUNET_PEER_change_rc (myid, 1);
5692 peer_info_add_path (peer, p, GNUNET_YES);
5694 /* Scheduled the task to clean up when shutdown is called */
5695 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
5702 * The main function for the mesh service.
5704 * @param argc number of arguments from the command line
5705 * @param argv command line arguments
5706 * @return 0 ok, 1 on error
5709 main (int argc, char *const *argv)
5713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5716 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");