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 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
303 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
313 uint32_t max_child_ack;
316 * How many messages are in the queue.
318 unsigned int queue_n;
321 * How many messages do we accept in the queue.
323 unsigned int queue_max;
326 * Is the speed on the tunnel limited to the slowest peer?
331 * Is the tunnel bufferless (minimum latency)?
336 * Flag to signal the destruction of the tunnel.
337 * If this is set GNUNET_YES the tunnel will be destroyed
338 * when the queue is empty.
343 * Last time the tunnel was used
345 struct GNUNET_TIME_Absolute timestamp;
348 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
349 * containing peers added by id or by type, not intermediate peers.
351 struct GNUNET_CONTAINER_MultiHashMap *peers;
354 * Number of peers that are connected and potentially ready to receive data
356 unsigned int peers_ready;
359 * Number of peers that have been added to the tunnel
361 unsigned int peers_total;
364 * Client owner of the tunnel, if any
366 struct MeshClient *owner;
369 * Clients that have been informed about the tunnel, if any
371 struct MeshClient **clients;
374 * Number of elements in clients
376 unsigned int nclients;
379 * Clients that have requested to leave the tunnel
381 struct MeshClient **ignore;
384 * Number of elements in clients
386 unsigned int nignore;
391 GNUNET_PEER_Id *blacklisted;
394 * Number of elements in blacklisted
396 unsigned int nblacklisted;
399 * Bloomfilter (for peer identities) to stop circular routes
401 char bloomfilter[MESH_BLOOM_SIZE];
406 struct MeshTunnelTree *tree;
409 * Application type we are looking for in this tunnel
411 GNUNET_MESH_ApplicationType type;
414 * Used to search peers offering a service
416 struct GNUNET_DHT_GetHandle *dht_get_type;
419 * Initial context of the regex search for a connect_by_string
421 struct MeshRegexSearchContext *regex_ctx;
424 * Task to keep the used paths alive
426 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
429 * Task to destroy the tunnel after timeout
431 * FIXME: merge the two? a tunnel will have either
432 * a path refresh OR a timeout, never both!
434 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
439 * Info about a child node in a tunnel, needed to perform flow control.
441 struct MeshTunnelChildInfo
444 * ID of the child node.
459 * Maximum PID allowed.
465 * Info needed to work with tunnel paths and peers
472 struct MeshTunnel *t;
475 * Neighbouring peer to whom we send the packet to
477 struct MeshPeerInfo *peer;
482 struct MeshPeerPath *path;
487 * Struct containing information about a client of the service
494 struct MeshClient *next;
499 struct MeshClient *prev;
502 * Tunnels that belong to this client, indexed by local id
504 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
507 * Tunnels this client has accepted, indexed by incoming local id
509 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
512 * Tunnels this client has rejected, indexed by incoming local id
514 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
516 * Handle to communicate with the client
518 struct GNUNET_SERVER_Client *handle;
521 * Applications that this client has claimed to provide
523 struct GNUNET_CONTAINER_MultiHashMap *apps;
526 * Messages that this client has declared interest in
528 struct GNUNET_CONTAINER_MultiHashMap *types;
531 * Whether the client is active or shutting down (don't send confirmations
532 * to a client that is shutting down.
537 * ID of the client, mainly for debug messages
542 * Regular expressions describing the services offered by this client.
544 char **regexes; // FIXME add timeout? API to remove a regex?
547 * Number of regular expressions in regexes.
549 unsigned int n_regex;
552 * Task to refresh all regular expresions in the DHT.
554 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
560 * Struct to keep information of searches of services described by a regex
561 * using a user-provided string service description.
563 struct MeshRegexSearchInfo
566 * Which tunnel is this for
568 struct MeshTunnel *t;
571 * User provided description of the searched service.
576 * Part of the description already consumed by the search.
583 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
586 * Results from running DHT GETs.
588 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
591 * Contexts, for each running DHT GET. Free all on end of search.
593 struct MeshRegexSearchContext **contexts;
596 * Number of contexts (branches/steps in search).
598 unsigned int n_contexts;
601 * Peer that is connecting via connect_by_string. When connected, free ctx.
606 * Other peers that are found but not yet being connected to.
608 GNUNET_PEER_Id *peers;
611 * Number of elements in peers.
613 unsigned int n_peers;
616 * Next peer to try to connect to.
621 * Timeout for a connect attempt.
622 * When reached, try to connect to a different peer, if any. If not,
623 * try the same peer again.
625 GNUNET_SCHEDULER_TaskIdentifier timeout;
630 * Struct to keep state of running searches that have consumed a part of
633 struct MeshRegexSearchContext
636 * Part of the description already consumed by
637 * this particular search branch.
642 * Information about the search.
644 struct MeshRegexSearchInfo *info;
648 /******************************************************************************/
649 /************************ DEBUG FUNCTIONS ****************************/
650 /******************************************************************************/
654 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
655 * @param cls string to print
656 * @param success GNUNET_OK if the PUT was transmitted,
657 * GNUNET_NO on timeout,
658 * GNUNET_SYSERR on disconnect from service
659 * after the PUT message was transmitted
660 * (so we don't know if it was received or not)
665 mesh_debug (void *cls, int success)
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
675 /******************************************************************************/
676 /*********************** GLOBAL VARIABLES ****************************/
677 /******************************************************************************/
681 * Configuration parameters
683 static struct GNUNET_TIME_Relative refresh_path_time;
684 static struct GNUNET_TIME_Relative app_announce_time;
685 static struct GNUNET_TIME_Relative id_announce_time;
686 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
687 static struct GNUNET_TIME_Relative connect_timeout;
688 static long long unsigned int default_ttl;
689 static long long unsigned int dht_replication_level;
693 * DLL with all the clients, head.
695 static struct MeshClient *clients;
698 * DLL with all the clients, tail.
700 static struct MeshClient *clients_tail;
703 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
705 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
708 * Tunnels incoming, indexed by MESH_TunnelNumber
709 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
711 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
714 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
716 static struct GNUNET_CONTAINER_MultiHashMap *peers;
719 * Handle to communicate with transport
721 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
724 * Handle to communicate with core.
726 static struct GNUNET_CORE_Handle *core_handle;
731 static struct GNUNET_DHT_Handle *dht_handle;
736 static struct GNUNET_SERVER_Handle *server_handle;
739 * Handle to the statistics service.
741 static struct GNUNET_STATISTICS_Handle *stats;
744 * Notification context, to send messages to local clients.
746 static struct GNUNET_SERVER_NotificationContext *nc;
749 * Local peer own ID (memory efficient handle).
751 static GNUNET_PEER_Id myid;
754 * Local peer own ID (full value).
756 static struct GNUNET_PeerIdentity my_full_id;
761 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
766 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
769 * Tunnel ID for the next created tunnel (global tunnel number).
771 static MESH_TunnelNumber next_tid;
774 * Tunnel ID for the next incoming tunnel (local tunnel number).
776 static MESH_TunnelNumber next_local_tid;
779 * All application types provided by this peer.
781 static struct GNUNET_CONTAINER_MultiHashMap *applications;
784 * All message types clients of this peer are interested in.
786 static struct GNUNET_CONTAINER_MultiHashMap *types;
789 * Task to periodically announce provided applications.
791 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
794 * Task to periodically announce itself in the network.
796 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
799 * Next ID to assign to a client.
801 unsigned int next_client_id;
804 /******************************************************************************/
805 /*********************** DECLARATIONS **************************/
806 /******************************************************************************/
808 /* FIXME move declarations here */
811 * Function to process paths received for a new peer addition. The recorded
812 * paths form the initial tunnel, which can be optimized later.
813 * Called on each result obtained for the DHT search.
816 * @param exp when will this value expire
817 * @param key key of the result
818 * @param type type of the result
819 * @param size number of bytes in data
820 * @param data pointer to the result data
823 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
824 const struct GNUNET_HashCode * key,
825 const struct GNUNET_PeerIdentity *get_path,
826 unsigned int get_path_length,
827 const struct GNUNET_PeerIdentity *put_path,
828 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
829 size_t size, const void *data);
833 * Function to process DHT string to regex matching.
834 * Called on each result obtained for the DHT search.
836 * @param cls closure (search context)
837 * @param exp when will this value expire
838 * @param key key of the result
839 * @param get_path path of the get request (not used)
840 * @param get_path_length lenght of get_path (not used)
841 * @param put_path path of the put request (not used)
842 * @param put_path_length length of the put_path (not used)
843 * @param type type of the result
844 * @param size number of bytes in data
845 * @param data pointer to the result data
847 * TODO: re-issue the request after certain time? cancel after X results?
850 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
851 const struct GNUNET_HashCode * key,
852 const struct GNUNET_PeerIdentity *get_path,
853 unsigned int get_path_length,
854 const struct GNUNET_PeerIdentity *put_path,
855 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
856 size_t size, const void *data);
860 * Function to process DHT string to regex matching.
861 * Called on each result obtained for the DHT search.
863 * @param cls closure (search context)
864 * @param exp when will this value expire
865 * @param key key of the result
866 * @param get_path path of the get request (not used)
867 * @param get_path_length lenght of get_path (not used)
868 * @param put_path path of the put request (not used)
869 * @param put_path_length length of the put_path (not used)
870 * @param type type of the result
871 * @param size number of bytes in data
872 * @param data pointer to the result data
875 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
876 const struct GNUNET_HashCode * key,
877 const struct GNUNET_PeerIdentity *get_path,
878 unsigned int get_path_length,
879 const struct GNUNET_PeerIdentity *put_path,
880 unsigned int put_path_length,
881 enum GNUNET_BLOCK_Type type,
882 size_t size, const void *data);
886 * Iterator over edges in a regex block retrieved from the DHT.
888 * @param cls Closure.
889 * @param token Token that follows to next state.
890 * @param len Lenght of token.
891 * @param key Hash of next state.
893 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
896 regex_edge_iterator (void *cls,
899 const struct GNUNET_HashCode *key);
903 * Find a path to a peer that offers a regex servcie compatible
904 * with a given string.
906 * @param key The key of the accepting state.
907 * @param ctx Context containing info about the string, tunnel, etc.
910 regex_find_path (const struct GNUNET_HashCode *key,
911 struct MeshRegexSearchContext *ctx);
915 * Queue and pass message to core when possible.
917 * @param cls Closure (type dependant).
918 * @param type Type of the message, 0 for a raw message.
919 * @param size Size of the message.
920 * @param dst Neighbor to send message to.
921 * @param t Tunnel this message belongs to.
924 queue_add (void *cls, uint16_t type, size_t size,
925 struct MeshPeerInfo *dst, struct MeshTunnel *t);
928 * Free a transmission that was already queued with all resources
929 * associated to the request.
931 * @param queue Queue handler to cancel.
932 * @param clear_cls Is it necessary to free associated cls?
935 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
937 /******************************************************************************/
938 /************************ ITERATORS ****************************/
939 /******************************************************************************/
941 /* FIXME move iterators here */
944 * Iterator over found existing mesh regex blocks that match an ongoing search.
947 * @param key current key code
948 * @param value value in the hash map
949 * @return GNUNET_YES if we should continue to iterate,
953 regex_result_iterator (void *cls,
954 const struct GNUNET_HashCode * key,
957 struct MeshRegexBlock *block = value;
958 struct MeshRegexSearchContext *ctx = cls;
960 if (GNUNET_YES == ntohl(block->accepting) &&
961 ctx->position == strlen (ctx->info->description))
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
964 regex_find_path (key, ctx);
965 return GNUNET_YES; // We found an accept state!
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
970 ctx->position, strlen(ctx->info->description),
971 ntohl(block->accepting));
974 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
975 ®ex_edge_iterator, ctx);
981 * Iterator over edges in a regex block retrieved from the DHT.
983 * @param cls Closure (context of the search).
984 * @param token Token that follows to next state.
985 * @param len Lenght of token.
986 * @param key Hash of next state.
988 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
991 regex_edge_iterator (void *cls,
994 const struct GNUNET_HashCode *key)
996 struct MeshRegexSearchContext *ctx = cls;
997 struct MeshRegexSearchContext *new_ctx;
998 struct MeshRegexSearchInfo *info = ctx->info;
999 struct GNUNET_DHT_GetHandle *get_h;
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1006 current = &info->description[ctx->position];
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1008 current_len = strlen (info->description) - ctx->position;
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1013 if (len > current_len)
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1016 return GNUNET_YES; // Token too long, wont match
1018 if (0 != strncmp (current, token, len))
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1021 return GNUNET_YES; // Token doesn't match
1023 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1024 new_ctx->info = info;
1025 new_ctx->position = ctx->position + len;
1026 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1028 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1031 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1032 ®ex_result_iterator,
1034 return GNUNET_YES; // We are already looking for it
1036 /* Start search in DHT */
1038 GNUNET_DHT_get_start (dht_handle, /* handle */
1039 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1040 key, /* key to search */
1041 dht_replication_level, /* replication level */
1042 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1043 NULL, /* xquery */ // FIXME BLOOMFILTER
1044 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1045 &dht_get_string_handler, new_ctx);
1047 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1048 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1058 * Iterator over hash map entries to cancel DHT GET requests after a
1059 * successful connect_by_string.
1061 * @param cls Closure (unused).
1062 * @param key Current key code (unused).
1063 * @param value Value in the hash map (get handle).
1064 * @return GNUNET_YES if we should continue to iterate,
1068 regex_cancel_dht_get (void *cls,
1069 const struct GNUNET_HashCode * key,
1072 struct GNUNET_DHT_GetHandle *h = value;
1074 GNUNET_DHT_get_stop (h);
1080 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1081 * search for connect_by_string.
1083 * @param cls Closure (unused).
1084 * @param key Current key code (unused).
1085 * @param value MeshRegexBlock in the hash map.
1086 * @return GNUNET_YES if we should continue to iterate,
1090 regex_free_result (void *cls,
1091 const struct GNUNET_HashCode * key,
1095 GNUNET_free (value);
1101 * Regex callback iterator to store own service description in the DHT.
1103 * @param cls closure.
1104 * @param key hash for current state.
1105 * @param proof proof for current state.
1106 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1107 * @param num_edges number of edges leaving current state.
1108 * @param edges edges leaving current state.
1111 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1112 int accepting, unsigned int num_edges,
1113 const struct GNUNET_REGEX_Edge *edges)
1115 struct MeshRegexBlock *block;
1116 struct MeshRegexEdge *block_edge;
1117 enum GNUNET_DHT_RouteOption opt;
1121 unsigned int offset;
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1125 " regex dht put for state %s\n",
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1135 if (GNUNET_YES == accepting)
1137 struct MeshRegexAccept block;
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 " state %s is accepting, putting own id\n",
1142 size = sizeof (block);
1144 block.id = my_full_id;
1146 GNUNET_DHT_put(dht_handle, key,
1147 dht_replication_level,
1148 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1149 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1152 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1157 len = strlen(proof);
1158 size = sizeof (struct MeshRegexBlock) + len;
1159 block = GNUNET_malloc (size);
1162 block->n_proof = htonl (len);
1163 block->n_edges = htonl (num_edges);
1164 block->accepting = htonl (accepting);
1166 /* Store the proof at the end of the block. */
1167 aux = (char *) &block[1];
1168 memcpy (aux, proof, len);
1171 /* Store each edge in a variable length MeshEdge struct at the
1172 * very end of the MeshRegexBlock structure.
1174 for (i = 0; i < num_edges; i++)
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 " edge %s towards %s\n",
1179 GNUNET_h2s(&edges[i].destination));
1181 /* aux points at the end of the last block */
1182 len = strlen (edges[i].label);
1183 size += sizeof (struct MeshRegexEdge) + len;
1184 // Calculate offset FIXME is this ok? use size instead?
1185 offset = aux - (char *) block;
1186 block = GNUNET_realloc (block, size);
1187 aux = &((char *) block)[offset];
1188 block_edge = (struct MeshRegexEdge *) aux;
1189 block_edge->key = edges[i].destination;
1190 block_edge->n_token = htonl (len);
1191 aux = (char *) &block_edge[1];
1192 memcpy (aux, edges[i].label, len);
1196 GNUNET_DHT_put(dht_handle, key,
1197 dht_replication_level,
1199 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1201 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1205 GNUNET_free (block);
1210 * Store the regular expression describing a local service into the DHT.
1212 * @param regex The regular expresion.
1215 regex_put (const char *regex)
1217 struct GNUNET_REGEX_Automaton *dfa;
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1220 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1221 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1222 GNUNET_REGEX_automaton_destroy (dfa);
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1228 * Find a path to a peer that offers a regex servcie compatible
1229 * with a given string.
1231 * @param key The key of the accepting state.
1232 * @param ctx Context containing info about the string, tunnel, etc.
1235 regex_find_path (const struct GNUNET_HashCode *key,
1236 struct MeshRegexSearchContext *ctx)
1238 struct GNUNET_DHT_GetHandle *get_h;
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1241 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1242 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1243 key, /* key to search */
1244 dht_replication_level, /* replication level */
1245 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1246 GNUNET_DHT_RO_RECORD_ROUTE,
1247 NULL, /* xquery */ // FIXME BLOOMFILTER
1248 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1249 &dht_get_string_accept_handler, ctx);
1250 GNUNET_break (GNUNET_OK ==
1251 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1254 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1258 /******************************************************************************/
1259 /************************ PERIODIC FUNCTIONS ****************************/
1260 /******************************************************************************/
1263 * Announce iterator over for each application provided by the peer
1265 * @param cls closure
1266 * @param key current key code
1267 * @param value value in the hash map
1268 * @return GNUNET_YES if we should continue to
1273 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1275 struct PBlock block;
1276 struct MeshClient *c;
1278 block.id = my_full_id;
1279 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1280 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1281 if (0 == block.type)
1286 block.type = htonl (block.type);
1287 /* FIXME are hashes in multihash map equal on all aquitectures? */
1288 /* FIXME: keep return value of 'put' to possibly cancel!? */
1289 GNUNET_DHT_put (dht_handle, key,
1290 dht_replication_level,
1291 GNUNET_DHT_RO_RECORD_ROUTE |
1292 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1293 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1295 (const char *) &block,
1296 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1298 app_announce_time, NULL, NULL);
1304 * Periodically announce what applications are provided by local clients
1307 * @param cls closure
1308 * @param tc task context
1311 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1313 struct MeshClient *c = cls;
1316 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1318 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1322 DEBUG_DHT ("Starting PUT for regex\n");
1324 for (i = 0; i < c->n_regex; i++)
1326 regex_put (c->regexes[i]);
1328 c->regex_announce_task =
1329 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1330 DEBUG_DHT ("Finished PUT for regex\n");
1337 * Periodically announce what applications are provided by local clients
1340 * @param cls closure
1341 * @param tc task context
1344 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1346 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1348 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1352 DEBUG_DHT ("Starting PUT for apps\n");
1354 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1356 announce_applications_task =
1357 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1359 DEBUG_DHT ("Finished PUT for apps\n");
1366 * Periodically announce self id in the DHT
1368 * @param cls closure
1369 * @param tc task context
1372 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1374 struct PBlock block;
1376 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1378 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1382 * - Set data expiration in function of X
1383 * - Adapt X to churn
1385 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1387 block.id = my_full_id;
1388 block.type = htonl (0);
1389 GNUNET_DHT_put (dht_handle, /* DHT handle */
1390 &my_full_id.hashPubKey, /* Key to use */
1391 dht_replication_level, /* Replication level */
1392 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1393 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1394 sizeof (block), /* Size of the data */
1395 (const char *) &block, /* Data itself */
1396 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1397 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1398 NULL, /* Continuation */
1399 NULL); /* Continuation closure */
1401 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1405 /******************************************************************************/
1406 /****************** GENERAL HELPER FUNCTIONS ************************/
1407 /******************************************************************************/
1410 * Cancel an ongoing regex search in the DHT and free all resources.
1412 * @param ctx The search context.
1415 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1417 struct MeshRegexSearchInfo *info = ctx->info;
1420 GNUNET_free (info->description);
1421 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1422 ®ex_cancel_dht_get, NULL);
1423 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1424 ®ex_free_result, NULL);
1425 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1426 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1427 info->t->regex_ctx = NULL;
1428 for (i = 0; i < info->n_contexts; i++)
1430 GNUNET_free (info->contexts[i]);
1432 if (0 < info->n_contexts)
1433 GNUNET_free (info->contexts);
1434 if (0 < info->n_peers)
1435 GNUNET_free (info->peers);
1436 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1438 GNUNET_SCHEDULER_cancel(info->timeout);
1444 * Search for a tunnel by global ID using full PeerIdentities
1446 * @param oid owner of the tunnel
1447 * @param tid global tunnel number
1449 * @return tunnel handler, NULL if doesn't exist
1451 static struct MeshTunnel *
1452 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1456 * Delete an active client from the tunnel.
1462 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1465 * Notify a tunnel that a connection has broken that affects at least
1466 * some of its peers.
1468 * @param t Tunnel affected.
1469 * @param p1 Peer that got disconnected from p2.
1470 * @param p2 Peer that got disconnected from p1.
1472 * @return Short ID of the peer disconnected (either p1 or p2).
1473 * 0 if the tunnel remained unaffected.
1475 static GNUNET_PEER_Id
1476 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1481 * Check if client has registered with the service and has not disconnected
1483 * @param client the client to check
1485 * @return non-NULL if client exists in the global DLL
1487 static struct MeshClient *
1488 client_get (struct GNUNET_SERVER_Client *client)
1490 struct MeshClient *c;
1495 if (c->handle == client)
1504 * Checks if a given client has subscribed to certain message type
1506 * @param message_type Type of message to check
1507 * @param c Client to check
1509 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1512 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1514 struct GNUNET_HashCode hc;
1516 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1517 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1522 * Allow a client to send more data after transmitting a multicast message
1523 * which some neighbor has not yet accepted altough a reasonable time has
1526 * @param cls Closure (DataDescriptor containing the task identifier)
1527 * @param tc Task Context
1529 * FIXME reference counter cshould be just int
1532 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1534 struct MeshData *mdata = cls;
1536 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1538 GNUNET_assert (NULL != mdata->reference_counter);
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1540 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1541 *(mdata->reference_counter));
1542 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1543 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1548 * Check whether client wants traffic from a tunnel.
1550 * @param c Client to check.
1551 * @param t Tunnel to be found.
1553 * @return GNUNET_YES if client knows tunnel.
1555 * TODO look in client hashmap
1558 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1562 for (i = 0; i < t->nclients; i++)
1563 if (t->clients[i] == c)
1570 * Check whether client has been informed about a tunnel.
1572 * @param c Client to check.
1573 * @param t Tunnel to be found.
1575 * @return GNUNET_YES if client knows tunnel.
1577 * TODO look in client hashmap
1580 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1584 for (i = 0; i < t->nignore; i++)
1585 if (t->ignore[i] == c)
1587 return client_wants_tunnel(c, t);
1592 * Marks a client as uninterested in traffic from the tunnel, updating both
1593 * client and tunnel to reflect this.
1595 * @param c Client that doesn't want traffic anymore.
1596 * @param t Tunnel which should be ignored.
1598 * FIXME when to delete an incoming tunnel?
1601 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1603 struct GNUNET_HashCode hash;
1605 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1606 GNUNET_break (GNUNET_YES ==
1607 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1609 GNUNET_break (GNUNET_YES ==
1610 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1611 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1612 tunnel_delete_active_client (t, c);
1613 GNUNET_array_append (t->ignore, t->nignore, c);
1618 * Deletes a tunnel from a client (either owner or destination). To be used on
1619 * tunnel destroy, otherwise, use client_ignore_tunnel.
1621 * @param c Client whose tunnel to delete.
1622 * @param t Tunnel which should be deleted.
1625 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1627 struct GNUNET_HashCode hash;
1631 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1632 GNUNET_assert (GNUNET_YES ==
1633 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1639 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1641 GNUNET_assert (GNUNET_YES ==
1642 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1646 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1655 * Send the message to all clients that have subscribed to its type
1657 * @param msg Pointer to the message itself
1658 * @param payload Pointer to the payload of the message.
1659 * @return number of clients this message was sent to
1662 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1663 const struct GNUNET_MessageHeader *payload)
1665 struct GNUNET_PeerIdentity *oid;
1666 struct MeshClient *c;
1667 struct MeshTunnel *t;
1668 MESH_TunnelNumber *tid;
1671 char cbuf[htons (msg->size)];
1673 type = ntohs (payload->type);
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1677 memcpy (cbuf, msg, sizeof (cbuf));
1678 switch (htons (msg->type))
1680 struct GNUNET_MESH_Unicast *uc;
1681 struct GNUNET_MESH_Multicast *mc;
1682 struct GNUNET_MESH_ToOrigin *to;
1684 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1685 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1689 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1690 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1694 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1695 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1703 t = tunnel_get (oid, ntohl (*tid));
1710 for (count = 0, c = clients; c != NULL; c = c->next)
1712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1713 if (client_is_subscribed (type, c))
1715 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1719 *tid = htonl (t->local_tid);
1723 if (GNUNET_NO == client_knows_tunnel (c, t))
1725 /* This client doesn't know the tunnel */
1726 struct GNUNET_MESH_TunnelNotification tmsg;
1727 struct GNUNET_HashCode hash;
1729 tmsg.header.size = htons (sizeof (tmsg));
1730 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1731 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1732 tmsg.tunnel_id = htonl (t->local_tid_dest);
1733 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1734 &tmsg.header, GNUNET_NO);
1735 GNUNET_array_append (t->clients, t->nclients, c);
1736 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1738 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1739 c->incoming_tunnels, &hash, t,
1740 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1742 *tid = htonl (t->local_tid_dest);
1745 /* Check if the client wants to get traffic from the tunnel */
1746 if (GNUNET_NO == client_wants_tunnel(c, t))
1749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1750 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1751 (struct GNUNET_MessageHeader
1752 *) cbuf, GNUNET_YES);
1760 * Notify the client that owns the tunnel that a peer has connected to it
1761 * (the requested path to it has been confirmed).
1763 * @param t Tunnel whose owner to notify
1764 * @param id Short id of the peer that has connected
1767 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1769 struct GNUNET_MESH_PeerControl pc;
1771 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1772 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1773 pc.tunnel_id = htonl (t->local_tid);
1774 GNUNET_PEER_resolve (id, &pc.peer);
1775 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1781 * Notify all clients (not depending on registration status) that the incoming
1782 * tunnel is no longer valid.
1784 * @param t Tunnel that was destroyed.
1787 send_clients_tunnel_destroy (struct MeshTunnel *t)
1789 struct GNUNET_MESH_TunnelMessage msg;
1791 msg.header.size = htons (sizeof (msg));
1792 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1793 msg.tunnel_id = htonl (t->local_tid_dest);
1794 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1799 * Notify clients of tunnel disconnections, if needed.
1800 * In case the origin disconnects, the destination clients get a tunnel destroy
1801 * notification. If the last destination disconnects (only one remaining client
1802 * in tunnel), the origin gets a (local ID) peer disconnected.
1803 * Note that the function must be called BEFORE removing the client from
1806 * @param t Tunnel that was destroyed.
1807 * @param c Client that disconnected.
1810 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1816 struct GNUNET_MESH_TunnelMessage msg;
1818 msg.header.size = htons (sizeof (msg));
1819 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1820 msg.tunnel_id = htonl (t->local_tid_dest);
1821 for (i = 0; i < t->nclients; i++)
1822 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1823 &msg.header, GNUNET_NO);
1825 // FIXME when to disconnect an incoming tunnel?
1826 else if (1 == t->nclients && NULL != t->owner)
1828 struct GNUNET_MESH_PeerControl msg;
1830 msg.header.size = htons (sizeof (msg));
1831 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1832 msg.tunnel_id = htonl (t->local_tid);
1833 msg.peer = my_full_id;
1834 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1835 &msg.header, GNUNET_NO);
1841 * Decrements the reference counter and frees all resources if needed
1843 * @param mesh_data Data Descriptor used in a multicast message.
1844 * Freed no longer needed (last message).
1847 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1849 /* Make sure it's a multicast packet */
1850 GNUNET_assert (NULL != mesh_data->reference_counter);
1852 if (0 == --(*(mesh_data->reference_counter)))
1854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1855 if (NULL != mesh_data->task)
1857 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1859 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1861 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1863 GNUNET_free (mesh_data->task);
1865 GNUNET_free (mesh_data->reference_counter);
1866 GNUNET_free (mesh_data->data);
1867 GNUNET_free (mesh_data);
1873 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1874 * and insert it in the appropiate structures if the peer is not known yet.
1876 * @param peer Full identity of the peer.
1878 * @return Existing or newly created peer info.
1880 static struct MeshPeerInfo *
1881 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1883 struct MeshPeerInfo *peer_info;
1885 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1886 if (NULL == peer_info)
1889 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1890 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1891 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1892 peer_info->id = GNUNET_PEER_intern (peer);
1900 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1901 * and insert it in the appropiate structures if the peer is not known yet.
1903 * @param peer Short identity of the peer.
1905 * @return Existing or newly created peer info.
1907 static struct MeshPeerInfo *
1908 peer_info_get_short (const GNUNET_PEER_Id peer)
1910 struct GNUNET_PeerIdentity id;
1912 GNUNET_PEER_resolve (peer, &id);
1913 return peer_info_get (&id);
1918 * Iterator to remove the tunnel from the list of tunnels a peer participates
1921 * @param cls Closure (tunnel info)
1922 * @param key GNUNET_PeerIdentity of the peer (unused)
1923 * @param value PeerInfo of the peer
1925 * @return always GNUNET_YES, to keep iterating
1928 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1930 struct MeshTunnel *t = cls;
1931 struct MeshPeerInfo *peer = value;
1934 for (i = 0; i < peer->ntunnels; i++)
1937 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1940 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1941 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1950 * Core callback to write a pre-constructed data packet to core buffer
1952 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1953 * @param size Number of bytes available in buf.
1954 * @param buf Where the to write the message.
1956 * @return number of bytes written to buf
1959 send_core_data_raw (void *cls, size_t size, void *buf)
1961 struct MeshTransmissionDescriptor *info = cls;
1962 struct GNUNET_MessageHeader *msg;
1965 GNUNET_assert (NULL != info);
1966 GNUNET_assert (NULL != info->mesh_data);
1967 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1968 total_size = ntohs (msg->size);
1970 if (total_size > size)
1975 memcpy (buf, msg, total_size);
1976 data_descriptor_decrement_rc (info->mesh_data);
1983 * Sends an already built non-multicast message to a peer,
1984 * properly registrating all used resources.
1986 * @param message Message to send. Function makes a copy of it.
1987 * @param peer Short ID of the neighbor whom to send the message.
1988 * @param t Tunnel on which this message is transmitted.
1991 send_message (const struct GNUNET_MessageHeader *message,
1992 const struct GNUNET_PeerIdentity *peer,
1993 struct MeshTunnel *t)
1995 struct MeshTransmissionDescriptor *info;
1996 struct MeshPeerInfo *neighbor;
1997 struct MeshPeerPath *p;
2000 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2002 size = ntohs (message->size);
2003 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2004 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2005 info->mesh_data->data = GNUNET_malloc (size);
2006 memcpy (info->mesh_data->data, message, size);
2007 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
2009 struct GNUNET_MESH_Unicast *m;
2011 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2012 m->ttl = htonl (ntohl (m->ttl) - 1);
2014 info->mesh_data->data_len = size;
2015 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2016 *info->mesh_data->reference_counter = 1;
2017 neighbor = peer_info_get (peer);
2018 for (p = neighbor->path_head; NULL != p; p = p->next)
2027 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2028 GNUNET_free (info->mesh_data->data);
2029 GNUNET_free (info->mesh_data);
2033 info->peer = neighbor;
2043 * Sends a CREATE PATH message for a path to a peer, properly registrating
2044 * all used resources.
2046 * @param peer PeerInfo of the final peer for whom this path is being created.
2047 * @param p Path itself.
2048 * @param t Tunnel for which the path is created.
2051 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2052 struct MeshTunnel *t)
2054 struct GNUNET_PeerIdentity id;
2055 struct MeshPathInfo *path_info;
2056 struct MeshPeerInfo *neighbor;
2062 p = tree_get_path_to_peer (t->tree, peer->id);
2069 for (i = 0; i < p->length; i++)
2071 if (p->peers[i] == myid)
2074 if (i >= p->length - 1)
2080 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2082 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2083 path_info->path = p;
2085 neighbor = peer_info_get (&id);
2086 path_info->peer = neighbor;
2087 queue_add (path_info,
2088 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2089 sizeof (struct GNUNET_MESH_ManipulatePath) +
2090 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2097 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2099 * @param t Tunnel whose path to destroy.
2100 * @param destination Short ID of the peer to whom the path to destroy.
2103 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2105 struct MeshPeerPath *p;
2108 p = tree_get_path_to_peer (t->tree, destination);
2114 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2115 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2117 struct GNUNET_MESH_ManipulatePath *msg;
2118 struct GNUNET_PeerIdentity *pi;
2122 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2123 msg->header.size = htons (size);
2124 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2125 msg->tid = htonl (t->id.tid);
2126 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2127 for (i = 0; i < p->length; i++)
2129 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2131 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2138 * Try to establish a new connection to this peer.
2139 * Use the best path for the given tunnel.
2140 * If the peer doesn't have any path to it yet, try to get one.
2141 * If the peer already has some path, send a CREATE PATH towards it.
2143 * @param peer PeerInfo of the peer.
2144 * @param t Tunnel for which to create the path, if possible.
2147 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2149 struct MeshPeerPath *p;
2150 struct MeshPathInfo *path_info;
2152 if (NULL != peer->path_head)
2154 p = tree_get_path_to_peer (t->tree, peer->id);
2161 // FIXME always send create path to self
2164 send_create_path (peer, p, t);
2168 struct GNUNET_HashCode hash;
2171 send_client_peer_connected (t, myid);
2172 t->local_tid_dest = next_local_tid++;
2173 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2176 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2177 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2184 else if (NULL == peer->dhtget)
2186 struct GNUNET_PeerIdentity id;
2188 GNUNET_PEER_resolve (peer->id, &id);
2189 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2190 path_info->peer = peer;
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2194 peer->dhtgetcls = path_info;
2195 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2196 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2197 &id.hashPubKey, /* key to search */
2198 dht_replication_level, /* replication level */
2199 GNUNET_DHT_RO_RECORD_ROUTE |
2200 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2201 NULL, /* xquery */ // FIXME BLOOMFILTER
2202 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2203 &dht_get_id_handler, path_info);
2205 /* Otherwise, there is no path but the DHT get is already started. */
2210 * Task to delay the connection of a peer
2212 * @param cls Closure (path info with tunnel and peer to connect).
2213 * Will be free'd on exection.
2214 * @param tc TaskContext
2217 peer_info_connect_task (void *cls,
2218 const struct GNUNET_SCHEDULER_TaskContext *tc)
2220 struct MeshPathInfo *path_info = cls;
2222 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2227 peer_info_connect (path_info->peer, path_info->t);
2233 * Destroy the peer_info and free any allocated resources linked to it
2235 * @param pi The peer_info to destroy.
2237 * @return GNUNET_OK on success
2240 peer_info_destroy (struct MeshPeerInfo *pi)
2242 struct GNUNET_PeerIdentity id;
2243 struct MeshPeerPath *p;
2244 struct MeshPeerPath *nextp;
2246 GNUNET_PEER_resolve (pi->id, &id);
2247 GNUNET_PEER_change_rc (pi->id, -1);
2250 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2254 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2256 if (NULL != pi->dhtget)
2258 GNUNET_DHT_get_stop (pi->dhtget);
2259 GNUNET_free (pi->dhtgetcls);
2265 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2275 * Remove all paths that rely on a direct connection between p1 and p2
2276 * from the peer itself and notify all tunnels about it.
2278 * @param peer PeerInfo of affected peer.
2279 * @param p1 GNUNET_PEER_Id of one peer.
2280 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2283 * TODO: optimize (see below)
2286 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2289 struct MeshPeerPath *p;
2290 struct MeshPeerPath *aux;
2291 struct MeshPeerInfo *peer_d;
2293 unsigned int destroyed;
2298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2300 p = peer->path_head;
2304 for (i = 0; i < (p->length - 1); i++)
2306 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2307 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2309 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2320 for (i = 0; i < peer->ntunnels; i++)
2322 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2326 * Problem: one or more peers have been deleted from the tunnel tree.
2327 * We don't know who they are to try to add them again.
2328 * We need to try to find a new path for each of the disconnected peers.
2329 * Some of them might already have a path to reach them that does not
2330 * involve p1 and p2. Adding all anew might render in a better tree than
2331 * the trivial immediate fix.
2333 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2334 * its children will be reachable trough him.
2336 peer_d = peer_info_get_short (d);
2339 for (p = peer_d->path_head; NULL != p; p = p->next)
2341 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2349 /* No callback, as peer will be already disconnected and a connection
2350 * scheduled by tunnel_notify_connection_broken.
2352 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2356 peer_info_connect (peer_d, peer->tunnels[i]);
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2364 * Add the path to the peer and update the path used to reach it in case this
2367 * @param peer_info Destination peer to add the path to.
2368 * @param path New path to add. Last peer must be the peer in arg 1.
2369 * Path will be either used of freed if already known.
2370 * @param trusted Do we trust that this path is real?
2373 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2376 struct MeshPeerPath *aux;
2380 if ((NULL == peer_info) || (NULL == path))
2383 path_destroy (path);
2386 if (path->peers[path->length - 1] != peer_info->id)
2389 path_destroy (path);
2392 if (path->length <= 2 && GNUNET_NO == trusted)
2394 /* Only allow CORE to tell us about direct paths */
2395 path_destroy (path);
2398 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2399 for (l = 1; l < path->length; l++)
2401 if (path->peers[l] == myid)
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2404 for (l2 = 0; l2 < path->length - l; l2++)
2406 path->peers[l2] = path->peers[l + l2];
2411 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2416 struct GNUNET_PeerIdentity id;
2418 GNUNET_PEER_resolve (peer_info->id, &id);
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2420 path->length, GNUNET_i2s (&id));
2423 l = path_get_length (path);
2430 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2431 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2433 l2 = path_get_length (aux);
2436 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2437 peer_info->path_tail, aux, path);
2442 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2444 path_destroy (path);
2449 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2456 * Add the path to the origin peer and update the path used to reach it in case
2457 * this is the shortest.
2458 * The path is given in peer_info -> destination, therefore we turn the path
2459 * upside down first.
2461 * @param peer_info Peer to add the path to, being the origin of the path.
2462 * @param path New path to add after being inversed.
2463 * @param trusted Do we trust that this path is real?
2466 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2467 struct MeshPeerPath *path, int trusted)
2470 peer_info_add_path (peer_info, path, trusted);
2475 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2476 * to obtain a local peer -> destination path and interning the peer ids.
2478 * @return Newly allocated and created path
2480 static struct MeshPeerPath *
2481 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2482 unsigned int get_path_length,
2483 const struct GNUNET_PeerIdentity *put_path,
2484 unsigned int put_path_length)
2486 struct MeshPeerPath *p;
2492 GNUNET_PEER_change_rc (myid, 1);
2493 i = get_path_length;
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2495 for (i--; i >= 0; i--)
2497 id = GNUNET_PEER_intern (&get_path[i]);
2498 if (p->length > 0 && id == p->peers[p->length - 1])
2500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2501 GNUNET_PEER_change_rc (id, -1);
2505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2506 GNUNET_i2s (&get_path[i]));
2508 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2509 p->peers[p->length - 1] = id;
2512 i = put_path_length;
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2514 for (i--; i >= 0; i--)
2516 id = GNUNET_PEER_intern (&put_path[i]);
2519 /* PUT path went through us, so discard the path up until now and start
2520 * from here to get a much shorter (and loop-free) path.
2525 if (p->length > 0 && id == p->peers[p->length - 1])
2527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2528 GNUNET_PEER_change_rc (id, -1);
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2533 GNUNET_i2s (&put_path[i]));
2535 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2536 p->peers[p->length - 1] = id;
2540 if (get_path_length > 0)
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2542 GNUNET_i2s (&get_path[0]));
2543 if (put_path_length > 0)
2544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2545 GNUNET_i2s (&put_path[0]));
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2548 for (i = 0; i < p->length; i++)
2550 struct GNUNET_PeerIdentity peer_id;
2552 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2554 GNUNET_i2s (&peer_id));
2562 * Adds a path to the peer_infos of all the peers in the path
2564 * @param p Path to process.
2565 * @param confirmed Whether we know if the path works or not. FIXME use
2568 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2572 /* TODO: invert and add */
2573 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2574 for (i++; i < p->length; i++)
2576 struct MeshPeerInfo *aux;
2577 struct MeshPeerPath *copy;
2579 aux = peer_info_get_short (p->peers[i]);
2580 copy = path_duplicate (p);
2581 copy->length = i + 1;
2582 peer_info_add_path (aux, copy, GNUNET_NO);
2588 * Send keepalive packets for a peer
2590 * @param cls Closure (tunnel for which to send the keepalive).
2591 * @param tc Notification context.
2593 * TODO: implement explicit multicast keepalive?
2596 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2600 * Search for a tunnel among the incoming tunnels
2602 * @param tid the local id of the tunnel
2604 * @return tunnel handler, NULL if doesn't exist
2606 static struct MeshTunnel *
2607 tunnel_get_incoming (MESH_TunnelNumber tid)
2609 struct GNUNET_HashCode hash;
2611 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2612 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2613 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2618 * Search for a tunnel among the tunnels for a client
2620 * @param c the client whose tunnels to search in
2621 * @param tid the local id of the tunnel
2623 * @return tunnel handler, NULL if doesn't exist
2625 static struct MeshTunnel *
2626 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2628 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2630 return tunnel_get_incoming (tid);
2634 struct GNUNET_HashCode hash;
2636 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2637 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2643 * Search for a tunnel by global ID using PEER_ID
2645 * @param pi owner of the tunnel
2646 * @param tid global tunnel number
2648 * @return tunnel handler, NULL if doesn't exist
2650 static struct MeshTunnel *
2651 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2653 struct MESH_TunnelID id;
2654 struct GNUNET_HashCode hash;
2659 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2660 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2665 * Search for a tunnel by global ID using full PeerIdentities
2667 * @param oid owner of the tunnel
2668 * @param tid global tunnel number
2670 * @return tunnel handler, NULL if doesn't exist
2672 static struct MeshTunnel *
2673 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2675 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2680 * Delete an active client from the tunnel.
2686 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2690 for (i = 0; i < t->nclients; i++)
2692 if (t->clients[i] == c)
2694 t->clients[i] = t->clients[t->nclients - 1];
2695 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2703 * Delete an ignored client from the tunnel.
2709 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2713 for (i = 0; i < t->nignore; i++)
2715 if (t->ignore[i] == c)
2717 t->ignore[i] = t->ignore[t->nignore - 1];
2718 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2726 * Delete a client from the tunnel. It should be only done on
2727 * client disconnection, otherwise use client_ignore_tunnel.
2733 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2735 tunnel_delete_ignored_client (t, c);
2736 tunnel_delete_active_client (t, c);
2741 * Iterator to free MeshTunnelChildInfo of tunnel children.
2743 * @param cls Closure (tunnel info).
2744 * @param key Hash of GNUNET_PEER_Id (unused).
2745 * @param value MeshTunnelChildInfo of the child.
2747 * @return always GNUNET_YES, to keep iterating
2750 tunnel_destroy_child (void *cls,
2751 const struct GNUNET_HashCode * key,
2754 GNUNET_free (value);
2760 * Callback used to notify a client owner of a tunnel that a peer has
2761 * disconnected, most likely because of a path change.
2763 * @param cls Closure (tunnel this notification is about).
2764 * @param peer_id Short ID of disconnected peer.
2767 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2769 struct MeshTunnel *t = cls;
2770 struct MeshPeerInfo *peer;
2771 struct MeshPathInfo *path_info;
2773 if (NULL != t->owner && NULL != nc)
2775 struct GNUNET_MESH_PeerControl msg;
2777 msg.header.size = htons (sizeof (msg));
2778 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2779 msg.tunnel_id = htonl (t->local_tid);
2780 GNUNET_PEER_resolve (peer_id, &msg.peer);
2781 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2782 &msg.header, GNUNET_NO);
2784 peer = peer_info_get_short (peer_id);
2785 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2786 path_info->peer = peer;
2788 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2793 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2794 * needed rescources.
2795 * If peer already exists, reevaluate shortest path and change if different.
2797 * @param t Tunnel we want to add a new peer to
2798 * @param peer PeerInfo of the peer being added
2802 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2804 struct GNUNET_PeerIdentity id;
2805 struct MeshPeerPath *best_p;
2806 struct MeshPeerPath *p;
2807 unsigned int best_cost;
2810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2811 GNUNET_PEER_resolve (peer->id, &id);
2813 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2816 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2817 GNUNET_assert (GNUNET_OK ==
2818 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2820 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2823 if (NULL != (p = peer->path_head))
2826 best_cost = tree_get_path_cost (t->tree, p);
2829 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2836 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2837 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2838 t->path_refresh_task =
2839 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2843 /* Start a DHT get */
2844 peer_info_connect (peer, t);
2846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2850 * Add a path to a tunnel which we don't own, just to remember the next hop.
2851 * If destination node was already in the tunnel, the first hop information
2852 * will be replaced with the new path.
2854 * @param t Tunnel we want to add a new peer to
2855 * @param p Path to add
2856 * @param own_pos Position of local node in path.
2860 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2861 unsigned int own_pos)
2863 struct GNUNET_PeerIdentity id;
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2866 GNUNET_assert (0 != own_pos);
2867 tree_add_path (t->tree, p, NULL, NULL);
2868 if (own_pos < p->length - 1)
2870 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2871 tree_update_first_hops (t->tree, myid, &id);
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2878 * Notifies a tunnel that a connection has broken that affects at least
2879 * some of its peers. Sends a notification towards the root of the tree.
2880 * In case the peer is the owner of the tree, notifies the client that owns
2881 * the tunnel and tries to reconnect.
2883 * @param t Tunnel affected.
2884 * @param p1 Peer that got disconnected from p2.
2885 * @param p2 Peer that got disconnected from p1.
2887 * @return Short ID of the peer disconnected (either p1 or p2).
2888 * 0 if the tunnel remained unaffected.
2890 static GNUNET_PEER_Id
2891 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2897 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2899 if (myid != p1 && myid != p2)
2905 if (tree_get_predecessor (t->tree) != 0)
2907 /* We are the peer still connected, notify owner of the disconnection. */
2908 struct GNUNET_MESH_PathBroken msg;
2909 struct GNUNET_PeerIdentity neighbor;
2911 msg.header.size = htons (sizeof (msg));
2912 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2913 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2914 msg.tid = htonl (t->id.tid);
2915 msg.peer1 = my_full_id;
2916 GNUNET_PEER_resolve (pid, &msg.peer2);
2917 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2918 send_message (&msg.header, &neighbor, t);
2926 * Send a multicast packet to a neighbor.
2928 * @param cls Closure (Info about the multicast packet)
2929 * @param neighbor_id Short ID of the neighbor to send the packet to.
2932 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2934 struct MeshData *mdata = cls;
2935 struct MeshTransmissionDescriptor *info;
2936 struct GNUNET_PeerIdentity neighbor;
2938 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2940 info->mesh_data = mdata;
2941 (*(mdata->reference_counter)) ++;
2942 info->destination = neighbor_id;
2943 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2945 GNUNET_i2s (&neighbor));
2946 info->peer = peer_info_get (&neighbor);
2947 GNUNET_assert (NULL != info->peer);
2949 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2950 info->mesh_data->data_len,
2957 * Send a message in a tunnel in multicast, sending a copy to each child node
2958 * down the local one in the tunnel tree.
2960 * @param t Tunnel in which to send the data.
2961 * @param msg Message to be sent.
2962 * @param internal Has the service generated this message?
2965 tunnel_send_multicast (struct MeshTunnel *t,
2966 const struct GNUNET_MessageHeader *msg,
2969 struct MeshData *mdata;
2971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2972 " sending a multicast packet...\n");
2973 mdata = GNUNET_malloc (sizeof (struct MeshData));
2974 mdata->data_len = ntohs (msg->size);
2975 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2977 mdata->data = GNUNET_malloc (mdata->data_len);
2978 memcpy (mdata->data, msg, mdata->data_len);
2979 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2981 struct GNUNET_MESH_Multicast *mcast;
2983 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2984 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2986 ntohl (mcast->ttl));
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2992 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2993 && GNUNET_NO == internal)
2995 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2997 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
2999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3003 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3004 if (*(mdata->reference_counter) == 0)
3006 GNUNET_free (mdata->data);
3007 GNUNET_free (mdata->reference_counter);
3008 if (NULL != mdata->task)
3010 GNUNET_SCHEDULER_cancel(*(mdata->task));
3011 GNUNET_free (mdata->task);
3012 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
3014 // FIXME change order?
3015 GNUNET_free (mdata);
3017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3018 " sending a multicast packet done\n");
3024 * Increase the SKIP value of all peers that
3025 * have not received a unicast message.
3027 * @param cls Closure (Short id of the peer that HAS received the message).
3028 * @param id Short ID of the neighbor.
3031 tunnel_add_skip (void *cls,
3032 const struct GNUNET_HashCode * key,
3035 struct GNUNET_PeerIdentity *neighbor = cls;
3036 struct MeshTunnelChildInfo *cinfo = value;
3038 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3049 * Iterator to get the appropiate ACK value from all children nodes.
3051 * @param cls Closue (tunnel).
3052 * @param id Id of the child node.
3055 tunnel_get_child_ack (void *cls,
3058 struct GNUNET_PeerIdentity peer_id;
3059 struct MeshTunnelChildInfo *cinfo;
3060 struct MeshTunnel *t = cls;
3063 GNUNET_PEER_resolve (id, &peer_id);
3064 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3065 &peer_id.hashPubKey);
3068 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3070 cinfo->pid = t->pid;
3071 cinfo->skip = t->pid;
3072 cinfo->max_pid = ack = t->pid + 1;
3073 GNUNET_assert (GNUNET_OK ==
3074 GNUNET_CONTAINER_multihashmap_put(t->children_fc,
3075 &peer_id.hashPubKey,
3077 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3081 ack = cinfo->max_pid;
3084 if (0 == t->max_child_ack)
3085 t->max_child_ack = ack;
3087 if (GNUNET_YES == t->speed_min)
3089 t->max_child_ack = t->max_child_ack > ack ? ack : t->max_child_ack;
3093 t->max_child_ack = t->max_child_ack > ack ? t->max_child_ack : ack;
3100 * Get the maximum PID allowed to transmit to any
3101 * tunnel child of the local peer.
3105 * @return Maximum PID allowed.
3108 tunnel_get_children_ack (struct MeshTunnel *t)
3110 t->max_child_ack = 0;
3111 tree_iterate_children (t->tree, tunnel_get_child_ack, t);
3112 return t->max_child_ack;
3117 * Send an ACK informing the predecessor about the available buffer space.
3118 * If buffering is off, send only on behalf of children or self if endpoint.
3119 * If buffering is on, send when sent to children and buffer space is free.
3121 * @param t Tunnel on which to send the ACK.
3124 tunnel_send_ack (struct MeshTunnel *t, uint16_t type)
3126 struct GNUNET_MESH_ACK msg;
3127 struct GNUNET_PeerIdentity id;
3129 uint32_t buffer_free;
3133 /* Is it after unicast / multicast retransmission? */
3134 if (GNUNET_MESSAGE_TYPE_MESH_ACK != type)
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ACK via DATA retransmission\n");
3137 if (GNUNET_YES == t->nobuffer)
3139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3142 if (t->queue_max > t->queue_n * 2)
3144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3149 /* Ok, ACK might be necessary, what PID to ACK? */
3150 count = t->pid - t->skip;
3151 buffer_free = t->queue_max - t->queue_n;
3152 ack = count + buffer_free;
3153 child_ack = tunnel_get_children_ack (t);
3155 if (GNUNET_YES == t->speed_min)
3157 ack = child_ack > ack ? ack : child_ack;
3161 ack = child_ack > ack ? child_ack : ack;
3164 /* If speed_min and not all children have ack'd, dont send yet */
3165 if (ack == t->last_ack)
3167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, not ready\n");
3172 msg.pid = htonl (ack);
3174 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3176 msg.header.size = htons (sizeof (msg));
3177 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3178 msg.tid = htonl (t->id.tid);
3179 GNUNET_PEER_resolve(t->id.oid, &msg.oid);
3180 send_message (&msg.header, &id, t);
3185 * Send a message to all peers in this tunnel that the tunnel is no longer
3188 * @param t The tunnel whose peers to notify.
3191 tunnel_send_destroy (struct MeshTunnel *t)
3193 struct GNUNET_MESH_TunnelDestroy msg;
3195 msg.header.size = htons (sizeof (msg));
3196 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3197 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3198 msg.tid = htonl (t->id.tid);
3199 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3204 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3206 * @param cls Closure (Tunnel which to cancel).
3207 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3210 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3212 struct MeshTunnel *t = cls;
3213 struct MeshPeerInfo *peer_info;
3214 struct MeshPeerQueue *pq;
3215 struct MeshPeerQueue *next;
3217 peer_info = peer_info_get_short (neighbor_id);
3218 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3221 if (pq->tunnel == t)
3223 queue_destroy (pq, GNUNET_YES);
3226 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3228 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3229 peer_info->core_transmit = NULL;
3234 * Destroy the tunnel and free any allocated resources linked to it.
3236 * @param t the tunnel to destroy
3238 * @return GNUNET_OK on success
3241 tunnel_destroy (struct MeshTunnel *t)
3243 struct MeshClient *c;
3244 struct GNUNET_HashCode hash;
3251 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3257 struct GNUNET_PeerIdentity id;
3259 GNUNET_PEER_resolve (t->id.oid, &id);
3260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3261 GNUNET_i2s (&id), t->id.tid);
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3267 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3268 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3273 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3276 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3280 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3281 for (i = 0; i < t->nclients; i++)
3285 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3290 for (i = 0; i < t->nignore; i++)
3294 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3299 if (t->nclients > 0)
3302 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3306 GNUNET_free (t->clients);
3308 if (NULL != t->peers)
3310 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3312 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3315 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3316 &tunnel_destroy_child,
3318 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3320 tree_destroy (t->tree);
3322 if (NULL != t->regex_ctx)
3323 regex_cancel_search (t->regex_ctx);
3324 if (NULL != t->dht_get_type)
3325 GNUNET_DHT_get_stop (t->dht_get_type);
3326 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3327 GNUNET_SCHEDULER_cancel (t->timeout_task);
3328 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3329 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3337 * Create a new tunnel
3339 * @param owner Who is the owner of the tunnel (short ID).
3340 * @param tid Tunnel Number of the tunnel.
3341 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3342 * @param local Tunnel Number for the tunnel, for the client point of view.
3345 static struct MeshTunnel *
3346 tunnel_new (GNUNET_PEER_Id owner,
3347 MESH_TunnelNumber tid,
3348 struct MeshClient *client,
3349 MESH_TunnelNumber local)
3351 struct MeshTunnel *t;
3352 struct GNUNET_HashCode hash;
3354 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3357 t->queue_max = 1000; // FIXME API parameter
3358 t->tree = tree_new (owner);
3360 t->local_tid = local;
3361 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
3363 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3365 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3366 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3371 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3377 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3379 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3380 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3384 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3394 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3395 * that are no longer needed, as well as nodes of no longer reachable peers.
3396 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3398 * @param t Tunnel from which to remove the path.
3399 * @param peer Short id of the peer which should be removed.
3402 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3404 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
3410 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3411 * client when the client disconnects. If the client is not the owner, the
3412 * owner will get notified if no more clients are in the tunnel and the client
3413 * get removed from the tunnel's list.
3415 * @param cls closure (client that is disconnecting)
3416 * @param key the hash of the local tunnel id (used to access the hashmap)
3417 * @param value the value stored at the key (tunnel to destroy)
3419 * @return GNUNET_OK on success
3422 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
3424 struct MeshTunnel *t = value;
3425 struct MeshClient *c = cls;
3428 send_client_tunnel_disconnect(t, c);
3431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3432 "Client %u is destination, keeping the tunnel alive.\n", c->id);
3433 tunnel_delete_client(t, c);
3434 client_delete_tunnel(c, t);
3437 tunnel_send_destroy(t);
3438 r = tunnel_destroy (t);
3444 * Timeout function, destroys tunnel if called
3446 * @param cls Closure (tunnel to destroy).
3447 * @param tc TaskContext
3450 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3452 struct MeshTunnel *t = cls;
3454 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3456 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3461 * Resets the tunnel timeout. Starts it if no timeout was running.
3463 * @param t Tunnel whose timeout to reset.
3466 tunnel_reset_timeout (struct MeshTunnel *t)
3468 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3469 GNUNET_SCHEDULER_cancel (t->timeout_task);
3471 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3472 (refresh_path_time, 4), &tunnel_timeout, t);
3476 /******************************************************************************/
3477 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3478 /******************************************************************************/
3481 * Function to send a create path packet to a peer.
3483 * @param cls closure
3484 * @param size number of bytes available in buf
3485 * @param buf where the callee should write the message
3486 * @return number of bytes written to buf
3489 send_core_path_create (void *cls, size_t size, void *buf)
3491 struct MeshPathInfo *info = cls;
3492 struct GNUNET_MESH_ManipulatePath *msg;
3493 struct GNUNET_PeerIdentity *peer_ptr;
3494 struct MeshTunnel *t = info->t;
3495 struct MeshPeerPath *p = info->path;
3500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3502 sizeof (struct GNUNET_MESH_ManipulatePath) +
3503 p->length * sizeof (struct GNUNET_PeerIdentity);
3505 if (size < size_needed || NULL == buf)
3510 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3511 msg->header.size = htons (size_needed);
3512 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3513 msg->tid = ntohl (t->id.tid);
3515 if (GNUNET_YES == t->speed_min)
3516 opt = MESH_TUNNEL_OPT_SPEED_MIN;
3517 if (GNUNET_YES == t->nobuffer)
3518 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3519 msg->opt = htonl(opt);
3521 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3522 for (i = 0; i < p->length; i++)
3524 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3531 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3537 * Fill the core buffer
3539 * @param cls closure (data itself)
3540 * @param size number of bytes available in buf
3541 * @param buf where the callee should write the message
3543 * @return number of bytes written to buf
3546 send_core_data_multicast (void *cls, size_t size, void *buf)
3548 struct MeshTransmissionDescriptor *info = cls;
3551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3552 GNUNET_assert (NULL != info);
3553 GNUNET_assert (NULL != info->peer);
3554 total_size = info->mesh_data->data_len;
3555 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3557 if (total_size > size)
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3563 memcpy (buf, info->mesh_data->data, total_size);
3566 struct GNUNET_MESH_Multicast *mc;
3567 struct GNUNET_MessageHeader *mh;
3570 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3572 mc = (struct GNUNET_MESH_Multicast *) mh;
3573 mh = (struct GNUNET_MessageHeader *) &mc[1];
3574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3575 " multicast, payload type %u\n", ntohs (mh->type));
3576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3577 " multicast, payload size %u\n", ntohs (mh->size));
3581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
3586 data_descriptor_decrement_rc (info->mesh_data);
3587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3595 * Creates a path ack message in buf and frees all unused resources.
3597 * @param cls closure (MeshTransmissionDescriptor)
3598 * @param size number of bytes available in buf
3599 * @param buf where the callee should write the message
3600 * @return number of bytes written to buf
3603 send_core_path_ack (void *cls, size_t size, void *buf)
3605 struct MeshTransmissionDescriptor *info = cls;
3606 struct GNUNET_MESH_PathACK *msg = buf;
3608 GNUNET_assert (NULL != info);
3609 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3614 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3615 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3616 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3617 msg->tid = htonl (info->origin->tid);
3618 msg->peer_id = my_full_id;
3621 /* TODO add signature */
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3624 return sizeof (struct GNUNET_MESH_PathACK);
3629 * Free a transmission that was already queued with all resources
3630 * associated to the request.
3632 * @param queue Queue handler to cancel.
3633 * @param clear_cls Is it necessary to free associated cls?
3636 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3638 struct MeshTransmissionDescriptor *dd;
3639 struct MeshPathInfo *path_info;
3641 if (GNUNET_YES == clear_cls)
3643 switch (queue->type)
3645 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3646 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3647 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
3650 data_descriptor_decrement_rc (dd->mesh_data);
3652 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3654 path_info = queue->cls;
3655 path_destroy (path_info->path);
3659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
3661 GNUNET_free_non_null (queue->cls);
3663 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3664 queue->peer->queue_tail,
3666 GNUNET_free (queue);
3671 * Core callback to write a queued packet to core buffer
3673 * @param cls Closure (peer info).
3674 * @param size Number of bytes available in buf.
3675 * @param buf Where the to write the message.
3677 * @return number of bytes written to buf
3680 queue_send (void *cls, size_t size, void *buf)
3682 struct MeshPeerInfo *peer = cls;
3683 struct GNUNET_MessageHeader *msg;
3684 struct MeshPeerQueue *queue;
3685 struct MeshTunnel *t;
3688 peer->core_transmit = NULL;
3689 queue = peer->queue_head;
3691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3693 /* If queue is empty, send should have been cancelled */
3699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3701 /* Check if buffer size is enough for the message */
3702 if (queue->size > size)
3704 struct GNUNET_PeerIdentity id;
3706 GNUNET_PEER_resolve (peer->id, &id);
3707 peer->core_transmit =
3708 GNUNET_CORE_notify_transmit_ready(core_handle,
3711 GNUNET_TIME_UNIT_FOREVER_REL,
3718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3724 switch (queue->type)
3727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
3728 data_size = send_core_data_raw (queue->cls, size, buf);
3729 msg = (struct GNUNET_MessageHeader *) buf;
3730 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
3731 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3733 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
3735 data_size = send_core_data_multicast(queue->cls, size, buf);
3736 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3738 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3740 data_size = send_core_path_create(queue->cls, size, buf);
3742 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3744 data_size = send_core_path_ack(queue->cls, size, buf);
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
3752 /* Free queue, but cls was freed by send_core_* */
3753 queue_destroy (queue, GNUNET_NO);
3755 if (GNUNET_YES == t->destroy && 0 == t->queue_n)
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3761 /* If more data in queue, send next */
3762 if (NULL != peer->queue_head)
3764 struct GNUNET_PeerIdentity id;
3766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3767 GNUNET_PEER_resolve (peer->id, &id);
3768 peer->core_transmit =
3769 GNUNET_CORE_notify_transmit_ready(core_handle,
3772 GNUNET_TIME_UNIT_FOREVER_REL,
3774 peer->queue_head->size,
3778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3784 * Queue and pass message to core when possible.
3786 * @param cls Closure (type dependant).
3787 * @param type Type of the message, 0 for a raw message.
3788 * @param size Size of the message.
3789 * @param dst Neighbor to send message to.
3790 * @param t Tunnel this message belongs to.
3793 queue_add (void *cls, uint16_t type, size_t size,
3794 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3796 struct MeshPeerQueue *queue;
3798 if (t->queue_n >= t->queue_max)
3800 if (NULL == t->owner)
3801 GNUNET_break_op(0); // TODO: kill connection?
3804 return; // Drop message
3807 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3813 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3814 if (NULL == dst->core_transmit)
3816 struct GNUNET_PeerIdentity id;
3818 GNUNET_PEER_resolve (dst->id, &id);
3819 dst->core_transmit =
3820 GNUNET_CORE_notify_transmit_ready(core_handle,
3823 GNUNET_TIME_UNIT_FOREVER_REL,
3832 /******************************************************************************/
3833 /******************** MESH NETWORK HANDLERS **************************/
3834 /******************************************************************************/
3838 * Core handler for path creation
3840 * @param cls closure
3841 * @param message message
3842 * @param peer peer identity this notification is about
3843 * @param atsi performance data
3844 * @param atsi_count number of records in 'atsi'
3846 * @return GNUNET_OK to keep the connection open,
3847 * GNUNET_SYSERR to close it (signal serious error)
3850 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3851 const struct GNUNET_MessageHeader *message,
3852 const struct GNUNET_ATS_Information *atsi,
3853 unsigned int atsi_count)
3855 unsigned int own_pos;
3858 MESH_TunnelNumber tid;
3859 struct GNUNET_MESH_ManipulatePath *msg;
3860 struct GNUNET_PeerIdentity *pi;
3861 struct GNUNET_HashCode hash;
3862 struct MeshPeerPath *path;
3863 struct MeshPeerInfo *dest_peer_info;
3864 struct MeshPeerInfo *orig_peer_info;
3865 struct MeshTunnel *t;
3867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3868 "Received a path create msg [%s]\n",
3869 GNUNET_i2s (&my_full_id));
3870 size = ntohs (message->size);
3871 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3873 GNUNET_break_op (0);
3877 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3878 if (size % sizeof (struct GNUNET_PeerIdentity))
3880 GNUNET_break_op (0);
3883 size /= sizeof (struct GNUNET_PeerIdentity);
3886 GNUNET_break_op (0);
3889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3890 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3892 tid = ntohl (msg->tid);
3893 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3895 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3896 t = tunnel_get (pi, tid);
3897 if (NULL == t) // FIXME only for INCOMING tunnels?
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3902 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3903 opt = ntohl (msg->opt);
3904 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
3905 GNUNET_YES : GNUNET_NO;
3906 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
3907 GNUNET_YES : GNUNET_NO;
3909 if (GNUNET_YES == t->nobuffer)
3912 while (NULL != tunnel_get_incoming (next_local_tid))
3913 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3914 t->local_tid_dest = next_local_tid++;
3915 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3917 tunnel_reset_timeout (t);
3918 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3920 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3921 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3929 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3930 if (NULL == dest_peer_info)
3932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3933 " Creating PeerInfo for destination.\n");
3934 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3935 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3936 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3938 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3940 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3941 if (NULL == orig_peer_info)
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3944 " Creating PeerInfo for origin.\n");
3945 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3946 orig_peer_info->id = GNUNET_PEER_intern (pi);
3947 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3948 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3951 path = path_new (size);
3953 for (i = 0; i < size; i++)
3955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3956 GNUNET_i2s (&pi[i]));
3957 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3958 if (path->peers[i] == myid)
3961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3964 /* cannot be self, must be 'not found' */
3965 /* create path: self not found in path through self */
3966 GNUNET_break_op (0);
3967 path_destroy (path);
3968 /* FIXME error. destroy tunnel? leave for timeout? */
3971 path_add_to_peers (path, GNUNET_NO);
3972 tunnel_add_path (t, path, own_pos);
3973 if (own_pos == size - 1)
3975 /* It is for us! Send ack. */
3976 struct MeshTransmissionDescriptor *info;
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3979 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3980 if (NULL == t->peers)
3982 /* New tunnel! Notify clients on data. */
3983 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
3985 GNUNET_break (GNUNET_OK ==
3986 GNUNET_CONTAINER_multihashmap_put (t->peers,
3987 &my_full_id.hashPubKey,
3990 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
3991 // FIXME URGENT (GNUNET_NO?)
3992 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3993 info->origin = &t->id;
3994 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3995 GNUNET_assert (NULL != info->peer);
3997 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3998 sizeof (struct GNUNET_MESH_PathACK),
4004 struct MeshPeerPath *path2;
4006 /* It's for somebody else! Retransmit. */
4007 path2 = path_duplicate (path);
4008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4009 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4010 path2 = path_duplicate (path);
4011 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4012 send_create_path (dest_peer_info, path, t);
4019 * Core handler for path destruction
4021 * @param cls closure
4022 * @param message message
4023 * @param peer peer identity this notification is about
4024 * @param atsi performance data
4025 * @param atsi_count number of records in 'atsi'
4027 * @return GNUNET_OK to keep the connection open,
4028 * GNUNET_SYSERR to close it (signal serious error)
4031 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4032 const struct GNUNET_MessageHeader *message,
4033 const struct GNUNET_ATS_Information *atsi,
4034 unsigned int atsi_count)
4036 struct GNUNET_MESH_ManipulatePath *msg;
4037 struct GNUNET_PeerIdentity *pi;
4038 struct MeshPeerPath *path;
4039 struct MeshTunnel *t;
4040 unsigned int own_pos;
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4045 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4046 size = ntohs (message->size);
4047 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4049 GNUNET_break_op (0);
4053 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4054 if (size % sizeof (struct GNUNET_PeerIdentity))
4056 GNUNET_break_op (0);
4059 size /= sizeof (struct GNUNET_PeerIdentity);
4062 GNUNET_break_op (0);
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4067 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4068 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4070 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4072 t = tunnel_get (pi, ntohl (msg->tid));
4075 /* TODO notify back: we don't know this tunnel */
4076 GNUNET_break_op (0);
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4080 path = path_new (size);
4082 for (i = 0; i < size; i++)
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4085 GNUNET_i2s (&pi[i]));
4086 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4087 if (path->peers[i] == myid)
4090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4091 if (own_pos < path->length - 1)
4092 send_message (message, &pi[own_pos + 1], t);
4094 send_client_tunnel_disconnect(t, NULL);
4096 tunnel_delete_peer (t, path->peers[path->length - 1]);
4097 path_destroy (path);
4103 * Core handler for notifications of broken paths
4105 * @param cls closure
4106 * @param message message
4107 * @param peer peer identity this notification is about
4108 * @param atsi performance data
4109 * @param atsi_count number of records in 'atsi'
4111 * @return GNUNET_OK to keep the connection open,
4112 * GNUNET_SYSERR to close it (signal serious error)
4115 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4116 const struct GNUNET_MessageHeader *message,
4117 const struct GNUNET_ATS_Information *atsi,
4118 unsigned int atsi_count)
4120 struct GNUNET_MESH_PathBroken *msg;
4121 struct MeshTunnel *t;
4123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4124 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4125 msg = (struct GNUNET_MESH_PathBroken *) message;
4126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4127 GNUNET_i2s (&msg->peer1));
4128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4129 GNUNET_i2s (&msg->peer2));
4130 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4133 GNUNET_break_op (0);
4136 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4137 GNUNET_PEER_search (&msg->peer2));
4144 * Core handler for tunnel destruction
4146 * @param cls closure
4147 * @param message message
4148 * @param peer peer identity this notification is about
4149 * @param atsi performance data
4150 * @param atsi_count number of records in 'atsi'
4152 * @return GNUNET_OK to keep the connection open,
4153 * GNUNET_SYSERR to close it (signal serious error)
4156 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4157 const struct GNUNET_MessageHeader *message,
4158 const struct GNUNET_ATS_Information *atsi,
4159 unsigned int atsi_count)
4161 struct GNUNET_MESH_TunnelDestroy *msg;
4162 struct MeshTunnel *t;
4164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4165 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4166 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4168 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4169 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4172 /* Probably already got the message from another path,
4173 * destroyed the tunnel and retransmitted to children.
4176 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4179 if (t->id.oid == myid)
4181 GNUNET_break_op (0);
4184 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4186 /* Tunnel was incoming, notify clients */
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4188 t->local_tid, t->local_tid_dest);
4189 send_clients_tunnel_destroy (t);
4191 tunnel_send_destroy (t);
4192 t->destroy = GNUNET_YES;
4193 // TODO: add timeout to destroy the tunnel anyway
4199 * Core handler for mesh network traffic going from the origin to a peer
4201 * @param cls closure
4202 * @param peer peer identity this notification is about
4203 * @param message message
4204 * @param atsi performance data
4205 * @param atsi_count number of records in 'atsi'
4206 * @return GNUNET_OK to keep the connection open,
4207 * GNUNET_SYSERR to close it (signal serious error)
4210 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4211 const struct GNUNET_MessageHeader *message,
4212 const struct GNUNET_ATS_Information *atsi,
4213 unsigned int atsi_count)
4215 struct GNUNET_MESH_Unicast *msg;
4216 struct GNUNET_PeerIdentity *neighbor;
4217 struct MeshTunnelChildInfo *cinfo;
4218 struct MeshTunnel *t;
4219 GNUNET_PEER_Id dest_id;
4224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4226 size = ntohs (message->size);
4228 sizeof (struct GNUNET_MESH_Unicast) +
4229 sizeof (struct GNUNET_MessageHeader))
4234 msg = (struct GNUNET_MESH_Unicast *) message;
4235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4236 ntohs (msg[1].header.type));
4237 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4240 /* TODO notify back: we don't know this tunnel */
4241 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4242 GNUNET_break_op (0);
4245 pid = ntohl (msg->pid);
4248 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4250 " Already seen mid %u, DROPPING!\n", pid);
4255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4256 " mid %u not seen yet, forwarding\n", pid);
4258 t->skip += (pid - t->pid) - 1;
4260 tunnel_reset_timeout (t);
4261 dest_id = GNUNET_PEER_search (&msg->destination);
4262 if (dest_id == myid)
4264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4265 " it's for us! sending to clients...\n");
4266 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4267 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4268 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK); // FIXME send after client processes the packet
4271 ttl = ntohl (msg->ttl);
4272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4275 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4276 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4280 " not for us, retransmitting...\n");
4281 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4283 neighbor = tree_get_first_hop (t->tree, dest_id);
4284 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4285 &neighbor->hashPubKey);
4288 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
4289 cinfo->id = GNUNET_PEER_intern (neighbor);
4291 cinfo->max_pid = pid + t->queue_max - t->queue_n; // FIXME review
4293 GNUNET_assert (GNUNET_OK ==
4294 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
4295 &neighbor->hashPubKey,
4297 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
4300 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4303 send_message (message, neighbor, t);
4309 * Core handler for mesh network traffic going from the origin to all peers
4311 * @param cls closure
4312 * @param message message
4313 * @param peer peer identity this notification is about
4314 * @param atsi performance data
4315 * @param atsi_count number of records in 'atsi'
4316 * @return GNUNET_OK to keep the connection open,
4317 * GNUNET_SYSERR to close it (signal serious error)
4319 * TODO: Check who we got this from, to validate route.
4322 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4323 const struct GNUNET_MessageHeader *message,
4324 const struct GNUNET_ATS_Information *atsi,
4325 unsigned int atsi_count)
4327 struct GNUNET_MESH_Multicast *msg;
4328 struct MeshTunnel *t;
4332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
4334 size = ntohs (message->size);
4335 if (sizeof (struct GNUNET_MESH_Multicast) +
4336 sizeof (struct GNUNET_MessageHeader) > size)
4338 GNUNET_break_op (0);
4341 msg = (struct GNUNET_MESH_Multicast *) message;
4342 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4346 /* TODO notify that we dont know that tunnel */
4347 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4348 GNUNET_break_op (0);
4351 pid = ntohl (msg->pid);
4354 /* already seen this packet, drop */
4355 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4357 " Already seen mid %u, DROPPING!\n", pid);
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4363 " mid %u not seen yet, forwarding\n", pid);
4365 t->skip += (pid - t->pid) - 1;
4367 tunnel_reset_timeout (t);
4369 /* Transmit to locally interested clients */
4370 if (NULL != t->peers &&
4371 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
4373 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
4374 send_subscribed_clients (message, &msg[1].header);
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
4377 if (ntohl (msg->ttl) == 0)
4379 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4380 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4383 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
4384 tunnel_send_multicast (t, message, GNUNET_NO);
4390 * Core handler for mesh network traffic toward the owner of a tunnel
4392 * @param cls closure
4393 * @param message message
4394 * @param peer peer identity this notification is about
4395 * @param atsi performance data
4396 * @param atsi_count number of records in 'atsi'
4398 * @return GNUNET_OK to keep the connection open,
4399 * GNUNET_SYSERR to close it (signal serious error)
4402 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4403 const struct GNUNET_MessageHeader *message,
4404 const struct GNUNET_ATS_Information *atsi,
4405 unsigned int atsi_count)
4407 struct GNUNET_MESH_ToOrigin *msg;
4408 struct GNUNET_PeerIdentity id;
4409 struct MeshPeerInfo *peer_info;
4410 struct MeshTunnel *t;
4413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4415 size = ntohs (message->size);
4416 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4417 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4419 GNUNET_break_op (0);
4422 msg = (struct GNUNET_MESH_ToOrigin *) message;
4423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4424 ntohs (msg[1].header.type));
4425 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4429 /* TODO notify that we dont know this tunnel (whom)? */
4430 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4431 GNUNET_break_op (0);
4435 if (t->id.oid == myid)
4438 struct GNUNET_MESH_ToOrigin *copy;
4440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4441 " it's for us! sending to clients...\n");
4442 if (NULL == t->owner)
4444 /* got data packet for ownerless tunnel */
4445 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
4447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
4448 GNUNET_break_op (0);
4451 /* TODO signature verification */
4452 memcpy (cbuf, message, size);
4453 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4454 copy->tid = htonl (t->local_tid);
4455 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4456 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4457 ©->header, GNUNET_YES);
4460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4461 " not for us, retransmitting...\n");
4463 peer_info = peer_info_get (&msg->oid);
4464 if (NULL == peer_info)
4466 /* unknown origin of tunnel */
4470 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4471 send_message (message, &id, t);
4472 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4479 * Core handler for mesh network traffic point-to-point acks.
4481 * @param cls closure
4482 * @param message message
4483 * @param peer peer identity this notification is about
4484 * @param atsi performance data
4485 * @param atsi_count number of records in 'atsi'
4487 * @return GNUNET_OK to keep the connection open,
4488 * GNUNET_SYSERR to close it (signal serious error)
4491 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4492 const struct GNUNET_MessageHeader *message,
4493 const struct GNUNET_ATS_Information *atsi,
4494 unsigned int atsi_count)
4496 struct GNUNET_MESH_ACK *msg;
4497 struct MeshTunnelChildInfo *cinfo;
4498 struct MeshTunnel *t;
4501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got an ACK packet from %s\n",
4503 msg = (struct GNUNET_MESH_ACK *) message;
4504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4505 ntohs (msg[1].header.type));
4506 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4510 /* TODO notify that we dont know this tunnel (whom)? */
4511 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4512 GNUNET_break_op (0);
4515 ack = ntohl (msg->pid);
4516 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
4520 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
4521 cinfo->id = GNUNET_PEER_intern (peer);
4522 cinfo->max_pid = ack;
4523 cinfo->skip = t->pid; // FIXME create on send?
4524 cinfo->pid = t->pid;
4526 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4532 * Core handler for path ACKs
4534 * @param cls closure
4535 * @param message message
4536 * @param peer peer identity this notification is about
4537 * @param atsi performance data
4538 * @param atsi_count number of records in 'atsi'
4540 * @return GNUNET_OK to keep the connection open,
4541 * GNUNET_SYSERR to close it (signal serious error)
4544 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4545 const struct GNUNET_MessageHeader *message,
4546 const struct GNUNET_ATS_Information *atsi,
4547 unsigned int atsi_count)
4549 struct GNUNET_MESH_PathACK *msg;
4550 struct GNUNET_PeerIdentity id;
4551 struct MeshPeerInfo *peer_info;
4552 struct MeshPeerPath *p;
4553 struct MeshTunnel *t;
4555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4556 GNUNET_i2s (&my_full_id));
4557 msg = (struct GNUNET_MESH_PathACK *) message;
4558 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4561 /* TODO notify that we don't know the tunnel */
4562 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4564 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4568 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4570 peer_info = peer_info_get (&msg->peer_id);
4571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4572 GNUNET_i2s (&msg->peer_id));
4573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4576 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
4578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4579 "connect_by_string completed, stopping search\n");
4580 regex_cancel_search (t->regex_ctx);
4581 t->regex_ctx = NULL;
4584 /* Add paths to peers? */
4585 p = tree_get_path_to_peer (t->tree, peer_info->id);
4588 path_add_to_peers (p, GNUNET_YES);
4596 /* Message for us? */
4597 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4600 if (NULL == t->owner)
4602 GNUNET_break_op (0);
4605 if (NULL != t->dht_get_type)
4607 GNUNET_DHT_get_stop (t->dht_get_type);
4608 t->dht_get_type = NULL;
4610 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4612 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4613 send_client_peer_connected (t, peer_info->id);
4618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4619 " not for us, retransmitting...\n");
4620 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4621 peer_info = peer_info_get (&msg->oid);
4622 if (NULL == peer_info)
4624 /* If we know the tunnel, we should DEFINITELY know the peer */
4628 send_message (message, &id, t);
4634 * Functions to handle messages from core
4636 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4637 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4638 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4639 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4640 sizeof (struct GNUNET_MESH_PathBroken)},
4641 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4642 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4643 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4644 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4645 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4646 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4647 sizeof (struct GNUNET_MESH_ACK)},
4648 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4649 sizeof (struct GNUNET_MESH_PathACK)},
4655 /******************************************************************************/
4656 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4657 /******************************************************************************/
4660 * deregister_app: iterator for removing each application registered by a client
4662 * @param cls closure
4663 * @param key the hash of the application id (used to access the hashmap)
4664 * @param value the value stored at the key (client)
4666 * @return GNUNET_OK on success
4669 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
4671 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
4672 GNUNET_break (GNUNET_YES ==
4673 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
4679 * notify_client_connection_failure: notify a client that the connection to the
4680 * requested remote peer is not possible (for instance, no route found)
4681 * Function called when the socket is ready to queue more data. "buf" will be
4682 * NULL and "size" zero if the socket was closed for writing in the meantime.
4684 * @param cls closure
4685 * @param size number of bytes available in buf
4686 * @param buf where the callee should write the message
4687 * @return number of bytes written to buf
4690 notify_client_connection_failure (void *cls, size_t size, void *buf)
4693 struct MeshPeerInfo *peer_info;
4694 struct GNUNET_MESH_PeerControl *msg;
4695 struct GNUNET_PeerIdentity id;
4697 if (0 == size && NULL == buf)
4699 // TODO retry? cancel?
4703 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4704 peer_info = (struct MeshPeerInfo *) cls;
4705 msg = (struct GNUNET_MESH_PeerControl *) buf;
4706 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4707 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4708 // msg->tunnel_id = htonl(peer_info->t->tid);
4709 GNUNET_PEER_resolve (peer_info->id, &id);
4710 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4718 * Send keepalive packets for a peer
4720 * @param cls Closure (tunnel for which to send the keepalive).
4721 * @param tc Notification context.
4723 * TODO: implement explicit multicast keepalive?
4726 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4728 struct MeshTunnel *t = cls;
4729 struct GNUNET_MessageHeader *payload;
4730 struct GNUNET_MESH_Multicast *msg;
4732 sizeof (struct GNUNET_MESH_Multicast) +
4733 sizeof (struct GNUNET_MessageHeader);
4736 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4740 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4743 "sending keepalive for tunnel %d\n", t->id.tid);
4745 msg = (struct GNUNET_MESH_Multicast *) cbuf;
4746 msg->header.size = htons (size);
4747 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4748 msg->oid = my_full_id;
4749 msg->tid = htonl (t->id.tid);
4750 msg->ttl = htonl (default_ttl);
4751 msg->pid = htonl (t->pid + 1);
4753 payload = (struct GNUNET_MessageHeader *) &msg[1];
4754 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
4755 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4756 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
4758 t->path_refresh_task =
4759 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4765 * Function to process paths received for a new peer addition. The recorded
4766 * paths form the initial tunnel, which can be optimized later.
4767 * Called on each result obtained for the DHT search.
4769 * @param cls closure
4770 * @param exp when will this value expire
4771 * @param key key of the result
4772 * @param get_path path of the get request
4773 * @param get_path_length lenght of get_path
4774 * @param put_path path of the put request
4775 * @param put_path_length length of the put_path
4776 * @param type type of the result
4777 * @param size number of bytes in data
4778 * @param data pointer to the result data
4780 * TODO: re-issue the request after certain time? cancel after X results?
4783 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4784 const struct GNUNET_HashCode * key,
4785 const struct GNUNET_PeerIdentity *get_path,
4786 unsigned int get_path_length,
4787 const struct GNUNET_PeerIdentity *put_path,
4788 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4789 size_t size, const void *data)
4791 struct MeshPathInfo *path_info = cls;
4792 struct MeshPeerPath *p;
4793 struct GNUNET_PeerIdentity pi;
4796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4797 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4800 p = path_build_from_dht (get_path, get_path_length, put_path,
4802 path_add_to_peers (p, GNUNET_NO);
4804 for (i = 0; i < path_info->peer->ntunnels; i++)
4806 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4807 peer_info_connect (path_info->peer, path_info->t);
4815 * Function to process paths received for a new peer addition. The recorded
4816 * paths form the initial tunnel, which can be optimized later.
4817 * Called on each result obtained for the DHT search.
4819 * @param cls closure
4820 * @param exp when will this value expire
4821 * @param key key of the result
4822 * @param get_path path of the get request
4823 * @param get_path_length lenght of get_path
4824 * @param put_path path of the put request
4825 * @param put_path_length length of the put_path
4826 * @param type type of the result
4827 * @param size number of bytes in data
4828 * @param data pointer to the result data
4831 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4832 const struct GNUNET_HashCode * key,
4833 const struct GNUNET_PeerIdentity *get_path,
4834 unsigned int get_path_length,
4835 const struct GNUNET_PeerIdentity *put_path,
4836 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4837 size_t size, const void *data)
4839 const struct PBlock *pb = data;
4840 const struct GNUNET_PeerIdentity *pi = &pb->id;
4841 struct MeshTunnel *t = cls;
4842 struct MeshPeerInfo *peer_info;
4843 struct MeshPeerPath *p;
4845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
4846 if (size != sizeof (struct PBlock))
4848 GNUNET_break_op (0);
4851 if (ntohl(pb->type) != t->type)
4853 GNUNET_break_op (0);
4856 GNUNET_assert (NULL != t->owner);
4857 peer_info = peer_info_get (pi);
4858 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
4860 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
4862 p = path_build_from_dht (get_path, get_path_length, put_path,
4864 path_add_to_peers (p, GNUNET_NO);
4866 tunnel_add_peer (t, peer_info);
4867 peer_info_connect (peer_info, t);
4872 * Function called if the connect attempt to a peer found via
4873 * connect_by_string times out. Try to connect to another peer, if any.
4874 * Otherwise try to reconnect to the same peer.
4876 * @param cls Closure (info about regex search).
4877 * @param tc TaskContext.
4880 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4882 struct MeshRegexSearchInfo *info = cls;
4883 struct MeshPeerInfo *peer_info;
4887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
4888 info->timeout = GNUNET_SCHEDULER_NO_TASK;
4889 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
4897 if (0 < info->n_peers)
4899 // Select next peer, put current in that spot.
4900 id = info->peers[info->i_peer];
4901 info->peers[info->i_peer] = info->peer;
4902 info->i_peer = (info->i_peer + 1) % info->n_peers;
4906 // Try to connect to same peer again.
4909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
4911 peer_info = peer_info_get_short(id);
4912 tunnel_add_peer (info->t, peer_info);
4914 tunnel_delete_peer (info->t, old);
4915 peer_info_connect (peer_info, info->t);
4916 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
4917 ®ex_connect_timeout,
4919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
4924 * Function to process DHT string to regex matching.
4925 * Called on each result obtained for the DHT search.
4927 * @param cls closure (search context)
4928 * @param exp when will this value expire
4929 * @param key key of the result
4930 * @param get_path path of the get request (not used)
4931 * @param get_path_length lenght of get_path (not used)
4932 * @param put_path path of the put request (not used)
4933 * @param put_path_length length of the put_path (not used)
4934 * @param type type of the result
4935 * @param size number of bytes in data
4936 * @param data pointer to the result data
4939 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4940 const struct GNUNET_HashCode * key,
4941 const struct GNUNET_PeerIdentity *get_path,
4942 unsigned int get_path_length,
4943 const struct GNUNET_PeerIdentity *put_path,
4944 unsigned int put_path_length,
4945 enum GNUNET_BLOCK_Type type,
4946 size_t size, const void *data)
4948 const struct MeshRegexAccept *block = data;
4949 struct MeshRegexSearchContext *ctx = cls;
4950 struct MeshRegexSearchInfo *info = ctx->info;
4951 struct MeshPeerPath *p;
4952 struct MeshPeerInfo *peer_info;
4954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
4955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
4957 peer_info = peer_info_get(&block->id);
4958 p = path_build_from_dht (get_path, get_path_length, put_path,
4960 path_add_to_peers (p, GNUNET_NO);
4963 tunnel_add_peer (info->t, peer_info);
4964 peer_info_connect (peer_info, info->t);
4965 if (0 == info->peer)
4967 info->peer = peer_info->id;
4971 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
4974 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
4975 ®ex_connect_timeout,
4983 * Function to process DHT string to regex matching.
4984 * Called on each result obtained for the DHT search.
4986 * @param cls closure (search context)
4987 * @param exp when will this value expire
4988 * @param key key of the result
4989 * @param get_path path of the get request (not used)
4990 * @param get_path_length lenght of get_path (not used)
4991 * @param put_path path of the put request (not used)
4992 * @param put_path_length length of the put_path (not used)
4993 * @param type type of the result
4994 * @param size number of bytes in data
4995 * @param data pointer to the result data
4997 * TODO: re-issue the request after certain time? cancel after X results?
5000 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5001 const struct GNUNET_HashCode * key,
5002 const struct GNUNET_PeerIdentity *get_path,
5003 unsigned int get_path_length,
5004 const struct GNUNET_PeerIdentity *put_path,
5005 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5006 size_t size, const void *data)
5008 const struct MeshRegexBlock *block = data;
5009 struct MeshRegexSearchContext *ctx = cls;
5010 struct MeshRegexSearchInfo *info = ctx->info;
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5015 "DHT GET STRING RETURNED RESULTS\n");
5016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5017 " key: %s\n", GNUNET_h2s (key));
5019 copy = GNUNET_malloc (size);
5020 memcpy (copy, data, size);
5021 GNUNET_break (GNUNET_OK ==
5022 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5023 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5024 len = ntohl (block->n_proof);
5026 char proof[len + 1];
5028 memcpy (proof, &block[1], len);
5030 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5032 GNUNET_break_op (0);
5036 len = strlen (info->description);
5037 if (len == ctx->position) // String processed
5039 if (GNUNET_YES == ntohl (block->accepting))
5041 regex_find_path(key, ctx);
5045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5046 // FIXME REGEX this block not successful, wait for more? start timeout?
5050 GNUNET_break (GNUNET_OK ==
5051 GNUNET_MESH_regex_block_iterate (block, size,
5052 ®ex_edge_iterator, ctx));
5056 /******************************************************************************/
5057 /********************* MESH LOCAL HANDLES **************************/
5058 /******************************************************************************/
5062 * Handler for client disconnection
5064 * @param cls closure
5065 * @param client identification of the client; NULL
5066 * for the last call when the server is destroyed
5069 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5071 struct MeshClient *c;
5072 struct MeshClient *next;
5075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5084 if (c->handle != client)
5086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5092 GNUNET_SERVER_client_drop (c->handle);
5093 c->shutting_down = GNUNET_YES;
5094 GNUNET_assert (NULL != c->own_tunnels);
5095 GNUNET_assert (NULL != c->incoming_tunnels);
5096 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5097 &tunnel_destroy_iterator, c);
5098 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5099 &tunnel_destroy_iterator, c);
5100 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5101 &tunnel_destroy_iterator, c);
5102 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5103 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5104 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5106 /* deregister clients applications */
5107 if (NULL != c->apps)
5109 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5110 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5112 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5113 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5115 GNUNET_SCHEDULER_cancel (announce_applications_task);
5116 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5118 if (NULL != c->types)
5119 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5120 for (i = 0; i < c->n_regex; i++)
5122 GNUNET_free (c->regexes[i]);
5124 GNUNET_free_non_null (c->regexes);
5125 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5126 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5128 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5131 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5140 * Handler for new clients
5142 * @param cls closure
5143 * @param client identification of the client
5144 * @param message the actual message, which includes messages the client wants
5147 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5148 const struct GNUNET_MessageHeader *message)
5150 struct GNUNET_MESH_ClientConnect *cc_msg;
5151 struct MeshClient *c;
5152 GNUNET_MESH_ApplicationType *a;
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5160 /* Check data sanity */
5161 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5162 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5163 ntypes = ntohs (cc_msg->types);
5164 napps = ntohs (cc_msg->applications);
5166 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5173 /* Create new client structure */
5174 c = GNUNET_malloc (sizeof (struct MeshClient));
5175 c->id = next_client_id++;
5176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5178 GNUNET_SERVER_client_keep (client);
5179 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5182 GNUNET_MESH_ApplicationType at;
5183 struct GNUNET_HashCode hc;
5185 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5186 for (i = 0; i < napps; i++)
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5190 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5191 /* store in clients hashmap */
5192 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5193 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5194 /* store in global hashmap, for announcements */
5195 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5196 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5198 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5199 announce_applications_task =
5200 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5206 struct GNUNET_HashCode hc;
5208 t = (uint16_t *) & a[napps];
5209 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5210 for (i = 0; i < ntypes; i++)
5213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5214 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5216 /* store in clients hashmap */
5217 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5218 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5219 /* store in global hashmap */
5220 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5221 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5225 " client has %u+%u subscriptions\n", napps, ntypes);
5227 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5228 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5229 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5230 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5231 GNUNET_SERVER_notification_context_add (nc, client);
5232 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5234 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5240 * Handler for clients announcing available services by a regular expression.
5242 * @param cls closure
5243 * @param client identification of the client
5244 * @param message the actual message, which includes messages the client wants
5247 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5248 const struct GNUNET_MessageHeader *message)
5250 struct MeshClient *c;
5254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5256 /* Sanity check for client registration */
5257 if (NULL == (c = client_get (client)))
5260 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5265 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5266 regex = GNUNET_malloc (len + 1);
5267 memcpy (regex, &message[1], len);
5269 GNUNET_array_append (c->regexes, c->n_regex, regex);
5270 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5272 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5278 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5284 * Handler for requests of new tunnels
5286 * @param cls closure
5287 * @param client identification of the client
5288 * @param message the actual message
5291 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5292 const struct GNUNET_MessageHeader *message)
5294 struct GNUNET_MESH_TunnelMessage *t_msg;
5295 struct MeshTunnel *t;
5296 struct MeshClient *c;
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5300 /* Sanity check for client registration */
5301 if (NULL == (c = client_get (client)))
5304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5309 /* Message sanity check */
5310 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5313 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5317 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5318 /* Sanity check for tunnel numbering */
5319 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5325 /* Sanity check for duplicate tunnel IDs */
5326 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
5329 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5333 while (NULL != tunnel_get_by_pi (myid, next_tid))
5334 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5335 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
5336 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5338 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5339 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
5341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5342 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5348 * Handler for requests of deleting tunnels
5350 * @param cls closure
5351 * @param client identification of the client
5352 * @param message the actual message
5355 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5356 const struct GNUNET_MessageHeader *message)
5358 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5359 struct MeshClient *c;
5360 struct MeshTunnel *t;
5361 MESH_TunnelNumber tid;
5363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5364 "Got a DESTROY TUNNEL from client!\n");
5366 /* Sanity check for client registration */
5367 if (NULL == (c = client_get (client)))
5370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5373 /* Message sanity check */
5374 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5377 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5381 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5383 /* Retrieve tunnel */
5384 tid = ntohl (tunnel_msg->tunnel_id);
5385 t = tunnel_get_by_local_id(c, tid);
5389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5393 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5395 client_ignore_tunnel (c, t);
5397 // TODO: when to destroy incoming tunnel?
5398 if (t->nclients == 0)
5400 GNUNET_assert (GNUNET_YES ==
5401 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
5403 GNUNET_assert (GNUNET_YES ==
5404 GNUNET_CONTAINER_multihashmap_remove (t->peers,
5405 &my_full_id.hashPubKey,
5409 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5412 send_client_tunnel_disconnect(t, c);
5413 client_delete_tunnel(c, t);
5415 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5417 tunnel_send_destroy (t);
5418 t->destroy = GNUNET_YES;
5419 // The tunnel will be destroyed when the last message is transmitted.
5420 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5426 * Handler for requests of seeting tunnel's speed.
5428 * @param cls Closure (unused).
5429 * @param client Identification of the client.
5430 * @param message The actual message.
5433 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
5434 const struct GNUNET_MessageHeader *message)
5436 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5437 struct MeshClient *c;
5438 struct MeshTunnel *t;
5439 MESH_TunnelNumber tid;
5441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5442 "Got a SPEED request from client!\n");
5444 /* Sanity check for client registration */
5445 if (NULL == (c = client_get (client)))
5448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5453 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5455 /* Retrieve tunnel */
5456 tid = ntohl (tunnel_msg->tunnel_id);
5457 t = tunnel_get_by_local_id(c, tid);
5461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5462 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5466 switch (ntohs(message->type))
5468 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
5469 t->speed_min = GNUNET_YES;
5471 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
5472 t->speed_min = GNUNET_NO;
5477 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5482 * Handler for requests of seeting tunnel's buffering policy.
5484 * @param cls Closure (unused).
5485 * @param client Identification of the client.
5486 * @param message The actual message.
5489 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5490 const struct GNUNET_MessageHeader *message)
5492 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5493 struct MeshClient *c;
5494 struct MeshTunnel *t;
5495 MESH_TunnelNumber tid;
5497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5498 "Got a BUFFER request from client!\n");
5500 /* Sanity check for client registration */
5501 if (NULL == (c = client_get (client)))
5504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5509 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5511 /* Retrieve tunnel */
5512 tid = ntohl (tunnel_msg->tunnel_id);
5513 t = tunnel_get_by_local_id(c, tid);
5517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5518 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5522 switch (ntohs(message->type))
5524 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5525 t->nobuffer = GNUNET_NO;
5527 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5528 t->nobuffer = GNUNET_YES;
5534 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5539 * Handler for connection requests to new peers
5541 * @param cls closure
5542 * @param client identification of the client
5543 * @param message the actual message (PeerControl)
5546 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
5547 const struct GNUNET_MessageHeader *message)
5549 struct GNUNET_MESH_PeerControl *peer_msg;
5550 struct MeshPeerInfo *peer_info;
5551 struct MeshClient *c;
5552 struct MeshTunnel *t;
5553 MESH_TunnelNumber tid;
5555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
5556 /* Sanity check for client registration */
5557 if (NULL == (c = client_get (client)))
5560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5564 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5565 /* Sanity check for message size */
5566 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5573 /* Tunnel exists? */
5574 tid = ntohl (peer_msg->tunnel_id);
5575 t = tunnel_get_by_local_id (c, tid);
5579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5583 /* Does client own tunnel? */
5584 if (t->owner->handle != client)
5587 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
5591 GNUNET_i2s (&peer_msg->peer));
5592 peer_info = peer_info_get (&peer_msg->peer);
5594 tunnel_add_peer (t, peer_info);
5595 peer_info_connect (peer_info, t);
5597 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5603 * Handler for disconnection requests of peers in a tunnel
5605 * @param cls closure
5606 * @param client identification of the client
5607 * @param message the actual message (PeerControl)
5610 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
5611 const struct GNUNET_MessageHeader *message)
5613 struct GNUNET_MESH_PeerControl *peer_msg;
5614 struct MeshPeerInfo *peer_info;
5615 struct MeshClient *c;
5616 struct MeshTunnel *t;
5617 MESH_TunnelNumber tid;
5619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
5620 /* Sanity check for client registration */
5621 if (NULL == (c = client_get (client)))
5624 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5627 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5628 /* Sanity check for message size */
5629 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5632 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5636 /* Tunnel exists? */
5637 tid = ntohl (peer_msg->tunnel_id);
5638 t = tunnel_get_by_local_id (c, tid);
5642 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5647 /* Does client own tunnel? */
5648 if (t->owner->handle != client)
5651 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
5656 GNUNET_i2s (&peer_msg->peer));
5657 /* Is the peer in the tunnel? */
5659 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
5660 if (NULL == peer_info)
5663 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5667 /* Ok, delete peer from tunnel */
5668 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
5669 &peer_msg->peer.hashPubKey);
5671 send_destroy_path (t, peer_info->id);
5672 tunnel_delete_peer (t, peer_info->id);
5673 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5678 * Handler for blacklist requests of peers in a tunnel
5680 * @param cls closure
5681 * @param client identification of the client
5682 * @param message the actual message (PeerControl)
5685 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
5686 const struct GNUNET_MessageHeader *message)
5688 struct GNUNET_MESH_PeerControl *peer_msg;
5689 struct MeshClient *c;
5690 struct MeshTunnel *t;
5691 MESH_TunnelNumber tid;
5693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
5694 /* Sanity check for client registration */
5695 if (NULL == (c = client_get (client)))
5698 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5701 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5703 /* Sanity check for message size */
5704 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5711 /* Tunnel exists? */
5712 tid = ntohl (peer_msg->tunnel_id);
5713 t = tunnel_get_by_local_id (c, tid);
5717 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5722 GNUNET_array_append(t->blacklisted, t->nblacklisted,
5723 GNUNET_PEER_intern(&peer_msg->peer));
5728 * Handler for unblacklist requests of peers in a tunnel
5730 * @param cls closure
5731 * @param client identification of the client
5732 * @param message the actual message (PeerControl)
5735 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
5736 const struct GNUNET_MessageHeader *message)
5738 struct GNUNET_MESH_PeerControl *peer_msg;
5739 struct MeshClient *c;
5740 struct MeshTunnel *t;
5741 MESH_TunnelNumber tid;
5745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
5746 /* Sanity check for client registration */
5747 if (NULL == (c = client_get (client)))
5750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5753 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
5755 /* Sanity check for message size */
5756 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
5759 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5763 /* Tunnel exists? */
5764 tid = ntohl (peer_msg->tunnel_id);
5765 t = tunnel_get_by_local_id (c, tid);
5769 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
5774 /* if peer is not known, complain */
5775 pid = GNUNET_PEER_search (&peer_msg->peer);
5782 /* search and remove from list */
5783 for (i = 0; i < t->nblacklisted; i++)
5785 if (t->blacklisted[i] == pid)
5787 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
5788 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
5793 /* if peer hasn't been blacklisted, complain */
5799 * Handler for connection requests to new peers by type
5801 * @param cls closure
5802 * @param client identification of the client
5803 * @param message the actual message (ConnectPeerByType)
5806 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
5807 const struct GNUNET_MessageHeader *message)
5809 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
5810 struct MeshClient *c;
5811 struct MeshTunnel *t;
5812 struct GNUNET_HashCode hash;
5813 MESH_TunnelNumber tid;
5815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
5816 /* Sanity check for client registration */
5817 if (NULL == (c = client_get (client)))
5820 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5824 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
5825 /* Sanity check for message size */
5826 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
5827 ntohs (connect_msg->header.size))
5830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5834 /* Tunnel exists? */
5835 tid = ntohl (connect_msg->tunnel_id);
5836 t = tunnel_get_by_local_id (c, tid);
5840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5844 /* Does client own tunnel? */
5845 if (t->owner->handle != client)
5848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5852 /* Do WE have the service? */
5853 t->type = ntohl (connect_msg->type);
5854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
5855 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
5856 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
5859 /* Yes! Fast forward, add ourselves to the tunnel and send the
5860 * good news to the client, and alert the destination client of
5861 * an incoming tunnel.
5863 * FIXME send a path create to self, avoid code duplication
5865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
5866 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
5867 peer_info_get (&my_full_id),
5868 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
5871 send_client_peer_connected (t, myid);
5872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
5873 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5875 t->local_tid_dest = next_local_tid++;
5876 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5877 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5878 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5882 /* Ok, lets find a peer offering the service */
5883 if (NULL != t->dht_get_type)
5885 GNUNET_DHT_get_stop (t->dht_get_type);
5887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
5888 GNUNET_h2s (&hash));
5890 GNUNET_DHT_get_start (dht_handle,
5891 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
5893 dht_replication_level,
5894 GNUNET_DHT_RO_RECORD_ROUTE |
5895 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
5897 &dht_get_type_handler, t);
5899 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5905 * Handler for connection requests to new peers by a string service description.
5907 * @param cls closure
5908 * @param client identification of the client
5909 * @param message the actual message, which includes messages the client wants
5912 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
5913 const struct GNUNET_MessageHeader *message)
5915 struct GNUNET_MESH_ConnectPeerByString *msg;
5916 struct MeshRegexSearchContext *ctx;
5917 struct MeshRegexSearchInfo *info;
5918 struct GNUNET_DHT_GetHandle *get_h;
5919 struct GNUNET_HashCode key;
5920 struct MeshTunnel *t;
5921 struct MeshClient *c;
5922 MESH_TunnelNumber tid;
5926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5927 "Connect by string started\n");
5928 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
5929 size = htons (message->size);
5931 /* Sanity check for client registration */
5932 if (NULL == (c = client_get (client)))
5935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5939 /* Message size sanity check */
5940 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
5943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5947 /* Tunnel exists? */
5948 tid = ntohl (msg->tunnel_id);
5949 t = tunnel_get_by_local_id (c, tid);
5953 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5957 /* Does client own tunnel? */
5958 if (t->owner->handle != client)
5961 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5966 " on tunnel %s [%u]\n",
5967 GNUNET_i2s(&my_full_id),
5970 /* Only one connect_by_string allowed at the same time! */
5971 /* FIXME: allow more, return handle at api level to cancel, document */
5972 if (NULL != t->regex_ctx)
5975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5979 /* Find string itself */
5980 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
5981 string = (const char *) &msg[1];
5983 /* Initialize context */
5984 size = GNUNET_REGEX_get_first_key(string, len, &key);
5985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5986 " consumed %u bits out of %u\n", size, len);
5987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5988 " looking for %s\n", GNUNET_h2s (&key));
5990 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
5992 info->description = GNUNET_malloc (len + 1);
5993 memcpy (info->description, string, len);
5994 info->description[len] = '\0';
5995 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
5996 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
5997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
5999 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6000 ctx->position = size;
6004 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6006 /* Start search in DHT */
6007 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6008 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6009 &key, /* key to search */
6010 dht_replication_level, /* replication level */
6011 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6012 NULL, /* xquery */ // FIXME BLOOMFILTER
6013 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6014 &dht_get_string_handler, ctx);
6016 GNUNET_break (GNUNET_OK ==
6017 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6020 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6022 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6028 * Handler for client traffic directed to one peer
6030 * @param cls closure
6031 * @param client identification of the client
6032 * @param message the actual message
6035 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6036 const struct GNUNET_MessageHeader *message)
6038 struct MeshClient *c;
6039 struct MeshTunnel *t;
6040 struct MeshPeerInfo *pi;
6041 struct GNUNET_MESH_Unicast *data_msg;
6042 MESH_TunnelNumber tid;
6045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6046 "Got a unicast request from a client!\n");
6048 /* Sanity check for client registration */
6049 if (NULL == (c = client_get (client)))
6052 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6055 data_msg = (struct GNUNET_MESH_Unicast *) message;
6056 /* Sanity check for message size */
6057 size = ntohs (message->size);
6058 if (sizeof (struct GNUNET_MESH_Unicast) +
6059 sizeof (struct GNUNET_MessageHeader) > size)
6062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6066 /* Tunnel exists? */
6067 tid = ntohl (data_msg->tid);
6068 t = tunnel_get_by_local_id (c, tid);
6072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6076 /* Is it a local tunnel? Then, does client own the tunnel? */
6077 if (t->owner->handle != client)
6080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6084 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6085 &data_msg->destination.hashPubKey);
6086 /* Is the selected peer in the tunnel? */
6090 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6094 /* Ok, everything is correct, send the message
6095 * (pretend we got it from a mesh peer)
6098 char buf[ntohs (message->size)] GNUNET_ALIGN;
6099 struct GNUNET_MESH_Unicast *copy;
6101 /* Work around const limitation */
6102 copy = (struct GNUNET_MESH_Unicast *) buf;
6103 memcpy (buf, data_msg, size);
6104 copy->oid = my_full_id;
6105 copy->tid = htonl (t->id.tid);
6106 copy->ttl = htonl (default_ttl);
6107 copy->pid = htonl (t->pid + 1);
6108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6109 " calling generic handler...\n");
6110 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6112 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6118 * Handler for client traffic directed to the origin
6120 * @param cls closure
6121 * @param client identification of the client
6122 * @param message the actual message
6125 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6126 const struct GNUNET_MessageHeader *message)
6128 struct GNUNET_MESH_ToOrigin *data_msg;
6129 struct GNUNET_PeerIdentity id;
6130 struct MeshClient *c;
6131 struct MeshTunnel *t;
6132 MESH_TunnelNumber tid;
6135 /* Sanity check for client registration */
6136 if (NULL == (c = client_get (client)))
6139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6142 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6143 /* Sanity check for message size */
6144 size = ntohs (message->size);
6145 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6146 sizeof (struct GNUNET_MessageHeader) > size)
6149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6153 /* Tunnel exists? */
6154 tid = ntohl (data_msg->tid);
6155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6156 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6157 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6160 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6163 t = tunnel_get_by_local_id (c, tid);
6167 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6171 /* It should be sent by someone who has this as incoming tunnel. */
6172 if (-1 == client_knows_tunnel (c, t))
6175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6178 GNUNET_PEER_resolve (t->id.oid, &id);
6180 /* Ok, everything is correct, send the message
6181 * (pretend we got it from a mesh peer)
6184 char buf[ntohs (message->size)] GNUNET_ALIGN;
6185 struct GNUNET_MESH_ToOrigin *copy;
6187 /* Work around const limitation */
6188 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6189 memcpy (buf, data_msg, size);
6191 copy->tid = htonl (t->id.tid);
6192 copy->sender = my_full_id;
6193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6194 " calling generic handler...\n");
6195 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6197 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6203 * Handler for client traffic directed to all peers in a tunnel
6205 * @param cls closure
6206 * @param client identification of the client
6207 * @param message the actual message
6210 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6211 const struct GNUNET_MessageHeader *message)
6213 struct MeshClient *c;
6214 struct MeshTunnel *t;
6215 struct GNUNET_MESH_Multicast *data_msg;
6216 MESH_TunnelNumber tid;
6218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6219 "Got a multicast request from a client!\n");
6221 /* Sanity check for client registration */
6222 if (NULL == (c = client_get (client)))
6225 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6228 data_msg = (struct GNUNET_MESH_Multicast *) message;
6229 /* Sanity check for message size */
6230 if (sizeof (struct GNUNET_MESH_Multicast) +
6231 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6234 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6238 /* Tunnel exists? */
6239 tid = ntohl (data_msg->tid);
6240 t = tunnel_get_by_local_id (c, tid);
6244 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6248 /* Does client own tunnel? */
6249 if (t->owner->handle != client)
6252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6257 char buf[ntohs (message->size)] GNUNET_ALIGN;
6258 struct GNUNET_MESH_Multicast *copy;
6260 copy = (struct GNUNET_MESH_Multicast *) buf;
6261 memcpy (buf, message, ntohs (message->size));
6262 copy->oid = my_full_id;
6263 copy->tid = htonl (t->id.tid);
6264 copy->ttl = htonl (default_ttl);
6265 copy->pid = htonl (t->pid + 1);
6266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6267 " calling generic handler...\n");
6268 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
6271 /* receive done gets called when last copy is sent to a neighbor */
6277 * Functions to handle messages from clients
6279 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6280 {&handle_local_new_client, NULL,
6281 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6282 {&handle_local_announce_regex, NULL,
6283 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
6284 {&handle_local_tunnel_create, NULL,
6285 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6286 sizeof (struct GNUNET_MESH_TunnelMessage)},
6287 {&handle_local_tunnel_destroy, NULL,
6288 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6289 sizeof (struct GNUNET_MESH_TunnelMessage)},
6290 {&handle_local_tunnel_speed, NULL,
6291 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
6292 sizeof (struct GNUNET_MESH_TunnelMessage)},
6293 {&handle_local_tunnel_speed, NULL,
6294 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
6295 sizeof (struct GNUNET_MESH_TunnelMessage)},
6296 {&handle_local_tunnel_buffer, NULL,
6297 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6298 sizeof (struct GNUNET_MESH_TunnelMessage)},
6299 {&handle_local_tunnel_buffer, NULL,
6300 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6301 sizeof (struct GNUNET_MESH_TunnelMessage)},
6302 {&handle_local_connect_add, NULL,
6303 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
6304 sizeof (struct GNUNET_MESH_PeerControl)},
6305 {&handle_local_connect_del, NULL,
6306 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
6307 sizeof (struct GNUNET_MESH_PeerControl)},
6308 {&handle_local_blacklist, NULL,
6309 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
6310 sizeof (struct GNUNET_MESH_PeerControl)},
6311 {&handle_local_unblacklist, NULL,
6312 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
6313 sizeof (struct GNUNET_MESH_PeerControl)},
6314 {&handle_local_connect_by_type, NULL,
6315 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
6316 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
6317 {&handle_local_connect_by_string, NULL,
6318 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
6319 {&handle_local_unicast, NULL,
6320 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6321 {&handle_local_to_origin, NULL,
6322 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6323 {&handle_local_multicast, NULL,
6324 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6330 * To be called on core init/fail.
6332 * @param cls service closure
6333 * @param server handle to the server for this service
6334 * @param identity the public identity of this peer
6337 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6338 const struct GNUNET_PeerIdentity *identity)
6340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6341 core_handle = server;
6342 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6346 GNUNET_SCHEDULER_shutdown ();
6353 * Method called whenever a given peer connects.
6355 * @param cls closure
6356 * @param peer peer identity this notification is about
6357 * @param atsi performance data for the connection
6358 * @param atsi_count number of records in 'atsi'
6361 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
6362 const struct GNUNET_ATS_Information *atsi,
6363 unsigned int atsi_count)
6365 struct MeshPeerInfo *peer_info;
6366 struct MeshPeerPath *path;
6368 DEBUG_CONN ("Peer connected\n");
6369 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6370 peer_info = peer_info_get (peer);
6371 if (myid == peer_info->id)
6373 DEBUG_CONN (" (self)\n");
6378 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6380 path = path_new (2);
6381 path->peers[0] = myid;
6382 path->peers[1] = peer_info->id;
6383 GNUNET_PEER_change_rc (myid, 1);
6384 GNUNET_PEER_change_rc (peer_info->id, 1);
6385 peer_info_add_path (peer_info, path, GNUNET_YES);
6386 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6392 * Method called whenever a peer disconnects.
6394 * @param cls closure
6395 * @param peer peer identity this notification is about
6398 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6400 struct MeshPeerInfo *pi;
6401 struct MeshPeerQueue *q;
6402 struct MeshPeerQueue *n;
6404 DEBUG_CONN ("Peer disconnected\n");
6405 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6417 /* try to reroute this traffic instead */
6418 queue_destroy(q, GNUNET_YES);
6422 peer_info_remove_path (pi, pi->id, myid);
6425 DEBUG_CONN (" (self)\n");
6427 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6432 /******************************************************************************/
6433 /************************ MAIN FUNCTIONS ****************************/
6434 /******************************************************************************/
6437 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6439 * @param cls closure
6440 * @param key current key code
6441 * @param value value in the hash map
6442 * @return GNUNET_YES if we should continue to iterate,
6446 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6448 struct MeshTunnel *t = value;
6455 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
6457 * @param cls closure
6458 * @param key current key code
6459 * @param value value in the hash map
6460 * @return GNUNET_YES if we should continue to iterate,
6464 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
6466 struct MeshPeerInfo *p = value;
6467 struct MeshPeerQueue *q;
6468 struct MeshPeerQueue *n;
6476 queue_destroy(q, GNUNET_YES);
6480 peer_info_destroy (p);
6485 * Task run during shutdown.
6491 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6495 if (core_handle != NULL)
6497 GNUNET_CORE_disconnect (core_handle);
6500 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6501 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6502 if (dht_handle != NULL)
6504 GNUNET_DHT_disconnect (dht_handle);
6509 GNUNET_SERVER_notification_context_destroy (nc);
6512 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6514 GNUNET_SCHEDULER_cancel (announce_id_task);
6515 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6521 * Process mesh requests.
6523 * @param cls closure
6524 * @param server the initialized server
6525 * @param c configuration to use
6528 run (void *cls, struct GNUNET_SERVER_Handle *server,
6529 const struct GNUNET_CONFIGURATION_Handle *c)
6531 struct MeshPeerInfo *peer;
6532 struct MeshPeerPath *p;
6535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6536 server_handle = server;
6537 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6538 NULL, /* Closure passed to MESH functions */
6539 &core_init, /* Call core_init once connected */
6540 &core_connect, /* Handle connects */
6541 &core_disconnect, /* remove peers on disconnects */
6542 NULL, /* Don't notify about all incoming messages */
6543 GNUNET_NO, /* For header only in notification */
6544 NULL, /* Don't notify about all outbound messages */
6545 GNUNET_NO, /* For header-only out notification */
6546 core_handlers); /* Register these handlers */
6548 if (core_handle == NULL)
6551 GNUNET_SCHEDULER_shutdown ();
6556 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
6559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6561 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6563 GNUNET_SCHEDULER_shutdown ();
6568 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6569 &refresh_path_time))
6571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6573 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6574 "refresh path time");
6575 GNUNET_SCHEDULER_shutdown ();
6580 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6581 &app_announce_time))
6583 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6585 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6586 "app announce time");
6587 GNUNET_SCHEDULER_shutdown ();
6592 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6595 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6597 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6598 "id announce time");
6599 GNUNET_SCHEDULER_shutdown ();
6604 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
6605 &unacknowledged_wait_time))
6607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6609 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6610 "unacknowledged wait time");
6611 GNUNET_SCHEDULER_shutdown ();
6616 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6621 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
6623 GNUNET_SCHEDULER_shutdown ();
6628 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6631 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6633 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
6639 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6640 &dht_replication_level))
6642 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6644 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
6645 "dht replication level", 10);
6646 dht_replication_level = 10;
6650 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6651 GNUNET_free (keyfile);
6652 if (my_private_key == NULL)
6654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6655 _("Mesh service could not access hostkey. Exiting.\n"));
6656 GNUNET_SCHEDULER_shutdown ();
6659 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6660 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6661 &my_full_id.hashPubKey);
6662 myid = GNUNET_PEER_intern (&my_full_id);
6664 // transport_handle = GNUNET_TRANSPORT_connect(c,
6671 dht_handle = GNUNET_DHT_connect (c, 64);
6672 if (dht_handle == NULL)
6677 stats = GNUNET_STATISTICS_create ("mesh", c);
6681 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6683 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6684 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6685 peers = GNUNET_CONTAINER_multihashmap_create (32);
6686 applications = GNUNET_CONTAINER_multihashmap_create (32);
6687 types = GNUNET_CONTAINER_multihashmap_create (32);
6689 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6690 nc = GNUNET_SERVER_notification_context_create (server_handle,
6692 GNUNET_SERVER_disconnect_notify (server_handle,
6693 &handle_local_client_disconnect, NULL);
6697 clients_tail = NULL;
6700 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6701 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6703 /* Create a peer_info for the local peer */
6704 peer = peer_info_get (&my_full_id);
6707 GNUNET_PEER_change_rc (myid, 1);
6708 peer_info_add_path (peer, p, GNUNET_YES);
6710 /* Scheduled the task to clean up when shutdown is called */
6711 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
6718 * The main function for the mesh service.
6720 * @param argc number of arguments from the command line
6721 * @param argv command line arguments
6722 * @return 0 ok, 1 on error
6725 main (int argc, char *const *argv)
6729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6732 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");