2 This file is part of GNUnet.
3 (C) 2001-2012 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?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh2_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "gnunet_dht_service.h"
54 #include "gnunet_statistics_service.h"
55 #include "gnunet_regex_lib.h"
57 #define MESH_BLOOM_SIZE 128
59 #define MESH_DEBUG_REGEX GNUNET_YES
60 #define MESH_DEBUG_DHT GNUNET_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
64 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
65 GNUNET_TIME_UNIT_MINUTES,\
68 #if MESH_DEBUG_CONNECTION
69 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
71 #define DEBUG_CONN(...)
75 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
77 #define DEBUG_DHT(...)
81 #define DEBUG_REGEX(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
83 #define DEBUG_REGEX(...)
90 struct timespec __mesh_start;
91 struct timespec __mesh_end;
92 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
93 #define INTERVAL_END \
95 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
96 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
97 if (__diff < 0) __diff += 1000000000;\
101 #define INTERVAL_SHOW \
103 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
105 #define INTERVAL_START
107 #define INTERVAL_SHOW
110 /******************************************************************************/
111 /************************ DATA STRUCTURES ****************************/
112 /******************************************************************************/
114 /** FWD declaration */
120 * Struct representing a piece of data being sent to other peers
124 /** Tunnel it belongs to. */
125 struct MeshTunnel *t;
127 /** How many remaining neighbors still hav't got it. */
128 unsigned int reference_counter;
130 /** How many remaining neighbors we need to send this to. */
131 unsigned int total_out;
133 /** Size of the data. */
142 * Struct containing info about a queued transmission to this peer
149 struct MeshPeerQueue *next;
154 struct MeshPeerQueue *prev;
157 * Peer this transmission is directed to.
159 struct MeshPeerInfo *peer;
162 * Tunnel this message belongs to.
164 struct MeshTunnel *tunnel;
167 * Pointer to info stucture used as cls.
177 * Size of the message
184 * Struct to store regex information announced by clients.
186 struct MeshRegexDescriptor
189 * Regular expression itself.
194 * How many characters per edge can we squeeze?
196 uint16_t compression;
199 * Handle to announce the regex.
201 struct GNUNET_REGEX_announce_handle *h;
206 * Struct to keep information of searches of services described by a regex
207 * using a user-provided string service description.
209 struct MeshRegexSearchInfo
212 * Which tunnel is this for
214 struct MeshTunnel *t;
217 * User provided description of the searched service.
222 * Regex search handle.
224 struct GNUNET_REGEX_search_handle *search_handle;
227 * Peer that is connecting via connect_by_string. When connected, free ctx.
232 * Other peers that are found but not yet being connected to.
234 GNUNET_PEER_Id *peers;
237 * Number of elements in peers.
239 unsigned int n_peers;
242 * Next peer to try to connect to.
247 * Timeout for a connect attempt.
248 * When reached, try to connect to a different peer, if any. If not,
249 * try the same peer again.
251 GNUNET_SCHEDULER_TaskIdentifier timeout;
257 * Struct containing all info possibly needed to build a package when called
260 struct MeshTransmissionDescriptor
262 /** ID of the tunnel this packet travels in */
263 struct MESH_TunnelID *origin;
265 /** Who was this message being sent to */
266 struct MeshPeerInfo *peer;
268 /** Ultimate destination of the packet */
269 GNUNET_PEER_Id destination;
271 /** Data descriptor */
272 struct MeshData* mesh_data;
277 * Struct containing all information regarding a given peer
287 * Last time we heard from this peer
289 struct GNUNET_TIME_Absolute last_contact;
292 * Task handler for delayed connect task;
294 GNUNET_SCHEDULER_TaskIdentifier connect_task;
297 * Number of attempts to reconnect so far
299 int n_reconnect_attempts;
302 * Paths to reach the peer, ordered by ascending hop count
304 struct MeshPeerPath *path_head;
307 * Paths to reach the peer, ordered by ascending hop count
309 struct MeshPeerPath *path_tail;
312 * Handle to stop the DHT search for a path to this peer
314 struct GNUNET_DHT_GetHandle *dhtget;
317 * Closure given to the DHT GET
319 struct MeshPathInfo *dhtgetcls;
322 * Array of tunnels this peer participates in
323 * (most probably a small amount, therefore not a hashmap)
324 * When the path to the peer changes, notify these tunnels to let them
325 * re-adjust their path trees.
327 struct MeshTunnel **tunnels;
330 * Number of tunnels this peers participates in
332 unsigned int ntunnels;
335 * Transmission queue to core DLL head
337 struct MeshPeerQueue *queue_head;
340 * Transmission queue to core DLL tail
342 struct MeshPeerQueue *queue_tail;
345 * How many messages are in the queue to this peer.
347 unsigned int queue_n;
350 * Handle to for queued transmissions
352 struct GNUNET_CORE_TransmitHandle *core_transmit;
357 * Globally unique tunnel identification (owner + number)
358 * DO NOT USE OVER THE NETWORK
363 * Node that owns the tunnel
368 * Tunnel number to differentiate all the tunnels owned by the node oid
369 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
371 MESH_TunnelNumber tid;
376 * Struct containing all information regarding a tunnel
377 * For an intermediate node the improtant info used will be:
378 * - id Tunnel unique identification
379 * - paths[0] To know where to send it next
380 * - metainfo: ready, speeds, accounting
387 struct MESH_TunnelID id;
390 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
392 MESH_TunnelNumber local_tid;
395 * Local tunnel number for local destination clients (incoming number)
396 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
399 MESH_TunnelNumber local_tid_dest;
402 * Is the tunnel bufferless (minimum latency)?
407 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
412 * Packet ID of the last bck packet sent (unique counter per hop).
417 * Force sending ACK? Flag to allow duplicate ACK on POLL.
422 * Last ACK sent towards the origin (for traffic towards leaf node).
424 uint32_t last_fwd_ack;
427 * BCK ACK value received from the hop towards the owner of the tunnel,
428 * (previous node / owner): up to what message PID can we sent back to him.
433 * How many messages are in the forward queue (towards leaves).
435 unsigned int fwd_queue_n;
438 * How many messages do we accept in the forward queue.
440 unsigned int fwd_queue_max;
443 * How many messages are in the backward queue (towards origin).
445 unsigned int bck_queue_n;
448 * How many messages do we accept in the backward queue.
450 unsigned int bck_queue_max;
453 * Task to poll peer in case of a stall.
455 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
458 * Last time the tunnel was used
460 struct GNUNET_TIME_Absolute timestamp;
463 * Peer of the tunnel.
468 * Client owner of the tunnel, if any
470 struct MeshClient *owner;
473 * Clients that have been informed about and want to stay in the tunnel.
475 struct MeshClient **clients;
478 * Flow control info for each client.
480 struct MeshTunnelClientInfo *clients_fc;
483 * Number of elements in clients/clients_fc
485 unsigned int nclients;
488 * Clients that have been informed but requested to leave the tunnel.
490 struct MeshClient **ignore;
493 * Number of elements in clients
495 unsigned int nignore;
498 * Bloomfilter (for peer identities) to stop circular routes
500 char bloomfilter[MESH_BLOOM_SIZE];
506 struct MeshTunnelTree *tree;
509 * Task to keep the used paths alive
511 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
514 * Task to destroy the tunnel after timeout
516 * FIXME: merge the two? a tunnel will have either
517 * a path refresh OR a timeout, never both!
519 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
522 * Flag to signal the destruction of the tunnel.
523 * If this is set GNUNET_YES the tunnel will be destroyed
524 * when the queue is empty.
529 * Total messages pending for this tunnels, payload or not.
531 unsigned int pending_messages;
534 * If the tunnel is empty, destoy it.
536 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
541 * Info about a leaf client of a tunnel, needed to perform flow control.
543 struct MeshTunnelClientInfo
546 * PID of the last packet sent to the client (FWD).
551 * PID of the last packet received from the client (BCK).
556 * Maximum PID allowed (FWD ACK received).
561 * Last ACK sent to that child (BCK ACK).
568 * Info needed to work with tunnel paths and peers
575 struct MeshTunnel *t;
578 * Neighbouring peer to whom we send the packet to
580 struct MeshPeerInfo *peer;
585 struct MeshPeerPath *path;
590 * Struct containing information about a client of the service
597 struct MeshClient *next;
602 struct MeshClient *prev;
605 * Tunnels that belong to this client, indexed by local id
607 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
610 * Tunnels this client has accepted, indexed by incoming local id
612 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
615 * Tunnels this client has rejected, indexed by incoming local id
617 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
619 * Handle to communicate with the client
621 struct GNUNET_SERVER_Client *handle;
624 * Messages that this client has declared interest in
626 struct GNUNET_CONTAINER_MultiHashMap *types;
629 * Whether the client is active or shutting down (don't send confirmations
630 * to a client that is shutting down.
635 * ID of the client, mainly for debug messages
640 * Regular expressions describing the services offered by this client.
642 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
645 * Number of regular expressions in regexes.
647 unsigned int n_regex;
650 * Task to refresh all regular expresions in the DHT.
652 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
655 * Tmp store for partially retrieved regex.
662 /******************************************************************************/
663 /************************ DEBUG FUNCTIONS ****************************/
664 /******************************************************************************/
668 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
669 * @param cls string to print
670 * @param success GNUNET_OK if the PUT was transmitted,
671 * GNUNET_NO on timeout,
672 * GNUNET_SYSERR on disconnect from service
673 * after the PUT message was transmitted
674 * (so we don't know if it was received or not)
679 mesh_debug (void *cls, int success)
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
687 unsigned int debug_fwd_ack;
688 unsigned int debug_bck_ack;
692 /******************************************************************************/
693 /*********************** GLOBAL VARIABLES ****************************/
694 /******************************************************************************/
696 /************************** Configuration parameters **************************/
699 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
701 static struct GNUNET_TIME_Relative refresh_path_time;
704 * How often to PUT local application numbers in the DHT.
706 static struct GNUNET_TIME_Relative app_announce_time;
709 * How often to PUT own ID in the DHT.
711 static struct GNUNET_TIME_Relative id_announce_time;
714 * Maximum time allowed to connect to a peer found by string.
716 static struct GNUNET_TIME_Relative connect_timeout;
719 * Default TTL for payload packets.
721 static unsigned long long default_ttl;
724 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
726 static unsigned long long dht_replication_level;
729 * How many tunnels are we willing to maintain.
730 * Local tunnels are always allowed, even if there are more tunnels than max.
732 static unsigned long long max_tunnels;
735 * How many messages *in total* are we willing to queue, divided by number of
736 * tunnels to get tunnel queue size.
738 static unsigned long long max_msgs_queue;
741 * How many peers do we want to remember?
743 static unsigned long long max_peers;
746 /*************************** Static global variables **************************/
749 * Hostkey generation context
751 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
754 * DLL with all the clients, head.
756 static struct MeshClient *clients;
759 * DLL with all the clients, tail.
761 static struct MeshClient *clients_tail;
764 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
766 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
769 * Number of tunnels known.
771 static unsigned long long n_tunnels;
774 * Tunnels incoming, indexed by MESH_TunnelNumber
775 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
777 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
780 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
782 static struct GNUNET_CONTAINER_MultiHashMap *peers;
785 * Handle to communicate with transport
787 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
790 * Handle to communicate with core.
792 static struct GNUNET_CORE_Handle *core_handle;
797 static struct GNUNET_DHT_Handle *dht_handle;
802 static struct GNUNET_SERVER_Handle *server_handle;
805 * Handle to the statistics service.
807 static struct GNUNET_STATISTICS_Handle *stats;
810 * Notification context, to send messages to local clients.
812 static struct GNUNET_SERVER_NotificationContext *nc;
815 * Local peer own ID (memory efficient handle).
817 static GNUNET_PEER_Id myid;
820 * Local peer own ID (full value).
822 static struct GNUNET_PeerIdentity my_full_id;
827 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
832 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
835 * Tunnel ID for the next created tunnel (global tunnel number).
837 static MESH_TunnelNumber next_tid;
840 * Tunnel ID for the next incoming tunnel (local tunnel number).
842 static MESH_TunnelNumber next_local_tid;
845 * All message types clients of this peer are interested in.
847 static struct GNUNET_CONTAINER_MultiHashMap *types;
850 * Task to periodically announce itself in the network.
852 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
855 * Next ID to assign to a client.
857 unsigned int next_client_id;
860 /******************************************************************************/
861 /*********************** DECLARATIONS **************************/
862 /******************************************************************************/
865 * Function to process paths received for a new peer addition. The recorded
866 * paths form the initial tunnel, which can be optimized later.
867 * Called on each result obtained for the DHT search.
870 * @param exp when will this value expire
871 * @param key key of the result
872 * @param type type of the result
873 * @param size number of bytes in data
874 * @param data pointer to the result data
877 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
878 const struct GNUNET_HashCode * key,
879 const struct GNUNET_PeerIdentity *get_path,
880 unsigned int get_path_length,
881 const struct GNUNET_PeerIdentity *put_path,
882 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
883 size_t size, const void *data);
887 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
888 * and insert it in the appropiate structures if the peer is not known yet.
890 * @param peer Full identity of the peer.
892 * @return Existing or newly created peer info.
894 static struct MeshPeerInfo *
895 peer_info_get (const struct GNUNET_PeerIdentity *peer);
899 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
900 * and insert it in the appropiate structures if the peer is not known yet.
902 * @param peer Short identity of the peer.
904 * @return Existing or newly created peer info.
906 static struct MeshPeerInfo *
907 peer_info_get_short (const GNUNET_PEER_Id peer);
911 * Try to establish a new connection to this peer.
912 * Use the best path for the given tunnel.
913 * If the peer doesn't have any path to it yet, try to get one.
914 * If the peer already has some path, send a CREATE PATH towards it.
916 * @param peer PeerInfo of the peer.
917 * @param t Tunnel for which to create the path, if possible.
920 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
924 * Build a PeerPath from the paths returned from the DHT, reversing the paths
925 * to obtain a local peer -> destination path and interning the peer ids.
927 * @return Newly allocated and created path
929 static struct MeshPeerPath *
930 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
931 unsigned int get_path_length,
932 const struct GNUNET_PeerIdentity *put_path,
933 unsigned int put_path_length);
937 * Adds a path to the peer_infos of all the peers in the path
939 * @param p Path to process.
940 * @param confirmed Whether we know if the path works or not.
943 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
948 * Search for a tunnel by global ID using full PeerIdentities.
950 * @param oid owner of the tunnel.
951 * @param tid global tunnel number.
953 * @return tunnel handler, NULL if doesn't exist.
955 static struct MeshTunnel *
956 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
960 * Delete an active client from the tunnel.
966 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
969 * Notify a tunnel that a connection has broken that affects at least
972 * @param t Tunnel affected.
973 * @param p1 Peer that got disconnected from p2.
974 * @param p2 Peer that got disconnected from p1.
976 * @return Short ID of the peer disconnected (either p1 or p2).
977 * 0 if the tunnel remained unaffected.
979 static GNUNET_PEER_Id
980 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
985 * Get the current ack value for a tunnel, for data going from root to leaves,
986 * taking in account the tunnel mode and the status of all children and clients.
990 * @return Maximum PID allowed.
993 tunnel_get_fwd_ack (struct MeshTunnel *t);
997 * Add a client to a tunnel, initializing all needed data structures.
999 * @param t Tunnel to which add the client.
1000 * @param c Client which to add to the tunnel.
1003 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1007 * @brief Queue and pass message to core when possible.
1009 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1010 * and accounts for it. In case the queue is full, the message is dropped and
1013 * Otherwise, message is treated as internal and allowed to go regardless of
1016 * @param cls Closure (@c type dependant). It will be used by queue_send to
1017 * build the message to be sent if not already prebuilt.
1018 * @param type Type of the message, 0 for a raw message.
1019 * @param size Size of the message.
1020 * @param dst Neighbor to send message to.
1021 * @param t Tunnel this message belongs to.
1024 queue_add (void *cls, uint16_t type, size_t size,
1025 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1029 * Free a transmission that was already queued with all resources
1030 * associated to the request.
1032 * @param queue Queue handler to cancel.
1033 * @param clear_cls Is it necessary to free associated cls?
1036 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1040 * @brief Get the next transmittable message from the queue.
1042 * This will be the head, except in the case of being a data packet
1043 * not allowed by the destination peer.
1045 * @param peer Destination peer.
1047 * @return The next viable MeshPeerQueue element to send to that peer.
1048 * NULL when there are no transmittable messages.
1050 struct MeshPeerQueue *
1051 queue_get_next (const struct MeshPeerInfo *peer);
1055 * Core callback to write a queued packet to core buffer
1057 * @param cls Closure (peer info).
1058 * @param size Number of bytes available in buf.
1059 * @param buf Where the to write the message.
1061 * @return number of bytes written to buf
1064 queue_send (void *cls, size_t size, void *buf);
1067 /******************************************************************************/
1068 /************************ PERIODIC FUNCTIONS ****************************/
1069 /******************************************************************************/
1072 * Periodically announce self id in the DHT
1074 * @param cls closure
1075 * @param tc task context
1078 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1080 struct PBlock block;
1082 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1084 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1088 * - Set data expiration in function of X
1089 * - Adapt X to churn
1091 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1093 block.id = my_full_id;
1094 block.type = htonl (0);
1095 GNUNET_DHT_put (dht_handle, /* DHT handle */
1096 &my_full_id.hashPubKey, /* Key to use */
1097 dht_replication_level, /* Replication level */
1098 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1099 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1100 sizeof (block), /* Size of the data */
1101 (const char *) &block, /* Data itself */
1102 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1103 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1104 NULL, /* Continuation */
1105 NULL); /* Continuation closure */
1107 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1111 /******************************************************************************/
1112 /****************** GENERAL HELPER FUNCTIONS ************************/
1113 /******************************************************************************/
1116 * Decrements the reference counter and frees all resources if needed
1118 * @param mesh_data Data Descriptor used in a multicast message.
1119 * Freed no longer needed (last message).
1122 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1124 if (0 == --(mesh_data->reference_counter))
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1127 GNUNET_free (mesh_data->data);
1128 GNUNET_free (mesh_data);
1134 * Check if client has registered with the service and has not disconnected
1136 * @param client the client to check
1138 * @return non-NULL if client exists in the global DLL
1140 static struct MeshClient *
1141 client_get (struct GNUNET_SERVER_Client *client)
1143 struct MeshClient *c;
1148 if (c->handle == client)
1157 * Checks if a given client has subscribed to certain message type
1159 * @param message_type Type of message to check
1160 * @param c Client to check
1162 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1164 * FIXME: use of crypto_hash slows it down
1165 * The hash function alone takes 8-10us out of the ~55us for the whole
1166 * process of retransmitting the message from one local client to another.
1167 * Find faster implementation!
1170 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1172 struct GNUNET_HashCode hc;
1174 if (NULL == c->types)
1177 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1178 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1183 * Check whether client wants traffic from a tunnel.
1185 * @param c Client to check.
1186 * @param t Tunnel to be found.
1188 * @return GNUNET_YES if client knows tunnel.
1190 * TODO look in client hashmap
1193 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1197 for (i = 0; i < t->nclients; i++)
1198 if (t->clients[i] == c)
1205 * Check whether client has been informed about a tunnel.
1207 * @param c Client to check.
1208 * @param t Tunnel to be found.
1210 * @return GNUNET_YES if client knows tunnel.
1212 * TODO look in client hashmap
1215 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1219 for (i = 0; i < t->nignore; i++)
1220 if (t->ignore[i] == c)
1222 return client_wants_tunnel(c, t);
1227 * Marks a client as uninterested in traffic from the tunnel, updating both
1228 * client and tunnel to reflect this.
1230 * @param c Client that doesn't want traffic anymore.
1231 * @param t Tunnel which should be ignored.
1233 * FIXME when to delete an incoming tunnel?
1236 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1238 struct GNUNET_HashCode hash;
1240 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1241 GNUNET_break (GNUNET_YES ==
1242 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1244 GNUNET_break (GNUNET_YES ==
1245 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1246 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1247 tunnel_delete_active_client (t, c);
1248 GNUNET_array_append (t->ignore, t->nignore, c);
1253 * Deletes a tunnel from a client (either owner or destination). To be used on
1254 * tunnel destroy, otherwise, use client_ignore_tunnel.
1256 * @param c Client whose tunnel to delete.
1257 * @param t Tunnel which should be deleted.
1260 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1262 struct GNUNET_HashCode hash;
1266 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1267 GNUNET_assert (GNUNET_YES ==
1268 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1274 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1276 GNUNET_assert (GNUNET_YES ==
1277 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1281 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1289 * Send the message to all clients that have subscribed to its type
1291 * @param msg Pointer to the message itself
1292 * @param payload Pointer to the payload of the message.
1293 * @param t The tunnel to whose clients this message goes.
1295 * @return number of clients this message was sent to
1298 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1299 const struct GNUNET_MessageHeader *payload,
1300 struct MeshTunnel *t)
1302 struct MeshClient *c;
1303 MESH_TunnelNumber *tid;
1306 char cbuf[htons (msg->size)];
1308 type = ntohs (payload->type);
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1311 GNUNET_MESH_DEBUG_M2S (type));
1313 memcpy (cbuf, msg, sizeof (cbuf));
1314 switch (htons (msg->type))
1316 struct GNUNET_MESH_Unicast *uc;
1317 struct GNUNET_MESH_ToOrigin *to;
1319 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1320 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1323 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1324 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1332 for (count = 0, c = clients; c != NULL; c = c->next)
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1335 if (client_is_subscribed (type, c))
1337 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1341 *tid = htonl (t->local_tid);
1345 if (GNUNET_NO == client_knows_tunnel (c, t))
1347 /* This client doesn't know the tunnel */
1348 struct GNUNET_MESH_TunnelNotification tmsg;
1349 struct GNUNET_HashCode hash;
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1352 tmsg.header.size = htons (sizeof (tmsg));
1353 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1354 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1355 tmsg.tunnel_id = htonl (t->local_tid_dest);
1357 if (GNUNET_YES == t->nobuffer)
1358 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1359 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1360 &tmsg.header, GNUNET_NO);
1361 tunnel_add_client (t, c);
1362 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1364 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1365 c->incoming_tunnels, &hash, t,
1366 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1368 *tid = htonl (t->local_tid_dest);
1371 /* Check if the client wants to get traffic from the tunnel */
1372 if (GNUNET_NO == client_wants_tunnel(c, t))
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1376 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1377 (struct GNUNET_MessageHeader
1378 *) cbuf, GNUNET_NO);
1387 * Notify all clients (not depending on registration status) that the incoming
1388 * tunnel is no longer valid.
1390 * @param t Tunnel that was destroyed.
1393 send_clients_tunnel_destroy (struct MeshTunnel *t)
1395 struct GNUNET_MESH_TunnelMessage msg;
1397 msg.header.size = htons (sizeof (msg));
1398 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1399 msg.tunnel_id = htonl (t->local_tid_dest);
1400 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1405 * Notify clients of tunnel disconnections, if needed.
1406 * In case the origin disconnects, the destination clients get a tunnel destroy
1407 * notification. If the last destination disconnects (only one remaining client
1408 * in tunnel), the origin gets a (local ID) peer disconnected.
1409 * Note that the function must be called BEFORE removing the client from
1412 * @param t Tunnel that was destroyed.
1413 * @param c Client that disconnected.
1416 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1422 struct GNUNET_MESH_TunnelMessage msg;
1424 msg.header.size = htons (sizeof (msg));
1425 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1426 msg.tunnel_id = htonl (t->local_tid_dest);
1427 for (i = 0; i < t->nclients; i++)
1428 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1429 &msg.header, GNUNET_NO);
1431 // FIXME when to disconnect an incoming tunnel?
1436 * Iterator over all the peers to remove the oldest not-used entry.
1438 * @param cls Closure (unsued).
1439 * @param key ID of the peer.
1440 * @param value Peer_Info of the peer.
1445 peer_info_timeout (void *cls,
1446 const struct GNUNET_HashCode *key,
1453 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1454 * and insert it in the appropiate structures if the peer is not known yet.
1456 * @param peer Full identity of the peer.
1458 * @return Existing or newly created peer info.
1460 static struct MeshPeerInfo *
1461 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1463 struct MeshPeerInfo *peer_info;
1465 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1466 if (NULL == peer_info)
1469 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1470 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1472 GNUNET_CONTAINER_multihashmap_iterate (peers,
1476 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1477 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1478 peer_info->id = GNUNET_PEER_intern (peer);
1480 peer_info->last_contact = GNUNET_TIME_absolute_get();
1487 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1488 * and insert it in the appropiate structures if the peer is not known yet.
1490 * @param peer Short identity of the peer.
1492 * @return Existing or newly created peer info.
1494 static struct MeshPeerInfo *
1495 peer_info_get_short (const GNUNET_PEER_Id peer)
1497 struct GNUNET_PeerIdentity id;
1499 GNUNET_PEER_resolve (peer, &id);
1500 return peer_info_get (&id);
1505 * Iterator to remove the tunnel from the list of tunnels a peer participates
1508 * @param cls Closure (tunnel info)
1509 * @param key GNUNET_PeerIdentity of the peer (unused)
1510 * @param value PeerInfo of the peer
1512 * @return always GNUNET_YES, to keep iterating
1515 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1517 struct MeshTunnel *t = cls;
1518 struct MeshPeerInfo *peer = value;
1521 for (i = 0; i < peer->ntunnels; i++)
1524 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1527 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1529 GNUNET_realloc (peer->tunnels,
1530 peer->ntunnels * sizeof(struct MeshTunnel *));
1539 * Core callback to write a pre-constructed data packet to core buffer
1541 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1542 * @param size Number of bytes available in buf.
1543 * @param buf Where the to write the message.
1545 * @return number of bytes written to buf
1548 send_core_data_raw (void *cls, size_t size, void *buf)
1550 struct MeshTransmissionDescriptor *info = cls;
1551 struct GNUNET_MessageHeader *msg;
1554 GNUNET_assert (NULL != info);
1555 GNUNET_assert (NULL != info->mesh_data);
1556 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1557 total_size = ntohs (msg->size);
1559 if (total_size > size)
1564 memcpy (buf, msg, total_size);
1565 data_descriptor_decrement_rc (info->mesh_data);
1572 * Sends an already built non-multicast message to a peer,
1573 * properly registrating all used resources.
1575 * @param message Message to send. Function makes a copy of it.
1576 * @param peer Short ID of the neighbor whom to send the message.
1577 * @param t Tunnel on which this message is transmitted.
1580 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1581 const struct GNUNET_PeerIdentity *peer,
1582 struct MeshTunnel *t)
1584 struct MeshTransmissionDescriptor *info;
1585 struct MeshPeerInfo *neighbor;
1586 struct MeshPeerPath *p;
1590 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1592 size = ntohs (message->size);
1593 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1594 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1595 info->mesh_data->data = GNUNET_malloc (size);
1596 memcpy (info->mesh_data->data, message, size);
1597 type = ntohs(message->type);
1600 struct GNUNET_MESH_Unicast *m;
1601 struct GNUNET_MESH_ToOrigin *to;
1603 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1604 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
1605 m->ttl = htonl (ntohl (m->ttl) - 1);
1607 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1608 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
1610 to->pid = htonl(t->bck_pid);
1612 info->mesh_data->data_len = size;
1613 info->mesh_data->reference_counter = 1;
1614 info->mesh_data->total_out = 1;
1615 neighbor = peer_info_get (peer);
1616 for (p = neighbor->path_head; NULL != p; p = p->next)
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627 " %s IS NOT DIRECTLY CONNECTED\n",
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1632 for (p = neighbor->path_head; NULL != p; p = p->next)
1634 struct GNUNET_PeerIdentity debug_id;
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1638 " path with %u hops through:\n",
1640 for (i = 0; i < p->length; i++)
1642 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1645 i, GNUNET_i2s(&debug_id));
1649 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1651 " no direct connection to %s\n",
1653 GNUNET_free (info->mesh_data->data);
1654 GNUNET_free (info->mesh_data);
1658 info->peer = neighbor;
1659 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1670 * Sends a CREATE PATH message for a path to a peer, properly registrating
1671 * all used resources.
1673 * @param peer PeerInfo of the final peer for whom this path is being created.
1674 * @param p Path itself.
1675 * @param t Tunnel for which the path is created.
1678 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1679 struct MeshTunnel *t)
1681 struct GNUNET_PeerIdentity id;
1682 struct MeshPathInfo *path_info;
1683 struct MeshPeerInfo *neighbor;
1689 p = tree_get_path_to_peer (t->tree, peer->id);
1696 for (i = 0; i < p->length; i++)
1698 if (p->peers[i] == myid)
1701 if (i >= p->length - 1)
1707 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1709 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1710 path_info->path = p;
1712 neighbor = peer_info_get (&id);
1713 path_info->peer = neighbor;
1714 queue_add (path_info,
1715 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1716 sizeof (struct GNUNET_MESH_ManipulatePath) +
1717 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1724 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1726 * @param t Tunnel whose path to destroy.
1727 * @param destination Short ID of the peer to whom the path to destroy.
1730 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1732 struct MeshPeerPath *p;
1735 p = tree_get_path_to_peer (t->tree, destination);
1741 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1742 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1744 struct GNUNET_MESH_ManipulatePath *msg;
1745 struct GNUNET_PeerIdentity *pi;
1749 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1750 msg->header.size = htons (size);
1751 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1752 msg->tid = htonl (t->id.tid);
1753 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1754 for (i = 0; i < p->length; i++)
1756 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1758 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1765 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1767 * @param t Tunnel which to confirm.
1770 send_path_ack (struct MeshTunnel *t)
1772 struct MeshTransmissionDescriptor *info;
1773 struct GNUNET_PeerIdentity id;
1774 GNUNET_PEER_Id peer;
1776 peer = tree_get_predecessor (t->tree);
1777 GNUNET_PEER_resolve (peer, &id);
1778 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1779 info->origin = &t->id;
1780 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1781 GNUNET_assert (NULL != info->peer);
1784 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1785 sizeof (struct GNUNET_MESH_PathACK),
1792 * Try to establish a new connection to this peer.
1793 * Use the best path for the given tunnel.
1794 * If the peer doesn't have any path to it yet, try to get one.
1795 * If the peer already has some path, send a CREATE PATH towards it.
1797 * @param peer PeerInfo of the peer.
1798 * @param t Tunnel for which to create the path, if possible.
1801 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1803 struct MeshPeerPath *p;
1804 struct MeshPathInfo *path_info;
1806 if (NULL != peer->path_head)
1808 p = tree_get_path_to_peer (t->tree, peer->id);
1815 // FIXME always send create path to self
1818 send_create_path (peer, p, t);
1822 struct GNUNET_HashCode hash;
1825 t->local_tid_dest = next_local_tid++;
1826 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1829 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1830 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1837 else if (NULL == peer->dhtget)
1839 struct GNUNET_PeerIdentity id;
1841 GNUNET_PEER_resolve (peer->id, &id);
1842 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1843 path_info->peer = peer;
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1847 peer->dhtgetcls = path_info;
1848 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1849 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1850 &id.hashPubKey, /* key to search */
1851 dht_replication_level, /* replication level */
1852 GNUNET_DHT_RO_RECORD_ROUTE |
1853 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1854 NULL, /* xquery */ // FIXME BLOOMFILTER
1855 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1856 &dht_get_id_handler, path_info);
1858 /* Otherwise, there is no path but the DHT get is already started. */
1863 * Task to delay the connection of a peer
1865 * @param cls Closure (path info with tunnel and peer to connect).
1866 * Will be free'd on exection.
1867 * @param tc TaskContext
1870 peer_info_connect_task (void *cls,
1871 const struct GNUNET_SCHEDULER_TaskContext *tc)
1873 struct MeshPathInfo *path_info = cls;
1875 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
1877 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1882 peer_info_connect (path_info->peer, path_info->t);
1888 * Destroy the peer_info and free any allocated resources linked to it
1890 * @param pi The peer_info to destroy.
1892 * @return GNUNET_OK on success
1895 peer_info_destroy (struct MeshPeerInfo *pi)
1897 struct GNUNET_PeerIdentity id;
1898 struct MeshPeerPath *p;
1899 struct MeshPeerPath *nextp;
1901 GNUNET_PEER_resolve (pi->id, &id);
1902 GNUNET_PEER_change_rc (pi->id, -1);
1905 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1908 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1909 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1911 if (NULL != pi->dhtget)
1913 GNUNET_DHT_get_stop (pi->dhtget);
1914 GNUNET_free (pi->dhtgetcls);
1920 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1924 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
1926 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
1934 * Remove all paths that rely on a direct connection between p1 and p2
1935 * from the peer itself and notify all tunnels about it.
1937 * @param peer PeerInfo of affected peer.
1938 * @param p1 GNUNET_PEER_Id of one peer.
1939 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1942 * TODO: optimize (see below)
1945 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1948 struct MeshPeerPath *p;
1949 struct MeshPeerPath *aux;
1950 struct MeshPeerInfo *peer_d;
1952 unsigned int destroyed;
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1959 p = peer->path_head;
1963 for (i = 0; i < (p->length - 1); i++)
1965 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1966 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1968 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1979 for (i = 0; i < peer->ntunnels; i++)
1981 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1985 * Problem: one or more peers have been deleted from the tunnel tree.
1986 * We don't know who they are to try to add them again.
1987 * We need to try to find a new path for each of the disconnected peers.
1988 * Some of them might already have a path to reach them that does not
1989 * involve p1 and p2. Adding all anew might render in a better tree than
1990 * the trivial immediate fix.
1992 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1993 * its children will be reachable trough him.
1995 peer_d = peer_info_get_short (d);
1998 for (p = peer_d->path_head; NULL != p; p = p->next)
2000 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2008 /* No callback, as peer will be already disconnected and a connection
2009 * scheduled by tunnel_notify_connection_broken.
2011 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2015 peer_info_connect (peer_d, peer->tunnels[i]);
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2023 * Add the path to the peer and update the path used to reach it in case this
2026 * @param peer_info Destination peer to add the path to.
2027 * @param path New path to add. Last peer must be the peer in arg 1.
2028 * Path will be either used of freed if already known.
2029 * @param trusted Do we trust that this path is real?
2032 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2035 struct MeshPeerPath *aux;
2039 if ((NULL == peer_info) || (NULL == path))
2042 path_destroy (path);
2045 if (path->peers[path->length - 1] != peer_info->id)
2048 path_destroy (path);
2051 if (path->length <= 2 && GNUNET_NO == trusted)
2053 /* Only allow CORE to tell us about direct paths */
2054 path_destroy (path);
2057 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2058 for (l = 1; l < path->length; l++)
2060 if (path->peers[l] == myid)
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2063 for (l2 = 0; l2 < path->length - l; l2++)
2065 path->peers[l2] = path->peers[l + l2];
2070 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2075 struct GNUNET_PeerIdentity id;
2077 GNUNET_PEER_resolve (peer_info->id, &id);
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2079 path->length, GNUNET_i2s (&id));
2082 l = path_get_length (path);
2089 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2090 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2092 l2 = path_get_length (aux);
2095 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2096 peer_info->path_tail, aux, path);
2101 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2103 path_destroy (path);
2108 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2115 * Add the path to the origin peer and update the path used to reach it in case
2116 * this is the shortest.
2117 * The path is given in peer_info -> destination, therefore we turn the path
2118 * upside down first.
2120 * @param peer_info Peer to add the path to, being the origin of the path.
2121 * @param path New path to add after being inversed.
2122 * @param trusted Do we trust that this path is real?
2125 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2126 struct MeshPeerPath *path, int trusted)
2129 peer_info_add_path (peer_info, path, trusted);
2134 * Function called if the connection to the peer has been stalled for a while,
2135 * possibly due to a missed ACK. Poll the peer about its ACK status.
2137 * @param cls Closure (cinfo).
2138 * @param tc TaskContext.
2141 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2143 // struct GNUNET_MESH_Poll msg;
2144 // struct GNUNET_PeerIdentity id;
2145 // struct MeshTunnel *t;
2147 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2148 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2154 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2155 // msg.header.size = htons (sizeof (msg));
2156 // msg.tid = htonl (t->id.tid);
2157 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2158 // msg.last_ack = htonl (cinfo->fwd_ack);
2160 // GNUNET_PEER_resolve (cinfo->id, &id);
2161 // send_prebuilt_message (&msg.header, &id, cinfo->t);
2162 // cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2163 // MESH_MAX_POLL_TIME,
2164 // GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2165 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2166 // &tunnel_poll, cinfo);
2171 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2172 * to obtain a local peer -> destination path and interning the peer ids.
2174 * @return Newly allocated and created path
2176 static struct MeshPeerPath *
2177 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2178 unsigned int get_path_length,
2179 const struct GNUNET_PeerIdentity *put_path,
2180 unsigned int put_path_length)
2182 struct MeshPeerPath *p;
2188 GNUNET_PEER_change_rc (myid, 1);
2189 i = get_path_length;
2190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2191 for (i--; i >= 0; i--)
2193 id = GNUNET_PEER_intern (&get_path[i]);
2194 if (p->length > 0 && id == p->peers[p->length - 1])
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2197 GNUNET_PEER_change_rc (id, -1);
2201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2202 GNUNET_i2s (&get_path[i]));
2204 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2205 p->peers[p->length - 1] = id;
2208 i = put_path_length;
2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2210 for (i--; i >= 0; i--)
2212 id = GNUNET_PEER_intern (&put_path[i]);
2215 /* PUT path went through us, so discard the path up until now and start
2216 * from here to get a much shorter (and loop-free) path.
2221 if (p->length > 0 && id == p->peers[p->length - 1])
2223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2224 GNUNET_PEER_change_rc (id, -1);
2228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2229 GNUNET_i2s (&put_path[i]));
2231 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2232 p->peers[p->length - 1] = id;
2236 if (get_path_length > 0)
2237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2238 GNUNET_i2s (&get_path[0]));
2239 if (put_path_length > 0)
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2241 GNUNET_i2s (&put_path[0]));
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2244 for (i = 0; i < p->length; i++)
2246 struct GNUNET_PeerIdentity peer_id;
2248 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2250 GNUNET_i2s (&peer_id));
2258 * Adds a path to the peer_infos of all the peers in the path
2260 * @param p Path to process.
2261 * @param confirmed Whether we know if the path works or not.
2264 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2268 /* TODO: invert and add */
2269 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2270 for (i++; i < p->length; i++)
2272 struct MeshPeerInfo *aux;
2273 struct MeshPeerPath *copy;
2275 aux = peer_info_get_short (p->peers[i]);
2276 copy = path_duplicate (p);
2277 copy->length = i + 1;
2278 peer_info_add_path (aux, copy, GNUNET_NO);
2284 * Send keepalive packets for a peer
2286 * @param cls Closure (tunnel for which to send the keepalive).
2287 * @param tc Notification context.
2289 * TODO: implement explicit multicast keepalive?
2292 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2296 * Search for a tunnel among the incoming tunnels
2298 * @param tid the local id of the tunnel
2300 * @return tunnel handler, NULL if doesn't exist
2302 static struct MeshTunnel *
2303 tunnel_get_incoming (MESH_TunnelNumber tid)
2305 struct GNUNET_HashCode hash;
2307 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2308 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2309 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2314 * Search for a tunnel among the tunnels for a client
2316 * @param c the client whose tunnels to search in
2317 * @param tid the local id of the tunnel
2319 * @return tunnel handler, NULL if doesn't exist
2321 static struct MeshTunnel *
2322 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2324 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2326 return tunnel_get_incoming (tid);
2330 struct GNUNET_HashCode hash;
2332 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2333 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2339 * Search for a tunnel by global ID using PEER_ID
2341 * @param pi owner of the tunnel
2342 * @param tid global tunnel number
2344 * @return tunnel handler, NULL if doesn't exist
2346 static struct MeshTunnel *
2347 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2349 struct MESH_TunnelID id;
2350 struct GNUNET_HashCode hash;
2355 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2356 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2361 * Search for a tunnel by global ID using full PeerIdentities
2363 * @param oid owner of the tunnel
2364 * @param tid global tunnel number
2366 * @return tunnel handler, NULL if doesn't exist
2368 static struct MeshTunnel *
2369 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2371 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2376 * Delete an active client from the tunnel.
2382 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2386 for (i = 0; i < t->nclients; i++)
2388 if (t->clients[i] == c)
2390 t->clients[i] = t->clients[t->nclients - 1];
2391 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2392 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2394 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2402 * Delete an ignored client from the tunnel.
2408 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2412 for (i = 0; i < t->nignore; i++)
2414 if (t->ignore[i] == c)
2416 t->ignore[i] = t->ignore[t->nignore - 1];
2417 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2425 * Delete a client from the tunnel. It should be only done on
2426 * client disconnection, otherwise use client_ignore_tunnel.
2432 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2434 tunnel_delete_ignored_client (t, c);
2435 tunnel_delete_active_client (t, c);
2440 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2441 * needed rescources.
2442 * If peer already exists, reevaluate shortest path and change if different.
2444 * @param t Tunnel we want to add a new peer to
2445 * @param peer PeerInfo of the peer being added
2449 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2451 struct MeshPeerPath *best_p;
2452 struct MeshPeerPath *p;
2453 unsigned int best_cost;
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2458 if (t->peer != peer->id)
2460 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2464 if (NULL != (p = peer->path_head))
2467 best_cost = tree_get_path_cost (t->tree, p);
2470 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2477 tree_add_path (t->tree, best_p, NULL, NULL); // FIXME
2478 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2479 t->path_refresh_task =
2480 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2484 /* Start a DHT get */
2485 peer_info_connect (peer, t);
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2491 * Add a path to a tunnel which we don't own, just to remember the next hop.
2492 * If destination node was already in the tunnel, the first hop information
2493 * will be replaced with the new path.
2495 * @param t Tunnel we want to add a new peer to
2496 * @param p Path to add
2497 * @param own_pos Position of local node in path.
2501 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2502 unsigned int own_pos)
2504 struct GNUNET_PeerIdentity id;
2506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2507 GNUNET_assert (0 != own_pos);
2508 tree_add_path (t->tree, p, NULL, NULL);
2509 if (own_pos < p->length - 1)
2511 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2512 tree_update_first_hops (t->tree, myid, &id);
2514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2518 * Add a client to a tunnel, initializing all needed data structures.
2520 * @param t Tunnel to which add the client.
2521 * @param c Client which to add to the tunnel.
2524 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2526 struct MeshTunnelClientInfo clinfo;
2528 GNUNET_array_append (t->clients, t->nclients, c);
2529 clinfo.fwd_ack = t->fwd_pid + 1;
2530 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
2531 clinfo.fwd_pid = t->fwd_pid;
2532 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
2534 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
2539 * Notifies a tunnel that a connection has broken that affects at least
2540 * some of its peers. Sends a notification towards the root of the tree.
2541 * In case the peer is the owner of the tree, notifies the client that owns
2542 * the tunnel and tries to reconnect.
2544 * @param t Tunnel affected.
2545 * @param p1 Peer that got disconnected from p2.
2546 * @param p2 Peer that got disconnected from p1.
2548 * @return Short ID of the peer disconnected (either p1 or p2).
2549 * 0 if the tunnel remained unaffected.
2551 static GNUNET_PEER_Id
2552 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2555 // if (myid != p1 && myid != p2) FIXME
2560 // if (tree_get_predecessor (t->tree) != 0)
2562 // /* We are the peer still connected, notify owner of the disconnection. */
2563 // struct GNUNET_MESH_PathBroken msg;
2564 // struct GNUNET_PeerIdentity neighbor;
2566 // msg.header.size = htons (sizeof (msg));
2567 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2568 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2569 // msg.tid = htonl (t->id.tid);
2570 // msg.peer1 = my_full_id;
2571 // GNUNET_PEER_resolve (pid, &msg.peer2);
2572 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2573 // send_prebuilt_message (&msg.header, &neighbor, t);
2580 * Get the Flow Control info of a client.
2582 * @param t Tunnel on which to look.
2583 * @param c Client whose ACK to get.
2585 * @return ACK value.
2587 static struct MeshTunnelClientInfo *
2588 tunnel_get_client_fc (struct MeshTunnel *t,
2589 struct MeshClient *c)
2593 for (i = 0; i < t->nclients; i++)
2595 if (t->clients[i] != c)
2597 return &t->clients_fc[i];
2600 return NULL; // avoid compiler / coverity complaints
2605 * Set the FWD ACK value of a client in a particular tunnel.
2607 * @param t Tunnel affected.
2608 * @param c Client whose ACK to set.
2609 * @param ack ACK value.
2612 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
2613 struct MeshClient *c,
2618 for (i = 0; i < t->nclients; i++)
2620 if (t->clients[i] != c)
2622 t->clients_fc[i].fwd_ack = ack;
2630 * Get the highest ACK value of all clients in a particular tunnel,
2631 * according to the buffering/speed settings.
2633 * @param t Tunnel on which to look.
2635 * @return Corresponding ACK value (max uint32_t).
2636 * If no clients are suscribed, -1LL.
2639 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
2644 if (0 == t->nclients)
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2647 " tunnel has no clients, no FWD ACK\n");
2651 for (ack = -1LL, i = 0; i < t->nclients; i++)
2654 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack))
2656 ack = t->clients_fc[i].fwd_ack;
2660 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
2661 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
2663 return (uint32_t) ack;
2668 * Get the current fwd ack value for a tunnel, taking in account the tunnel
2669 * mode and the status of all children nodes.
2673 * @return Maximum PID allowed.
2676 tunnel_get_fwd_ack (struct MeshTunnel *t)
2680 uint32_t buffer_free;
2684 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
2685 client_ack = tunnel_get_clients_fwd_ack (t);
2686 if (GNUNET_YES == t->nobuffer)
2692 ack = count + buffer_free; // Overflow? OK!
2694 if (-1LL == client_ack)
2699 ack = GMC_max_pid ((uint32_t) client_ack, ack);
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2702 "c %u, bf %u, cl %lld, ACK: %u\n",
2703 count, buffer_free, client_ack, ack);
2709 * Build a local ACK message and send it to a local client.
2711 * @param t Tunnel on which to send the ACK.
2712 * @param c Client to whom send the ACK.
2713 * @param ack Value of the ACK.
2716 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
2718 struct GNUNET_MESH_LocalAck msg;
2720 msg.header.size = htons (sizeof (msg));
2721 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2722 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
2723 msg.max_pid = htonl (ack);
2724 GNUNET_SERVER_notification_context_unicast(nc,
2731 * Build an ACK message and queue it to send to the given peer.
2733 * @param t Tunnel on which to send the ACK.
2734 * @param peer Peer to whom send the ACK.
2735 * @param ack Value of the ACK.
2738 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
2740 struct GNUNET_MESH_ACK msg;
2742 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2743 msg.header.size = htons (sizeof (msg));
2744 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2745 msg.pid = htonl (ack);
2746 msg.tid = htonl (t->id.tid);
2748 send_prebuilt_message (&msg.header, peer, t);
2753 * Notify a the owner of a tunnel about how many more
2754 * payload packages will we accept on a given tunnel.
2756 * @param t Tunnel on which to send the ACK.
2759 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2764 "Sending client FWD ACK on tunnel %X\n",
2767 ack = tunnel_get_fwd_ack (t);
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2770 if (t->last_fwd_ack == ack)
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2777 t->last_fwd_ack = ack;
2778 send_local_ack (t, t->owner, ack);
2783 * Send an ACK informing the predecessor about the available buffer space.
2784 * In case there is no predecessor, inform the owning client.
2785 * If buffering is off, send only on behalf of children or self if endpoint.
2786 * If buffering is on, send when sent to children and buffer space is free.
2787 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2788 * the ACK itself goes "back" (towards root).
2790 * @param t Tunnel on which to send the ACK.
2791 * @param type Type of message that triggered the ACK transmission.
2794 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2796 struct GNUNET_PeerIdentity id;
2799 if (NULL != t->owner)
2801 tunnel_send_client_fwd_ack (t);
2804 /* Is it after unicast / multicast retransmission? */
2807 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2808 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2810 "ACK due to FWD DATA retransmission\n");
2811 if (GNUNET_YES == t->nobuffer)
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2817 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2818 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2820 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2821 t->force_ack = GNUNET_YES;
2827 /* Check if we need to transmit the ACK */
2828 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
2829 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
2830 GNUNET_NO == t->force_ack)
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2834 " t->qmax: %u, t->qn: %u\n",
2835 t->fwd_queue_max, t->fwd_queue_n);
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2837 " t->pid: %u, t->ack: %u\n",
2838 t->fwd_pid, t->last_fwd_ack);
2842 /* Ok, ACK might be necessary, what PID to ACK? */
2843 ack = tunnel_get_fwd_ack (t);
2845 /* If speed_min and not all children have ack'd, dont send yet */
2846 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
2848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
2852 t->last_fwd_ack = ack;
2853 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
2854 send_ack (t, &id, ack);
2856 t->force_ack = GNUNET_NO;
2861 * Iterator to send a child node a BCK ACK to allow him to send more
2864 * @param cls Closure (tunnel).
2865 * @param id Id of the child node.
2868 tunnel_send_child_bck_ack (void *cls,
2871 struct MeshTunnel *t = cls;
2872 struct GNUNET_PeerIdentity peer;
2873 uint32_t ack = 0; // FIXME
2875 GNUNET_PEER_resolve (id, &peer);
2876 // ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
2878 // if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
2880 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2881 // " Not sending ACK, not needed\n");
2884 // cinfo->bck_ack = ack;
2886 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2887 // " Sending BCK ACK %u (last sent: %u)\n",
2888 // ack, cinfo->bck_ack);
2889 send_ack (t, &peer, ack);
2894 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
2896 * Iterates over all clients and sends BCK ACKs to the ones that need it.
2898 * FIXME fc: what happens if we have 2 clients but q_size is 1?
2899 * - implement a size 1 buffer in each client_fc AND children_fc
2900 * to hold at least 1 message per "child".
2901 * problem: violates no buffer policy
2902 * - ack 0 and make "children" poll for transmission slots
2903 * problem: big overhead, extra latency even in low traffic
2906 * @param t Tunnel on which to send the BCK ACKs.
2909 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
2912 unsigned int tunnel_delta;
2914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
2916 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
2917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
2919 /* Find client whom to allow to send to origin (with lowest buffer space) */
2920 for (i = 0; i < t->nclients; i++)
2922 struct MeshTunnelClientInfo *clinfo;
2925 clinfo = &t->clients_fc[i];
2926 delta = clinfo->bck_ack - clinfo->bck_pid;
2927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
2928 t->clients[i]->id, delta);
2930 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
2931 (GNUNET_YES == t->nobuffer && 0 == delta))
2935 ack = clinfo->bck_pid;
2936 ack += t->nobuffer ? 1 : tunnel_delta;
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2938 " sending ack to client %u: %u\n",
2939 t->clients[i]->id, ack);
2940 send_local_ack (t, t->clients[i], ack);
2941 clinfo->bck_ack = ack;
2945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2946 " not sending ack to client %u (td %u, d %u)\n",
2947 t->clients[i]->id, tunnel_delta, delta);
2954 * Send an ACK informing the children nodes and destination clients about
2955 * the available buffer space.
2956 * If buffering is off, send only on behalf of root (can be self).
2957 * If buffering is on, send when sent to predecessor and buffer space is free.
2958 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2959 * the ACK itself goes "forward" (towards children/clients).
2961 * @param t Tunnel on which to send the ACK.
2962 * @param type Type of message that triggered the ACK transmission.
2965 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2968 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2969 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2970 /* Is it after data to_origin retransmission? */
2973 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2974 if (GNUNET_YES == t->nobuffer)
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2977 " Not sending ACK, nobuffer\n");
2981 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2982 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2984 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2985 t->force_ack = GNUNET_YES;
2991 tunnel_send_clients_bck_ack (t);
2992 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
2993 t->force_ack = GNUNET_NO;
2998 * @brief Re-initiate traffic to this peer if necessary.
3000 * Check if there is traffic queued towards this peer
3001 * and the core transmit handle is NULL (traffic was stalled).
3002 * If so, call core tmt rdy.
3004 * @param cls Closure (unused)
3005 * @param peer_id Short ID of peer to which initiate traffic.
3008 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3010 struct MeshPeerInfo *peer;
3011 struct GNUNET_PeerIdentity id;
3012 struct MeshPeerQueue *q;
3015 peer = peer_info_get_short(peer_id);
3016 if (NULL != peer->core_transmit)
3019 q = queue_get_next(peer);
3022 /* Might br multicast traffic already sent to this particular peer but
3023 * not to other children in this tunnel.
3024 * This way t->queue_n would be > 0 but the queue of this particular peer
3030 GNUNET_PEER_resolve (peer->id, &id);
3031 peer->core_transmit =
3032 GNUNET_CORE_notify_transmit_ready(core_handle,
3035 GNUNET_TIME_UNIT_FOREVER_REL,
3045 * @brief Allow transmission of FWD traffic on this tunnel
3047 * Check if there is traffic queued towards any children
3048 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3050 * @param t Tunnel on which to unlock FWD traffic.
3053 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3055 if (0 == t->fwd_queue_n)
3058 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3063 * @brief Allow transmission of BCK traffic on this tunnel
3065 * Check if there is traffic queued towards the root of the tree
3066 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3068 * @param t Tunnel on which to unlock BCK traffic.
3071 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3073 if (0 == t->bck_queue_n)
3076 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3081 * Send a message to all peers in this tunnel that the tunnel is no longer
3084 * @param t The tunnel whose peers to notify.
3085 * @param parent ID of the parent, in case the tree is already destroyed.
3088 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
3090 struct GNUNET_MESH_TunnelDestroy msg;
3091 struct GNUNET_PeerIdentity id;
3093 msg.header.size = htons (sizeof (msg));
3094 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3095 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3096 msg.tid = htonl (t->id.tid);
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3098 " sending tunnel destroy for tunnel: %s [%X]\n",
3099 GNUNET_i2s (&msg.oid), t->id.tid);
3101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to child\n");
3102 /* FIXME tunnel_send_multicast (t, &msg.header); */
3105 parent = tree_get_predecessor (t->tree);
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
3110 GNUNET_PEER_resolve (parent, &id);
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3112 " sending back to %s\n",
3114 send_prebuilt_message (&msg.header, &id, t);
3119 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3121 * @param cls Closure (Tunnel which to cancel).
3122 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3125 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3127 struct MeshTunnel *t = cls;
3128 struct MeshPeerInfo *peer_info;
3129 struct MeshPeerQueue *pq;
3130 struct MeshPeerQueue *next;
3132 peer_info = peer_info_get_short (neighbor_id);
3133 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3136 if (pq->tunnel == t)
3138 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
3139 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
3140 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
3142 // Should have been removed on destroy children
3145 queue_destroy (pq, GNUNET_YES);
3148 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3150 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3151 peer_info->core_transmit = NULL;
3156 * Destroy the tunnel and free any allocated resources linked to it.
3158 * @param t the tunnel to destroy
3160 * @return GNUNET_OK on success
3163 tunnel_destroy (struct MeshTunnel *t)
3165 struct MeshClient *c;
3166 struct GNUNET_HashCode hash;
3177 struct GNUNET_PeerIdentity id;
3179 GNUNET_PEER_resolve (t->id.oid, &id);
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3181 GNUNET_i2s (&id), t->id.tid);
3183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3187 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3188 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3196 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3198 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3205 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3206 for (i = 0; i < t->nclients; i++)
3210 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3216 for (i = 0; i < t->nignore; i++)
3220 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3227 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
3228 GNUNET_free_non_null (t->clients);
3229 GNUNET_free_non_null (t->ignore);
3230 GNUNET_free_non_null (t->clients_fc);
3232 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3233 tree_destroy (t->tree);
3235 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3236 GNUNET_SCHEDULER_cancel (t->timeout_task);
3237 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3238 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3241 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3246 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
3249 * Tunnel is empty: destroy it.
3251 * @param cls Closure (Tunnel).
3252 * @param tc TaskContext.
3255 tunnel_destroy_empty_delayed (void *cls,
3256 const struct GNUNET_SCHEDULER_TaskContext *tc)
3258 struct MeshTunnel *t = cls;
3260 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
3261 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3264 if (0 != t->nclients ||
3265 0 != tree_count_children (t->tree))
3270 struct GNUNET_PeerIdentity id;
3272 GNUNET_PEER_resolve (t->id.oid, &id);
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3274 "executing destruction of empty tunnel %s [%X]\n",
3275 GNUNET_i2s (&id), t->id.tid);
3279 tunnel_send_destroy (t, 0);
3280 if (0 == t->pending_messages)
3283 t->destroy = GNUNET_YES;
3288 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
3290 * @param t Tunnel to destroy if empty.
3293 tunnel_destroy_empty (struct MeshTunnel *t)
3295 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
3297 0 != tree_count_children (t->tree))
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3301 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
3307 struct GNUNET_PeerIdentity id;
3309 GNUNET_PEER_resolve (t->id.oid, &id);
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3311 "scheduling destruction of empty tunnel %s [%X]\n",
3312 GNUNET_i2s (&id), t->id.tid);
3316 t->delayed_destroy =
3317 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
3318 &tunnel_destroy_empty_delayed,
3324 * Create a new tunnel
3326 * @param owner Who is the owner of the tunnel (short ID).
3327 * @param tid Tunnel Number of the tunnel.
3328 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3329 * @param local Tunnel Number for the tunnel, for the client point of view.
3331 * @return A new initialized tunnel. NULL on error.
3333 static struct MeshTunnel *
3334 tunnel_new (GNUNET_PEER_Id owner,
3335 MESH_TunnelNumber tid,
3336 struct MeshClient *client,
3337 MESH_TunnelNumber local)
3339 struct MeshTunnel *t;
3340 struct GNUNET_HashCode hash;
3342 if (n_tunnels >= max_tunnels && NULL == client)
3345 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3348 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
3349 t->bck_queue_max = t->fwd_queue_max;
3350 t->tree = tree_new (owner);
3352 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
3353 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
3354 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
3355 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
3356 t->local_tid = local;
3358 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3360 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3362 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3370 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);
3395 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3396 * client when the client disconnects. If the client is not the owner, the
3397 * owner will get notified if no more clients are in the tunnel and the client
3398 * get removed from the tunnel's list.
3400 * @param cls closure (client that is disconnecting)
3401 * @param key the hash of the local tunnel id (used to access the hashmap)
3402 * @param value the value stored at the key (tunnel to destroy)
3404 * @return GNUNET_OK, keep iterating.
3407 tunnel_destroy_iterator (void *cls,
3408 const struct GNUNET_HashCode * key,
3411 struct MeshTunnel *t = value;
3412 struct MeshClient *c = cls;
3414 send_client_tunnel_disconnect (t, c);
3417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
3418 tunnel_delete_client (t, c);
3419 client_delete_tunnel (c, t);
3420 tunnel_destroy_empty (t);
3423 tunnel_send_destroy (t, 0);
3425 t->destroy = GNUNET_YES;
3432 * Timeout function, destroys tunnel if called
3434 * @param cls Closure (tunnel to destroy).
3435 * @param tc TaskContext
3438 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3440 struct MeshTunnel *t = cls;
3441 struct GNUNET_PeerIdentity id;
3443 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3444 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3446 GNUNET_PEER_resolve(t->id.oid, &id);
3447 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3448 "Tunnel %s [%X] timed out. Destroying.\n",
3449 GNUNET_i2s(&id), t->id.tid);
3450 send_clients_tunnel_destroy (t);
3455 * Resets the tunnel timeout. Starts it if no timeout was running.
3457 * @param t Tunnel whose timeout to reset.
3459 * TODO use heap to improve efficiency of scheduler.
3462 tunnel_reset_timeout (struct MeshTunnel *t)
3464 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3465 GNUNET_SCHEDULER_cancel (t->timeout_task);
3467 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3468 (refresh_path_time, 4), &tunnel_timeout, t);
3472 /******************************************************************************/
3473 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3474 /******************************************************************************/
3477 * Function to send a create path packet to a peer.
3479 * @param cls closure
3480 * @param size number of bytes available in buf
3481 * @param buf where the callee should write the message
3482 * @return number of bytes written to buf
3485 send_core_path_create (void *cls, size_t size, void *buf)
3487 struct MeshPathInfo *info = cls;
3488 struct GNUNET_MESH_ManipulatePath *msg;
3489 struct GNUNET_PeerIdentity *peer_ptr;
3490 struct MeshTunnel *t = info->t;
3491 struct MeshPeerPath *p = info->path;
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3498 sizeof (struct GNUNET_MESH_ManipulatePath) +
3499 p->length * sizeof (struct GNUNET_PeerIdentity);
3501 if (size < size_needed || NULL == buf)
3506 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3507 msg->header.size = htons (size_needed);
3508 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3509 msg->tid = ntohl (t->id.tid);
3512 if (GNUNET_YES == t->nobuffer)
3513 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3514 msg->opt = htonl(opt);
3517 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3518 for (i = 0; i < p->length; i++)
3520 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3527 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3533 * Fill the core buffer
3535 * @param cls closure (data itself)
3536 * @param size number of bytes available in buf
3537 * @param buf where the callee should write the message
3539 * @return number of bytes written to buf
3542 send_core_data_multicast (void *cls, size_t size, void *buf)
3544 struct MeshTransmissionDescriptor *info = cls;
3547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3548 GNUNET_assert (NULL != info);
3549 GNUNET_assert (NULL != info->peer);
3550 total_size = info->mesh_data->data_len;
3551 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3553 if (total_size > size)
3558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3559 memcpy (buf, info->mesh_data->data, total_size);
3562 struct GNUNET_MessageHeader *mh;
3565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
3566 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
3569 data_descriptor_decrement_rc (info->mesh_data);
3570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3578 * Creates a path ack message in buf and frees all unused resources.
3580 * @param cls closure (MeshTransmissionDescriptor)
3581 * @param size number of bytes available in buf
3582 * @param buf where the callee should write the message
3583 * @return number of bytes written to buf
3586 send_core_path_ack (void *cls, size_t size, void *buf)
3588 struct MeshTransmissionDescriptor *info = cls;
3589 struct GNUNET_MESH_PathACK *msg = buf;
3591 GNUNET_assert (NULL != info);
3592 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3597 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3598 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3599 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3600 msg->tid = htonl (info->origin->tid);
3601 msg->peer_id = my_full_id;
3604 /* TODO add signature */
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3607 return sizeof (struct GNUNET_MESH_PathACK);
3612 * Free a transmission that was already queued with all resources
3613 * associated to the request.
3615 * @param queue Queue handler to cancel.
3616 * @param clear_cls Is it necessary to free associated cls?
3619 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3621 struct MeshTransmissionDescriptor *dd;
3622 struct MeshPathInfo *path_info;
3623 // unsigned int max;
3625 if (GNUNET_YES == clear_cls)
3627 switch (queue->type)
3629 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3630 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3631 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3633 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3634 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3635 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3636 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3637 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3639 " prebuilt message\n");
3640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3642 GNUNET_MESH_DEBUG_M2S(queue->type));
3644 data_descriptor_decrement_rc (dd->mesh_data);
3646 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3648 path_info = queue->cls;
3649 path_destroy (path_info->path);
3653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3654 " type %s unknown!\n",
3655 GNUNET_MESH_DEBUG_M2S(queue->type));
3657 GNUNET_free_non_null (queue->cls);
3659 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3660 queue->peer->queue_tail,
3663 /* Delete from child_fc in the appropiate tunnel */
3664 // max = queue->tunnel->fwd_queue_max;
3665 // GNUNET_PEER_resolve (queue->peer->id, &id);
3666 // if (NULL != cinfo)
3668 // for (i = 0; i < cinfo->send_buffer_n; i++)
3670 // i2 = (cinfo->send_buffer_start + i) % max;
3671 // if (cinfo->send_buffer[i2] == queue)
3673 // /* Found corresponding entry in the send_buffer. Move all others back. */
3677 // for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
3679 // j2 = (cinfo->send_buffer_start + j) % max;
3680 // j3 = (cinfo->send_buffer_start + j + 1) % max;
3681 // cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
3684 // cinfo->send_buffer[j3] = NULL;
3685 // cinfo->send_buffer_n--;
3690 GNUNET_free (queue);
3695 * @brief Get the next transmittable message from the queue.
3697 * This will be the head, except in the case of being a data packet
3698 * not allowed by the destination peer.
3700 * @param peer Destination peer.
3702 * @return The next viable MeshPeerQueue element to send to that peer.
3703 * NULL when there are no transmittable messages.
3705 struct MeshPeerQueue *
3706 queue_get_next (const struct MeshPeerInfo *peer)
3708 struct MeshPeerQueue *q;
3710 struct MeshTransmissionDescriptor *info;
3711 // struct GNUNET_MESH_Unicast *ucast;
3712 // struct GNUNET_MESH_ToOrigin *to_orig;
3713 struct GNUNET_PeerIdentity id;
3715 // uint32_t ack; FIXME
3717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
3718 for (q = peer->queue_head; NULL != q; q = q->next)
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3724 GNUNET_MESH_DEBUG_M2S(q->type));
3727 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3728 // ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
3729 // pid = ntohl (ucast->pid);
3730 GNUNET_PEER_resolve (info->peer->id, &id);
3731 // ack = cinfo->fwd_ack;
3733 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3734 // to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
3735 // pid = ntohl (to_orig->pid);
3736 // ack = t->bck_ack;
3739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3743 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3744 // "********* ACK: %u, PID: %u\n",
3746 // if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
3748 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3749 // "********* OK!\n");
3754 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3755 // "********* NEXT!\n");
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3759 "********* nothing found\n");
3765 * Core callback to write a queued packet to core buffer
3767 * @param cls Closure (peer info).
3768 * @param size Number of bytes available in buf.
3769 * @param buf Where the to write the message.
3771 * @return number of bytes written to buf
3774 queue_send (void *cls, size_t size, void *buf)
3776 struct MeshPeerInfo *peer = cls;
3777 struct GNUNET_MessageHeader *msg;
3778 struct MeshPeerQueue *queue;
3779 struct MeshTunnel *t;
3780 struct GNUNET_PeerIdentity dst_id;
3783 peer->core_transmit = NULL;
3785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3786 queue = queue_get_next (peer);
3788 /* Queue has no internal mesh traffic nor sendable payload */
3791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
3792 if (NULL == peer->queue_head)
3793 GNUNET_break (0); // Should've been canceled
3796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3798 GNUNET_PEER_resolve (peer->id, &dst_id);
3799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3800 "********* towards %s\n",
3801 GNUNET_i2s(&dst_id));
3802 /* Check if buffer size is enough for the message */
3803 if (queue->size > size)
3805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3806 "********* not enough room, reissue\n");
3807 peer->core_transmit =
3808 GNUNET_CORE_notify_transmit_ready (core_handle,
3811 GNUNET_TIME_UNIT_FOREVER_REL,
3818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3821 GNUNET_assert (0 < t->pending_messages);
3822 t->pending_messages--;
3823 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
3826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3827 "********* unicast: t->q (%u/%u)\n",
3828 t->fwd_queue_n, t->fwd_queue_max);
3830 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
3833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
3837 switch (queue->type)
3840 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3841 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3842 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3843 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3844 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3846 "********* raw: %s\n",
3847 GNUNET_MESH_DEBUG_M2S (queue->type));
3849 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3850 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3851 data_size = send_core_data_raw (queue->cls, size, buf);
3852 msg = (struct GNUNET_MessageHeader *) buf;
3853 switch (ntohs (msg->type)) // Type of preconstructed message
3855 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3856 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3858 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3859 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3865 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3867 data_size = send_core_path_create (queue->cls, size, buf);
3869 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3871 data_size = send_core_path_ack (queue->cls, size, buf);
3873 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
3875 data_size = send_core_data_multicast (queue->cls, size, buf);
3879 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3880 "********* type unknown: %u\n",
3884 switch (queue->type)
3886 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3887 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3888 // if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
3890 // GNUNET_break (0);
3891 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3892 // "at pos %u (%p) != %p\n",
3893 // cinfo->send_buffer_start,
3894 // cinfo->send_buffer[cinfo->send_buffer_start],
3897 // if (cinfo->send_buffer_n > 0)
3899 // cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
3900 // cinfo->send_buffer_n--;
3901 // cinfo->send_buffer_start++;
3902 // cinfo->send_buffer_start %= t->fwd_queue_max;
3906 // GNUNET_break (0);
3913 /* Free queue, but cls was freed by send_core_* */
3914 queue_destroy (queue, GNUNET_NO);
3916 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3922 /* If more data in queue, send next */
3923 queue = queue_get_next(peer);
3926 struct GNUNET_PeerIdentity id;
3928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3929 GNUNET_PEER_resolve (peer->id, &id);
3930 peer->core_transmit =
3931 GNUNET_CORE_notify_transmit_ready(core_handle,
3934 GNUNET_TIME_UNIT_FOREVER_REL,
3942 if (NULL != peer->queue_head)
3944 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3945 "********* %s stalled\n",
3946 GNUNET_i2s(&my_full_id));
3947 // if (NULL == cinfo) FIXME
3948 // cinfo = tunnel_get_neighbor_fc (t, &dst_id);
3949 // // FIXME unify bck/fwd structures, bck does not have cinfo right now
3950 // if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
3952 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3953 // &tunnel_poll, cinfo);
3957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3963 * @brief Queue and pass message to core when possible.
3965 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
3966 * and accounts for it. In case the queue is full, the message is dropped and
3969 * Otherwise, message is treated as internal and allowed to go regardless of
3972 * @param cls Closure (@c type dependant). It will be used by queue_send to
3973 * build the message to be sent if not already prebuilt.
3974 * @param type Type of the message, 0 for a raw message.
3975 * @param size Size of the message.
3976 * @param dst Neighbor to send message to.
3977 * @param t Tunnel this message belongs to.
3980 queue_add (void *cls, uint16_t type, size_t size,
3981 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3983 struct MeshPeerQueue *queue;
3984 struct GNUNET_PeerIdentity id;
3989 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
3990 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
3992 n = &t->fwd_queue_n;
3993 max = &t->fwd_queue_max;
3995 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3997 n = &t->bck_queue_n;
3998 max = &t->bck_queue_max;
4005 GNUNET_STATISTICS_update(stats,
4006 "# messages dropped (buffer full)",
4008 return; // Drop message
4012 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4018 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4019 GNUNET_PEER_resolve (dst->id, &id);
4020 if (NULL == dst->core_transmit)
4022 dst->core_transmit =
4023 GNUNET_CORE_notify_transmit_ready (core_handle,
4026 GNUNET_TIME_UNIT_FOREVER_REL,
4032 t->pending_messages++;
4033 if (NULL == n) // Is this internal mesh traffic?
4036 // It's payload, keep track of buffer per peer. FIXME
4037 // i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4038 // if (NULL != cinfo->send_buffer[i])
4040 // GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4041 // queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4042 // cinfo->send_buffer_start++;
4043 // cinfo->send_buffer_start %= t->fwd_queue_max;
4047 // cinfo->send_buffer_n++;
4049 // cinfo->send_buffer[i] = queue;
4050 // if (cinfo->send_buffer_n > t->fwd_queue_max)
4052 // GNUNET_break (0);
4053 // cinfo->send_buffer_n = t->fwd_queue_max;
4058 /******************************************************************************/
4059 /******************** MESH NETWORK HANDLERS **************************/
4060 /******************************************************************************/
4064 * Core handler for path creation
4066 * @param cls closure
4067 * @param message message
4068 * @param peer peer identity this notification is about
4070 * @return GNUNET_OK to keep the connection open,
4071 * GNUNET_SYSERR to close it (signal serious error)
4074 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4075 const struct GNUNET_MessageHeader *message)
4077 unsigned int own_pos;
4080 MESH_TunnelNumber tid;
4081 struct GNUNET_MESH_ManipulatePath *msg;
4082 struct GNUNET_PeerIdentity *pi;
4083 struct GNUNET_HashCode hash;
4084 struct MeshPeerPath *path;
4085 struct MeshPeerInfo *dest_peer_info;
4086 struct MeshPeerInfo *orig_peer_info;
4087 struct MeshTunnel *t;
4089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4090 "Received a path create msg [%s]\n",
4091 GNUNET_i2s (&my_full_id));
4092 size = ntohs (message->size);
4093 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4095 GNUNET_break_op (0);
4099 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4100 if (size % sizeof (struct GNUNET_PeerIdentity))
4102 GNUNET_break_op (0);
4105 size /= sizeof (struct GNUNET_PeerIdentity);
4108 GNUNET_break_op (0);
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4112 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4114 tid = ntohl (msg->tid);
4115 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4117 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4118 t = tunnel_get (pi, tid);
4119 if (NULL == t) // FIXME only for INCOMING tunnels?
4123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4124 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4127 // FIXME notify failure
4130 opt = ntohl (msg->opt);
4131 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
4133 t->nobuffer = GNUNET_YES;
4134 t->last_fwd_ack = t->fwd_pid + 1;
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
4138 if (GNUNET_YES == t->nobuffer)
4140 t->bck_queue_max = 1;
4141 t->fwd_queue_max = 1;
4144 // FIXME only assign a local tid if a local client is interested (on demand)
4145 while (NULL != tunnel_get_incoming (next_local_tid))
4146 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4147 t->local_tid_dest = next_local_tid++;
4148 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4151 tunnel_reset_timeout (t);
4152 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4154 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4155 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4163 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4164 if (NULL == dest_peer_info)
4166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4167 " Creating PeerInfo for destination.\n");
4168 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4169 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4170 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4172 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4174 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4175 if (NULL == orig_peer_info)
4177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4178 " Creating PeerInfo for origin.\n");
4179 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4180 orig_peer_info->id = GNUNET_PEER_intern (pi);
4181 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4182 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4185 path = path_new (size);
4187 for (i = 0; i < size; i++)
4189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4190 GNUNET_i2s (&pi[i]));
4191 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4192 if (path->peers[i] == myid)
4195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4198 /* cannot be self, must be 'not found' */
4199 /* create path: self not found in path through self */
4200 GNUNET_break_op (0);
4201 path_destroy (path);
4205 path_add_to_peers (path, GNUNET_NO);
4206 tunnel_add_path (t, path, own_pos);
4207 if (own_pos == size - 1)
4209 /* It is for us! Send ack. */
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4211 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4217 struct MeshPeerPath *path2;
4219 /* It's for somebody else! Retransmit. */
4220 path2 = path_duplicate (path);
4221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4222 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4223 path2 = path_duplicate (path);
4224 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4225 send_create_path (dest_peer_info, path, t);
4232 * Core handler for path destruction
4234 * @param cls closure
4235 * @param message message
4236 * @param peer peer identity this notification is about
4238 * @return GNUNET_OK to keep the connection open,
4239 * GNUNET_SYSERR to close it (signal serious error)
4242 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4243 const struct GNUNET_MessageHeader *message)
4245 struct GNUNET_MESH_ManipulatePath *msg;
4246 struct GNUNET_PeerIdentity *pi;
4247 struct MeshPeerPath *path;
4248 struct MeshTunnel *t;
4249 unsigned int own_pos;
4253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4254 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4255 size = ntohs (message->size);
4256 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4258 GNUNET_break_op (0);
4262 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4263 if (size % sizeof (struct GNUNET_PeerIdentity))
4265 GNUNET_break_op (0);
4268 size /= sizeof (struct GNUNET_PeerIdentity);
4271 GNUNET_break_op (0);
4274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4276 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4277 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4279 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4281 t = tunnel_get (pi, ntohl (msg->tid));
4284 /* TODO notify back: we don't know this tunnel */
4285 GNUNET_break_op (0);
4288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4289 path = path_new (size);
4291 for (i = 0; i < size; i++)
4293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4294 GNUNET_i2s (&pi[i]));
4295 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4296 if (path->peers[i] == myid)
4299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4300 if (own_pos < path->length - 1)
4301 send_prebuilt_message (message, &pi[own_pos + 1], t);
4303 send_client_tunnel_disconnect(t, NULL);
4305 // tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
4306 path_destroy (path);
4312 * Core handler for notifications of broken paths
4314 * @param cls closure
4315 * @param message message
4316 * @param peer peer identity this notification is about
4318 * @return GNUNET_OK to keep the connection open,
4319 * GNUNET_SYSERR to close it (signal serious error)
4322 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4323 const struct GNUNET_MessageHeader *message)
4325 struct GNUNET_MESH_PathBroken *msg;
4326 struct MeshTunnel *t;
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4329 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4330 msg = (struct GNUNET_MESH_PathBroken *) message;
4331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4332 GNUNET_i2s (&msg->peer1));
4333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4334 GNUNET_i2s (&msg->peer2));
4335 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4338 GNUNET_break_op (0);
4341 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4342 GNUNET_PEER_search (&msg->peer2));
4349 * Core handler for tunnel destruction
4351 * @param cls closure
4352 * @param message message
4353 * @param peer peer identity this notification is about
4355 * @return GNUNET_OK to keep the connection open,
4356 * GNUNET_SYSERR to close it (signal serious error)
4359 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4360 const struct GNUNET_MessageHeader *message)
4362 struct GNUNET_MESH_TunnelDestroy *msg;
4363 struct MeshTunnel *t;
4364 GNUNET_PEER_Id parent;
4367 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4369 "Got a TUNNEL DESTROY packet from %s\n",
4371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4372 " for tunnel %s [%u]\n",
4373 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4374 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4375 /* Check signature */
4378 /* Probably already got the message from another path,
4379 * destroyed the tunnel and retransmitted to children.
4382 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4386 parent = tree_get_predecessor (t->tree);
4387 pid = GNUNET_PEER_search (peer);
4392 // tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
4393 nc = tree_count_children (t->tree);
4394 if (nc > 0 || NULL != t->owner || t->nclients > 0)
4396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4397 "still in use: %u cl, %u ch\n",
4402 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4404 /* Tunnel was incoming, notify clients */
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4406 t->local_tid, t->local_tid_dest);
4407 send_clients_tunnel_destroy (t);
4409 tunnel_send_destroy (t, parent);
4410 t->destroy = GNUNET_YES;
4411 // TODO: add timeout to destroy the tunnel anyway
4417 * Core handler for mesh network traffic going from the origin to a peer
4419 * @param cls closure
4420 * @param peer peer identity this notification is about
4421 * @param message message
4422 * @return GNUNET_OK to keep the connection open,
4423 * GNUNET_SYSERR to close it (signal serious error)
4426 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4427 const struct GNUNET_MessageHeader *message)
4429 struct GNUNET_MESH_Unicast *msg;
4430 struct GNUNET_PeerIdentity *neighbor;
4431 struct MeshTunnel *t;
4432 GNUNET_PEER_Id dest_id;
4437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4440 size = ntohs (message->size);
4442 sizeof (struct GNUNET_MESH_Unicast) +
4443 sizeof (struct GNUNET_MessageHeader))
4448 msg = (struct GNUNET_MESH_Unicast *) message;
4449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4450 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4452 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4455 /* TODO notify back: we don't know this tunnel */
4456 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4457 GNUNET_break_op (0);
4460 pid = ntohl (msg->pid);
4461 if (t->fwd_pid == pid)
4463 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4464 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4465 " Already seen pid %u, DROPPING!\n", pid);
4470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4471 " pid %u not seen yet, forwarding\n", pid);
4476 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4478 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4479 GNUNET_break_op (0);
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4481 "Received PID %u, ACK %u\n",
4482 pid, t->last_fwd_ack);
4486 tunnel_reset_timeout (t);
4488 if (dest_id == myid)
4490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4491 " it's for us! sending to clients...\n");
4492 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4493 send_subscribed_clients (message, &msg[1].header, t);
4494 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4497 ttl = ntohl (msg->ttl);
4498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4501 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4502 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4503 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4507 " not for us, retransmitting...\n");
4509 neighbor = tree_get_first_hop (t->tree, dest_id);
4511 /* cinfo->fwd_pid = pid; FIXME
4513 if (GNUNET_YES == t->nobuffer &&
4514 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
4516 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4517 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
4518 GNUNET_break_op (0);
4521 send_prebuilt_message (message, neighbor, t);
4522 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4528 * Core handler for mesh network traffic toward the owner of a tunnel
4530 * @param cls closure
4531 * @param message message
4532 * @param peer peer identity this notification is about
4534 * @return GNUNET_OK to keep the connection open,
4535 * GNUNET_SYSERR to close it (signal serious error)
4538 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4539 const struct GNUNET_MessageHeader *message)
4541 struct GNUNET_MESH_ToOrigin *msg;
4542 struct GNUNET_PeerIdentity id;
4543 struct MeshPeerInfo *peer_info;
4544 struct MeshTunnel *t;
4545 GNUNET_PEER_Id predecessor;
4549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4551 size = ntohs (message->size);
4552 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4553 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4555 GNUNET_break_op (0);
4558 msg = (struct GNUNET_MESH_ToOrigin *) message;
4559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4560 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4561 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4562 pid = ntohl (msg->pid);
4566 /* TODO notify that we dont know this tunnel (whom)? */
4567 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4569 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4570 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4575 // if (cinfo->bck_pid == pid) FIXME
4577 // /* already seen this packet, drop */
4578 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4579 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4580 // " Already seen pid %u, DROPPING!\n", pid);
4581 // tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4582 // return GNUNET_OK;
4585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4586 " pid %u not seen yet, forwarding\n", pid);
4587 // cinfo->bck_pid = pid; FIXME
4589 if (NULL != t->owner)
4592 struct GNUNET_MESH_ToOrigin *copy;
4594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4595 " it's for us! sending to clients...\n");
4596 /* TODO signature verification */
4597 memcpy (cbuf, message, size);
4598 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4599 copy->tid = htonl (t->local_tid);
4601 copy->pid = htonl (t->bck_pid);
4602 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4603 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4604 ©->header, GNUNET_NO);
4605 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4609 " not for us, retransmitting...\n");
4611 peer_info = peer_info_get (&msg->oid);
4612 if (NULL == peer_info)
4614 /* unknown origin of tunnel */
4618 predecessor = tree_get_predecessor (t->tree);
4619 if (0 == predecessor)
4621 if (GNUNET_YES == t->destroy)
4623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4624 "to orig received on a dying tunnel %s [%X]\n",
4625 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4628 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4629 "unknown to origin at %s\n",
4630 GNUNET_i2s (&my_full_id));
4631 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4634 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4635 "for tunnel %s [%X]\n",
4636 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4637 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4639 tree_debug (t->tree);
4642 GNUNET_PEER_resolve (predecessor, &id);
4643 send_prebuilt_message (message, &id, t);
4644 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4651 * Core handler for mesh network traffic point-to-point acks.
4653 * @param cls closure
4654 * @param message message
4655 * @param peer peer identity this notification is about
4657 * @return GNUNET_OK to keep the connection open,
4658 * GNUNET_SYSERR to close it (signal serious error)
4661 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4662 const struct GNUNET_MessageHeader *message)
4664 struct GNUNET_MESH_ACK *msg;
4665 struct MeshTunnel *t;
4668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4670 msg = (struct GNUNET_MESH_ACK *) message;
4672 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4676 /* TODO notify that we dont know this tunnel (whom)? */
4677 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4680 ack = ntohl (msg->pid);
4681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4683 /* Is this a forward or backward ACK? */
4684 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
4688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4689 // cinfo->fwd_ack = ack; FIXME
4690 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4691 tunnel_unlock_fwd_queues (t);
4692 // if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll) FIXME
4694 // GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
4695 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
4696 // cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
4701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4703 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4704 tunnel_unlock_bck_queue (t);
4711 * Core handler for mesh network traffic point-to-point ack polls.
4713 * @param cls closure
4714 * @param message message
4715 * @param peer peer identity this notification is about
4717 * @return GNUNET_OK to keep the connection open,
4718 * GNUNET_SYSERR to close it (signal serious error)
4721 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4722 const struct GNUNET_MessageHeader *message)
4724 struct GNUNET_MESH_Poll *msg;
4725 struct MeshTunnel *t;
4727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4730 msg = (struct GNUNET_MESH_Poll *) message;
4732 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4736 /* TODO notify that we dont know this tunnel (whom)? */
4737 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4738 GNUNET_break_op (0);
4742 /* Is this a forward or backward ACK? */
4743 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4746 /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
4747 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4752 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4759 * Core handler for path ACKs
4761 * @param cls closure
4762 * @param message message
4763 * @param peer peer identity this notification is about
4765 * @return GNUNET_OK to keep the connection open,
4766 * GNUNET_SYSERR to close it (signal serious error)
4769 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4770 const struct GNUNET_MessageHeader *message)
4772 struct GNUNET_MESH_PathACK *msg;
4773 struct GNUNET_PeerIdentity id;
4774 struct MeshPeerInfo *peer_info;
4775 struct MeshPeerPath *p;
4776 struct MeshTunnel *t;
4778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4779 GNUNET_i2s (&my_full_id));
4780 msg = (struct GNUNET_MESH_PathACK *) message;
4781 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4784 /* TODO notify that we don't know the tunnel */
4785 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4787 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4791 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4793 peer_info = peer_info_get (&msg->peer_id);
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4795 GNUNET_i2s (&msg->peer_id));
4796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4799 /* Add paths to peers? */
4800 p = tree_get_path_to_peer (t->tree, peer_info->id);
4803 path_add_to_peers (p, GNUNET_YES);
4811 /* Message for us? */
4812 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4815 if (NULL == t->owner)
4817 GNUNET_break_op (0);
4820 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4822 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4823 // send_client_peer_connected (t, peer_info->id);
4825 if (NULL != peer_info->dhtget)
4827 GNUNET_DHT_get_stop (peer_info->dhtget);
4828 peer_info->dhtget = NULL;
4833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4834 " not for us, retransmitting...\n");
4835 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4836 peer_info = peer_info_get (&msg->oid);
4837 send_prebuilt_message (message, &id, t);
4843 * Core handler for mesh keepalives.
4845 * @param cls closure
4846 * @param message message
4847 * @param peer peer identity this notification is about
4848 * @return GNUNET_OK to keep the connection open,
4849 * GNUNET_SYSERR to close it (signal serious error)
4851 * TODO: Check who we got this from, to validate route.
4854 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4855 const struct GNUNET_MessageHeader *message)
4857 struct GNUNET_MESH_TunnelKeepAlive *msg;
4858 struct MeshTunnel *t;
4860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4863 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4864 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4868 /* TODO notify that we dont know that tunnel */
4869 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4874 tunnel_reset_timeout (t);
4876 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4877 /* FIXME tunnel_send_multicast (t, message); */
4884 * Functions to handle messages from core
4886 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4887 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4888 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4889 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4890 sizeof (struct GNUNET_MESH_PathBroken)},
4891 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4892 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4893 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4894 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4895 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4896 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4897 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4898 sizeof (struct GNUNET_MESH_ACK)},
4899 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4900 sizeof (struct GNUNET_MESH_Poll)},
4901 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4902 sizeof (struct GNUNET_MESH_PathACK)},
4908 /******************************************************************************/
4909 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4910 /******************************************************************************/
4915 * notify_client_connection_failure: notify a client that the connection to the
4916 * requested remote peer is not possible (for instance, no route found)
4917 * Function called when the socket is ready to queue more data. "buf" will be
4918 * NULL and "size" zero if the socket was closed for writing in the meantime.
4920 * @param cls closure
4921 * @param size number of bytes available in buf
4922 * @param buf where the callee should write the message
4923 * @return number of bytes written to buf
4926 notify_client_connection_failure (void *cls, size_t size, void *buf)
4929 struct MeshPeerInfo *peer_info;
4930 struct GNUNET_MESH_PeerControl *msg;
4931 struct GNUNET_PeerIdentity id;
4933 if (0 == size && NULL == buf)
4935 // TODO retry? cancel?
4939 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4940 peer_info = (struct MeshPeerInfo *) cls;
4941 msg = (struct GNUNET_MESH_PeerControl *) buf;
4942 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4943 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4944 // msg->tunnel_id = htonl(peer_info->t->tid);
4945 GNUNET_PEER_resolve (peer_info->id, &id);
4946 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4954 * Send keepalive packets for a peer
4956 * @param cls Closure (tunnel for which to send the keepalive).
4957 * @param tc Notification context.
4960 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4962 struct MeshTunnel *t = cls;
4963 struct GNUNET_MESH_TunnelKeepAlive *msg;
4964 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4967 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4968 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4974 "sending keepalive for tunnel %d\n", t->id.tid);
4976 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4977 msg->header.size = htons (size);
4978 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4979 msg->oid = my_full_id;
4980 msg->tid = htonl (t->id.tid);
4981 /* FIXME tunnel_send_multicast (t, &msg->header); */
4983 t->path_refresh_task =
4984 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4985 tunnel_reset_timeout(t);
4990 * Function to process paths received for a new peer addition. The recorded
4991 * paths form the initial tunnel, which can be optimized later.
4992 * Called on each result obtained for the DHT search.
4994 * @param cls closure
4995 * @param exp when will this value expire
4996 * @param key key of the result
4997 * @param get_path path of the get request
4998 * @param get_path_length lenght of get_path
4999 * @param put_path path of the put request
5000 * @param put_path_length length of the put_path
5001 * @param type type of the result
5002 * @param size number of bytes in data
5003 * @param data pointer to the result data
5005 * TODO: re-issue the request after certain time? cancel after X results?
5008 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5009 const struct GNUNET_HashCode * key,
5010 const struct GNUNET_PeerIdentity *get_path,
5011 unsigned int get_path_length,
5012 const struct GNUNET_PeerIdentity *put_path,
5013 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5014 size_t size, const void *data)
5016 struct MeshPathInfo *path_info = cls;
5017 struct MeshPeerPath *p;
5018 struct GNUNET_PeerIdentity pi;
5021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5022 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5025 p = path_build_from_dht (get_path, get_path_length,
5026 put_path, put_path_length);
5027 path_add_to_peers (p, GNUNET_NO);
5029 for (i = 0; i < path_info->peer->ntunnels; i++)
5031 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5032 peer_info_connect (path_info->peer, path_info->t);
5039 /******************************************************************************/
5040 /********************* MESH LOCAL HANDLES **************************/
5041 /******************************************************************************/
5045 * Handler for client disconnection
5047 * @param cls closure
5048 * @param client identification of the client; NULL
5049 * for the last call when the server is destroyed
5052 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5054 struct MeshClient *c;
5055 struct MeshClient *next;
5058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5068 if (c->handle != client)
5070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5076 GNUNET_SERVER_client_drop (c->handle);
5077 c->shutting_down = GNUNET_YES;
5078 GNUNET_assert (NULL != c->own_tunnels);
5079 GNUNET_assert (NULL != c->incoming_tunnels);
5080 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5081 &tunnel_destroy_iterator, c);
5082 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5083 &tunnel_destroy_iterator, c);
5084 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5085 &tunnel_destroy_iterator, c);
5086 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5087 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5088 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5090 if (NULL != c->types)
5091 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5092 for (i = 0; i < c->n_regex; i++)
5094 GNUNET_free (c->regexes[i].regex);
5095 if (NULL != c->regexes[i].h)
5096 GNUNET_REGEX_announce_cancel (c->regexes[i].h);
5098 GNUNET_free_non_null (c->regexes);
5099 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5100 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5102 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5105 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5114 * Handler for new clients
5116 * @param cls closure
5117 * @param client identification of the client
5118 * @param message the actual message, which includes messages the client wants
5121 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5122 const struct GNUNET_MessageHeader *message)
5124 struct GNUNET_MESH_ClientConnect *cc_msg;
5125 struct MeshClient *c;
5131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5132 /* Check data sanity */
5133 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5134 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5135 ntypes = ntohs (cc_msg->types);
5136 if (size != ntypes * sizeof (uint16_t))
5139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5143 /* Create new client structure */
5144 c = GNUNET_malloc (sizeof (struct MeshClient));
5145 c->id = next_client_id++;
5146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5148 GNUNET_SERVER_client_keep (client);
5152 struct GNUNET_HashCode hc;
5154 t = (uint16_t *) &cc_msg[1];
5155 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
5156 for (i = 0; i < ntypes; i++)
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5160 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5162 /* store in clients hashmap */
5163 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5164 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5165 /* store in global hashmap */
5166 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
5172 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5173 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5174 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5175 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5176 GNUNET_SERVER_notification_context_add (nc, client);
5177 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5179 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5185 * Handler for requests of new tunnels
5187 * @param cls closure
5188 * @param client identification of the client
5189 * @param message the actual message
5192 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5193 const struct GNUNET_MessageHeader *message)
5195 struct GNUNET_MESH_TunnelMessage *t_msg;
5196 struct MeshTunnel *t;
5197 struct MeshClient *c;
5198 MESH_TunnelNumber tid;
5200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5202 /* Sanity check for client registration */
5203 if (NULL == (c = client_get (client)))
5206 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5211 /* Message sanity check */
5212 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5219 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5220 /* Sanity check for tunnel numbering */
5221 tid = ntohl (t_msg->tunnel_id);
5222 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5225 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5228 /* Sanity check for duplicate tunnel IDs */
5229 if (NULL != tunnel_get_by_local_id (c, tid))
5232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5236 while (NULL != tunnel_get_by_pi (myid, next_tid))
5237 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5238 t = tunnel_new (myid, next_tid++, c, tid);
5241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5243 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5246 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5248 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5251 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5257 * Handler for requests of deleting tunnels
5259 * @param cls closure
5260 * @param client identification of the client
5261 * @param message the actual message
5264 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5265 const struct GNUNET_MessageHeader *message)
5267 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5268 struct MeshClient *c;
5269 struct MeshTunnel *t;
5270 MESH_TunnelNumber tid;
5272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5273 "Got a DESTROY TUNNEL from client!\n");
5275 /* Sanity check for client registration */
5276 if (NULL == (c = client_get (client)))
5279 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5284 /* Message sanity check */
5285 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5292 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5294 /* Retrieve tunnel */
5295 tid = ntohl (tunnel_msg->tunnel_id);
5296 t = tunnel_get_by_local_id(c, tid);
5299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5301 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5304 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5306 client_ignore_tunnel (c, t);
5307 tunnel_destroy_empty (t);
5308 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5311 send_client_tunnel_disconnect (t, c);
5312 client_delete_tunnel (c, t);
5314 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5316 tunnel_send_destroy (t, 0);
5317 t->destroy = GNUNET_YES;
5318 /* The tunnel will be destroyed when the last message is transmitted. */
5319 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5325 * Handler for requests of seeting tunnel's buffering policy.
5327 * @param cls Closure (unused).
5328 * @param client Identification of the client.
5329 * @param message The actual message.
5332 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5333 const struct GNUNET_MessageHeader *message)
5335 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5336 struct MeshClient *c;
5337 struct MeshTunnel *t;
5338 MESH_TunnelNumber tid;
5340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5341 "Got a BUFFER request from client!\n");
5343 /* Sanity check for client registration */
5344 if (NULL == (c = client_get (client)))
5347 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5352 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5354 /* Retrieve tunnel */
5355 tid = ntohl (tunnel_msg->tunnel_id);
5356 t = tunnel_get_by_local_id(c, tid);
5359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5361 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5365 switch (ntohs(message->type))
5367 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5368 t->nobuffer = GNUNET_NO;
5370 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5371 t->nobuffer = GNUNET_YES;
5377 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5382 * Handler for client traffic directed to one peer
5384 * @param cls closure
5385 * @param client identification of the client
5386 * @param message the actual message
5389 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5390 const struct GNUNET_MessageHeader *message)
5392 struct MeshClient *c;
5393 struct MeshTunnel *t;
5394 struct GNUNET_MESH_Unicast *data_msg;
5395 MESH_TunnelNumber tid;
5398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5399 "Got a unicast request from a client!\n");
5401 /* Sanity check for client registration */
5402 if (NULL == (c = client_get (client)))
5405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5410 data_msg = (struct GNUNET_MESH_Unicast *) message;
5412 /* Sanity check for message size */
5413 size = ntohs (message->size);
5414 if (sizeof (struct GNUNET_MESH_Unicast) +
5415 sizeof (struct GNUNET_MessageHeader) > size)
5418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5422 /* Tunnel exists? */
5423 tid = ntohl (data_msg->tid);
5424 t = tunnel_get_by_local_id (c, tid);
5428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5432 /* Is it a local tunnel? Then, does client own the tunnel? */
5433 if (t->owner->handle != client)
5436 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5440 /* PID should be as expected */
5441 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
5444 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5445 "Unicast PID, expected %u, got %u\n",
5446 t->fwd_pid + 1, ntohl (data_msg->pid));
5447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5451 /* Ok, everything is correct, send the message
5452 * (pretend we got it from a mesh peer)
5455 /* Work around const limitation */
5456 char buf[ntohs (message->size)] GNUNET_ALIGN;
5457 struct GNUNET_MESH_Unicast *copy;
5459 copy = (struct GNUNET_MESH_Unicast *) buf;
5460 memcpy (buf, data_msg, size);
5461 copy->oid = my_full_id;
5462 copy->tid = htonl (t->id.tid);
5463 copy->ttl = htonl (default_ttl);
5464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5465 " calling generic handler...\n");
5466 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
5468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5469 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5476 * Handler for client traffic directed to the origin
5478 * @param cls closure
5479 * @param client identification of the client
5480 * @param message the actual message
5483 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5484 const struct GNUNET_MessageHeader *message)
5486 struct GNUNET_MESH_ToOrigin *data_msg;
5487 struct MeshTunnelClientInfo *clinfo;
5488 struct MeshClient *c;
5489 struct MeshTunnel *t;
5490 MESH_TunnelNumber tid;
5493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5494 "Got a ToOrigin request from a client!\n");
5495 /* Sanity check for client registration */
5496 if (NULL == (c = client_get (client)))
5499 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5504 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5506 /* Sanity check for message size */
5507 size = ntohs (message->size);
5508 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5509 sizeof (struct GNUNET_MessageHeader) > size)
5512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5516 /* Tunnel exists? */
5517 tid = ntohl (data_msg->tid);
5518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5519 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5525 t = tunnel_get_by_local_id (c, tid);
5528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5529 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5535 /* It should be sent by someone who has this as incoming tunnel. */
5536 if (GNUNET_NO == client_knows_tunnel (c, t))
5539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5543 /* PID should be as expected */
5544 clinfo = tunnel_get_client_fc (t, c);
5545 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
5548 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5549 "To Origin PID, expected %u, got %u\n",
5550 clinfo->bck_pid + 1,
5551 ntohl (data_msg->pid));
5552 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5557 /* Ok, everything is correct, send the message
5558 * (pretend we got it from a mesh peer)
5561 char buf[ntohs (message->size)] GNUNET_ALIGN;
5562 struct GNUNET_MESH_ToOrigin *copy;
5564 /* Work around const limitation */
5565 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5566 memcpy (buf, data_msg, size);
5567 GNUNET_PEER_resolve (t->id.oid, ©->oid);
5568 copy->tid = htonl (t->id.tid);
5569 copy->ttl = htonl (default_ttl);
5570 copy->pid = htonl (t->bck_pid + 1);
5572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5573 " calling generic handler...\n");
5574 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
5576 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5583 * Handler for client's ACKs for payload traffic.
5585 * @param cls Closure (unused).
5586 * @param client Identification of the client.
5587 * @param message The actual message.
5590 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5591 const struct GNUNET_MessageHeader *message)
5593 struct GNUNET_MESH_LocalAck *msg;
5594 struct MeshTunnel *t;
5595 struct MeshClient *c;
5596 MESH_TunnelNumber tid;
5599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5600 /* Sanity check for client registration */
5601 if (NULL == (c = client_get (client)))
5604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5609 msg = (struct GNUNET_MESH_LocalAck *) message;
5611 /* Tunnel exists? */
5612 tid = ntohl (msg->tunnel_id);
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5614 t = tunnel_get_by_local_id (c, tid);
5618 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5620 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5624 ack = ntohl (msg->max_pid);
5625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
5627 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5628 if (NULL != t->owner && t->owner->handle == client)
5630 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5632 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5636 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5637 tunnel_set_client_fwd_ack (t, c, ack);
5638 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5641 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5649 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5651 * @param cls Closure (client handle).
5652 * @param key Key (hashed tunnel ID, unused).
5653 * @param value Tunnel info.
5655 * @return GNUNET_YES, to keep iterating.
5658 monitor_all_tunnels_iterator (void *cls,
5659 const struct GNUNET_HashCode * key,
5662 struct GNUNET_SERVER_Client *client = cls;
5663 struct MeshTunnel *t = value;
5664 struct GNUNET_MESH_LocalMonitor *msg;
5666 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5667 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5668 msg->tunnel_id = htonl (t->id.tid);
5669 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5670 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5671 GNUNET_PEER_resolve (t->peer, &msg->destination);
5673 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5674 "* sending info about tunnel %s [%u]\n",
5675 GNUNET_i2s (&msg->owner), t->id.tid);
5677 GNUNET_SERVER_notification_context_unicast (nc, client,
5678 &msg->header, GNUNET_NO);
5684 * Handler for client's MONITOR request.
5686 * @param cls Closure (unused).
5687 * @param client Identification of the client.
5688 * @param message The actual message.
5691 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5692 const struct GNUNET_MessageHeader *message)
5694 struct MeshClient *c;
5696 /* Sanity check for client registration */
5697 if (NULL == (c = client_get (client)))
5700 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5704 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5705 "Received get tunnels request from client %u\n",
5707 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5708 monitor_all_tunnels_iterator,
5710 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5711 "Get tunnels request from client %u completed\n",
5713 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5718 * Handler for client's MONITOR_TUNNEL request.
5720 * @param cls Closure (unused).
5721 * @param client Identification of the client.
5722 * @param message The actual message.
5725 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5726 const struct GNUNET_MessageHeader *message)
5728 const struct GNUNET_MESH_LocalMonitor *msg;
5729 struct GNUNET_MESH_LocalMonitor *resp;
5730 struct MeshClient *c;
5731 struct MeshTunnel *t;
5733 /* Sanity check for client registration */
5734 if (NULL == (c = client_get (client)))
5737 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5741 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5742 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5743 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5746 ntohl (msg->tunnel_id));
5747 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5750 /* We don't know the tunnel FIXME */
5751 struct GNUNET_MESH_LocalMonitor warn;
5754 GNUNET_SERVER_notification_context_unicast (nc, client,
5757 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5761 /* Initialize context */
5762 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5764 GNUNET_PEER_resolve (t->peer, &resp->destination);
5765 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5766 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5767 &resp->header, GNUNET_NO);
5770 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5771 "Monitor tunnel request from client %u completed\n",
5773 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5778 * Functions to handle messages from clients
5780 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5781 {&handle_local_new_client, NULL,
5782 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5783 {&handle_local_tunnel_create, NULL,
5784 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5785 sizeof (struct GNUNET_MESH_TunnelMessage)},
5786 {&handle_local_tunnel_destroy, NULL,
5787 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5788 sizeof (struct GNUNET_MESH_TunnelMessage)},
5789 {&handle_local_tunnel_buffer, NULL,
5790 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
5791 sizeof (struct GNUNET_MESH_TunnelMessage)},
5792 {&handle_local_tunnel_buffer, NULL,
5793 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
5794 sizeof (struct GNUNET_MESH_TunnelMessage)},
5795 {&handle_local_unicast, NULL,
5796 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5797 {&handle_local_to_origin, NULL,
5798 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5799 {&handle_local_ack, NULL,
5800 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5801 sizeof (struct GNUNET_MESH_LocalAck)},
5802 {&handle_local_get_tunnels, NULL,
5803 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5804 sizeof (struct GNUNET_MessageHeader)},
5805 {&handle_local_show_tunnel, NULL,
5806 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5807 sizeof (struct GNUNET_MESH_LocalMonitor)},
5813 * To be called on core init/fail.
5815 * @param cls service closure
5816 * @param server handle to the server for this service
5817 * @param identity the public identity of this peer
5820 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5821 const struct GNUNET_PeerIdentity *identity)
5824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5825 core_handle = server;
5826 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5829 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5830 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5832 GNUNET_i2s (identity));
5833 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5835 GNUNET_i2s (&my_full_id));
5836 GNUNET_SCHEDULER_shutdown (); // Try gracefully
5838 GNUNET_abort(); // Try harder
5845 * Method called whenever a given peer connects.
5847 * @param cls closure
5848 * @param peer peer identity this notification is about
5851 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5853 struct MeshPeerInfo *peer_info;
5854 struct MeshPeerPath *path;
5856 DEBUG_CONN ("Peer connected\n");
5857 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5858 peer_info = peer_info_get (peer);
5859 if (myid == peer_info->id)
5861 DEBUG_CONN (" (self)\n");
5866 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5868 path = path_new (2);
5869 path->peers[0] = myid;
5870 path->peers[1] = peer_info->id;
5871 GNUNET_PEER_change_rc (myid, 1);
5872 GNUNET_PEER_change_rc (peer_info->id, 1);
5873 peer_info_add_path (peer_info, path, GNUNET_YES);
5874 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5880 * Method called whenever a peer disconnects.
5882 * @param cls closure
5883 * @param peer peer identity this notification is about
5886 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5888 struct MeshPeerInfo *pi;
5889 struct MeshPeerQueue *q;
5890 struct MeshPeerQueue *n;
5892 DEBUG_CONN ("Peer disconnected\n");
5893 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5903 /* TODO try to reroute this traffic instead */
5904 queue_destroy(q, GNUNET_YES);
5907 if (NULL != pi->core_transmit)
5909 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5910 pi->core_transmit = NULL;
5912 peer_info_remove_path (pi, pi->id, myid);
5915 DEBUG_CONN (" (self)\n");
5917 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5922 /******************************************************************************/
5923 /************************ MAIN FUNCTIONS ****************************/
5924 /******************************************************************************/
5927 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5929 * @param cls closure
5930 * @param key current key code
5931 * @param value value in the hash map
5932 * @return GNUNET_YES if we should continue to iterate,
5936 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5938 struct MeshTunnel *t = value;
5945 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5947 * @param cls closure
5948 * @param key current key code
5949 * @param value value in the hash map
5950 * @return GNUNET_YES if we should continue to iterate,
5954 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5956 struct MeshPeerInfo *p = value;
5957 struct MeshPeerQueue *q;
5958 struct MeshPeerQueue *n;
5966 queue_destroy(q, GNUNET_YES);
5970 peer_info_destroy (p);
5976 * Task run during shutdown.
5982 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5986 if (core_handle != NULL)
5988 GNUNET_CORE_disconnect (core_handle);
5993 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
5996 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5997 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5998 if (dht_handle != NULL)
6000 GNUNET_DHT_disconnect (dht_handle);
6005 GNUNET_SERVER_notification_context_destroy (nc);
6008 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6010 GNUNET_SCHEDULER_cancel (announce_id_task);
6011 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6018 * Callback for hostkey read/generation.
6020 * @param cls Closure (Configuration handle).
6021 * @param pk The ECC private key.
6022 * @param emsg Error message, if any.
6025 key_generation_cb (void *cls,
6026 struct GNUNET_CRYPTO_EccPrivateKey *pk,
6029 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6030 struct MeshPeerInfo *peer;
6031 struct MeshPeerPath *p;
6036 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6037 _("Mesh service could not access hostkey: %s. Exiting.\n"),
6039 GNUNET_SCHEDULER_shutdown ();
6042 my_private_key = pk;
6043 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6044 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6045 &my_full_id.hashPubKey);
6046 myid = GNUNET_PEER_intern (&my_full_id);
6047 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6048 "Mesh for peer [%s] starting\n",
6049 GNUNET_i2s(&my_full_id));
6051 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6052 NULL, /* Closure passed to MESH functions */
6053 &core_init, /* Call core_init once connected */
6054 &core_connect, /* Handle connects */
6055 &core_disconnect, /* remove peers on disconnects */
6056 NULL, /* Don't notify about all incoming messages */
6057 GNUNET_NO, /* For header only in notification */
6058 NULL, /* Don't notify about all outbound messages */
6059 GNUNET_NO, /* For header-only out notification */
6060 core_handlers); /* Register these handlers */
6062 if (core_handle == NULL)
6065 GNUNET_SCHEDULER_shutdown ();
6070 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6073 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6074 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6075 GNUNET_SERVER_disconnect_notify (server_handle,
6076 &handle_local_client_disconnect, NULL);
6080 clients_tail = NULL;
6083 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6085 /* Create a peer_info for the local peer */
6086 peer = peer_info_get (&my_full_id);
6089 GNUNET_PEER_change_rc (myid, 1);
6090 peer_info_add_path (peer, p, GNUNET_YES);
6091 GNUNET_SERVER_resume (server_handle);
6092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6097 * Process mesh requests.
6099 * @param cls closure
6100 * @param server the initialized server
6101 * @param c configuration to use
6104 run (void *cls, struct GNUNET_SERVER_Handle *server,
6105 const struct GNUNET_CONFIGURATION_Handle *c)
6109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6110 server_handle = server;
6113 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6118 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6119 "mesh", "peer/privatekey");
6120 GNUNET_SCHEDULER_shutdown ();
6125 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6126 &refresh_path_time))
6128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6130 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6131 "mesh", "refresh path time");
6132 GNUNET_SCHEDULER_shutdown ();
6137 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6138 &app_announce_time))
6140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6142 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6143 "mesh", "app announce time");
6144 GNUNET_SCHEDULER_shutdown ();
6147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6148 "APP_ANNOUNCE_TIME %llu ms\n",
6149 app_announce_time.rel_value);
6151 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6156 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6157 "mesh", "id announce time");
6158 GNUNET_SCHEDULER_shutdown ();
6163 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6168 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6169 "mesh", "connect timeout");
6170 GNUNET_SCHEDULER_shutdown ();
6175 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6180 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6181 "mesh", "max msgs queue");
6182 GNUNET_SCHEDULER_shutdown ();
6187 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6192 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6193 "mesh", "max tunnels");
6194 GNUNET_SCHEDULER_shutdown ();
6199 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6202 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6204 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6205 "mesh", "default ttl", 64);
6210 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6213 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6214 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6215 "mesh", "max peers", 1000);
6220 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6221 &dht_replication_level))
6223 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6225 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6226 "mesh", "dht replication level", 3);
6227 dht_replication_level = 3;
6230 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6231 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6232 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6233 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6235 dht_handle = GNUNET_DHT_connect (c, 64);
6236 if (NULL == dht_handle)
6240 stats = GNUNET_STATISTICS_create ("mesh", c);
6242 GNUNET_SERVER_suspend (server_handle);
6243 /* Scheduled the task to clean up when shutdown is called */
6244 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6246 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
6249 GNUNET_free (keyfile);
6254 * The main function for the mesh service.
6256 * @param argc number of arguments from the command line
6257 * @param argv command line arguments
6258 * @return 0 ok, 1 on error
6261 main (int argc, char *const *argv)
6266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6267 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6269 ret = (GNUNET_OK == r) ? 0 : 1;
6270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
6274 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6275 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
6276 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);