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_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_YES
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #if MESH_DEBUG_CONNECTION
64 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
66 #define DEBUG_CONN(...)
70 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
72 #define DEBUG_DHT(...)
75 /******************************************************************************/
76 /************************ DATA STRUCTURES ****************************/
77 /******************************************************************************/
79 /** FWD declaration */
84 * Struct representing a piece of data being sent to other peers
88 /** Tunnel it belongs to. */
91 /** In case of a multicast, task to allow a client to send more data if
92 * some neighbor is too slow. */
93 GNUNET_SCHEDULER_TaskIdentifier *task;
95 /** How many remaining neighbors we need to send this to. */
96 unsigned int *reference_counter;
98 /** Size of the data. */
107 * Struct containing info about a queued transmission to this peer
114 struct MeshPeerQueue *next;
119 struct MeshPeerQueue *prev;
122 * Peer this transmission is directed to.
124 struct MeshPeerInfo *peer;
127 * Tunnel this message belongs to.
129 struct MeshTunnel *tunnel;
132 * Pointer to info stucture used as cls.
142 * Size of the message
149 * Struct containing all info possibly needed to build a package when called
152 struct MeshTransmissionDescriptor
154 /** ID of the tunnel this packet travels in */
155 struct MESH_TunnelID *origin;
157 /** Who was this message being sent to */
158 struct MeshPeerInfo *peer;
160 /** Ultimate destination of the packet */
161 GNUNET_PEER_Id destination;
163 /** Data descriptor */
164 struct MeshData* mesh_data;
169 * Struct containing all information regarding a given peer
179 * Last time we heard from this peer
181 struct GNUNET_TIME_Absolute last_contact;
184 * Number of attempts to reconnect so far
186 int n_reconnect_attempts;
189 * Paths to reach the peer, ordered by ascending hop count
191 struct MeshPeerPath *path_head;
194 * Paths to reach the peer, ordered by ascending hop count
196 struct MeshPeerPath *path_tail;
199 * Handle to stop the DHT search for a path to this peer
201 struct GNUNET_DHT_GetHandle *dhtget;
204 * Closure given to the DHT GET
206 struct MeshPathInfo *dhtgetcls;
209 * Array of tunnels this peer participates in
210 * (most probably a small amount, therefore not a hashmap)
211 * When the path to the peer changes, notify these tunnels to let them
212 * re-adjust their path trees.
214 struct MeshTunnel **tunnels;
217 * Number of tunnels this peers participates in
219 unsigned int ntunnels;
222 * Transmission queue to core DLL head
224 struct MeshPeerQueue *queue_head;
227 * Transmission queue to core DLL tail
229 struct MeshPeerQueue *queue_tail;
232 * How many messages are in the queue to this peer.
234 unsigned int queue_n;
237 * Handle to for queued transmissions
239 struct GNUNET_CORE_TransmitHandle *core_transmit;
244 * Globally unique tunnel identification (owner + number)
245 * DO NOT USE OVER THE NETWORK
250 * Node that owns the tunnel
255 * Tunnel number to differentiate all the tunnels owned by the node oid
256 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
258 MESH_TunnelNumber tid;
262 struct MeshClient; /* FWD declaration */
265 * Struct containing all information regarding a tunnel
266 * For an intermediate node the improtant info used will be:
267 * - id Tunnel unique identification
268 * - paths[0] To know where to send it next
269 * - metainfo: ready, speeds, accounting
276 struct MESH_TunnelID id;
279 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
281 MESH_TunnelNumber local_tid;
284 * Local tunnel number for local destination clients (incoming number)
285 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
288 MESH_TunnelNumber local_tid_dest;
291 * Local count ID of the last packet seen/sent.
296 * SKIP value for this tunnel.
301 * How many messages are in the queue.
303 unsigned int queue_n;
306 * How many messages do we accept in the queue.
308 unsigned int queue_max;
311 * Is the speed on the tunnel limited to the slowest peer?
316 * Is the tunnel bufferless (minimum latency)?
321 * Flag to signal the destruction of the tunnel.
322 * If this is set GNUNET_YES the tunnel will be destroyed
323 * when the queue is empty.
328 * Last time the tunnel was used
330 struct GNUNET_TIME_Absolute timestamp;
333 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
334 * containing peers added by id or by type, not intermediate peers.
336 struct GNUNET_CONTAINER_MultiHashMap *peers;
339 * Number of peers that are connected and potentially ready to receive data
341 unsigned int peers_ready;
344 * Number of peers that have been added to the tunnel
346 unsigned int peers_total;
349 * Client owner of the tunnel, if any
351 struct MeshClient *owner;
354 * Clients that have been informed about the tunnel, if any
356 struct MeshClient **clients;
359 * Number of elements in clients
361 unsigned int nclients;
364 * Clients that have requested to leave the tunnel
366 struct MeshClient **ignore;
369 * Number of elements in clients
371 unsigned int nignore;
376 GNUNET_PEER_Id *blacklisted;
379 * Number of elements in blacklisted
381 unsigned int nblacklisted;
384 * Bloomfilter (for peer identities) to stop circular routes
386 char bloomfilter[MESH_BLOOM_SIZE];
391 struct MeshTunnelTree *tree;
394 * Application type we are looking for in this tunnel
396 GNUNET_MESH_ApplicationType type;
399 * Used to search peers offering a service
401 struct GNUNET_DHT_GetHandle *dht_get_type;
404 * Initial context of the regex search for a connect_by_string
406 struct MeshRegexSearchContext *regex_ctx;
409 * Task to keep the used paths alive
411 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
414 * Task to destroy the tunnel after timeout
416 * FIXME: merge the two? a tunnel will have either
417 * a path refresh OR a timeout, never both!
419 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
424 * Info needed to work with tunnel paths and peers
431 struct MeshTunnel *t;
434 * Neighbouring peer to whom we send the packet to
436 struct MeshPeerInfo *peer;
441 struct MeshPeerPath *path;
446 * Struct containing information about a client of the service
453 struct MeshClient *next;
458 struct MeshClient *prev;
461 * Tunnels that belong to this client, indexed by local id
463 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
466 * Tunnels this client has accepted, indexed by incoming local id
468 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
471 * Tunnels this client has rejected, indexed by incoming local id
473 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
475 * Handle to communicate with the client
477 struct GNUNET_SERVER_Client *handle;
480 * Applications that this client has claimed to provide
482 struct GNUNET_CONTAINER_MultiHashMap *apps;
485 * Messages that this client has declared interest in
487 struct GNUNET_CONTAINER_MultiHashMap *types;
490 * Whether the client is active or shutting down (don't send confirmations
491 * to a client that is shutting down.
496 * ID of the client, mainly for debug messages
501 * Regular expressions describing the services offered by this client.
503 char **regexes; // FIXME add timeout? API to remove a regex?
506 * Number of regular expressions in regexes.
508 unsigned int n_regex;
511 * Task to refresh all regular expresions in the DHT.
513 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
519 * Struct to keep information of searches of services described by a regex
520 * using a user-provided string service description.
522 struct MeshRegexSearchInfo
525 * Which tunnel is this for
527 struct MeshTunnel *t;
530 * User provided description of the searched service.
535 * Part of the description already consumed by the search.
542 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
545 * Results from running DHT GETs.
547 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
550 * Contexts, for each running DHT GET. Free all on end of search.
552 struct MeshRegexSearchContext **contexts;
555 * Number of contexts (branches/steps in search).
557 unsigned int n_contexts;
560 * Peer that is connecting via connect_by_string. When connected, free ctx.
565 * Other peers that are found but not yet being connected to.
567 GNUNET_PEER_Id *peers;
570 * Number of elements in peers.
572 unsigned int n_peers;
575 * Next peer to try to connect to.
580 * Timeout for a connect attempt.
581 * When reached, try to connect to a different peer, if any. If not,
582 * try the same peer again.
584 GNUNET_SCHEDULER_TaskIdentifier timeout;
589 * Struct to keep state of running searches that have consumed a part of
592 struct MeshRegexSearchContext
595 * Part of the description already consumed by
596 * this particular search branch.
601 * Information about the search.
603 struct MeshRegexSearchInfo *info;
607 /******************************************************************************/
608 /************************ DEBUG FUNCTIONS ****************************/
609 /******************************************************************************/
613 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
614 * @param cls string to print
615 * @param success GNUNET_OK if the PUT was transmitted,
616 * GNUNET_NO on timeout,
617 * GNUNET_SYSERR on disconnect from service
618 * after the PUT message was transmitted
619 * (so we don't know if it was received or not)
624 mesh_debug (void *cls, int success)
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
634 /******************************************************************************/
635 /*********************** GLOBAL VARIABLES ****************************/
636 /******************************************************************************/
640 * Configuration parameters
642 static struct GNUNET_TIME_Relative refresh_path_time;
643 static struct GNUNET_TIME_Relative app_announce_time;
644 static struct GNUNET_TIME_Relative id_announce_time;
645 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
646 static struct GNUNET_TIME_Relative connect_timeout;
647 static long long unsigned int default_ttl;
648 static long long unsigned int dht_replication_level;
652 * DLL with all the clients, head.
654 static struct MeshClient *clients;
657 * DLL with all the clients, tail.
659 static struct MeshClient *clients_tail;
662 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
664 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
667 * Tunnels incoming, indexed by MESH_TunnelNumber
668 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
670 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
673 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
675 static struct GNUNET_CONTAINER_MultiHashMap *peers;
678 * Handle to communicate with transport
680 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
683 * Handle to communicate with core.
685 static struct GNUNET_CORE_Handle *core_handle;
690 static struct GNUNET_DHT_Handle *dht_handle;
695 static struct GNUNET_SERVER_Handle *server_handle;
698 * Handle to the statistics service.
700 static struct GNUNET_STATISTICS_Handle *stats;
703 * Notification context, to send messages to local clients.
705 static struct GNUNET_SERVER_NotificationContext *nc;
708 * Local peer own ID (memory efficient handle).
710 static GNUNET_PEER_Id myid;
713 * Local peer own ID (full value).
715 static struct GNUNET_PeerIdentity my_full_id;
720 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
725 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
728 * Tunnel ID for the next created tunnel (global tunnel number).
730 static MESH_TunnelNumber next_tid;
733 * Tunnel ID for the next incoming tunnel (local tunnel number).
735 static MESH_TunnelNumber next_local_tid;
738 * All application types provided by this peer.
740 static struct GNUNET_CONTAINER_MultiHashMap *applications;
743 * All message types clients of this peer are interested in.
745 static struct GNUNET_CONTAINER_MultiHashMap *types;
748 * Task to periodically announce provided applications.
750 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
753 * Task to periodically announce itself in the network.
755 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
758 * Next ID to assign to a client.
760 unsigned int next_client_id;
763 /******************************************************************************/
764 /*********************** DECLARATIONS **************************/
765 /******************************************************************************/
767 /* FIXME move declarations here */
770 * Function to process paths received for a new peer addition. The recorded
771 * paths form the initial tunnel, which can be optimized later.
772 * Called on each result obtained for the DHT search.
775 * @param exp when will this value expire
776 * @param key key of the result
777 * @param type type of the result
778 * @param size number of bytes in data
779 * @param data pointer to the result data
782 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
783 const struct GNUNET_HashCode * key,
784 const struct GNUNET_PeerIdentity *get_path,
785 unsigned int get_path_length,
786 const struct GNUNET_PeerIdentity *put_path,
787 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
788 size_t size, const void *data);
792 * Function to process DHT string to regex matching.
793 * Called on each result obtained for the DHT search.
795 * @param cls closure (search context)
796 * @param exp when will this value expire
797 * @param key key of the result
798 * @param get_path path of the get request (not used)
799 * @param get_path_length lenght of get_path (not used)
800 * @param put_path path of the put request (not used)
801 * @param put_path_length length of the put_path (not used)
802 * @param type type of the result
803 * @param size number of bytes in data
804 * @param data pointer to the result data
806 * TODO: re-issue the request after certain time? cancel after X results?
809 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
810 const struct GNUNET_HashCode * key,
811 const struct GNUNET_PeerIdentity *get_path,
812 unsigned int get_path_length,
813 const struct GNUNET_PeerIdentity *put_path,
814 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
815 size_t size, const void *data);
819 * Function to process DHT string to regex matching.
820 * Called on each result obtained for the DHT search.
822 * @param cls closure (search context)
823 * @param exp when will this value expire
824 * @param key key of the result
825 * @param get_path path of the get request (not used)
826 * @param get_path_length lenght of get_path (not used)
827 * @param put_path path of the put request (not used)
828 * @param put_path_length length of the put_path (not used)
829 * @param type type of the result
830 * @param size number of bytes in data
831 * @param data pointer to the result data
834 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
835 const struct GNUNET_HashCode * key,
836 const struct GNUNET_PeerIdentity *get_path,
837 unsigned int get_path_length,
838 const struct GNUNET_PeerIdentity *put_path,
839 unsigned int put_path_length,
840 enum GNUNET_BLOCK_Type type,
841 size_t size, const void *data);
845 * Iterator over edges in a regex block retrieved from the DHT.
847 * @param cls Closure.
848 * @param token Token that follows to next state.
849 * @param len Lenght of token.
850 * @param key Hash of next state.
852 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
855 regex_edge_iterator (void *cls,
858 const struct GNUNET_HashCode *key);
862 * Find a path to a peer that offers a regex servcie compatible
863 * with a given string.
865 * @param key The key of the accepting state.
866 * @param ctx Context containing info about the string, tunnel, etc.
869 regex_find_path (const struct GNUNET_HashCode *key,
870 struct MeshRegexSearchContext *ctx);
874 * Queue and pass message to core when possible.
876 * @param cls Closure (type dependant).
877 * @param type Type of the message.
878 * @param size Size of the message.
879 * @param dst Neighbor to send message to.
880 * @param t Tunnel this message belongs to.
883 queue_add (void *cls, uint16_t type, size_t size,
884 struct MeshPeerInfo *dst, struct MeshTunnel *t);
887 * Free a transmission that was already queued with all resources
888 * associated to the request.
890 * @param queue Queue handler to cancel.
891 * @param clear_cls Is it necessary to free associated cls?
894 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
896 /******************************************************************************/
897 /************************ ITERATORS ****************************/
898 /******************************************************************************/
900 /* FIXME move iterators here */
903 * Iterator over found existing mesh regex blocks that match an ongoing search.
906 * @param key current key code
907 * @param value value in the hash map
908 * @return GNUNET_YES if we should continue to iterate,
912 regex_result_iterator (void *cls,
913 const struct GNUNET_HashCode * key,
916 struct MeshRegexBlock *block = value;
917 struct MeshRegexSearchContext *ctx = cls;
919 if (GNUNET_YES == ntohl(block->accepting) &&
920 ctx->position == strlen (ctx->info->description))
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
923 regex_find_path (key, ctx);
924 return GNUNET_YES; // We found an accept state!
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
929 ctx->position, strlen(ctx->info->description),
930 ntohl(block->accepting));
933 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
934 ®ex_edge_iterator, ctx);
940 * Iterator over edges in a regex block retrieved from the DHT.
942 * @param cls Closure (context of the search).
943 * @param token Token that follows to next state.
944 * @param len Lenght of token.
945 * @param key Hash of next state.
947 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
950 regex_edge_iterator (void *cls,
953 const struct GNUNET_HashCode *key)
955 struct MeshRegexSearchContext *ctx = cls;
956 struct MeshRegexSearchContext *new_ctx;
957 struct MeshRegexSearchInfo *info = ctx->info;
958 struct GNUNET_DHT_GetHandle *get_h;
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
965 current = &info->description[ctx->position];
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
967 current_len = strlen (info->description) - ctx->position;
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
972 if (len > current_len)
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
975 return GNUNET_YES; // Token too long, wont match
977 if (0 != strncmp (current, token, len))
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
980 return GNUNET_YES; // Token doesn't match
982 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
983 new_ctx->info = info;
984 new_ctx->position = ctx->position + len;
985 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
987 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
990 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
991 ®ex_result_iterator,
993 return GNUNET_YES; // We are already looking for it
995 /* Start search in DHT */
997 GNUNET_DHT_get_start (dht_handle, /* handle */
998 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
999 key, /* key to search */
1000 dht_replication_level, /* replication level */
1001 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1002 NULL, /* xquery */ // FIXME BLOOMFILTER
1003 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1004 &dht_get_string_handler, new_ctx);
1006 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1007 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1017 * Iterator over hash map entries to cancel DHT GET requests after a
1018 * successful connect_by_string.
1020 * @param cls Closure (unused).
1021 * @param key Current key code (unused).
1022 * @param value Value in the hash map (get handle).
1023 * @return GNUNET_YES if we should continue to iterate,
1027 regex_cancel_dht_get (void *cls,
1028 const struct GNUNET_HashCode * key,
1031 struct GNUNET_DHT_GetHandle *h = value;
1033 GNUNET_DHT_get_stop (h);
1039 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1040 * search for connect_by_string.
1042 * @param cls Closure (unused).
1043 * @param key Current key code (unused).
1044 * @param value MeshRegexBlock in the hash map.
1045 * @return GNUNET_YES if we should continue to iterate,
1049 regex_free_result (void *cls,
1050 const struct GNUNET_HashCode * key,
1054 GNUNET_free (value);
1060 * Regex callback iterator to store own service description in the DHT.
1062 * @param cls closure.
1063 * @param key hash for current state.
1064 * @param proof proof for current state.
1065 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1066 * @param num_edges number of edges leaving current state.
1067 * @param edges edges leaving current state.
1070 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1071 int accepting, unsigned int num_edges,
1072 const struct GNUNET_REGEX_Edge *edges)
1074 struct MeshRegexBlock *block;
1075 struct MeshRegexEdge *block_edge;
1076 enum GNUNET_DHT_RouteOption opt;
1080 unsigned int offset;
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 " regex dht put for state %s\n",
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1094 if (GNUNET_YES == accepting)
1096 struct MeshRegexAccept block;
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1099 " state %s is accepting, putting own id\n",
1101 size = sizeof (block);
1103 block.id = my_full_id;
1105 GNUNET_DHT_put(dht_handle, key,
1106 dht_replication_level,
1107 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1108 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1111 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1116 len = strlen(proof);
1117 size = sizeof (struct MeshRegexBlock) + len;
1118 block = GNUNET_malloc (size);
1121 block->n_proof = htonl (len);
1122 block->n_edges = htonl (num_edges);
1123 block->accepting = htonl (accepting);
1125 /* Store the proof at the end of the block. */
1126 aux = (char *) &block[1];
1127 memcpy (aux, proof, len);
1130 /* Store each edge in a variable length MeshEdge struct at the
1131 * very end of the MeshRegexBlock structure.
1133 for (i = 0; i < num_edges; i++)
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 " edge %s towards %s\n",
1138 GNUNET_h2s(&edges[i].destination));
1140 /* aux points at the end of the last block */
1141 len = strlen (edges[i].label);
1142 size += sizeof (struct MeshRegexEdge) + len;
1143 // Calculate offset FIXME is this ok? use size instead?
1144 offset = aux - (char *) block;
1145 block = GNUNET_realloc (block, size);
1146 aux = &((char *) block)[offset];
1147 block_edge = (struct MeshRegexEdge *) aux;
1148 block_edge->key = edges[i].destination;
1149 block_edge->n_token = htonl (len);
1150 aux = (char *) &block_edge[1];
1151 memcpy (aux, edges[i].label, len);
1155 GNUNET_DHT_put(dht_handle, key,
1156 dht_replication_level,
1158 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1160 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1164 GNUNET_free (block);
1169 * Store the regular expression describing a local service into the DHT.
1171 * @param regex The regular expresion.
1174 regex_put (const char *regex)
1176 struct GNUNET_REGEX_Automaton *dfa;
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1179 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1180 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1181 GNUNET_REGEX_automaton_destroy (dfa);
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1187 * Find a path to a peer that offers a regex servcie compatible
1188 * with a given string.
1190 * @param key The key of the accepting state.
1191 * @param ctx Context containing info about the string, tunnel, etc.
1194 regex_find_path (const struct GNUNET_HashCode *key,
1195 struct MeshRegexSearchContext *ctx)
1197 struct GNUNET_DHT_GetHandle *get_h;
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1200 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1201 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1202 key, /* key to search */
1203 dht_replication_level, /* replication level */
1204 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1205 GNUNET_DHT_RO_RECORD_ROUTE,
1206 NULL, /* xquery */ // FIXME BLOOMFILTER
1207 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1208 &dht_get_string_accept_handler, ctx);
1209 GNUNET_break (GNUNET_OK ==
1210 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1213 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1217 /******************************************************************************/
1218 /************************ PERIODIC FUNCTIONS ****************************/
1219 /******************************************************************************/
1222 * Announce iterator over for each application provided by the peer
1224 * @param cls closure
1225 * @param key current key code
1226 * @param value value in the hash map
1227 * @return GNUNET_YES if we should continue to
1232 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1234 struct PBlock block;
1235 struct MeshClient *c;
1237 block.id = my_full_id;
1238 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1239 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1240 if (0 == block.type)
1245 block.type = htonl (block.type);
1246 /* FIXME are hashes in multihash map equal on all aquitectures? */
1247 /* FIXME: keep return value of 'put' to possibly cancel!? */
1248 GNUNET_DHT_put (dht_handle, key,
1249 dht_replication_level,
1250 GNUNET_DHT_RO_RECORD_ROUTE |
1251 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1252 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1254 (const char *) &block,
1255 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1257 app_announce_time, NULL, NULL);
1263 * Periodically announce what applications are provided by local clients
1266 * @param cls closure
1267 * @param tc task context
1270 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1272 struct MeshClient *c = cls;
1275 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1277 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1281 DEBUG_DHT ("Starting PUT for regex\n");
1283 for (i = 0; i < c->n_regex; i++)
1285 regex_put (c->regexes[i]);
1287 c->regex_announce_task =
1288 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1289 DEBUG_DHT ("Finished PUT for regex\n");
1296 * Periodically announce what applications are provided by local clients
1299 * @param cls closure
1300 * @param tc task context
1303 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1305 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1307 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1311 DEBUG_DHT ("Starting PUT for apps\n");
1313 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1315 announce_applications_task =
1316 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1318 DEBUG_DHT ("Finished PUT for apps\n");
1325 * Periodically announce self id in the DHT
1327 * @param cls closure
1328 * @param tc task context
1331 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1333 struct PBlock block;
1335 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1337 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1341 * - Set data expiration in function of X
1342 * - Adapt X to churn
1344 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1346 block.id = my_full_id;
1347 block.type = htonl (0);
1348 GNUNET_DHT_put (dht_handle, /* DHT handle */
1349 &my_full_id.hashPubKey, /* Key to use */
1350 dht_replication_level, /* Replication level */
1351 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1352 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1353 sizeof (block), /* Size of the data */
1354 (const char *) &block, /* Data itself */
1355 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1356 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1357 NULL, /* Continuation */
1358 NULL); /* Continuation closure */
1360 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1364 /******************************************************************************/
1365 /****************** GENERAL HELPER FUNCTIONS ************************/
1366 /******************************************************************************/
1369 * Cancel an ongoing regex search in the DHT and free all resources.
1371 * @param ctx The search context.
1374 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1376 struct MeshRegexSearchInfo *info = ctx->info;
1379 GNUNET_free (info->description);
1380 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1381 ®ex_cancel_dht_get, NULL);
1382 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1383 ®ex_free_result, NULL);
1384 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1385 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1386 info->t->regex_ctx = NULL;
1387 for (i = 0; i < info->n_contexts; i++)
1389 GNUNET_free (info->contexts[i]);
1391 if (0 < info->n_contexts)
1392 GNUNET_free (info->contexts);
1393 if (0 < info->n_peers)
1394 GNUNET_free (info->peers);
1395 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1397 GNUNET_SCHEDULER_cancel(info->timeout);
1403 * Search for a tunnel by global ID using full PeerIdentities
1405 * @param oid owner of the tunnel
1406 * @param tid global tunnel number
1408 * @return tunnel handler, NULL if doesn't exist
1410 static struct MeshTunnel *
1411 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1415 * Delete an active client from the tunnel.
1421 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1424 * Notify a tunnel that a connection has broken that affects at least
1425 * some of its peers.
1427 * @param t Tunnel affected.
1428 * @param p1 Peer that got disconnected from p2.
1429 * @param p2 Peer that got disconnected from p1.
1431 * @return Short ID of the peer disconnected (either p1 or p2).
1432 * 0 if the tunnel remained unaffected.
1434 static GNUNET_PEER_Id
1435 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1440 * Check if client has registered with the service and has not disconnected
1442 * @param client the client to check
1444 * @return non-NULL if client exists in the global DLL
1446 static struct MeshClient *
1447 client_get (struct GNUNET_SERVER_Client *client)
1449 struct MeshClient *c;
1454 if (c->handle == client)
1463 * Checks if a given client has subscribed to certain message type
1465 * @param message_type Type of message to check
1466 * @param c Client to check
1468 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1471 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1473 struct GNUNET_HashCode hc;
1475 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1476 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1481 * Allow a client to send more data after transmitting a multicast message
1482 * which some neighbor has not yet accepted altough a reasonable time has
1485 * @param cls Closure (DataDescriptor containing the task identifier)
1486 * @param tc Task Context
1488 * FIXME reference counter cshould be just int
1491 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1493 struct MeshData *mdata = cls;
1495 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1497 GNUNET_assert (NULL != mdata->reference_counter);
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1499 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1500 *(mdata->reference_counter));
1501 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1502 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1507 * Check whether client wants traffic from a tunnel.
1509 * @param c Client to check.
1510 * @param t Tunnel to be found.
1512 * @return GNUNET_YES if client knows tunnel.
1514 * TODO look in client hashmap
1517 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1521 for (i = 0; i < t->nclients; i++)
1522 if (t->clients[i] == c)
1529 * Check whether client has been informed about a tunnel.
1531 * @param c Client to check.
1532 * @param t Tunnel to be found.
1534 * @return GNUNET_YES if client knows tunnel.
1536 * TODO look in client hashmap
1539 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1543 for (i = 0; i < t->nignore; i++)
1544 if (t->ignore[i] == c)
1546 return client_wants_tunnel(c, t);
1551 * Marks a client as uninterested in traffic from the tunnel, updating both
1552 * client and tunnel to reflect this.
1554 * @param c Client that doesn't want traffic anymore.
1555 * @param t Tunnel which should be ignored.
1557 * FIXME when to delete an incoming tunnel?
1560 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1562 struct GNUNET_HashCode hash;
1564 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1565 GNUNET_break (GNUNET_YES ==
1566 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1568 GNUNET_break (GNUNET_YES ==
1569 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1570 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1571 tunnel_delete_active_client (t, c);
1572 GNUNET_array_append (t->ignore, t->nignore, c);
1577 * Deletes a tunnel from a client (either owner or destination). To be used on
1578 * tunnel destroy, otherwise, use client_ignore_tunnel.
1580 * @param c Client whose tunnel to delete.
1581 * @param t Tunnel which should be deleted.
1584 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1586 struct GNUNET_HashCode hash;
1590 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1591 GNUNET_assert (GNUNET_YES ==
1592 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1598 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1600 GNUNET_assert (GNUNET_YES ==
1601 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1605 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1614 * Send the message to all clients that have subscribed to its type
1616 * @param msg Pointer to the message itself
1617 * @param payload Pointer to the payload of the message.
1618 * @return number of clients this message was sent to
1621 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1622 const struct GNUNET_MessageHeader *payload)
1624 struct GNUNET_PeerIdentity *oid;
1625 struct MeshClient *c;
1626 struct MeshTunnel *t;
1627 MESH_TunnelNumber *tid;
1630 char cbuf[htons (msg->size)];
1632 type = ntohs (payload->type);
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1636 memcpy (cbuf, msg, sizeof (cbuf));
1637 switch (htons (msg->type))
1639 struct GNUNET_MESH_Unicast *uc;
1640 struct GNUNET_MESH_Multicast *mc;
1641 struct GNUNET_MESH_ToOrigin *to;
1643 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1644 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1648 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1649 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1653 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1654 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1662 t = tunnel_get (oid, ntohl (*tid));
1669 for (count = 0, c = clients; c != NULL; c = c->next)
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1672 if (client_is_subscribed (type, c))
1674 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1678 *tid = htonl (t->local_tid);
1682 if (GNUNET_NO == client_knows_tunnel (c, t))
1684 /* This client doesn't know the tunnel */
1685 struct GNUNET_MESH_TunnelNotification tmsg;
1686 struct GNUNET_HashCode hash;
1688 tmsg.header.size = htons (sizeof (tmsg));
1689 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1690 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1691 tmsg.tunnel_id = htonl (t->local_tid_dest);
1692 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1693 &tmsg.header, GNUNET_NO);
1694 GNUNET_array_append (t->clients, t->nclients, c);
1695 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1697 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1698 c->incoming_tunnels, &hash, t,
1699 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1701 *tid = htonl (t->local_tid_dest);
1704 /* Check if the client wants to get traffic from the tunnel */
1705 if (GNUNET_NO == client_wants_tunnel(c, t))
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1709 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1710 (struct GNUNET_MessageHeader
1711 *) cbuf, GNUNET_YES);
1719 * Notify the client that owns the tunnel that a peer has connected to it
1720 * (the requested path to it has been confirmed).
1722 * @param t Tunnel whose owner to notify
1723 * @param id Short id of the peer that has connected
1726 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1728 struct GNUNET_MESH_PeerControl pc;
1730 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1731 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1732 pc.tunnel_id = htonl (t->local_tid);
1733 GNUNET_PEER_resolve (id, &pc.peer);
1734 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1740 * Notify all clients (not depending on registration status) that the incoming
1741 * tunnel is no longer valid.
1743 * @param t Tunnel that was destroyed.
1746 send_clients_tunnel_destroy (struct MeshTunnel *t)
1748 struct GNUNET_MESH_TunnelMessage msg;
1750 msg.header.size = htons (sizeof (msg));
1751 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1752 msg.tunnel_id = htonl (t->local_tid_dest);
1753 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1758 * Notify clients of tunnel disconnections, if needed.
1759 * In case the origin disconnects, the destination clients get a tunnel destroy
1760 * notification. If the last destination disconnects (only one remaining client
1761 * in tunnel), the origin gets a (local ID) peer disconnected.
1762 * Note that the function must be called BEFORE removing the client from
1765 * @param t Tunnel that was destroyed.
1766 * @param c Client that disconnected.
1769 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1775 struct GNUNET_MESH_TunnelMessage msg;
1777 msg.header.size = htons (sizeof (msg));
1778 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1779 msg.tunnel_id = htonl (t->local_tid_dest);
1780 for (i = 0; i < t->nclients; i++)
1781 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1782 &msg.header, GNUNET_NO);
1784 // FIXME when to disconnect an incoming tunnel?
1785 else if (1 == t->nclients && NULL != t->owner)
1787 struct GNUNET_MESH_PeerControl msg;
1789 msg.header.size = htons (sizeof (msg));
1790 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1791 msg.tunnel_id = htonl (t->local_tid);
1792 msg.peer = my_full_id;
1793 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1794 &msg.header, GNUNET_NO);
1800 * Decrements the reference counter and frees all resources if needed
1802 * @param mesh_data Data Descriptor used in a multicast message.
1803 * Freed no longer needed (last message).
1806 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1808 /* Make sure it's a multicast packet */
1809 GNUNET_assert (NULL != mesh_data->reference_counter);
1811 if (0 == --(*(mesh_data->reference_counter)))
1813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1814 if (NULL != mesh_data->task)
1816 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1818 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1820 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1822 GNUNET_free (mesh_data->task);
1824 GNUNET_free (mesh_data->reference_counter);
1825 GNUNET_free (mesh_data->data);
1826 GNUNET_free (mesh_data);
1832 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1833 * and insert it in the appropiate structures if the peer is not known yet.
1835 * @param peer Full identity of the peer.
1837 * @return Existing or newly created peer info.
1839 static struct MeshPeerInfo *
1840 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1842 struct MeshPeerInfo *peer_info;
1844 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1845 if (NULL == peer_info)
1848 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1849 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1850 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1851 peer_info->id = GNUNET_PEER_intern (peer);
1859 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1860 * and insert it in the appropiate structures if the peer is not known yet.
1862 * @param peer Short identity of the peer.
1864 * @return Existing or newly created peer info.
1866 static struct MeshPeerInfo *
1867 peer_info_get_short (const GNUNET_PEER_Id peer)
1869 struct GNUNET_PeerIdentity id;
1871 GNUNET_PEER_resolve (peer, &id);
1872 return peer_info_get (&id);
1877 * Iterator to remove the tunnel from the list of tunnels a peer participates
1880 * @param cls Closure (tunnel info)
1881 * @param key GNUNET_PeerIdentity of the peer (unused)
1882 * @param value PeerInfo of the peer
1884 * @return always GNUNET_YES, to keep iterating
1887 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1889 struct MeshTunnel *t = cls;
1890 struct MeshPeerInfo *peer = value;
1893 for (i = 0; i < peer->ntunnels; i++)
1896 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1899 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1900 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1909 * Core callback to write a pre-constructed data packet to core buffer
1911 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1912 * @param size Number of bytes available in buf.
1913 * @param buf Where the to write the message.
1915 * @return number of bytes written to buf
1918 send_core_data_raw (void *cls, size_t size, void *buf)
1920 struct MeshTransmissionDescriptor *info = cls;
1921 struct GNUNET_MessageHeader *msg;
1924 GNUNET_assert (NULL != info);
1925 GNUNET_assert (NULL != info->mesh_data);
1926 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1927 total_size = ntohs (msg->size);
1929 if (total_size > size)
1934 memcpy (buf, msg, total_size);
1935 data_descriptor_decrement_rc (info->mesh_data);
1942 * Sends an already built unicast message to a peer, properly registrating
1943 * all used resources.
1945 * @param message Message to send. Function makes a copy of it.
1946 * @param peer Short ID of the neighbor whom to send the message.
1947 * @param t Tunnel on which this message is transmitted.
1950 send_message (const struct GNUNET_MessageHeader *message,
1951 const struct GNUNET_PeerIdentity *peer,
1952 struct MeshTunnel *t)
1954 struct MeshTransmissionDescriptor *info;
1955 struct MeshPeerInfo *neighbor;
1956 struct MeshPeerPath *p;
1959 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1961 size = ntohs (message->size);
1962 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1963 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1964 info->mesh_data->data = GNUNET_malloc (size);
1965 memcpy (info->mesh_data->data, message, size);
1966 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
1968 struct GNUNET_MESH_Unicast *m;
1970 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
1971 m->ttl = htonl (ntohl (m->ttl) - 1);
1973 info->mesh_data->data_len = size;
1974 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
1975 *info->mesh_data->reference_counter = 1;
1976 neighbor = peer_info_get (peer);
1977 for (p = neighbor->path_head; NULL != p; p = p->next)
1986 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1987 GNUNET_free (info->mesh_data->data);
1988 GNUNET_free (info->mesh_data);
1992 info->peer = neighbor;
1994 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
2002 * Sends a CREATE PATH message for a path to a peer, properly registrating
2003 * all used resources.
2005 * @param peer PeerInfo of the final peer for whom this path is being created.
2006 * @param p Path itself.
2007 * @param t Tunnel for which the path is created.
2010 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2011 struct MeshTunnel *t)
2013 struct GNUNET_PeerIdentity id;
2014 struct MeshPathInfo *path_info;
2015 struct MeshPeerInfo *neighbor;
2021 p = tree_get_path_to_peer (t->tree, peer->id);
2028 for (i = 0; i < p->length; i++)
2030 if (p->peers[i] == myid)
2033 if (i >= p->length - 1)
2039 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2041 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2042 path_info->path = p;
2044 neighbor = peer_info_get (&id);
2045 path_info->peer = neighbor;
2046 queue_add (path_info,
2047 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2048 sizeof (struct GNUNET_MESH_ManipulatePath) +
2049 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2056 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2058 * @param t Tunnel whose path to destroy.
2059 * @param destination Short ID of the peer to whom the path to destroy.
2062 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2064 struct MeshPeerPath *p;
2067 p = tree_get_path_to_peer (t->tree, destination);
2073 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2074 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2076 struct GNUNET_MESH_ManipulatePath *msg;
2077 struct GNUNET_PeerIdentity *pi;
2081 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2082 msg->header.size = htons (size);
2083 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2084 msg->tid = htonl (t->id.tid);
2085 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2086 for (i = 0; i < p->length; i++)
2088 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2090 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2097 * Try to establish a new connection to this peer.
2098 * Use the best path for the given tunnel.
2099 * If the peer doesn't have any path to it yet, try to get one.
2100 * If the peer already has some path, send a CREATE PATH towards it.
2102 * @param peer PeerInfo of the peer.
2103 * @param t Tunnel for which to create the path, if possible.
2106 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2108 struct MeshPeerPath *p;
2109 struct MeshPathInfo *path_info;
2111 if (NULL != peer->path_head)
2113 p = tree_get_path_to_peer (t->tree, peer->id);
2120 // FIXME always send create path to self
2123 send_create_path (peer, p, t);
2127 struct GNUNET_HashCode hash;
2130 send_client_peer_connected (t, myid);
2131 t->local_tid_dest = next_local_tid++;
2132 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2135 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2136 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2143 else if (NULL == peer->dhtget)
2145 struct GNUNET_PeerIdentity id;
2147 GNUNET_PEER_resolve (peer->id, &id);
2148 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2149 path_info->peer = peer;
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2152 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2153 peer->dhtgetcls = path_info;
2154 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2155 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2156 &id.hashPubKey, /* key to search */
2157 dht_replication_level, /* replication level */
2158 GNUNET_DHT_RO_RECORD_ROUTE |
2159 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2160 NULL, /* xquery */ // FIXME BLOOMFILTER
2161 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2162 &dht_get_id_handler, path_info);
2164 /* Otherwise, there is no path but the DHT get is already started. */
2169 * Task to delay the connection of a peer
2171 * @param cls Closure (path info with tunnel and peer to connect).
2172 * Will be free'd on exection.
2173 * @param tc TaskContext
2176 peer_info_connect_task (void *cls,
2177 const struct GNUNET_SCHEDULER_TaskContext *tc)
2179 struct MeshPathInfo *path_info = cls;
2181 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2186 peer_info_connect (path_info->peer, path_info->t);
2192 * Destroy the peer_info and free any allocated resources linked to it
2194 * @param pi The peer_info to destroy.
2196 * @return GNUNET_OK on success
2199 peer_info_destroy (struct MeshPeerInfo *pi)
2201 struct GNUNET_PeerIdentity id;
2202 struct MeshPeerPath *p;
2203 struct MeshPeerPath *nextp;
2205 GNUNET_PEER_resolve (pi->id, &id);
2206 GNUNET_PEER_change_rc (pi->id, -1);
2209 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2212 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2213 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2215 if (NULL != pi->dhtget)
2217 GNUNET_DHT_get_stop (pi->dhtget);
2218 GNUNET_free (pi->dhtgetcls);
2224 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2234 * Remove all paths that rely on a direct connection between p1 and p2
2235 * from the peer itself and notify all tunnels about it.
2237 * @param peer PeerInfo of affected peer.
2238 * @param p1 GNUNET_PEER_Id of one peer.
2239 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2242 * TODO: optimize (see below)
2245 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2248 struct MeshPeerPath *p;
2249 struct MeshPeerPath *aux;
2250 struct MeshPeerInfo *peer_d;
2252 unsigned int destroyed;
2257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2259 p = peer->path_head;
2263 for (i = 0; i < (p->length - 1); i++)
2265 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2266 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2268 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2279 for (i = 0; i < peer->ntunnels; i++)
2281 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2285 * Problem: one or more peers have been deleted from the tunnel tree.
2286 * We don't know who they are to try to add them again.
2287 * We need to try to find a new path for each of the disconnected peers.
2288 * Some of them might already have a path to reach them that does not
2289 * involve p1 and p2. Adding all anew might render in a better tree than
2290 * the trivial immediate fix.
2292 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2293 * its children will be reachable trough him.
2295 peer_d = peer_info_get_short (d);
2298 for (p = peer_d->path_head; NULL != p; p = p->next)
2300 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2308 /* No callback, as peer will be already disconnected and a connection
2309 * scheduled by tunnel_notify_connection_broken.
2311 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2315 peer_info_connect (peer_d, peer->tunnels[i]);
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2323 * Add the path to the peer and update the path used to reach it in case this
2326 * @param peer_info Destination peer to add the path to.
2327 * @param path New path to add. Last peer must be the peer in arg 1.
2328 * Path will be either used of freed if already known.
2329 * @param trusted Do we trust that this path is real?
2332 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2335 struct MeshPeerPath *aux;
2339 if ((NULL == peer_info) || (NULL == path))
2342 path_destroy (path);
2345 if (path->peers[path->length - 1] != peer_info->id)
2348 path_destroy (path);
2351 if (path->length <= 2 && GNUNET_NO == trusted)
2353 /* Only allow CORE to tell us about direct paths */
2354 path_destroy (path);
2357 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2358 for (l = 1; l < path->length; l++)
2360 if (path->peers[l] == myid)
2362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2363 for (l2 = 0; l2 < path->length - l; l2++)
2365 path->peers[l2] = path->peers[l + l2];
2370 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2375 struct GNUNET_PeerIdentity id;
2377 GNUNET_PEER_resolve (peer_info->id, &id);
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2379 path->length, GNUNET_i2s (&id));
2382 l = path_get_length (path);
2389 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2390 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2392 l2 = path_get_length (aux);
2395 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2396 peer_info->path_tail, aux, path);
2401 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2403 path_destroy (path);
2408 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2415 * Add the path to the origin peer and update the path used to reach it in case
2416 * this is the shortest.
2417 * The path is given in peer_info -> destination, therefore we turn the path
2418 * upside down first.
2420 * @param peer_info Peer to add the path to, being the origin of the path.
2421 * @param path New path to add after being inversed.
2422 * @param trusted Do we trust that this path is real?
2425 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2426 struct MeshPeerPath *path, int trusted)
2429 peer_info_add_path (peer_info, path, trusted);
2434 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2435 * to obtain a local peer -> destination path and interning the peer ids.
2437 * @return Newly allocated and created path
2439 static struct MeshPeerPath *
2440 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2441 unsigned int get_path_length,
2442 const struct GNUNET_PeerIdentity *put_path,
2443 unsigned int put_path_length)
2445 struct MeshPeerPath *p;
2451 GNUNET_PEER_change_rc (myid, 1);
2452 i = get_path_length;
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2454 for (i--; i >= 0; i--)
2456 id = GNUNET_PEER_intern (&get_path[i]);
2457 if (p->length > 0 && id == p->peers[p->length - 1])
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2460 GNUNET_PEER_change_rc (id, -1);
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2465 GNUNET_i2s (&get_path[i]));
2467 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2468 p->peers[p->length - 1] = id;
2471 i = put_path_length;
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2473 for (i--; i >= 0; i--)
2475 id = GNUNET_PEER_intern (&put_path[i]);
2478 /* PUT path went through us, so discard the path up until now and start
2479 * from here to get a much shorter (and loop-free) path.
2484 if (p->length > 0 && id == p->peers[p->length - 1])
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2487 GNUNET_PEER_change_rc (id, -1);
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2492 GNUNET_i2s (&put_path[i]));
2494 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2495 p->peers[p->length - 1] = id;
2499 if (get_path_length > 0)
2500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2501 GNUNET_i2s (&get_path[0]));
2502 if (put_path_length > 0)
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2504 GNUNET_i2s (&put_path[0]));
2505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2507 for (i = 0; i < p->length; i++)
2509 struct GNUNET_PeerIdentity peer_id;
2511 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2513 GNUNET_i2s (&peer_id));
2521 * Adds a path to the peer_infos of all the peers in the path
2523 * @param p Path to process.
2524 * @param confirmed Whether we know if the path works or not. FIXME use
2527 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2531 /* TODO: invert and add */
2532 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2533 for (i++; i < p->length; i++)
2535 struct MeshPeerInfo *aux;
2536 struct MeshPeerPath *copy;
2538 aux = peer_info_get_short (p->peers[i]);
2539 copy = path_duplicate (p);
2540 copy->length = i + 1;
2541 peer_info_add_path (aux, copy, GNUNET_NO);
2547 * Send keepalive packets for a peer
2549 * @param cls Closure (tunnel for which to send the keepalive).
2550 * @param tc Notification context.
2552 * TODO: implement explicit multicast keepalive?
2555 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2559 * Search for a tunnel among the incoming tunnels
2561 * @param tid the local id of the tunnel
2563 * @return tunnel handler, NULL if doesn't exist
2565 static struct MeshTunnel *
2566 tunnel_get_incoming (MESH_TunnelNumber tid)
2568 struct GNUNET_HashCode hash;
2570 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2571 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2572 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2577 * Search for a tunnel among the tunnels for a client
2579 * @param c the client whose tunnels to search in
2580 * @param tid the local id of the tunnel
2582 * @return tunnel handler, NULL if doesn't exist
2584 static struct MeshTunnel *
2585 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2587 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2589 return tunnel_get_incoming (tid);
2593 struct GNUNET_HashCode hash;
2595 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2596 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2602 * Search for a tunnel by global ID using PEER_ID
2604 * @param pi owner of the tunnel
2605 * @param tid global tunnel number
2607 * @return tunnel handler, NULL if doesn't exist
2609 static struct MeshTunnel *
2610 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2612 struct MESH_TunnelID id;
2613 struct GNUNET_HashCode hash;
2618 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2619 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2624 * Search for a tunnel by global ID using full PeerIdentities
2626 * @param oid owner of the tunnel
2627 * @param tid global tunnel number
2629 * @return tunnel handler, NULL if doesn't exist
2631 static struct MeshTunnel *
2632 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2634 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2639 * Delete an active client from the tunnel.
2645 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2649 for (i = 0; i < t->nclients; i++)
2651 if (t->clients[i] == c)
2653 t->clients[i] = t->clients[t->nclients - 1];
2654 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2662 * Delete an ignored client from the tunnel.
2668 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2672 for (i = 0; i < t->nignore; i++)
2674 if (t->ignore[i] == c)
2676 t->ignore[i] = t->ignore[t->nignore - 1];
2677 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2685 * Delete a client from the tunnel. It should be only done on
2686 * client disconnection, otherwise use client_ignore_tunnel.
2692 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2694 tunnel_delete_ignored_client (t, c);
2695 tunnel_delete_active_client (t, c);
2700 * Callback used to notify a client owner of a tunnel that a peer has
2701 * disconnected, most likely because of a path change.
2703 * @param cls Closure (tunnel this notification is about).
2704 * @param peer_id Short ID of disconnected peer.
2707 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2709 struct MeshTunnel *t = cls;
2710 struct MeshPeerInfo *peer;
2711 struct MeshPathInfo *path_info;
2713 if (NULL != t->owner && NULL != nc)
2715 struct GNUNET_MESH_PeerControl msg;
2717 msg.header.size = htons (sizeof (msg));
2718 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2719 msg.tunnel_id = htonl (t->local_tid);
2720 GNUNET_PEER_resolve (peer_id, &msg.peer);
2721 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2722 &msg.header, GNUNET_NO);
2724 peer = peer_info_get_short (peer_id);
2725 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2726 path_info->peer = peer;
2728 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2733 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2734 * needed rescources.
2735 * If peer already exists, reevaluate shortest path and change if different.
2737 * @param t Tunnel we want to add a new peer to
2738 * @param peer PeerInfo of the peer being added
2742 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2744 struct GNUNET_PeerIdentity id;
2745 struct MeshPeerPath *best_p;
2746 struct MeshPeerPath *p;
2747 unsigned int best_cost;
2750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2751 GNUNET_PEER_resolve (peer->id, &id);
2753 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2756 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2757 GNUNET_assert (GNUNET_OK ==
2758 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2760 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2763 if (NULL != (p = peer->path_head))
2766 best_cost = tree_get_path_cost (t->tree, p);
2769 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2776 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2777 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2778 t->path_refresh_task =
2779 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2783 /* Start a DHT get */
2784 peer_info_connect (peer, t);
2786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2790 * Add a path to a tunnel which we don't own, just to remember the next hop.
2791 * If destination node was already in the tunnel, the first hop information
2792 * will be replaced with the new path.
2794 * @param t Tunnel we want to add a new peer to
2795 * @param p Path to add
2796 * @param own_pos Position of local node in path.
2800 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2801 unsigned int own_pos)
2803 struct GNUNET_PeerIdentity id;
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2806 GNUNET_assert (0 != own_pos);
2807 tree_add_path (t->tree, p, NULL, NULL);
2808 if (own_pos < p->length - 1)
2810 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2811 tree_update_first_hops (t->tree, myid, &id);
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2818 * Notifies a tunnel that a connection has broken that affects at least
2819 * some of its peers. Sends a notification towards the root of the tree.
2820 * In case the peer is the owner of the tree, notifies the client that owns
2821 * the tunnel and tries to reconnect.
2823 * @param t Tunnel affected.
2824 * @param p1 Peer that got disconnected from p2.
2825 * @param p2 Peer that got disconnected from p1.
2827 * @return Short ID of the peer disconnected (either p1 or p2).
2828 * 0 if the tunnel remained unaffected.
2830 static GNUNET_PEER_Id
2831 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2837 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2839 if (myid != p1 && myid != p2)
2845 if (tree_get_predecessor (t->tree) != 0)
2847 /* We are the peer still connected, notify owner of the disconnection. */
2848 struct GNUNET_MESH_PathBroken msg;
2849 struct GNUNET_PeerIdentity neighbor;
2851 msg.header.size = htons (sizeof (msg));
2852 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2853 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2854 msg.tid = htonl (t->id.tid);
2855 msg.peer1 = my_full_id;
2856 GNUNET_PEER_resolve (pid, &msg.peer2);
2857 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2858 send_message (&msg.header, &neighbor, t);
2866 * Send a multicast packet to a neighbor.
2868 * @param cls Closure (Info about the multicast packet)
2869 * @param neighbor_id Short ID of the neighbor to send the packet to.
2872 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2874 struct MeshData *mdata = cls;
2875 struct MeshTransmissionDescriptor *info;
2876 struct GNUNET_PeerIdentity neighbor;
2878 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2880 info->mesh_data = mdata;
2881 (*(mdata->reference_counter)) ++;
2882 info->destination = neighbor_id;
2883 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2885 GNUNET_i2s (&neighbor));
2886 info->peer = peer_info_get (&neighbor);
2887 GNUNET_assert (NULL != info->peer);
2889 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2890 info->mesh_data->data_len,
2896 * Send a message in a tunnel in multicast, sending a copy to each child node
2897 * down the local one in the tunnel tree.
2899 * @param t Tunnel in which to send the data.
2900 * @param msg Message to be sent.
2901 * @param internal Has the service generated this message?
2904 tunnel_send_multicast (struct MeshTunnel *t,
2905 const struct GNUNET_MessageHeader *msg,
2908 struct MeshData *mdata;
2910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2911 " sending a multicast packet...\n");
2912 mdata = GNUNET_malloc (sizeof (struct MeshData));
2913 mdata->data_len = ntohs (msg->size);
2914 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2916 mdata->data = GNUNET_malloc (mdata->data_len);
2917 memcpy (mdata->data, msg, mdata->data_len);
2918 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2920 struct GNUNET_MESH_Multicast *mcast;
2922 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2923 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2925 ntohl (mcast->ttl));
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2931 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2932 && GNUNET_NO == internal)
2934 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2936 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2942 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2943 if (*(mdata->reference_counter) == 0)
2945 GNUNET_free (mdata->data);
2946 GNUNET_free (mdata->reference_counter);
2947 if (NULL != mdata->task)
2949 GNUNET_SCHEDULER_cancel(*(mdata->task));
2950 GNUNET_free (mdata->task);
2951 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2953 // FIXME change order?
2954 GNUNET_free (mdata);
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2957 " sending a multicast packet done\n");
2963 * Send a message to all peers in this tunnel that the tunnel is no longer
2966 * @param t The tunnel whose peers to notify.
2969 tunnel_send_destroy (struct MeshTunnel *t)
2971 struct GNUNET_MESH_TunnelDestroy msg;
2973 msg.header.size = htons (sizeof (msg));
2974 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2975 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2976 msg.tid = htonl (t->id.tid);
2977 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2981 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
2983 * @param cls Closure (Tunnel which to cancel).
2984 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
2987 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
2989 struct MeshTunnel *t = cls;
2990 struct MeshPeerInfo *peer_info;
2991 struct MeshPeerQueue *pq;
2992 struct MeshPeerQueue *next;
2994 peer_info = peer_info_get_short (neighbor_id);
2995 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2998 if (pq->tunnel == t)
3000 queue_destroy (pq, GNUNET_YES);
3003 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3005 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3006 peer_info->core_transmit = NULL;
3011 * Destroy the tunnel and free any allocated resources linked to it.
3013 * @param t the tunnel to destroy
3015 * @return GNUNET_OK on success
3018 tunnel_destroy (struct MeshTunnel *t)
3020 struct MeshClient *c;
3021 struct GNUNET_HashCode hash;
3028 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3034 struct GNUNET_PeerIdentity id;
3036 GNUNET_PEER_resolve (t->id.oid, &id);
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3038 GNUNET_i2s (&id), t->id.tid);
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3044 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3045 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3050 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3053 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3057 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3058 for (i = 0; i < t->nclients; i++)
3062 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3067 for (i = 0; i < t->nignore; i++)
3071 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3076 if (t->nclients > 0)
3079 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3083 GNUNET_free (t->clients);
3085 if (NULL != t->peers)
3087 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3089 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3092 tree_destroy (t->tree);
3094 if (NULL != t->regex_ctx)
3095 regex_cancel_search (t->regex_ctx);
3096 if (NULL != t->dht_get_type)
3097 GNUNET_DHT_get_stop (t->dht_get_type);
3098 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3099 GNUNET_SCHEDULER_cancel (t->timeout_task);
3100 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3101 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3109 * Create a new tunnel
3111 * @param owner Who is the owner of the tunnel (short ID).
3112 * @param tid Tunnel Number of the tunnel.
3113 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3114 * @param local Tunnel Number for the tunnel, for the client point of view.
3117 static struct MeshTunnel *
3118 tunnel_new (GNUNET_PEER_Id owner,
3119 MESH_TunnelNumber tid,
3120 struct MeshClient *client,
3121 MESH_TunnelNumber local)
3123 struct MeshTunnel *t;
3124 struct GNUNET_HashCode hash;
3126 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3129 t->queue_max = 1000; // FIXME API parameter
3130 t->tree = tree_new (owner);
3132 t->local_tid = local;
3134 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3136 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3137 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3142 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3148 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3150 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3151 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3155 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3165 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3166 * that are no longer needed, as well as nodes of no longer reachable peers.
3167 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3169 * @param t Tunnel from which to remove the path.
3170 * @param peer Short id of the peer which should be removed.
3173 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3175 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3181 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3182 * client when the client disconnects. If the client is not the owner, the
3183 * owner will get notified if no more clients are in the tunnel and the client
3184 * get removed from the tunnel's list.
3186 * @param cls closure (client that is disconnecting)
3187 * @param key the hash of the local tunnel id (used to access the hashmap)
3188 * @param value the value stored at the key (tunnel to destroy)
3190 * @return GNUNET_OK on success
3193 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3195 struct MeshTunnel *t = value;
3196 struct MeshClient *c = cls;
3199 send_client_tunnel_disconnect(t, c);
3202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3203 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3204 tunnel_delete_client(t, c);
3205 client_delete_tunnel(c, t);
3208 tunnel_send_destroy(t);
3209 r = tunnel_destroy (t);
3215 * Timeout function, destroys tunnel if called
3217 * @param cls Closure (tunnel to destroy).
3218 * @param tc TaskContext
3221 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3223 struct MeshTunnel *t = cls;
3225 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3227 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3232 * Resets the tunnel timeout. Starts it if no timeout was running.
3234 * @param t Tunnel whose timeout to reset.
3237 tunnel_reset_timeout (struct MeshTunnel *t)
3239 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3240 GNUNET_SCHEDULER_cancel (t->timeout_task);
3242 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3243 (refresh_path_time, 4), &tunnel_timeout, t);
3247 /******************************************************************************/
3248 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3249 /******************************************************************************/
3252 * Function to send a create path packet to a peer.
3254 * @param cls closure
3255 * @param size number of bytes available in buf
3256 * @param buf where the callee should write the message
3257 * @return number of bytes written to buf
3260 send_core_path_create (void *cls, size_t size, void *buf)
3262 struct MeshPathInfo *info = cls;
3263 struct GNUNET_MESH_ManipulatePath *msg;
3264 struct GNUNET_PeerIdentity *peer_ptr;
3265 struct MeshTunnel *t = info->t;
3266 struct MeshPeerPath *p = info->path;
3271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3273 sizeof (struct GNUNET_MESH_ManipulatePath) +
3274 p->length * sizeof (struct GNUNET_PeerIdentity);
3276 if (size < size_needed || NULL == buf)
3281 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3282 msg->header.size = htons (size_needed);
3283 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3284 msg->tid = ntohl (t->id.tid);
3286 if (GNUNET_YES == t->speed_min)
3287 opt = MESH_TUNNEL_OPT_SPEED_MIN;
3288 if (GNUNET_YES == t->nobuffer)
3289 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3290 msg->opt = htonl(opt);
3292 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3293 for (i = 0; i < p->length; i++)
3295 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3302 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3308 * Fill the core buffer
3310 * @param cls closure (data itself)
3311 * @param size number of bytes available in buf
3312 * @param buf where the callee should write the message
3314 * @return number of bytes written to buf
3317 send_core_data_multicast (void *cls, size_t size, void *buf)
3319 struct MeshTransmissionDescriptor *info = cls;
3322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3323 GNUNET_assert (NULL != info);
3324 GNUNET_assert (NULL != info->peer);
3325 total_size = info->mesh_data->data_len;
3326 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3328 if (total_size > size)
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3334 memcpy (buf, info->mesh_data->data, total_size);
3337 struct GNUNET_MESH_Multicast *mc;
3338 struct GNUNET_MessageHeader *mh;
3341 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3343 mc = (struct GNUNET_MESH_Multicast *) mh;
3344 mh = (struct GNUNET_MessageHeader *) &mc[1];
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3346 " multicast, payload type %u\n", ntohs (mh->type));
3347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3348 " multicast, payload size %u\n", ntohs (mh->size));
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3357 data_descriptor_decrement_rc (info->mesh_data);
3358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3366 * Creates a path ack message in buf and frees all unused resources.
3368 * @param cls closure (MeshTransmissionDescriptor)
3369 * @param size number of bytes available in buf
3370 * @param buf where the callee should write the message
3371 * @return number of bytes written to buf
3374 send_core_path_ack (void *cls, size_t size, void *buf)
3376 struct MeshTransmissionDescriptor *info = cls;
3377 struct GNUNET_MESH_PathACK *msg = buf;
3379 GNUNET_assert (NULL != info);
3380 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3385 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3386 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3387 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3388 msg->tid = htonl (info->origin->tid);
3389 msg->peer_id = my_full_id;
3392 /* TODO add signature */
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3395 return sizeof (struct GNUNET_MESH_PathACK);
3400 * Free a transmission that was already queued with all resources
3401 * associated to the request.
3403 * @param queue Queue handler to cancel.
3404 * @param clear_cls Is it necessary to free associated cls?
3407 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3409 struct MeshTransmissionDescriptor *dd;
3410 struct MeshPathInfo *path_info;
3412 if (GNUNET_YES == clear_cls)
3414 switch (queue->type)
3416 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3417 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3418 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
3421 data_descriptor_decrement_rc (dd->mesh_data);
3423 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3425 path_info = queue->cls;
3426 path_destroy (path_info->path);
3430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
3432 GNUNET_free_non_null (queue->cls);
3434 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3435 queue->peer->queue_tail,
3437 GNUNET_free (queue);
3442 * Core callback to write a queued packet to core buffer
3444 * @param cls Closure (peer info).
3445 * @param size Number of bytes available in buf.
3446 * @param buf Where the to write the message.
3448 * @return number of bytes written to buf
3451 queue_send (void *cls, size_t size, void *buf)
3453 struct MeshPeerInfo *peer = cls;
3454 struct MeshPeerQueue *queue;
3455 struct MeshTunnel *t;
3458 peer->core_transmit = NULL;
3459 queue = peer->queue_head;
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3463 /* If queue is empty, send should have been cancelled */
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3471 /* Check if buffer size is enough for the message */
3472 if (queue->size > size)
3474 struct GNUNET_PeerIdentity id;
3476 GNUNET_PEER_resolve (peer->id, &id);
3477 peer->core_transmit =
3478 GNUNET_CORE_notify_transmit_ready(core_handle,
3481 GNUNET_TIME_UNIT_FOREVER_REL,
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3491 switch (queue->type)
3493 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
3495 data_size = send_core_data_raw (queue->cls, size, buf);
3497 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
3499 data_size = send_core_data_multicast(queue->cls, size, buf);
3501 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3503 data_size = send_core_path_create(queue->cls, size, buf);
3505 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3507 data_size = send_core_path_ack(queue->cls, size, buf);
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
3518 /* Free queue, but cls was freed by send_core_* */
3519 queue_destroy(queue, GNUNET_NO);
3521 if (GNUNET_YES == t->destroy && 0 == t->queue_n)
3523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3527 /* If more data in queue, send next */
3528 if (NULL != peer->queue_head)
3530 struct GNUNET_PeerIdentity id;
3532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3533 GNUNET_PEER_resolve (peer->id, &id);
3534 peer->core_transmit =
3535 GNUNET_CORE_notify_transmit_ready(core_handle,
3538 GNUNET_TIME_UNIT_FOREVER_REL,
3540 peer->queue_head->size,
3544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3550 * Queue and pass message to core when possible.
3552 * @param cls Closure (type dependant).
3553 * @param type Type of the message.
3554 * @param size Size of the message.
3555 * @param dst Neighbor to send message to.
3556 * @param t Tunnel this message belongs to.
3559 queue_add (void *cls, uint16_t type, size_t size,
3560 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3562 struct MeshPeerQueue *queue;
3564 if (t->queue_n >= t->queue_max)
3566 if (NULL == t->owner)
3567 GNUNET_break_op(0); // TODO: kill connection?
3570 return; // Drop message
3573 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3579 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3580 if (NULL == dst->core_transmit)
3582 struct GNUNET_PeerIdentity id;
3584 GNUNET_PEER_resolve (dst->id, &id);
3585 dst->core_transmit =
3586 GNUNET_CORE_notify_transmit_ready(core_handle,
3589 GNUNET_TIME_UNIT_FOREVER_REL,
3598 /******************************************************************************/
3599 /******************** MESH NETWORK HANDLERS **************************/
3600 /******************************************************************************/
3604 * Core handler for path creation
3606 * @param cls closure
3607 * @param message message
3608 * @param peer peer identity this notification is about
3609 * @param atsi performance data
3610 * @param atsi_count number of records in 'atsi'
3612 * @return GNUNET_OK to keep the connection open,
3613 * GNUNET_SYSERR to close it (signal serious error)
3616 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3617 const struct GNUNET_MessageHeader *message,
3618 const struct GNUNET_ATS_Information *atsi,
3619 unsigned int atsi_count)
3621 unsigned int own_pos;
3624 MESH_TunnelNumber tid;
3625 struct GNUNET_MESH_ManipulatePath *msg;
3626 struct GNUNET_PeerIdentity *pi;
3627 struct GNUNET_HashCode hash;
3628 struct MeshPeerPath *path;
3629 struct MeshPeerInfo *dest_peer_info;
3630 struct MeshPeerInfo *orig_peer_info;
3631 struct MeshTunnel *t;
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3634 "Received a path create msg [%s]\n",
3635 GNUNET_i2s (&my_full_id));
3636 size = ntohs (message->size);
3637 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3639 GNUNET_break_op (0);
3643 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3644 if (size % sizeof (struct GNUNET_PeerIdentity))
3646 GNUNET_break_op (0);
3649 size /= sizeof (struct GNUNET_PeerIdentity);
3652 GNUNET_break_op (0);
3655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3656 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3658 tid = ntohl (msg->tid);
3659 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3661 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3662 t = tunnel_get (pi, tid);
3663 if (NULL == t) // FIXME only for INCOMING tunnels?
3667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3668 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3669 opt = ntohl (msg->opt);
3670 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
3671 GNUNET_YES : GNUNET_NO;
3672 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
3673 GNUNET_YES : GNUNET_NO;;
3675 while (NULL != tunnel_get_incoming (next_local_tid))
3676 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3677 t->local_tid_dest = next_local_tid++;
3678 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3680 tunnel_reset_timeout (t);
3681 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3683 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3692 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3693 if (NULL == dest_peer_info)
3695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3696 " Creating PeerInfo for destination.\n");
3697 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3698 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3699 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3701 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3703 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3704 if (NULL == orig_peer_info)
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3707 " Creating PeerInfo for origin.\n");
3708 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3709 orig_peer_info->id = GNUNET_PEER_intern (pi);
3710 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3711 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3714 path = path_new (size);
3716 for (i = 0; i < size; i++)
3718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3719 GNUNET_i2s (&pi[i]));
3720 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3721 if (path->peers[i] == myid)
3724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3727 /* cannot be self, must be 'not found' */
3728 /* create path: self not found in path through self */
3729 GNUNET_break_op (0);
3730 path_destroy (path);
3731 /* FIXME error. destroy tunnel? leave for timeout? */
3734 path_add_to_peers (path, GNUNET_NO);
3735 tunnel_add_path (t, path, own_pos);
3736 if (own_pos == size - 1)
3738 /* It is for us! Send ack. */
3739 struct MeshTransmissionDescriptor *info;
3741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3742 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3743 if (NULL == t->peers)
3745 /* New tunnel! Notify clients on data. */
3746 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
3748 GNUNET_break (GNUNET_OK ==
3749 GNUNET_CONTAINER_multihashmap_put (t->peers,
3750 &my_full_id.hashPubKey,
3753 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
3754 // FIXME URGENT (GNUNET_NO?)
3755 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3756 info->origin = &t->id;
3757 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3758 GNUNET_assert (NULL != info->peer);
3760 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3761 sizeof (struct GNUNET_MESH_PathACK),
3767 struct MeshPeerPath *path2;
3769 /* It's for somebody else! Retransmit. */
3770 path2 = path_duplicate (path);
3771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3772 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3773 path2 = path_duplicate (path);
3774 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3775 send_create_path (dest_peer_info, path, t);
3782 * Core handler for path destruction
3784 * @param cls closure
3785 * @param message message
3786 * @param peer peer identity this notification is about
3787 * @param atsi performance data
3788 * @param atsi_count number of records in 'atsi'
3790 * @return GNUNET_OK to keep the connection open,
3791 * GNUNET_SYSERR to close it (signal serious error)
3794 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3795 const struct GNUNET_MessageHeader *message,
3796 const struct GNUNET_ATS_Information *atsi,
3797 unsigned int atsi_count)
3799 struct GNUNET_MESH_ManipulatePath *msg;
3800 struct GNUNET_PeerIdentity *pi;
3801 struct MeshPeerPath *path;
3802 struct MeshTunnel *t;
3803 unsigned int own_pos;
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3808 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3809 size = ntohs (message->size);
3810 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3812 GNUNET_break_op (0);
3816 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3817 if (size % sizeof (struct GNUNET_PeerIdentity))
3819 GNUNET_break_op (0);
3822 size /= sizeof (struct GNUNET_PeerIdentity);
3825 GNUNET_break_op (0);
3828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3830 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3831 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3833 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3835 t = tunnel_get (pi, ntohl (msg->tid));
3838 /* TODO notify back: we don't know this tunnel */
3839 GNUNET_break_op (0);
3842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3843 path = path_new (size);
3845 for (i = 0; i < size; i++)
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3848 GNUNET_i2s (&pi[i]));
3849 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3850 if (path->peers[i] == myid)
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3854 if (own_pos < path->length - 1)
3855 send_message (message, &pi[own_pos + 1], t);
3857 send_client_tunnel_disconnect(t, NULL);
3859 tunnel_delete_peer (t, path->peers[path->length - 1]);
3860 path_destroy (path);
3866 * Core handler for notifications of broken paths
3868 * @param cls closure
3869 * @param message message
3870 * @param peer peer identity this notification is about
3871 * @param atsi performance data
3872 * @param atsi_count number of records in 'atsi'
3874 * @return GNUNET_OK to keep the connection open,
3875 * GNUNET_SYSERR to close it (signal serious error)
3878 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3879 const struct GNUNET_MessageHeader *message,
3880 const struct GNUNET_ATS_Information *atsi,
3881 unsigned int atsi_count)
3883 struct GNUNET_MESH_PathBroken *msg;
3884 struct MeshTunnel *t;
3886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3887 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3888 msg = (struct GNUNET_MESH_PathBroken *) message;
3889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3890 GNUNET_i2s (&msg->peer1));
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3892 GNUNET_i2s (&msg->peer2));
3893 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3896 GNUNET_break_op (0);
3899 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3900 GNUNET_PEER_search (&msg->peer2));
3907 * Core handler for tunnel destruction
3909 * @param cls closure
3910 * @param message message
3911 * @param peer peer identity this notification is about
3912 * @param atsi performance data
3913 * @param atsi_count number of records in 'atsi'
3915 * @return GNUNET_OK to keep the connection open,
3916 * GNUNET_SYSERR to close it (signal serious error)
3919 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3920 const struct GNUNET_MessageHeader *message,
3921 const struct GNUNET_ATS_Information *atsi,
3922 unsigned int atsi_count)
3924 struct GNUNET_MESH_TunnelDestroy *msg;
3925 struct MeshTunnel *t;
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3928 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3929 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3931 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3932 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3935 /* Probably already got the message from another path,
3936 * destroyed the tunnel and retransmitted to children.
3939 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3942 if (t->id.oid == myid)
3944 GNUNET_break_op (0);
3947 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3949 /* Tunnel was incoming, notify clients */
3950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3951 t->local_tid, t->local_tid_dest);
3952 send_clients_tunnel_destroy (t);
3954 tunnel_send_destroy (t);
3955 t->destroy = GNUNET_YES;
3956 // TODO: add timeout to destroy the tunnel anyway
3962 * Core handler for mesh network traffic going from the origin to a peer
3964 * @param cls closure
3965 * @param peer peer identity this notification is about
3966 * @param message message
3967 * @param atsi performance data
3968 * @param atsi_count number of records in 'atsi'
3969 * @return GNUNET_OK to keep the connection open,
3970 * GNUNET_SYSERR to close it (signal serious error)
3973 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3974 const struct GNUNET_MessageHeader *message,
3975 const struct GNUNET_ATS_Information *atsi,
3976 unsigned int atsi_count)
3978 struct GNUNET_MESH_Unicast *msg;
3979 struct MeshTunnel *t;
3984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3986 size = ntohs (message->size);
3988 sizeof (struct GNUNET_MESH_Unicast) +
3989 sizeof (struct GNUNET_MessageHeader))
3994 msg = (struct GNUNET_MESH_Unicast *) message;
3995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3996 ntohs (msg[1].header.type));
3997 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4000 /* TODO notify back: we don't know this tunnel */
4001 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4002 GNUNET_break_op (0);
4005 pid = ntohl (msg->pid);
4008 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4009 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4010 " Already seen mid %u, DROPPING!\n", pid);
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4016 " mid %u not seen yet, forwarding\n", pid);
4018 t->skip += (pid - t->pid) - 1;
4020 tunnel_reset_timeout (t);
4021 pid = GNUNET_PEER_search (&msg->destination);
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4025 " it's for us! sending to clients...\n");
4026 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4027 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4030 ttl = ntohl (msg->ttl);
4031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4034 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4035 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4039 " not for us, retransmitting...\n");
4040 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4042 send_message (message, tree_get_first_hop (t->tree, pid), t);
4048 * Core handler for mesh network traffic going from the origin to all peers
4050 * @param cls closure
4051 * @param message message
4052 * @param peer peer identity this notification is about
4053 * @param atsi performance data
4054 * @param atsi_count number of records in 'atsi'
4055 * @return GNUNET_OK to keep the connection open,
4056 * GNUNET_SYSERR to close it (signal serious error)
4058 * TODO: Check who we got this from, to validate route.
4061 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4062 const struct GNUNET_MessageHeader *message,
4063 const struct GNUNET_ATS_Information *atsi,
4064 unsigned int atsi_count)
4066 struct GNUNET_MESH_Multicast *msg;
4067 struct MeshTunnel *t;
4071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
4073 size = ntohs (message->size);
4074 if (sizeof (struct GNUNET_MESH_Multicast) +
4075 sizeof (struct GNUNET_MessageHeader) > size)
4077 GNUNET_break_op (0);
4080 msg = (struct GNUNET_MESH_Multicast *) message;
4081 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4085 /* TODO notify that we dont know that tunnel */
4086 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4087 GNUNET_break_op (0);
4090 pid = ntohl (msg->pid);
4093 /* already seen this packet, drop */
4094 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4095 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4096 " Already seen mid %u, DROPPING!\n", pid);
4101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4102 " mid %u not seen yet, forwarding\n", pid);
4104 t->skip += (pid - t->pid) - 1;
4106 tunnel_reset_timeout (t);
4108 /* Transmit to locally interested clients */
4109 if (NULL != t->peers &&
4110 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
4112 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
4113 send_subscribed_clients (message, &msg[1].header);
4115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
4116 if (ntohl (msg->ttl) == 0)
4118 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4122 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
4123 tunnel_send_multicast (t, message, GNUNET_NO);
4129 * Core handler for mesh network traffic toward the owner of a tunnel
4131 * @param cls closure
4132 * @param message message
4133 * @param peer peer identity this notification is about
4134 * @param atsi performance data
4135 * @param atsi_count number of records in 'atsi'
4137 * @return GNUNET_OK to keep the connection open,
4138 * GNUNET_SYSERR to close it (signal serious error)
4141 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4142 const struct GNUNET_MessageHeader *message,
4143 const struct GNUNET_ATS_Information *atsi,
4144 unsigned int atsi_count)
4146 struct GNUNET_MESH_ToOrigin *msg;
4147 struct GNUNET_PeerIdentity id;
4148 struct MeshPeerInfo *peer_info;
4149 struct MeshTunnel *t;
4152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4154 size = ntohs (message->size);
4155 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4156 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4158 GNUNET_break_op (0);
4161 msg = (struct GNUNET_MESH_ToOrigin *) message;
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4163 ntohs (msg[1].header.type));
4164 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4168 /* TODO notify that we dont know this tunnel (whom)? */
4169 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4170 GNUNET_break_op (0);
4174 if (t->id.oid == myid)
4177 struct GNUNET_MESH_ToOrigin *copy;
4179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4180 " it's for us! sending to clients...\n");
4181 if (NULL == t->owner)
4183 /* got data packet for ownerless tunnel */
4184 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4187 GNUNET_break_op (0);
4190 /* TODO signature verification */
4191 memcpy (cbuf, message, size);
4192 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4193 copy->tid = htonl (t->local_tid);
4194 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4195 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4196 ©->header, GNUNET_YES);
4199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4200 " not for us, retransmitting...\n");
4202 peer_info = peer_info_get (&msg->oid);
4203 if (NULL == peer_info)
4205 /* unknown origin of tunnel */
4209 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4210 send_message (message, &id, t);
4211 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4218 * Core handler for path ACKs
4220 * @param cls closure
4221 * @param message message
4222 * @param peer peer identity this notification is about
4223 * @param atsi performance data
4224 * @param atsi_count number of records in 'atsi'
4226 * @return GNUNET_OK to keep the connection open,
4227 * GNUNET_SYSERR to close it (signal serious error)
4230 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4231 const struct GNUNET_MessageHeader *message,
4232 const struct GNUNET_ATS_Information *atsi,
4233 unsigned int atsi_count)
4235 struct GNUNET_MESH_PathACK *msg;
4236 struct GNUNET_PeerIdentity id;
4237 struct MeshPeerInfo *peer_info;
4238 struct MeshPeerPath *p;
4239 struct MeshTunnel *t;
4241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4242 GNUNET_i2s (&my_full_id));
4243 msg = (struct GNUNET_MESH_PathACK *) message;
4244 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4247 /* TODO notify that we don't know the tunnel */
4248 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4250 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4254 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4256 peer_info = peer_info_get (&msg->peer_id);
4257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4258 GNUNET_i2s (&msg->peer_id));
4259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4262 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
4264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4265 "connect_by_string completed, stopping search\n");
4266 regex_cancel_search (t->regex_ctx);
4267 t->regex_ctx = NULL;
4270 /* Add paths to peers? */
4271 p = tree_get_path_to_peer (t->tree, peer_info->id);
4274 path_add_to_peers (p, GNUNET_YES);
4282 /* Message for us? */
4283 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4286 if (NULL == t->owner)
4288 GNUNET_break_op (0);
4291 if (NULL != t->dht_get_type)
4293 GNUNET_DHT_get_stop (t->dht_get_type);
4294 t->dht_get_type = NULL;
4296 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4298 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4299 send_client_peer_connected (t, peer_info->id);
4304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4305 " not for us, retransmitting...\n");
4306 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4307 peer_info = peer_info_get (&msg->oid);
4308 if (NULL == peer_info)
4310 /* If we know the tunnel, we should DEFINITELY know the peer */
4314 send_message (message, &id, t);
4320 * Functions to handle messages from core
4322 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4323 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4324 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4325 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4326 sizeof (struct GNUNET_MESH_PathBroken)},
4327 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
4328 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4329 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4330 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4331 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4332 sizeof (struct GNUNET_MESH_PathACK)},
4338 /******************************************************************************/
4339 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4340 /******************************************************************************/
4343 * deregister_app: iterator for removing each application registered by a client
4345 * @param cls closure
4346 * @param key the hash of the application id (used to access the hashmap)
4347 * @param value the value stored at the key (client)
4349 * @return GNUNET_OK on success
4352 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
4354 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
4355 GNUNET_break (GNUNET_YES ==
4356 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
4362 * notify_client_connection_failure: notify a client that the connection to the
4363 * requested remote peer is not possible (for instance, no route found)
4364 * Function called when the socket is ready to queue more data. "buf" will be
4365 * NULL and "size" zero if the socket was closed for writing in the meantime.
4367 * @param cls closure
4368 * @param size number of bytes available in buf
4369 * @param buf where the callee should write the message
4370 * @return number of bytes written to buf
4373 notify_client_connection_failure (void *cls, size_t size, void *buf)
4376 struct MeshPeerInfo *peer_info;
4377 struct GNUNET_MESH_PeerControl *msg;
4378 struct GNUNET_PeerIdentity id;
4380 if (0 == size && NULL == buf)
4382 // TODO retry? cancel?
4386 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4387 peer_info = (struct MeshPeerInfo *) cls;
4388 msg = (struct GNUNET_MESH_PeerControl *) buf;
4389 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4390 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4391 // msg->tunnel_id = htonl(peer_info->t->tid);
4392 GNUNET_PEER_resolve (peer_info->id, &id);
4393 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4401 * Send keepalive packets for a peer
4403 * @param cls Closure (tunnel for which to send the keepalive).
4404 * @param tc Notification context.
4406 * TODO: implement explicit multicast keepalive?
4409 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4411 struct MeshTunnel *t = cls;
4412 struct GNUNET_MessageHeader *payload;
4413 struct GNUNET_MESH_Multicast *msg;
4415 sizeof (struct GNUNET_MESH_Multicast) +
4416 sizeof (struct GNUNET_MessageHeader);
4419 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4423 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4426 "sending keepalive for tunnel %d\n", t->id.tid);
4428 msg = (struct GNUNET_MESH_Multicast *) cbuf;
4429 msg->header.size = htons (size);
4430 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4431 msg->oid = my_full_id;
4432 msg->tid = htonl (t->id.tid);
4433 msg->ttl = htonl (default_ttl);
4434 msg->pid = htonl (t->pid + 1);
4436 payload = (struct GNUNET_MessageHeader *) &msg[1];
4437 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
4438 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4439 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
4441 t->path_refresh_task =
4442 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4448 * Function to process paths received for a new peer addition. The recorded
4449 * paths form the initial tunnel, which can be optimized later.
4450 * Called on each result obtained for the DHT search.
4452 * @param cls closure
4453 * @param exp when will this value expire
4454 * @param key key of the result
4455 * @param get_path path of the get request
4456 * @param get_path_length lenght of get_path
4457 * @param put_path path of the put request
4458 * @param put_path_length length of the put_path
4459 * @param type type of the result
4460 * @param size number of bytes in data
4461 * @param data pointer to the result data
4463 * TODO: re-issue the request after certain time? cancel after X results?
4466 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4467 const struct GNUNET_HashCode * key,
4468 const struct GNUNET_PeerIdentity *get_path,
4469 unsigned int get_path_length,
4470 const struct GNUNET_PeerIdentity *put_path,
4471 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4472 size_t size, const void *data)
4474 struct MeshPathInfo *path_info = cls;
4475 struct MeshPeerPath *p;
4476 struct GNUNET_PeerIdentity pi;
4479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4480 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4483 p = path_build_from_dht (get_path, get_path_length, put_path,
4485 path_add_to_peers (p, GNUNET_NO);
4487 for (i = 0; i < path_info->peer->ntunnels; i++)
4489 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4490 peer_info_connect (path_info->peer, path_info->t);
4498 * Function to process paths received for a new peer addition. The recorded
4499 * paths form the initial tunnel, which can be optimized later.
4500 * Called on each result obtained for the DHT search.
4502 * @param cls closure
4503 * @param exp when will this value expire
4504 * @param key key of the result
4505 * @param get_path path of the get request
4506 * @param get_path_length lenght of get_path
4507 * @param put_path path of the put request
4508 * @param put_path_length length of the put_path
4509 * @param type type of the result
4510 * @param size number of bytes in data
4511 * @param data pointer to the result data
4514 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4515 const struct GNUNET_HashCode * key,
4516 const struct GNUNET_PeerIdentity *get_path,
4517 unsigned int get_path_length,
4518 const struct GNUNET_PeerIdentity *put_path,
4519 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4520 size_t size, const void *data)
4522 const struct PBlock *pb = data;
4523 const struct GNUNET_PeerIdentity *pi = &pb->id;
4524 struct MeshTunnel *t = cls;
4525 struct MeshPeerInfo *peer_info;
4526 struct MeshPeerPath *p;
4528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
4529 if (size != sizeof (struct PBlock))
4531 GNUNET_break_op (0);
4534 if (ntohl(pb->type) != t->type)
4536 GNUNET_break_op (0);
4539 GNUNET_assert (NULL != t->owner);
4540 peer_info = peer_info_get (pi);
4541 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
4543 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
4545 p = path_build_from_dht (get_path, get_path_length, put_path,
4547 path_add_to_peers (p, GNUNET_NO);
4549 tunnel_add_peer (t, peer_info);
4550 peer_info_connect (peer_info, t);
4555 * Function called if the connect attempt to a peer found via
4556 * connect_by_string times out. Try to connect to another peer, if any.
4557 * Otherwise try to reconnect to the same peer.
4559 * @param cls Closure (info about regex search).
4560 * @param tc TaskContext.
4563 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4565 struct MeshRegexSearchInfo *info = cls;
4566 struct MeshPeerInfo *peer_info;
4570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
4571 info->timeout = GNUNET_SCHEDULER_NO_TASK;
4572 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
4580 if (0 < info->n_peers)
4582 // Select next peer, put current in that spot.
4583 id = info->peers[info->i_peer];
4584 info->peers[info->i_peer] = info->peer;
4585 info->i_peer = (info->i_peer + 1) % info->n_peers;
4589 // Try to connect to same peer again.
4592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
4594 peer_info = peer_info_get_short(id);
4595 tunnel_add_peer (info->t, peer_info);
4597 tunnel_delete_peer (info->t, old);
4598 peer_info_connect (peer_info, info->t);
4599 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
4600 ®ex_connect_timeout,
4602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
4607 * Function to process DHT string to regex matching.
4608 * Called on each result obtained for the DHT search.
4610 * @param cls closure (search context)
4611 * @param exp when will this value expire
4612 * @param key key of the result
4613 * @param get_path path of the get request (not used)
4614 * @param get_path_length lenght of get_path (not used)
4615 * @param put_path path of the put request (not used)
4616 * @param put_path_length length of the put_path (not used)
4617 * @param type type of the result
4618 * @param size number of bytes in data
4619 * @param data pointer to the result data
4622 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4623 const struct GNUNET_HashCode * key,
4624 const struct GNUNET_PeerIdentity *get_path,
4625 unsigned int get_path_length,
4626 const struct GNUNET_PeerIdentity *put_path,
4627 unsigned int put_path_length,
4628 enum GNUNET_BLOCK_Type type,
4629 size_t size, const void *data)
4631 const struct MeshRegexAccept *block = data;
4632 struct MeshRegexSearchContext *ctx = cls;
4633 struct MeshRegexSearchInfo *info = ctx->info;
4634 struct MeshPeerPath *p;
4635 struct MeshPeerInfo *peer_info;
4637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
4638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
4640 peer_info = peer_info_get(&block->id);
4641 p = path_build_from_dht (get_path, get_path_length, put_path,
4643 path_add_to_peers (p, GNUNET_NO);
4646 tunnel_add_peer (info->t, peer_info);
4647 peer_info_connect (peer_info, info->t);
4648 if (0 == info->peer)
4650 info->peer = peer_info->id;
4654 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
4657 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
4658 ®ex_connect_timeout,
4666 * Function to process DHT string to regex matching.
4667 * Called on each result obtained for the DHT search.
4669 * @param cls closure (search context)
4670 * @param exp when will this value expire
4671 * @param key key of the result
4672 * @param get_path path of the get request (not used)
4673 * @param get_path_length lenght of get_path (not used)
4674 * @param put_path path of the put request (not used)
4675 * @param put_path_length length of the put_path (not used)
4676 * @param type type of the result
4677 * @param size number of bytes in data
4678 * @param data pointer to the result data
4680 * TODO: re-issue the request after certain time? cancel after X results?
4683 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4684 const struct GNUNET_HashCode * key,
4685 const struct GNUNET_PeerIdentity *get_path,
4686 unsigned int get_path_length,
4687 const struct GNUNET_PeerIdentity *put_path,
4688 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4689 size_t size, const void *data)
4691 const struct MeshRegexBlock *block = data;
4692 struct MeshRegexSearchContext *ctx = cls;
4693 struct MeshRegexSearchInfo *info = ctx->info;
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4698 "DHT GET STRING RETURNED RESULTS\n");
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4700 " key: %s\n", GNUNET_h2s (key));
4702 copy = GNUNET_malloc (size);
4703 memcpy (copy, data, size);
4704 GNUNET_break (GNUNET_OK ==
4705 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
4706 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
4707 len = ntohl (block->n_proof);
4709 char proof[len + 1];
4711 memcpy (proof, &block[1], len);
4713 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
4715 GNUNET_break_op (0);
4719 len = strlen (info->description);
4720 if (len == ctx->position) // String processed
4722 if (GNUNET_YES == ntohl (block->accepting))
4724 regex_find_path(key, ctx);
4728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
4729 // FIXME REGEX this block not successful, wait for more? start timeout?
4733 GNUNET_break (GNUNET_OK ==
4734 GNUNET_MESH_regex_block_iterate (block, size,
4735 ®ex_edge_iterator, ctx));
4739 /******************************************************************************/
4740 /********************* MESH LOCAL HANDLES **************************/
4741 /******************************************************************************/
4745 * Handler for client disconnection
4747 * @param cls closure
4748 * @param client identification of the client; NULL
4749 * for the last call when the server is destroyed
4752 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4754 struct MeshClient *c;
4755 struct MeshClient *next;
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4767 if (c->handle != client)
4769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4775 GNUNET_SERVER_client_drop (c->handle);
4776 c->shutting_down = GNUNET_YES;
4777 GNUNET_assert (NULL != c->own_tunnels);
4778 GNUNET_assert (NULL != c->incoming_tunnels);
4779 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4780 &tunnel_destroy_iterator, c);
4781 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4782 &tunnel_destroy_iterator, c);
4783 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4784 &tunnel_destroy_iterator, c);
4785 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4786 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4787 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4789 /* deregister clients applications */
4790 if (NULL != c->apps)
4792 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
4793 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
4795 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
4796 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
4798 GNUNET_SCHEDULER_cancel (announce_applications_task);
4799 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4801 if (NULL != c->types)
4802 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4803 for (i = 0; i < c->n_regex; i++)
4805 GNUNET_free (c->regexes[i]);
4807 GNUNET_free_non_null (c->regexes);
4808 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
4809 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
4811 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
4812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4814 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
4823 * Handler for new clients
4825 * @param cls closure
4826 * @param client identification of the client
4827 * @param message the actual message, which includes messages the client wants
4830 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4831 const struct GNUNET_MessageHeader *message)
4833 struct GNUNET_MESH_ClientConnect *cc_msg;
4834 struct MeshClient *c;
4835 GNUNET_MESH_ApplicationType *a;
4842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4843 /* Check data sanity */
4844 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4845 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4846 ntypes = ntohs (cc_msg->types);
4847 napps = ntohs (cc_msg->applications);
4849 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
4852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4856 /* Create new client structure */
4857 c = GNUNET_malloc (sizeof (struct MeshClient));
4858 c->id = next_client_id++;
4859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4861 GNUNET_SERVER_client_keep (client);
4862 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
4865 GNUNET_MESH_ApplicationType at;
4866 struct GNUNET_HashCode hc;
4868 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
4869 for (i = 0; i < napps; i++)
4872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
4873 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
4874 /* store in clients hashmap */
4875 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
4876 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4877 /* store in global hashmap, for announcements */
4878 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
4879 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4881 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
4882 announce_applications_task =
4883 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
4889 struct GNUNET_HashCode hc;
4891 t = (uint16_t *) & a[napps];
4892 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
4893 for (i = 0; i < ntypes; i++)
4896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4897 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
4899 /* store in clients hashmap */
4900 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4901 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4902 /* store in global hashmap */
4903 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4904 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4908 " client has %u+%u subscriptions\n", napps, ntypes);
4910 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
4911 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4912 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4913 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4914 GNUNET_SERVER_notification_context_add (nc, client);
4915 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4917 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4923 * Handler for clients announcing available services by a regular expression.
4925 * @param cls closure
4926 * @param client identification of the client
4927 * @param message the actual message, which includes messages the client wants
4930 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
4931 const struct GNUNET_MessageHeader *message)
4933 struct MeshClient *c;
4937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
4939 /* Sanity check for client registration */
4940 if (NULL == (c = client_get (client)))
4943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4948 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
4949 regex = GNUNET_malloc (len + 1);
4950 memcpy (regex, &message[1], len);
4952 GNUNET_array_append (c->regexes, c->n_regex, regex);
4953 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
4955 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
4961 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
4967 * Handler for requests of new tunnels
4969 * @param cls closure
4970 * @param client identification of the client
4971 * @param message the actual message
4974 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4975 const struct GNUNET_MessageHeader *message)
4977 struct GNUNET_MESH_TunnelMessage *t_msg;
4978 struct MeshTunnel *t;
4979 struct MeshClient *c;
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4983 /* Sanity check for client registration */
4984 if (NULL == (c = client_get (client)))
4987 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4992 /* Message sanity check */
4993 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5000 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5001 /* Sanity check for tunnel numbering */
5002 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5005 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5008 /* Sanity check for duplicate tunnel IDs */
5009 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
5012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5016 while (NULL != tunnel_get_by_pi (myid, next_tid))
5017 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5018 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
5019 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5021 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5022 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
5024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5025 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5031 * Handler for requests of deleting tunnels
5033 * @param cls closure
5034 * @param client identification of the client
5035 * @param message the actual message
5038 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5039 const struct GNUNET_MessageHeader *message)
5041 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5042 struct MeshClient *c;
5043 struct MeshTunnel *t;
5044 MESH_TunnelNumber tid;
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5047 "Got a DESTROY TUNNEL from client!\n");
5049 /* Sanity check for client registration */
5050 if (NULL == (c = client_get (client)))
5053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5056 /* Message sanity check */
5057 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5064 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5066 /* Retrieve tunnel */
5067 tid = ntohl (tunnel_msg->tunnel_id);
5068 t = tunnel_get_by_local_id(c, tid);
5072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5076 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5078 client_ignore_tunnel (c, t);
5080 // TODO: when to destroy incoming tunnel?
5081 if (t->nclients == 0)
5083 GNUNET_assert (GNUNET_YES ==
5084 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
5086 GNUNET_assert (GNUNET_YES ==
5087 GNUNET_CONTAINER_multihashmap_remove (t->peers,
5088 &my_full_id.hashPubKey,
5092 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5095 send_client_tunnel_disconnect(t, c);
5096 client_delete_tunnel(c, t);
5098 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5100 tunnel_send_destroy (t);
5101 t->destroy = GNUNET_YES;
5102 // The tunnel will be destroyed when the last message is transmitted.
5103 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5109 * Handler for requests of seeting tunnel's speed.
5111 * @param cls Closure (unused).
5112 * @param client Identification of the client.
5113 * @param message The actual message.
5116 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
5117 const struct GNUNET_MessageHeader *message)
5119 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5120 struct MeshClient *c;
5121 struct MeshTunnel *t;
5122 MESH_TunnelNumber tid;
5124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5125 "Got a SPEED request from client!\n");
5127 /* Sanity check for client registration */
5128 if (NULL == (c = client_get (client)))
5131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5136 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5138 /* Retrieve tunnel */
5139 tid = ntohl (tunnel_msg->tunnel_id);
5140 t = tunnel_get_by_local_id(c, tid);
5144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5145 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5149 switch (ntohs(message->type))
5151 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
5152 t->speed_min = GNUNET_YES;
5154 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
5155 t->speed_min = GNUNET_NO;
5160 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5165 * Handler for requests of seeting tunnel's buffering policy.
5167 * @param cls Closure (unused).
5168 * @param client Identification of the client.
5169 * @param message The actual message.
5172 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5173 const struct GNUNET_MessageHeader *message)
5175 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5176 struct MeshClient *c;
5177 struct MeshTunnel *t;
5178 MESH_TunnelNumber tid;
5180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5181 "Got a BUFFER request from client!\n");
5183 /* Sanity check for client registration */
5184 if (NULL == (c = client_get (client)))
5187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5192 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5194 /* Retrieve tunnel */
5195 tid = ntohl (tunnel_msg->tunnel_id);
5196 t = tunnel_get_by_local_id(c, tid);
5200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5205 switch (ntohs(message->type))
5207 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5208 t->nobuffer = GNUNET_NO;
5210 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5211 t->nobuffer = GNUNET_YES;
5217 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5222 * Handler for connection requests to new peers
5224 * @param cls closure
5225 * @param client identification of the client
5226 * @param message the actual message (PeerControl)
5229 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
5230 const struct GNUNET_MessageHeader *message)
5232 struct GNUNET_MESH_PeerControl *peer_msg;
5233 struct MeshPeerInfo *peer_info;
5234 struct MeshClient *c;
5235 struct MeshTunnel *t;
5236 MESH_TunnelNumber tid;
5238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
5239 /* Sanity check for client registration */
5240 if (NULL == (c = client_get (client)))
5243 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5247 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5248 /* Sanity check for message size */
5249 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5256 /* Tunnel exists? */
5257 tid = ntohl (peer_msg->tunnel_id);
5258 t = tunnel_get_by_local_id (c, tid);
5262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5266 /* Does client own tunnel? */
5267 if (t->owner->handle != client)
5270 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
5274 GNUNET_i2s (&peer_msg->peer));
5275 peer_info = peer_info_get (&peer_msg->peer);
5277 tunnel_add_peer (t, peer_info);
5278 peer_info_connect (peer_info, t);
5280 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5286 * Handler for disconnection requests of peers in a tunnel
5288 * @param cls closure
5289 * @param client identification of the client
5290 * @param message the actual message (PeerControl)
5293 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
5294 const struct GNUNET_MessageHeader *message)
5296 struct GNUNET_MESH_PeerControl *peer_msg;
5297 struct MeshPeerInfo *peer_info;
5298 struct MeshClient *c;
5299 struct MeshTunnel *t;
5300 MESH_TunnelNumber tid;
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
5303 /* Sanity check for client registration */
5304 if (NULL == (c = client_get (client)))
5307 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5310 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5311 /* Sanity check for message size */
5312 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5319 /* Tunnel exists? */
5320 tid = ntohl (peer_msg->tunnel_id);
5321 t = tunnel_get_by_local_id (c, tid);
5325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5330 /* Does client own tunnel? */
5331 if (t->owner->handle != client)
5334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
5339 GNUNET_i2s (&peer_msg->peer));
5340 /* Is the peer in the tunnel? */
5342 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
5343 if (NULL == peer_info)
5346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5350 /* Ok, delete peer from tunnel */
5351 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
5352 &peer_msg->peer.hashPubKey);
5354 send_destroy_path (t, peer_info->id);
5355 tunnel_delete_peer (t, peer_info->id);
5356 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5361 * Handler for blacklist requests of peers in a tunnel
5363 * @param cls closure
5364 * @param client identification of the client
5365 * @param message the actual message (PeerControl)
5368 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
5369 const struct GNUNET_MessageHeader *message)
5371 struct GNUNET_MESH_PeerControl *peer_msg;
5372 struct MeshClient *c;
5373 struct MeshTunnel *t;
5374 MESH_TunnelNumber tid;
5376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
5377 /* Sanity check for client registration */
5378 if (NULL == (c = client_get (client)))
5381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5384 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5386 /* Sanity check for message size */
5387 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5394 /* Tunnel exists? */
5395 tid = ntohl (peer_msg->tunnel_id);
5396 t = tunnel_get_by_local_id (c, tid);
5400 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5405 GNUNET_array_append(t->blacklisted, t->nblacklisted,
5406 GNUNET_PEER_intern(&peer_msg->peer));
5411 * Handler for unblacklist requests of peers in a tunnel
5413 * @param cls closure
5414 * @param client identification of the client
5415 * @param message the actual message (PeerControl)
5418 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
5419 const struct GNUNET_MessageHeader *message)
5421 struct GNUNET_MESH_PeerControl *peer_msg;
5422 struct MeshClient *c;
5423 struct MeshTunnel *t;
5424 MESH_TunnelNumber tid;
5428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
5429 /* Sanity check for client registration */
5430 if (NULL == (c = client_get (client)))
5433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5436 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5438 /* Sanity check for message size */
5439 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5446 /* Tunnel exists? */
5447 tid = ntohl (peer_msg->tunnel_id);
5448 t = tunnel_get_by_local_id (c, tid);
5452 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5457 /* if peer is not known, complain */
5458 pid = GNUNET_PEER_search (&peer_msg->peer);
5465 /* search and remove from list */
5466 for (i = 0; i < t->nblacklisted; i++)
5468 if (t->blacklisted[i] == pid)
5470 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
5471 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
5476 /* if peer hasn't been blacklisted, complain */
5482 * Handler for connection requests to new peers by type
5484 * @param cls closure
5485 * @param client identification of the client
5486 * @param message the actual message (ConnectPeerByType)
5489 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
5490 const struct GNUNET_MessageHeader *message)
5492 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
5493 struct MeshClient *c;
5494 struct MeshTunnel *t;
5495 struct GNUNET_HashCode hash;
5496 MESH_TunnelNumber tid;
5498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
5499 /* Sanity check for client registration */
5500 if (NULL == (c = client_get (client)))
5503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5507 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
5508 /* Sanity check for message size */
5509 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
5510 ntohs (connect_msg->header.size))
5513 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5517 /* Tunnel exists? */
5518 tid = ntohl (connect_msg->tunnel_id);
5519 t = tunnel_get_by_local_id (c, tid);
5523 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5527 /* Does client own tunnel? */
5528 if (t->owner->handle != client)
5531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5535 /* Do WE have the service? */
5536 t->type = ntohl (connect_msg->type);
5537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
5538 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
5539 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
5542 /* Yes! Fast forward, add ourselves to the tunnel and send the
5543 * good news to the client, and alert the destination client of
5544 * an incoming tunnel.
5546 * FIXME send a path create to self, avoid code duplication
5548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
5549 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
5550 peer_info_get (&my_full_id),
5551 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
5554 send_client_peer_connected (t, myid);
5555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
5556 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5558 t->local_tid_dest = next_local_tid++;
5559 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5560 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5561 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5565 /* Ok, lets find a peer offering the service */
5566 if (NULL != t->dht_get_type)
5568 GNUNET_DHT_get_stop (t->dht_get_type);
5570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
5571 GNUNET_h2s (&hash));
5573 GNUNET_DHT_get_start (dht_handle,
5574 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
5576 dht_replication_level,
5577 GNUNET_DHT_RO_RECORD_ROUTE |
5578 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5580 &dht_get_type_handler, t);
5582 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5588 * Handler for connection requests to new peers by a string service description.
5590 * @param cls closure
5591 * @param client identification of the client
5592 * @param message the actual message, which includes messages the client wants
5595 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
5596 const struct GNUNET_MessageHeader *message)
5598 struct GNUNET_MESH_ConnectPeerByString *msg;
5599 struct MeshRegexSearchContext *ctx;
5600 struct MeshRegexSearchInfo *info;
5601 struct GNUNET_DHT_GetHandle *get_h;
5602 struct GNUNET_HashCode key;
5603 struct MeshTunnel *t;
5604 struct MeshClient *c;
5605 MESH_TunnelNumber tid;
5609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5610 "Connect by string started\n");
5611 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
5612 size = htons (message->size);
5614 /* Sanity check for client registration */
5615 if (NULL == (c = client_get (client)))
5618 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5622 /* Message size sanity check */
5623 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
5626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5630 /* Tunnel exists? */
5631 tid = ntohl (msg->tunnel_id);
5632 t = tunnel_get_by_local_id (c, tid);
5636 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5640 /* Does client own tunnel? */
5641 if (t->owner->handle != client)
5644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5649 " on tunnel %s [%u]\n",
5650 GNUNET_i2s(&my_full_id),
5653 /* Only one connect_by_string allowed at the same time! */
5654 /* FIXME: allow more, return handle at api level to cancel, document */
5655 if (NULL != t->regex_ctx)
5658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5662 /* Find string itself */
5663 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
5664 string = (const char *) &msg[1];
5666 /* Initialize context */
5667 size = GNUNET_REGEX_get_first_key(string, len, &key);
5668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5669 " consumed %u bits out of %u\n", size, len);
5670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5671 " looking for %s\n", GNUNET_h2s (&key));
5673 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
5675 info->description = GNUNET_malloc (len + 1);
5676 memcpy (info->description, string, len);
5677 info->description[len] = '\0';
5678 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
5679 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
5682 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
5683 ctx->position = size;
5687 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
5689 /* Start search in DHT */
5690 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
5691 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
5692 &key, /* key to search */
5693 dht_replication_level, /* replication level */
5694 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5695 NULL, /* xquery */ // FIXME BLOOMFILTER
5696 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
5697 &dht_get_string_handler, ctx);
5699 GNUNET_break (GNUNET_OK ==
5700 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
5703 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
5705 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
5711 * Handler for client traffic directed to one peer
5713 * @param cls closure
5714 * @param client identification of the client
5715 * @param message the actual message
5718 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5719 const struct GNUNET_MessageHeader *message)
5721 struct MeshClient *c;
5722 struct MeshTunnel *t;
5723 struct MeshPeerInfo *pi;
5724 struct GNUNET_MESH_Unicast *data_msg;
5725 MESH_TunnelNumber tid;
5728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5729 "Got a unicast request from a client!\n");
5731 /* Sanity check for client registration */
5732 if (NULL == (c = client_get (client)))
5735 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5738 data_msg = (struct GNUNET_MESH_Unicast *) message;
5739 /* Sanity check for message size */
5740 size = ntohs (message->size);
5741 if (sizeof (struct GNUNET_MESH_Unicast) +
5742 sizeof (struct GNUNET_MessageHeader) > size)
5745 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5749 /* Tunnel exists? */
5750 tid = ntohl (data_msg->tid);
5751 t = tunnel_get_by_local_id (c, tid);
5755 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5759 /* Is it a local tunnel? Then, does client own the tunnel? */
5760 if (t->owner->handle != client)
5763 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5767 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
5768 &data_msg->destination.hashPubKey);
5769 /* Is the selected peer in the tunnel? */
5773 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5777 /* Ok, everything is correct, send the message
5778 * (pretend we got it from a mesh peer)
5781 char buf[ntohs (message->size)] GNUNET_ALIGN;
5782 struct GNUNET_MESH_Unicast *copy;
5784 /* Work around const limitation */
5785 copy = (struct GNUNET_MESH_Unicast *) buf;
5786 memcpy (buf, data_msg, size);
5787 copy->oid = my_full_id;
5788 copy->tid = htonl (t->id.tid);
5789 copy->ttl = htonl (default_ttl);
5790 copy->pid = htonl (t->pid + 1);
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5792 " calling generic handler...\n");
5793 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
5795 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5801 * Handler for client traffic directed to the origin
5803 * @param cls closure
5804 * @param client identification of the client
5805 * @param message the actual message
5808 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5809 const struct GNUNET_MessageHeader *message)
5811 struct GNUNET_MESH_ToOrigin *data_msg;
5812 struct GNUNET_PeerIdentity id;
5813 struct MeshClient *c;
5814 struct MeshTunnel *t;
5815 MESH_TunnelNumber tid;
5818 /* Sanity check for client registration */
5819 if (NULL == (c = client_get (client)))
5822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5825 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5826 /* Sanity check for message size */
5827 size = ntohs (message->size);
5828 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5829 sizeof (struct GNUNET_MessageHeader) > size)
5832 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5836 /* Tunnel exists? */
5837 tid = ntohl (data_msg->tid);
5838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5839 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
5840 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5843 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5846 t = tunnel_get_by_local_id (c, tid);
5850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5854 /* It should be sent by someone who has this as incoming tunnel. */
5855 if (-1 == client_knows_tunnel (c, t))
5858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5861 GNUNET_PEER_resolve (t->id.oid, &id);
5863 /* Ok, everything is correct, send the message
5864 * (pretend we got it from a mesh peer)
5867 char buf[ntohs (message->size)] GNUNET_ALIGN;
5868 struct GNUNET_MESH_ToOrigin *copy;
5870 /* Work around const limitation */
5871 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5872 memcpy (buf, data_msg, size);
5874 copy->tid = htonl (t->id.tid);
5875 copy->sender = my_full_id;
5876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5877 " calling generic handler...\n");
5878 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
5880 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5886 * Handler for client traffic directed to all peers in a tunnel
5888 * @param cls closure
5889 * @param client identification of the client
5890 * @param message the actual message
5893 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
5894 const struct GNUNET_MessageHeader *message)
5896 struct MeshClient *c;
5897 struct MeshTunnel *t;
5898 struct GNUNET_MESH_Multicast *data_msg;
5899 MESH_TunnelNumber tid;
5901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5902 "Got a multicast request from a client!\n");
5904 /* Sanity check for client registration */
5905 if (NULL == (c = client_get (client)))
5908 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5911 data_msg = (struct GNUNET_MESH_Multicast *) message;
5912 /* Sanity check for message size */
5913 if (sizeof (struct GNUNET_MESH_Multicast) +
5914 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
5917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5921 /* Tunnel exists? */
5922 tid = ntohl (data_msg->tid);
5923 t = tunnel_get_by_local_id (c, tid);
5927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5931 /* Does client own tunnel? */
5932 if (t->owner->handle != client)
5935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5940 char buf[ntohs (message->size)] GNUNET_ALIGN;
5941 struct GNUNET_MESH_Multicast *copy;
5943 copy = (struct GNUNET_MESH_Multicast *) buf;
5944 memcpy (buf, message, ntohs (message->size));
5945 copy->oid = my_full_id;
5946 copy->tid = htonl (t->id.tid);
5947 copy->ttl = htonl (default_ttl);
5948 copy->pid = htonl (t->pid + 1);
5949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5950 " calling generic handler...\n");
5951 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
5954 /* receive done gets called when last copy is sent to a neighbor */
5959 * Functions to handle messages from clients
5961 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5962 {&handle_local_new_client, NULL,
5963 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5964 {&handle_local_announce_regex, NULL,
5965 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
5966 {&handle_local_tunnel_create, NULL,
5967 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5968 sizeof (struct GNUNET_MESH_TunnelMessage)},
5969 {&handle_local_tunnel_destroy, NULL,
5970 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5971 sizeof (struct GNUNET_MESH_TunnelMessage)},
5972 {&handle_local_tunnel_speed, NULL,
5973 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
5974 sizeof (struct GNUNET_MESH_TunnelMessage)},
5975 {&handle_local_tunnel_speed, NULL,
5976 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
5977 sizeof (struct GNUNET_MESH_TunnelMessage)},
5978 {&handle_local_tunnel_buffer, NULL,
5979 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
5980 sizeof (struct GNUNET_MESH_TunnelMessage)},
5981 {&handle_local_tunnel_buffer, NULL,
5982 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
5983 sizeof (struct GNUNET_MESH_TunnelMessage)},
5984 {&handle_local_connect_add, NULL,
5985 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
5986 sizeof (struct GNUNET_MESH_PeerControl)},
5987 {&handle_local_connect_del, NULL,
5988 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
5989 sizeof (struct GNUNET_MESH_PeerControl)},
5990 {&handle_local_blacklist, NULL,
5991 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
5992 sizeof (struct GNUNET_MESH_PeerControl)},
5993 {&handle_local_unblacklist, NULL,
5994 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
5995 sizeof (struct GNUNET_MESH_PeerControl)},
5996 {&handle_local_connect_by_type, NULL,
5997 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
5998 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
5999 {&handle_local_connect_by_string, NULL,
6000 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
6001 {&handle_local_unicast, NULL,
6002 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6003 {&handle_local_to_origin, NULL,
6004 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6005 {&handle_local_multicast, NULL,
6006 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6012 * To be called on core init/fail.
6014 * @param cls service closure
6015 * @param server handle to the server for this service
6016 * @param identity the public identity of this peer
6019 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6020 const struct GNUNET_PeerIdentity *identity)
6022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6023 core_handle = server;
6024 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6027 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6028 GNUNET_SCHEDULER_shutdown ();
6034 * Method called whenever a given peer connects.
6036 * @param cls closure
6037 * @param peer peer identity this notification is about
6038 * @param atsi performance data for the connection
6039 * @param atsi_count number of records in 'atsi'
6042 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
6043 const struct GNUNET_ATS_Information *atsi,
6044 unsigned int atsi_count)
6046 struct MeshPeerInfo *peer_info;
6047 struct MeshPeerPath *path;
6049 DEBUG_CONN ("Peer connected\n");
6050 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6051 peer_info = peer_info_get (peer);
6052 if (myid == peer_info->id)
6054 DEBUG_CONN (" (self)\n");
6059 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6061 path = path_new (2);
6062 path->peers[0] = myid;
6063 path->peers[1] = peer_info->id;
6064 GNUNET_PEER_change_rc (myid, 1);
6065 GNUNET_PEER_change_rc (peer_info->id, 1);
6066 peer_info_add_path (peer_info, path, GNUNET_YES);
6067 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6072 * Method called whenever a peer disconnects.
6074 * @param cls closure
6075 * @param peer peer identity this notification is about
6078 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6080 struct MeshPeerInfo *pi;
6081 struct MeshPeerQueue *q;
6082 struct MeshPeerQueue *n;
6084 DEBUG_CONN ("Peer disconnected\n");
6085 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6097 /* try to reroute this traffic instead */
6098 queue_destroy(q, GNUNET_YES);
6102 peer_info_remove_path (pi, pi->id, myid);
6105 DEBUG_CONN (" (self)\n");
6107 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6112 /******************************************************************************/
6113 /************************ MAIN FUNCTIONS ****************************/
6114 /******************************************************************************/
6117 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6119 * @param cls closure
6120 * @param key current key code
6121 * @param value value in the hash map
6122 * @return GNUNET_YES if we should continue to iterate,
6126 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6128 struct MeshTunnel *t = value;
6135 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
6137 * @param cls closure
6138 * @param key current key code
6139 * @param value value in the hash map
6140 * @return GNUNET_YES if we should continue to iterate,
6144 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
6146 struct MeshPeerInfo *p = value;
6147 struct MeshPeerQueue *q;
6148 struct MeshPeerQueue *n;
6156 queue_destroy(q, GNUNET_YES);
6160 peer_info_destroy (p);
6165 * Task run during shutdown.
6171 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6175 if (core_handle != NULL)
6177 GNUNET_CORE_disconnect (core_handle);
6180 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6181 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6182 if (dht_handle != NULL)
6184 GNUNET_DHT_disconnect (dht_handle);
6189 GNUNET_SERVER_notification_context_destroy (nc);
6192 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6194 GNUNET_SCHEDULER_cancel (announce_id_task);
6195 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6201 * Process mesh requests.
6203 * @param cls closure
6204 * @param server the initialized server
6205 * @param c configuration to use
6208 run (void *cls, struct GNUNET_SERVER_Handle *server,
6209 const struct GNUNET_CONFIGURATION_Handle *c)
6211 struct MeshPeerInfo *peer;
6212 struct MeshPeerPath *p;
6215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6216 server_handle = server;
6217 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6218 NULL, /* Closure passed to MESH functions */
6219 &core_init, /* Call core_init once connected */
6220 &core_connect, /* Handle connects */
6221 &core_disconnect, /* remove peers on disconnects */
6222 NULL, /* Don't notify about all incoming messages */
6223 GNUNET_NO, /* For header only in notification */
6224 NULL, /* Don't notify about all outbound messages */
6225 GNUNET_NO, /* For header-only out notification */
6226 core_handlers); /* Register these handlers */
6228 if (core_handle == NULL)
6231 GNUNET_SCHEDULER_shutdown ();
6236 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
6239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6241 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6243 GNUNET_SCHEDULER_shutdown ();
6248 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6249 &refresh_path_time))
6251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6253 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6254 "refresh path time");
6255 GNUNET_SCHEDULER_shutdown ();
6260 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6261 &app_announce_time))
6263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6265 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6266 "app announce time");
6267 GNUNET_SCHEDULER_shutdown ();
6272 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6277 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6278 "id announce time");
6279 GNUNET_SCHEDULER_shutdown ();
6284 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
6285 &unacknowledged_wait_time))
6287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6289 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6290 "unacknowledged wait time");
6291 GNUNET_SCHEDULER_shutdown ();
6296 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6301 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6303 GNUNET_SCHEDULER_shutdown ();
6308 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6313 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
6319 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6320 &dht_replication_level))
6322 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6324 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
6325 "dht replication level", 10);
6326 dht_replication_level = 10;
6330 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6331 GNUNET_free (keyfile);
6332 if (my_private_key == NULL)
6334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6335 _("Mesh service could not access hostkey. Exiting.\n"));
6336 GNUNET_SCHEDULER_shutdown ();
6339 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6340 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6341 &my_full_id.hashPubKey);
6342 myid = GNUNET_PEER_intern (&my_full_id);
6344 // transport_handle = GNUNET_TRANSPORT_connect(c,
6351 dht_handle = GNUNET_DHT_connect (c, 64);
6352 if (dht_handle == NULL)
6357 stats = GNUNET_STATISTICS_create ("mesh", c);
6361 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6363 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6364 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6365 peers = GNUNET_CONTAINER_multihashmap_create (32);
6366 applications = GNUNET_CONTAINER_multihashmap_create (32);
6367 types = GNUNET_CONTAINER_multihashmap_create (32);
6369 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6370 nc = GNUNET_SERVER_notification_context_create (server_handle,
6372 GNUNET_SERVER_disconnect_notify (server_handle,
6373 &handle_local_client_disconnect, NULL);
6377 clients_tail = NULL;
6380 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6381 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6383 /* Create a peer_info for the local peer */
6384 peer = peer_info_get (&my_full_id);
6387 GNUNET_PEER_change_rc (myid, 1);
6388 peer_info_add_path (peer, p, GNUNET_YES);
6390 /* Scheduled the task to clean up when shutdown is called */
6391 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
6398 * The main function for the mesh service.
6400 * @param argc number of arguments from the command line
6401 * @param argv command line arguments
6402 * @return 0 ok, 1 on error
6405 main (int argc, char *const *argv)
6409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6412 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");