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"
56 #define MESH_BLOOM_SIZE 128
58 #define MESH_DEBUG_DHT GNUNET_NO
59 #define MESH_DEBUG_CONNECTION GNUNET_NO
60 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
62 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
63 GNUNET_TIME_UNIT_MINUTES,\
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_CONN(...)
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_DHT(...)
82 struct timespec __mesh_start;
83 struct timespec __mesh_end;
84 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
85 #define INTERVAL_END \
87 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
88 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
89 if (__diff < 0) __diff += 1000000000;\
93 #define INTERVAL_SHOW \
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
97 #define INTERVAL_START
102 /******************************************************************************/
103 /************************ DATA STRUCTURES ****************************/
104 /******************************************************************************/
106 /** FWD declaration */
112 * Struct representing a piece of data being sent to other peers
116 /** Tunnel it belongs to. */
117 struct MeshTunnel *t;
119 /** How many remaining neighbors still hav't got it. */
120 unsigned int reference_counter;
122 /** How many remaining neighbors we need to send this to. */
123 unsigned int total_out;
125 /** Size of the data. */
134 * Struct containing info about a queued transmission to this peer
141 struct MeshPeerQueue *next;
146 struct MeshPeerQueue *prev;
149 * Peer this transmission is directed to.
151 struct MeshPeerInfo *peer;
154 * Tunnel this message belongs to.
156 struct MeshTunnel *tunnel;
159 * Pointer to info stucture used as cls.
169 * Size of the message
176 * Struct containing all info possibly needed to build a package when called
179 struct MeshTransmissionDescriptor
181 /** ID of the tunnel this packet travels in */
182 struct MESH_TunnelID *origin;
184 /** Who was this message being sent to */
185 struct MeshPeerInfo *peer;
187 /** Ultimate destination of the packet */
188 GNUNET_PEER_Id destination;
190 /** Data descriptor */
191 struct MeshData* mesh_data;
196 * Struct containing all information regarding a given peer
206 * Last time we heard from this peer
208 struct GNUNET_TIME_Absolute last_contact;
211 * Task handler for delayed connect task;
213 GNUNET_SCHEDULER_TaskIdentifier connect_task;
216 * Number of attempts to reconnect so far
218 int n_reconnect_attempts;
221 * Paths to reach the peer, ordered by ascending hop count
223 struct MeshPeerPath *path_head;
226 * Paths to reach the peer, ordered by ascending hop count
228 struct MeshPeerPath *path_tail;
231 * Handle to stop the DHT search for a path to this peer
233 struct GNUNET_DHT_GetHandle *dhtget;
236 * Closure given to the DHT GET
238 struct MeshPathInfo *dhtgetcls;
241 * Array of tunnels this peer participates in
242 * (most probably a small amount, therefore not a hashmap)
243 * When the path to the peer changes, notify these tunnels to let them
244 * re-adjust their path trees.
246 struct MeshTunnel **tunnels;
249 * Number of tunnels this peers participates in
251 unsigned int ntunnels;
254 * Transmission queue to core DLL head
256 struct MeshPeerQueue *queue_head;
259 * Transmission queue to core DLL tail
261 struct MeshPeerQueue *queue_tail;
264 * How many messages are in the queue to this peer.
266 unsigned int queue_n;
269 * Handle to for queued transmissions
271 struct GNUNET_CORE_TransmitHandle *core_transmit;
276 * Globally unique tunnel identification (owner + number)
277 * DO NOT USE OVER THE NETWORK
282 * Node that owns the tunnel
287 * Tunnel number to differentiate all the tunnels owned by the node oid
288 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
290 MESH_TunnelNumber tid;
295 * Struct containing all information regarding a tunnel
296 * For an intermediate node the improtant info used will be:
297 * - id Tunnel unique identification
298 * - paths[0] To know where to send it next
299 * - metainfo: ready, speeds, accounting
306 struct MESH_TunnelID id;
309 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
311 MESH_TunnelNumber local_tid;
314 * Local tunnel number for local destination clients (incoming number)
315 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
318 MESH_TunnelNumber local_tid_dest;
321 * Is the tunnel bufferless (minimum latency)?
326 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
331 * Packet ID of the last bck packet sent (unique counter per hop).
336 * Force sending ACK? Flag to allow duplicate ACK on POLL.
341 * Last ACK sent towards the origin (for traffic towards leaf node).
343 uint32_t last_fwd_ack;
346 * BCK ACK value received from the hop towards the owner of the tunnel,
347 * (previous node / owner): up to what message PID can we sent back to him.
352 * How many messages are in the forward queue (towards leaves).
354 unsigned int fwd_queue_n;
357 * How many messages do we accept in the forward queue.
359 unsigned int fwd_queue_max;
362 * How many messages are in the backward queue (towards origin).
364 unsigned int bck_queue_n;
367 * How many messages do we accept in the backward queue.
369 unsigned int bck_queue_max;
372 * Task to poll peer in case of a stall.
374 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
377 * Last time the tunnel was used
379 struct GNUNET_TIME_Absolute timestamp;
382 * Peer of the tunnel.
387 * Client owner of the tunnel, if any
389 struct MeshClient *owner;
392 * Clients that have been informed about and want to stay in the tunnel.
394 struct MeshClient **clients;
397 * Flow control info for each client.
399 struct MeshTunnelClientInfo *clients_fc;
402 * Number of elements in clients/clients_fc
404 unsigned int nclients;
407 * Clients that have been informed but requested to leave the tunnel.
409 struct MeshClient **ignore;
412 * Number of elements in clients
414 unsigned int nignore;
417 * Bloomfilter (for peer identities) to stop circular routes
419 char bloomfilter[MESH_BLOOM_SIZE];
425 struct MeshTunnelTree *tree;
428 * Task to keep the used paths alive
430 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
433 * Task to destroy the tunnel after timeout
435 * FIXME: merge the two? a tunnel will have either
436 * a path refresh OR a timeout, never both!
438 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
441 * Flag to signal the destruction of the tunnel.
442 * If this is set GNUNET_YES the tunnel will be destroyed
443 * when the queue is empty.
448 * Total messages pending for this tunnels, payload or not.
450 unsigned int pending_messages;
453 * If the tunnel is empty, destoy it.
455 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
460 * Info about a leaf client of a tunnel, needed to perform flow control.
462 struct MeshTunnelClientInfo
465 * PID of the last packet sent to the client (FWD).
470 * PID of the last packet received from the client (BCK).
475 * Maximum PID allowed (FWD ACK received).
480 * Last ACK sent to that child (BCK ACK).
487 * Info needed to work with tunnel paths and peers
494 struct MeshTunnel *t;
497 * Neighbouring peer to whom we send the packet to
499 struct MeshPeerInfo *peer;
504 struct MeshPeerPath *path;
509 * Struct containing information about a client of the service
516 struct MeshClient *next;
521 struct MeshClient *prev;
524 * Tunnels that belong to this client, indexed by local id
526 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
529 * Tunnels this client has accepted, indexed by incoming local id
531 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
534 * Tunnels this client has rejected, indexed by incoming local id
536 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
538 * Handle to communicate with the client
540 struct GNUNET_SERVER_Client *handle;
543 * Messages that this client has declared interest in
545 struct GNUNET_CONTAINER_MultiHashMap *types;
548 * Whether the client is active or shutting down (don't send confirmations
549 * to a client that is shutting down.
554 * ID of the client, mainly for debug messages
561 /******************************************************************************/
562 /************************ DEBUG FUNCTIONS ****************************/
563 /******************************************************************************/
567 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
568 * @param cls string to print
569 * @param success GNUNET_OK if the PUT was transmitted,
570 * GNUNET_NO on timeout,
571 * GNUNET_SYSERR on disconnect from service
572 * after the PUT message was transmitted
573 * (so we don't know if it was received or not)
578 mesh_debug (void *cls, int success)
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
586 unsigned int debug_fwd_ack;
587 unsigned int debug_bck_ack;
591 /******************************************************************************/
592 /*********************** GLOBAL VARIABLES ****************************/
593 /******************************************************************************/
595 /************************** Configuration parameters **************************/
598 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
600 static struct GNUNET_TIME_Relative refresh_path_time;
603 * How often to PUT local application numbers in the DHT.
605 static struct GNUNET_TIME_Relative app_announce_time;
608 * How often to PUT own ID in the DHT.
610 static struct GNUNET_TIME_Relative id_announce_time;
613 * Maximum time allowed to connect to a peer found by string.
615 static struct GNUNET_TIME_Relative connect_timeout;
618 * Default TTL for payload packets.
620 static unsigned long long default_ttl;
623 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
625 static unsigned long long dht_replication_level;
628 * How many tunnels are we willing to maintain.
629 * Local tunnels are always allowed, even if there are more tunnels than max.
631 static unsigned long long max_tunnels;
634 * How many messages *in total* are we willing to queue, divided by number of
635 * tunnels to get tunnel queue size.
637 static unsigned long long max_msgs_queue;
640 * How many peers do we want to remember?
642 static unsigned long long max_peers;
645 /*************************** Static global variables **************************/
648 * Hostkey generation context
650 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
653 * DLL with all the clients, head.
655 static struct MeshClient *clients;
658 * DLL with all the clients, tail.
660 static struct MeshClient *clients_tail;
663 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
665 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
668 * Number of tunnels known.
670 static unsigned long long n_tunnels;
673 * Tunnels incoming, indexed by MESH_TunnelNumber
674 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
676 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
679 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
681 static struct GNUNET_CONTAINER_MultiHashMap *peers;
684 * Handle to communicate with transport
686 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
689 * Handle to communicate with core.
691 static struct GNUNET_CORE_Handle *core_handle;
696 static struct GNUNET_DHT_Handle *dht_handle;
701 static struct GNUNET_SERVER_Handle *server_handle;
704 * Handle to the statistics service.
706 static struct GNUNET_STATISTICS_Handle *stats;
709 * Notification context, to send messages to local clients.
711 static struct GNUNET_SERVER_NotificationContext *nc;
714 * Local peer own ID (memory efficient handle).
716 static GNUNET_PEER_Id myid;
719 * Local peer own ID (full value).
721 static struct GNUNET_PeerIdentity my_full_id;
726 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
731 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
734 * Tunnel ID for the next created tunnel (global tunnel number).
736 static MESH_TunnelNumber next_tid;
739 * Tunnel ID for the next incoming tunnel (local tunnel number).
741 static MESH_TunnelNumber next_local_tid;
744 * All message types clients of this peer are interested in.
746 static struct GNUNET_CONTAINER_MultiHashMap *types;
749 * Task to periodically announce itself in the network.
751 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
754 * Next ID to assign to a client.
756 unsigned int next_client_id;
759 /******************************************************************************/
760 /*********************** DECLARATIONS **************************/
761 /******************************************************************************/
764 * Function to process paths received for a new peer addition. The recorded
765 * paths form the initial tunnel, which can be optimized later.
766 * Called on each result obtained for the DHT search.
769 * @param exp when will this value expire
770 * @param key key of the result
771 * @param type type of the result
772 * @param size number of bytes in data
773 * @param data pointer to the result data
776 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
777 const struct GNUNET_HashCode * key,
778 const struct GNUNET_PeerIdentity *get_path,
779 unsigned int get_path_length,
780 const struct GNUNET_PeerIdentity *put_path,
781 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
782 size_t size, const void *data);
786 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
787 * and insert it in the appropiate structures if the peer is not known yet.
789 * @param peer Full identity of the peer.
791 * @return Existing or newly created peer info.
793 static struct MeshPeerInfo *
794 peer_info_get (const struct GNUNET_PeerIdentity *peer);
798 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
799 * and insert it in the appropiate structures if the peer is not known yet.
801 * @param peer Short identity of the peer.
803 * @return Existing or newly created peer info.
805 static struct MeshPeerInfo *
806 peer_info_get_short (const GNUNET_PEER_Id peer);
810 * Try to establish a new connection to this peer.
811 * Use the best path for the given tunnel.
812 * If the peer doesn't have any path to it yet, try to get one.
813 * If the peer already has some path, send a CREATE PATH towards it.
815 * @param peer PeerInfo of the peer.
816 * @param t Tunnel for which to create the path, if possible.
819 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
823 * Build a PeerPath from the paths returned from the DHT, reversing the paths
824 * to obtain a local peer -> destination path and interning the peer ids.
826 * @return Newly allocated and created path
828 static struct MeshPeerPath *
829 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
830 unsigned int get_path_length,
831 const struct GNUNET_PeerIdentity *put_path,
832 unsigned int put_path_length);
836 * Adds a path to the peer_infos of all the peers in the path
838 * @param p Path to process.
839 * @param confirmed Whether we know if the path works or not.
842 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
847 * Search for a tunnel by global ID using full PeerIdentities.
849 * @param oid owner of the tunnel.
850 * @param tid global tunnel number.
852 * @return tunnel handler, NULL if doesn't exist.
854 static struct MeshTunnel *
855 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
859 * Delete an active client from the tunnel.
865 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
868 * Notify a tunnel that a connection has broken that affects at least
871 * @param t Tunnel affected.
872 * @param p1 Peer that got disconnected from p2.
873 * @param p2 Peer that got disconnected from p1.
875 * @return Short ID of the peer disconnected (either p1 or p2).
876 * 0 if the tunnel remained unaffected.
878 static GNUNET_PEER_Id
879 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
884 * Get the current ack value for a tunnel, for data going from root to leaves,
885 * taking in account the tunnel mode and the status of all children and clients.
889 * @return Maximum PID allowed.
892 tunnel_get_fwd_ack (struct MeshTunnel *t);
896 * Add a client to a tunnel, initializing all needed data structures.
898 * @param t Tunnel to which add the client.
899 * @param c Client which to add to the tunnel.
902 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
906 * @brief Queue and pass message to core when possible.
908 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
909 * and accounts for it. In case the queue is full, the message is dropped and
912 * Otherwise, message is treated as internal and allowed to go regardless of
915 * @param cls Closure (@c type dependant). It will be used by queue_send to
916 * build the message to be sent if not already prebuilt.
917 * @param type Type of the message, 0 for a raw message.
918 * @param size Size of the message.
919 * @param dst Neighbor to send message to.
920 * @param t Tunnel this message belongs to.
923 queue_add (void *cls, uint16_t type, size_t size,
924 struct MeshPeerInfo *dst, struct MeshTunnel *t);
928 * Free a transmission that was already queued with all resources
929 * associated to the request.
931 * @param queue Queue handler to cancel.
932 * @param clear_cls Is it necessary to free associated cls?
935 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
939 * @brief Get the next transmittable message from the queue.
941 * This will be the head, except in the case of being a data packet
942 * not allowed by the destination peer.
944 * @param peer Destination peer.
946 * @return The next viable MeshPeerQueue element to send to that peer.
947 * NULL when there are no transmittable messages.
949 struct MeshPeerQueue *
950 queue_get_next (const struct MeshPeerInfo *peer);
954 * Core callback to write a queued packet to core buffer
956 * @param cls Closure (peer info).
957 * @param size Number of bytes available in buf.
958 * @param buf Where the to write the message.
960 * @return number of bytes written to buf
963 queue_send (void *cls, size_t size, void *buf);
966 /******************************************************************************/
967 /************************ PERIODIC FUNCTIONS ****************************/
968 /******************************************************************************/
971 * Periodically announce self id in the DHT
974 * @param tc task context
977 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
981 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
983 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
987 * - Set data expiration in function of X
990 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
992 block.id = my_full_id;
993 block.type = htonl (0);
994 GNUNET_DHT_put (dht_handle, /* DHT handle */
995 &my_full_id.hashPubKey, /* Key to use */
996 dht_replication_level, /* Replication level */
997 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
998 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
999 sizeof (block), /* Size of the data */
1000 (const char *) &block, /* Data itself */
1001 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1002 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1003 NULL, /* Continuation */
1004 NULL); /* Continuation closure */
1006 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1010 /******************************************************************************/
1011 /****************** GENERAL HELPER FUNCTIONS ************************/
1012 /******************************************************************************/
1015 * Decrements the reference counter and frees all resources if needed
1017 * @param mesh_data Data Descriptor used in a multicast message.
1018 * Freed no longer needed (last message).
1021 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1023 if (0 == --(mesh_data->reference_counter))
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1026 GNUNET_free (mesh_data->data);
1027 GNUNET_free (mesh_data);
1033 * Check if client has registered with the service and has not disconnected
1035 * @param client the client to check
1037 * @return non-NULL if client exists in the global DLL
1039 static struct MeshClient *
1040 client_get (struct GNUNET_SERVER_Client *client)
1042 struct MeshClient *c;
1047 if (c->handle == client)
1056 * Checks if a given client has subscribed to certain message type
1058 * @param message_type Type of message to check
1059 * @param c Client to check
1061 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1063 * FIXME: use of crypto_hash slows it down
1064 * The hash function alone takes 8-10us out of the ~55us for the whole
1065 * process of retransmitting the message from one local client to another.
1066 * Find faster implementation!
1069 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1071 struct GNUNET_HashCode hc;
1073 if (NULL == c->types)
1076 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1077 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1082 * Check whether client wants traffic from a tunnel.
1084 * @param c Client to check.
1085 * @param t Tunnel to be found.
1087 * @return GNUNET_YES if client knows tunnel.
1089 * TODO look in client hashmap
1092 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1096 for (i = 0; i < t->nclients; i++)
1097 if (t->clients[i] == c)
1104 * Check whether client has been informed about a tunnel.
1106 * @param c Client to check.
1107 * @param t Tunnel to be found.
1109 * @return GNUNET_YES if client knows tunnel.
1111 * TODO look in client hashmap
1114 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1118 for (i = 0; i < t->nignore; i++)
1119 if (t->ignore[i] == c)
1121 return client_wants_tunnel(c, t);
1126 * Marks a client as uninterested in traffic from the tunnel, updating both
1127 * client and tunnel to reflect this.
1129 * @param c Client that doesn't want traffic anymore.
1130 * @param t Tunnel which should be ignored.
1132 * FIXME when to delete an incoming tunnel?
1135 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1137 struct GNUNET_HashCode hash;
1139 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1140 GNUNET_break (GNUNET_YES ==
1141 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1143 GNUNET_break (GNUNET_YES ==
1144 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1145 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1146 tunnel_delete_active_client (t, c);
1147 GNUNET_array_append (t->ignore, t->nignore, c);
1152 * Deletes a tunnel from a client (either owner or destination). To be used on
1153 * tunnel destroy, otherwise, use client_ignore_tunnel.
1155 * @param c Client whose tunnel to delete.
1156 * @param t Tunnel which should be deleted.
1159 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1161 struct GNUNET_HashCode hash;
1165 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1166 GNUNET_assert (GNUNET_YES ==
1167 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1173 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1175 GNUNET_assert (GNUNET_YES ==
1176 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1180 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1188 * Send the message to all clients that have subscribed to its type
1190 * @param msg Pointer to the message itself
1191 * @param payload Pointer to the payload of the message.
1192 * @param t The tunnel to whose clients this message goes.
1194 * @return number of clients this message was sent to
1197 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1198 const struct GNUNET_MessageHeader *payload,
1199 struct MeshTunnel *t)
1201 struct MeshClient *c;
1202 MESH_TunnelNumber *tid;
1205 char cbuf[htons (msg->size)];
1207 type = ntohs (payload->type);
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1210 GNUNET_MESH_DEBUG_M2S (type));
1212 memcpy (cbuf, msg, sizeof (cbuf));
1213 switch (htons (msg->type))
1215 struct GNUNET_MESH_Unicast *uc;
1216 struct GNUNET_MESH_ToOrigin *to;
1218 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1219 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1222 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1223 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1231 for (count = 0, c = clients; c != NULL; c = c->next)
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1234 if (client_is_subscribed (type, c))
1236 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1240 *tid = htonl (t->local_tid);
1244 if (GNUNET_NO == client_knows_tunnel (c, t))
1246 /* This client doesn't know the tunnel */
1247 struct GNUNET_MESH_TunnelNotification tmsg;
1248 struct GNUNET_HashCode hash;
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1251 tmsg.header.size = htons (sizeof (tmsg));
1252 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1253 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1254 tmsg.tunnel_id = htonl (t->local_tid_dest);
1256 if (GNUNET_YES == t->nobuffer)
1257 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1258 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1259 &tmsg.header, GNUNET_NO);
1260 tunnel_add_client (t, c);
1261 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1263 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1264 c->incoming_tunnels, &hash, t,
1265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1267 *tid = htonl (t->local_tid_dest);
1270 /* Check if the client wants to get traffic from the tunnel */
1271 if (GNUNET_NO == client_wants_tunnel(c, t))
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1275 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1276 (struct GNUNET_MessageHeader
1277 *) cbuf, GNUNET_NO);
1286 * Notify all clients (not depending on registration status) that the incoming
1287 * tunnel is no longer valid.
1289 * @param t Tunnel that was destroyed.
1292 send_clients_tunnel_destroy (struct MeshTunnel *t)
1294 struct GNUNET_MESH_TunnelMessage msg;
1296 msg.header.size = htons (sizeof (msg));
1297 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1298 msg.tunnel_id = htonl (t->local_tid_dest);
1299 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1304 * Notify clients of tunnel disconnections, if needed.
1305 * In case the origin disconnects, the destination clients get a tunnel destroy
1306 * notification. If the last destination disconnects (only one remaining client
1307 * in tunnel), the origin gets a (local ID) peer disconnected.
1308 * Note that the function must be called BEFORE removing the client from
1311 * @param t Tunnel that was destroyed.
1312 * @param c Client that disconnected.
1315 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1321 struct GNUNET_MESH_TunnelMessage msg;
1323 msg.header.size = htons (sizeof (msg));
1324 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1325 msg.tunnel_id = htonl (t->local_tid_dest);
1326 for (i = 0; i < t->nclients; i++)
1327 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1328 &msg.header, GNUNET_NO);
1330 // FIXME when to disconnect an incoming tunnel?
1335 * Iterator over all the peers to remove the oldest not-used entry.
1337 * @param cls Closure (unsued).
1338 * @param key ID of the peer.
1339 * @param value Peer_Info of the peer.
1344 peer_info_timeout (void *cls,
1345 const struct GNUNET_HashCode *key,
1352 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1353 * and insert it in the appropiate structures if the peer is not known yet.
1355 * @param peer Full identity of the peer.
1357 * @return Existing or newly created peer info.
1359 static struct MeshPeerInfo *
1360 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1362 struct MeshPeerInfo *peer_info;
1364 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1365 if (NULL == peer_info)
1368 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1369 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1371 GNUNET_CONTAINER_multihashmap_iterate (peers,
1375 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1376 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1377 peer_info->id = GNUNET_PEER_intern (peer);
1379 peer_info->last_contact = GNUNET_TIME_absolute_get();
1386 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1387 * and insert it in the appropiate structures if the peer is not known yet.
1389 * @param peer Short identity of the peer.
1391 * @return Existing or newly created peer info.
1393 static struct MeshPeerInfo *
1394 peer_info_get_short (const GNUNET_PEER_Id peer)
1396 struct GNUNET_PeerIdentity id;
1398 GNUNET_PEER_resolve (peer, &id);
1399 return peer_info_get (&id);
1404 * Iterator to remove the tunnel from the list of tunnels a peer participates
1407 * @param cls Closure (tunnel info)
1408 * @param key GNUNET_PeerIdentity of the peer (unused)
1409 * @param value PeerInfo of the peer
1411 * @return always GNUNET_YES, to keep iterating
1414 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1416 struct MeshTunnel *t = cls;
1417 struct MeshPeerInfo *peer = value;
1420 for (i = 0; i < peer->ntunnels; i++)
1423 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1426 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1428 GNUNET_realloc (peer->tunnels,
1429 peer->ntunnels * sizeof(struct MeshTunnel *));
1438 * Core callback to write a pre-constructed data packet to core buffer
1440 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1441 * @param size Number of bytes available in buf.
1442 * @param buf Where the to write the message.
1444 * @return number of bytes written to buf
1447 send_core_data_raw (void *cls, size_t size, void *buf)
1449 struct MeshTransmissionDescriptor *info = cls;
1450 struct GNUNET_MessageHeader *msg;
1453 GNUNET_assert (NULL != info);
1454 GNUNET_assert (NULL != info->mesh_data);
1455 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1456 total_size = ntohs (msg->size);
1458 if (total_size > size)
1463 memcpy (buf, msg, total_size);
1464 data_descriptor_decrement_rc (info->mesh_data);
1471 * Sends an already built non-multicast message to a peer,
1472 * properly registrating all used resources.
1474 * @param message Message to send. Function makes a copy of it.
1475 * @param peer Short ID of the neighbor whom to send the message.
1476 * @param t Tunnel on which this message is transmitted.
1479 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1480 const struct GNUNET_PeerIdentity *peer,
1481 struct MeshTunnel *t)
1483 struct MeshTransmissionDescriptor *info;
1484 struct MeshPeerInfo *neighbor;
1485 struct MeshPeerPath *p;
1489 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1491 size = ntohs (message->size);
1492 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1493 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1494 info->mesh_data->data = GNUNET_malloc (size);
1495 memcpy (info->mesh_data->data, message, size);
1496 type = ntohs(message->type);
1499 struct GNUNET_MESH_Unicast *m;
1500 struct GNUNET_MESH_ToOrigin *to;
1502 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1503 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
1504 m->ttl = htonl (ntohl (m->ttl) - 1);
1506 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1507 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
1509 to->pid = htonl(t->bck_pid);
1511 info->mesh_data->data_len = size;
1512 info->mesh_data->reference_counter = 1;
1513 info->mesh_data->total_out = 1;
1514 neighbor = peer_info_get (peer);
1515 for (p = neighbor->path_head; NULL != p; p = p->next)
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 " %s IS NOT DIRECTLY CONNECTED\n",
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 for (p = neighbor->path_head; NULL != p; p = p->next)
1533 struct GNUNET_PeerIdentity debug_id;
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1537 " path with %u hops through:\n",
1539 for (i = 0; i < p->length; i++)
1541 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1544 i, GNUNET_i2s(&debug_id));
1548 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1550 " no direct connection to %s\n",
1552 GNUNET_free (info->mesh_data->data);
1553 GNUNET_free (info->mesh_data);
1557 info->peer = neighbor;
1558 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1569 * Sends a CREATE PATH message for a path to a peer, properly registrating
1570 * all used resources.
1572 * @param peer PeerInfo of the final peer for whom this path is being created.
1573 * @param p Path itself.
1574 * @param t Tunnel for which the path is created.
1577 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1578 struct MeshTunnel *t)
1580 struct GNUNET_PeerIdentity id;
1581 struct MeshPathInfo *path_info;
1582 struct MeshPeerInfo *neighbor;
1588 p = tree_get_path_to_peer (t->tree, peer->id);
1595 for (i = 0; i < p->length; i++)
1597 if (p->peers[i] == myid)
1600 if (i >= p->length - 1)
1606 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1608 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1609 path_info->path = p;
1611 neighbor = peer_info_get (&id);
1612 path_info->peer = neighbor;
1613 queue_add (path_info,
1614 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1615 sizeof (struct GNUNET_MESH_ManipulatePath) +
1616 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1623 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1625 * @param t Tunnel whose path to destroy.
1626 * @param destination Short ID of the peer to whom the path to destroy.
1629 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1631 struct MeshPeerPath *p;
1634 p = tree_get_path_to_peer (t->tree, destination);
1640 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1641 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1643 struct GNUNET_MESH_ManipulatePath *msg;
1644 struct GNUNET_PeerIdentity *pi;
1648 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1649 msg->header.size = htons (size);
1650 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1651 msg->tid = htonl (t->id.tid);
1652 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1653 for (i = 0; i < p->length; i++)
1655 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1657 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1664 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1666 * @param t Tunnel which to confirm.
1669 send_path_ack (struct MeshTunnel *t)
1671 struct MeshTransmissionDescriptor *info;
1672 struct GNUNET_PeerIdentity id;
1673 GNUNET_PEER_Id peer;
1675 peer = tree_get_predecessor (t->tree);
1676 GNUNET_PEER_resolve (peer, &id);
1677 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1678 info->origin = &t->id;
1679 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1680 GNUNET_assert (NULL != info->peer);
1683 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1684 sizeof (struct GNUNET_MESH_PathACK),
1691 * Try to establish a new connection to this peer.
1692 * Use the best path for the given tunnel.
1693 * If the peer doesn't have any path to it yet, try to get one.
1694 * If the peer already has some path, send a CREATE PATH towards it.
1696 * @param peer PeerInfo of the peer.
1697 * @param t Tunnel for which to create the path, if possible.
1700 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1702 struct MeshPeerPath *p;
1703 struct MeshPathInfo *path_info;
1705 if (NULL != peer->path_head)
1707 p = tree_get_path_to_peer (t->tree, peer->id);
1714 // FIXME always send create path to self
1717 send_create_path (peer, p, t);
1721 struct GNUNET_HashCode hash;
1724 t->local_tid_dest = next_local_tid++;
1725 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1728 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1729 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1736 else if (NULL == peer->dhtget)
1738 struct GNUNET_PeerIdentity id;
1740 GNUNET_PEER_resolve (peer->id, &id);
1741 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1742 path_info->peer = peer;
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1746 peer->dhtgetcls = path_info;
1747 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1748 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1749 &id.hashPubKey, /* key to search */
1750 dht_replication_level, /* replication level */
1751 GNUNET_DHT_RO_RECORD_ROUTE |
1752 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1753 NULL, /* xquery */ // FIXME BLOOMFILTER
1754 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1755 &dht_get_id_handler, path_info);
1757 /* Otherwise, there is no path but the DHT get is already started. */
1762 * Task to delay the connection of a peer
1764 * @param cls Closure (path info with tunnel and peer to connect).
1765 * Will be free'd on exection.
1766 * @param tc TaskContext
1769 peer_info_connect_task (void *cls,
1770 const struct GNUNET_SCHEDULER_TaskContext *tc)
1772 struct MeshPathInfo *path_info = cls;
1774 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
1776 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1781 peer_info_connect (path_info->peer, path_info->t);
1787 * Destroy the peer_info and free any allocated resources linked to it
1789 * @param pi The peer_info to destroy.
1791 * @return GNUNET_OK on success
1794 peer_info_destroy (struct MeshPeerInfo *pi)
1796 struct GNUNET_PeerIdentity id;
1797 struct MeshPeerPath *p;
1798 struct MeshPeerPath *nextp;
1800 GNUNET_PEER_resolve (pi->id, &id);
1801 GNUNET_PEER_change_rc (pi->id, -1);
1804 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1807 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1808 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1810 if (NULL != pi->dhtget)
1812 GNUNET_DHT_get_stop (pi->dhtget);
1813 GNUNET_free (pi->dhtgetcls);
1819 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1823 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
1825 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
1833 * Remove all paths that rely on a direct connection between p1 and p2
1834 * from the peer itself and notify all tunnels about it.
1836 * @param peer PeerInfo of affected peer.
1837 * @param p1 GNUNET_PEER_Id of one peer.
1838 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1841 * TODO: optimize (see below)
1844 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1847 struct MeshPeerPath *p;
1848 struct MeshPeerPath *aux;
1849 struct MeshPeerInfo *peer_d;
1851 unsigned int destroyed;
1856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1858 p = peer->path_head;
1862 for (i = 0; i < (p->length - 1); i++)
1864 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1865 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1867 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1878 for (i = 0; i < peer->ntunnels; i++)
1880 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1884 * Problem: one or more peers have been deleted from the tunnel tree.
1885 * We don't know who they are to try to add them again.
1886 * We need to try to find a new path for each of the disconnected peers.
1887 * Some of them might already have a path to reach them that does not
1888 * involve p1 and p2. Adding all anew might render in a better tree than
1889 * the trivial immediate fix.
1891 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1892 * its children will be reachable trough him.
1894 peer_d = peer_info_get_short (d);
1897 for (p = peer_d->path_head; NULL != p; p = p->next)
1899 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1907 /* No callback, as peer will be already disconnected and a connection
1908 * scheduled by tunnel_notify_connection_broken.
1910 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1914 peer_info_connect (peer_d, peer->tunnels[i]);
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1922 * Add the path to the peer and update the path used to reach it in case this
1925 * @param peer_info Destination peer to add the path to.
1926 * @param path New path to add. Last peer must be the peer in arg 1.
1927 * Path will be either used of freed if already known.
1928 * @param trusted Do we trust that this path is real?
1931 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1934 struct MeshPeerPath *aux;
1938 if ((NULL == peer_info) || (NULL == path))
1941 path_destroy (path);
1944 if (path->peers[path->length - 1] != peer_info->id)
1947 path_destroy (path);
1950 if (path->length <= 2 && GNUNET_NO == trusted)
1952 /* Only allow CORE to tell us about direct paths */
1953 path_destroy (path);
1956 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1957 for (l = 1; l < path->length; l++)
1959 if (path->peers[l] == myid)
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1962 for (l2 = 0; l2 < path->length - l; l2++)
1964 path->peers[l2] = path->peers[l + l2];
1969 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1974 struct GNUNET_PeerIdentity id;
1976 GNUNET_PEER_resolve (peer_info->id, &id);
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1978 path->length, GNUNET_i2s (&id));
1981 l = path_get_length (path);
1988 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1989 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1991 l2 = path_get_length (aux);
1994 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1995 peer_info->path_tail, aux, path);
2000 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2002 path_destroy (path);
2007 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2014 * Add the path to the origin peer and update the path used to reach it in case
2015 * this is the shortest.
2016 * The path is given in peer_info -> destination, therefore we turn the path
2017 * upside down first.
2019 * @param peer_info Peer to add the path to, being the origin of the path.
2020 * @param path New path to add after being inversed.
2021 * @param trusted Do we trust that this path is real?
2024 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2025 struct MeshPeerPath *path, int trusted)
2028 peer_info_add_path (peer_info, path, trusted);
2033 * Function called if the connection to the peer has been stalled for a while,
2034 * possibly due to a missed ACK. Poll the peer about its ACK status.
2036 * @param cls Closure (cinfo).
2037 * @param tc TaskContext.
2040 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2042 // struct GNUNET_MESH_Poll msg;
2043 // struct GNUNET_PeerIdentity id;
2044 // struct MeshTunnel *t;
2046 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2047 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2053 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2054 // msg.header.size = htons (sizeof (msg));
2055 // msg.tid = htonl (t->id.tid);
2056 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2057 // msg.last_ack = htonl (cinfo->fwd_ack);
2059 // GNUNET_PEER_resolve (cinfo->id, &id);
2060 // send_prebuilt_message (&msg.header, &id, cinfo->t);
2061 // cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2062 // MESH_MAX_POLL_TIME,
2063 // GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2064 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2065 // &tunnel_poll, cinfo);
2070 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2071 * to obtain a local peer -> destination path and interning the peer ids.
2073 * @return Newly allocated and created path
2075 static struct MeshPeerPath *
2076 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2077 unsigned int get_path_length,
2078 const struct GNUNET_PeerIdentity *put_path,
2079 unsigned int put_path_length)
2081 struct MeshPeerPath *p;
2087 GNUNET_PEER_change_rc (myid, 1);
2088 i = get_path_length;
2089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2090 for (i--; i >= 0; i--)
2092 id = GNUNET_PEER_intern (&get_path[i]);
2093 if (p->length > 0 && id == p->peers[p->length - 1])
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2096 GNUNET_PEER_change_rc (id, -1);
2100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2101 GNUNET_i2s (&get_path[i]));
2103 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2104 p->peers[p->length - 1] = id;
2107 i = put_path_length;
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2109 for (i--; i >= 0; i--)
2111 id = GNUNET_PEER_intern (&put_path[i]);
2114 /* PUT path went through us, so discard the path up until now and start
2115 * from here to get a much shorter (and loop-free) path.
2120 if (p->length > 0 && id == p->peers[p->length - 1])
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2123 GNUNET_PEER_change_rc (id, -1);
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2128 GNUNET_i2s (&put_path[i]));
2130 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2131 p->peers[p->length - 1] = id;
2135 if (get_path_length > 0)
2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2137 GNUNET_i2s (&get_path[0]));
2138 if (put_path_length > 0)
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2140 GNUNET_i2s (&put_path[0]));
2141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2143 for (i = 0; i < p->length; i++)
2145 struct GNUNET_PeerIdentity peer_id;
2147 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2149 GNUNET_i2s (&peer_id));
2157 * Adds a path to the peer_infos of all the peers in the path
2159 * @param p Path to process.
2160 * @param confirmed Whether we know if the path works or not.
2163 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2167 /* TODO: invert and add */
2168 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2169 for (i++; i < p->length; i++)
2171 struct MeshPeerInfo *aux;
2172 struct MeshPeerPath *copy;
2174 aux = peer_info_get_short (p->peers[i]);
2175 copy = path_duplicate (p);
2176 copy->length = i + 1;
2177 peer_info_add_path (aux, copy, GNUNET_NO);
2183 * Send keepalive packets for a peer
2185 * @param cls Closure (tunnel for which to send the keepalive).
2186 * @param tc Notification context.
2188 * TODO: implement explicit multicast keepalive?
2191 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2195 * Search for a tunnel among the incoming tunnels
2197 * @param tid the local id of the tunnel
2199 * @return tunnel handler, NULL if doesn't exist
2201 static struct MeshTunnel *
2202 tunnel_get_incoming (MESH_TunnelNumber tid)
2204 struct GNUNET_HashCode hash;
2206 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2207 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2208 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2213 * Search for a tunnel among the tunnels for a client
2215 * @param c the client whose tunnels to search in
2216 * @param tid the local id of the tunnel
2218 * @return tunnel handler, NULL if doesn't exist
2220 static struct MeshTunnel *
2221 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2223 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2225 return tunnel_get_incoming (tid);
2229 struct GNUNET_HashCode hash;
2231 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2232 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2238 * Search for a tunnel by global ID using PEER_ID
2240 * @param pi owner of the tunnel
2241 * @param tid global tunnel number
2243 * @return tunnel handler, NULL if doesn't exist
2245 static struct MeshTunnel *
2246 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2248 struct MESH_TunnelID id;
2249 struct GNUNET_HashCode hash;
2254 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2255 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2260 * Search for a tunnel by global ID using full PeerIdentities
2262 * @param oid owner of the tunnel
2263 * @param tid global tunnel number
2265 * @return tunnel handler, NULL if doesn't exist
2267 static struct MeshTunnel *
2268 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2270 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2275 * Delete an active client from the tunnel.
2281 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2285 for (i = 0; i < t->nclients; i++)
2287 if (t->clients[i] == c)
2289 t->clients[i] = t->clients[t->nclients - 1];
2290 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2291 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2293 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2301 * Delete an ignored client from the tunnel.
2307 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2311 for (i = 0; i < t->nignore; i++)
2313 if (t->ignore[i] == c)
2315 t->ignore[i] = t->ignore[t->nignore - 1];
2316 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2324 * Delete a client from the tunnel. It should be only done on
2325 * client disconnection, otherwise use client_ignore_tunnel.
2331 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2333 tunnel_delete_ignored_client (t, c);
2334 tunnel_delete_active_client (t, c);
2339 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2340 * needed rescources.
2341 * If peer already exists, reevaluate shortest path and change if different.
2343 * @param t Tunnel we want to add a new peer to
2344 * @param peer PeerInfo of the peer being added
2348 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2350 struct MeshPeerPath *best_p;
2351 struct MeshPeerPath *p;
2352 unsigned int best_cost;
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2357 if (t->peer != peer->id)
2359 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2363 if (NULL != (p = peer->path_head))
2366 best_cost = tree_get_path_cost (t->tree, p);
2369 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2376 tree_add_path (t->tree, best_p, NULL, NULL); // FIXME
2377 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2378 t->path_refresh_task =
2379 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2383 /* Start a DHT get */
2384 peer_info_connect (peer, t);
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2390 * Add a path to a tunnel which we don't own, just to remember the next hop.
2391 * If destination node was already in the tunnel, the first hop information
2392 * will be replaced with the new path.
2394 * @param t Tunnel we want to add a new peer to
2395 * @param p Path to add
2396 * @param own_pos Position of local node in path.
2400 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2401 unsigned int own_pos)
2403 struct GNUNET_PeerIdentity id;
2405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2406 GNUNET_assert (0 != own_pos);
2407 tree_add_path (t->tree, p, NULL, NULL);
2408 if (own_pos < p->length - 1)
2410 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2411 tree_update_first_hops (t->tree, myid, &id);
2413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2417 * Add a client to a tunnel, initializing all needed data structures.
2419 * @param t Tunnel to which add the client.
2420 * @param c Client which to add to the tunnel.
2423 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2425 struct MeshTunnelClientInfo clinfo;
2427 GNUNET_array_append (t->clients, t->nclients, c);
2428 clinfo.fwd_ack = t->fwd_pid + 1;
2429 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
2430 clinfo.fwd_pid = t->fwd_pid;
2431 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
2433 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
2438 * Notifies a tunnel that a connection has broken that affects at least
2439 * some of its peers. Sends a notification towards the root of the tree.
2440 * In case the peer is the owner of the tree, notifies the client that owns
2441 * the tunnel and tries to reconnect.
2443 * @param t Tunnel affected.
2444 * @param p1 Peer that got disconnected from p2.
2445 * @param p2 Peer that got disconnected from p1.
2447 * @return Short ID of the peer disconnected (either p1 or p2).
2448 * 0 if the tunnel remained unaffected.
2450 static GNUNET_PEER_Id
2451 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2454 // if (myid != p1 && myid != p2) FIXME
2459 // if (tree_get_predecessor (t->tree) != 0)
2461 // /* We are the peer still connected, notify owner of the disconnection. */
2462 // struct GNUNET_MESH_PathBroken msg;
2463 // struct GNUNET_PeerIdentity neighbor;
2465 // msg.header.size = htons (sizeof (msg));
2466 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2467 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2468 // msg.tid = htonl (t->id.tid);
2469 // msg.peer1 = my_full_id;
2470 // GNUNET_PEER_resolve (pid, &msg.peer2);
2471 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2472 // send_prebuilt_message (&msg.header, &neighbor, t);
2479 * Get the Flow Control info of a client.
2481 * @param t Tunnel on which to look.
2482 * @param c Client whose ACK to get.
2484 * @return ACK value.
2486 static struct MeshTunnelClientInfo *
2487 tunnel_get_client_fc (struct MeshTunnel *t,
2488 struct MeshClient *c)
2492 for (i = 0; i < t->nclients; i++)
2494 if (t->clients[i] != c)
2496 return &t->clients_fc[i];
2499 return NULL; // avoid compiler / coverity complaints
2504 * Set the FWD ACK value of a client in a particular tunnel.
2506 * @param t Tunnel affected.
2507 * @param c Client whose ACK to set.
2508 * @param ack ACK value.
2511 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
2512 struct MeshClient *c,
2517 for (i = 0; i < t->nclients; i++)
2519 if (t->clients[i] != c)
2521 t->clients_fc[i].fwd_ack = ack;
2529 * Get the highest ACK value of all clients in a particular tunnel,
2530 * according to the buffering/speed settings.
2532 * @param t Tunnel on which to look.
2534 * @return Corresponding ACK value (max uint32_t).
2535 * If no clients are suscribed, -1LL.
2538 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
2543 if (0 == t->nclients)
2545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2546 " tunnel has no clients, no FWD ACK\n");
2550 for (ack = -1LL, i = 0; i < t->nclients; i++)
2553 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack))
2555 ack = t->clients_fc[i].fwd_ack;
2559 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
2560 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
2562 return (uint32_t) ack;
2567 * Get the current fwd ack value for a tunnel, taking in account the tunnel
2568 * mode and the status of all children nodes.
2572 * @return Maximum PID allowed.
2575 tunnel_get_fwd_ack (struct MeshTunnel *t)
2579 uint32_t buffer_free;
2583 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
2584 client_ack = tunnel_get_clients_fwd_ack (t);
2585 if (GNUNET_YES == t->nobuffer)
2591 ack = count + buffer_free; // Overflow? OK!
2593 if (-1LL == client_ack)
2598 ack = GMC_max_pid ((uint32_t) client_ack, ack);
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2601 "c %u, bf %u, cl %lld, ACK: %u\n",
2602 count, buffer_free, client_ack, ack);
2608 * Build a local ACK message and send it to a local client.
2610 * @param t Tunnel on which to send the ACK.
2611 * @param c Client to whom send the ACK.
2612 * @param ack Value of the ACK.
2615 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
2617 struct GNUNET_MESH_LocalAck msg;
2619 msg.header.size = htons (sizeof (msg));
2620 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2621 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
2622 msg.max_pid = htonl (ack);
2623 GNUNET_SERVER_notification_context_unicast(nc,
2630 * Build an ACK message and queue it to send to the given peer.
2632 * @param t Tunnel on which to send the ACK.
2633 * @param peer Peer to whom send the ACK.
2634 * @param ack Value of the ACK.
2637 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
2639 struct GNUNET_MESH_ACK msg;
2641 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2642 msg.header.size = htons (sizeof (msg));
2643 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2644 msg.pid = htonl (ack);
2645 msg.tid = htonl (t->id.tid);
2647 send_prebuilt_message (&msg.header, peer, t);
2652 * Notify a the owner of a tunnel about how many more
2653 * payload packages will we accept on a given tunnel.
2655 * @param t Tunnel on which to send the ACK.
2658 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
2662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2663 "Sending client FWD ACK on tunnel %X\n",
2666 ack = tunnel_get_fwd_ack (t);
2668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2669 if (t->last_fwd_ack == ack)
2671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
2675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2676 t->last_fwd_ack = ack;
2677 send_local_ack (t, t->owner, ack);
2682 * Send an ACK informing the predecessor about the available buffer space.
2683 * In case there is no predecessor, inform the owning client.
2684 * If buffering is off, send only on behalf of children or self if endpoint.
2685 * If buffering is on, send when sent to children and buffer space is free.
2686 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2687 * the ACK itself goes "back" (towards root).
2689 * @param t Tunnel on which to send the ACK.
2690 * @param type Type of message that triggered the ACK transmission.
2693 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2695 struct GNUNET_PeerIdentity id;
2698 if (NULL != t->owner)
2700 tunnel_send_client_fwd_ack (t);
2703 /* Is it after unicast / multicast retransmission? */
2706 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2707 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2709 "ACK due to FWD DATA retransmission\n");
2710 if (GNUNET_YES == t->nobuffer)
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2716 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2717 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2719 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2720 t->force_ack = GNUNET_YES;
2726 /* Check if we need to transmit the ACK */
2727 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
2728 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
2729 GNUNET_NO == t->force_ack)
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2733 " t->qmax: %u, t->qn: %u\n",
2734 t->fwd_queue_max, t->fwd_queue_n);
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2736 " t->pid: %u, t->ack: %u\n",
2737 t->fwd_pid, t->last_fwd_ack);
2741 /* Ok, ACK might be necessary, what PID to ACK? */
2742 ack = tunnel_get_fwd_ack (t);
2744 /* If speed_min and not all children have ack'd, dont send yet */
2745 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
2747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
2751 t->last_fwd_ack = ack;
2752 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
2753 send_ack (t, &id, ack);
2755 t->force_ack = GNUNET_NO;
2760 * Iterator to send a child node a BCK ACK to allow him to send more
2763 * @param cls Closure (tunnel).
2764 * @param id Id of the child node.
2767 tunnel_send_child_bck_ack (void *cls,
2770 struct MeshTunnel *t = cls;
2771 struct GNUNET_PeerIdentity peer;
2772 uint32_t ack = 0; // FIXME
2774 GNUNET_PEER_resolve (id, &peer);
2775 // ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
2777 // if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
2779 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2780 // " Not sending ACK, not needed\n");
2783 // cinfo->bck_ack = ack;
2785 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2786 // " Sending BCK ACK %u (last sent: %u)\n",
2787 // ack, cinfo->bck_ack);
2788 send_ack (t, &peer, ack);
2793 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
2795 * Iterates over all clients and sends BCK ACKs to the ones that need it.
2797 * FIXME fc: what happens if we have 2 clients but q_size is 1?
2798 * - implement a size 1 buffer in each client_fc AND children_fc
2799 * to hold at least 1 message per "child".
2800 * problem: violates no buffer policy
2801 * - ack 0 and make "children" poll for transmission slots
2802 * problem: big overhead, extra latency even in low traffic
2805 * @param t Tunnel on which to send the BCK ACKs.
2808 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
2811 unsigned int tunnel_delta;
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
2815 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
2818 /* Find client whom to allow to send to origin (with lowest buffer space) */
2819 for (i = 0; i < t->nclients; i++)
2821 struct MeshTunnelClientInfo *clinfo;
2824 clinfo = &t->clients_fc[i];
2825 delta = clinfo->bck_ack - clinfo->bck_pid;
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
2827 t->clients[i]->id, delta);
2829 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
2830 (GNUNET_YES == t->nobuffer && 0 == delta))
2834 ack = clinfo->bck_pid;
2835 ack += t->nobuffer ? 1 : tunnel_delta;
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2837 " sending ack to client %u: %u\n",
2838 t->clients[i]->id, ack);
2839 send_local_ack (t, t->clients[i], ack);
2840 clinfo->bck_ack = ack;
2844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2845 " not sending ack to client %u (td %u, d %u)\n",
2846 t->clients[i]->id, tunnel_delta, delta);
2853 * Send an ACK informing the children nodes and destination clients about
2854 * the available buffer space.
2855 * If buffering is off, send only on behalf of root (can be self).
2856 * If buffering is on, send when sent to predecessor and buffer space is free.
2857 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2858 * the ACK itself goes "forward" (towards children/clients).
2860 * @param t Tunnel on which to send the ACK.
2861 * @param type Type of message that triggered the ACK transmission.
2864 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2867 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2868 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2869 /* Is it after data to_origin retransmission? */
2872 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2873 if (GNUNET_YES == t->nobuffer)
2875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2876 " Not sending ACK, nobuffer\n");
2880 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2881 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2883 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2884 t->force_ack = GNUNET_YES;
2890 tunnel_send_clients_bck_ack (t);
2891 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
2892 t->force_ack = GNUNET_NO;
2897 * @brief Re-initiate traffic to this peer if necessary.
2899 * Check if there is traffic queued towards this peer
2900 * and the core transmit handle is NULL (traffic was stalled).
2901 * If so, call core tmt rdy.
2903 * @param cls Closure (unused)
2904 * @param peer_id Short ID of peer to which initiate traffic.
2907 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
2909 struct MeshPeerInfo *peer;
2910 struct GNUNET_PeerIdentity id;
2911 struct MeshPeerQueue *q;
2914 peer = peer_info_get_short(peer_id);
2915 if (NULL != peer->core_transmit)
2918 q = queue_get_next(peer);
2921 /* Might br multicast traffic already sent to this particular peer but
2922 * not to other children in this tunnel.
2923 * This way t->queue_n would be > 0 but the queue of this particular peer
2929 GNUNET_PEER_resolve (peer->id, &id);
2930 peer->core_transmit =
2931 GNUNET_CORE_notify_transmit_ready(core_handle,
2934 GNUNET_TIME_UNIT_FOREVER_REL,
2944 * @brief Allow transmission of FWD traffic on this tunnel
2946 * Check if there is traffic queued towards any children
2947 * and the core transmit handle is NULL, and if so, call core tmt rdy.
2949 * @param t Tunnel on which to unlock FWD traffic.
2952 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
2954 if (0 == t->fwd_queue_n)
2957 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
2962 * @brief Allow transmission of BCK traffic on this tunnel
2964 * Check if there is traffic queued towards the root of the tree
2965 * and the core transmit handle is NULL, and if so, call core tmt rdy.
2967 * @param t Tunnel on which to unlock BCK traffic.
2970 tunnel_unlock_bck_queue (struct MeshTunnel *t)
2972 if (0 == t->bck_queue_n)
2975 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
2980 * Send a message to all peers in this tunnel that the tunnel is no longer
2983 * @param t The tunnel whose peers to notify.
2984 * @param parent ID of the parent, in case the tree is already destroyed.
2987 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
2989 struct GNUNET_MESH_TunnelDestroy msg;
2990 struct GNUNET_PeerIdentity id;
2992 msg.header.size = htons (sizeof (msg));
2993 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2994 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2995 msg.tid = htonl (t->id.tid);
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2997 " sending tunnel destroy for tunnel: %s [%X]\n",
2998 GNUNET_i2s (&msg.oid), t->id.tid);
3000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to child\n");
3001 /* FIXME tunnel_send_multicast (t, &msg.header); */
3004 parent = tree_get_predecessor (t->tree);
3005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
3009 GNUNET_PEER_resolve (parent, &id);
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3011 " sending back to %s\n",
3013 send_prebuilt_message (&msg.header, &id, t);
3018 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3020 * @param cls Closure (Tunnel which to cancel).
3021 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3024 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3026 struct MeshTunnel *t = cls;
3027 struct MeshPeerInfo *peer_info;
3028 struct MeshPeerQueue *pq;
3029 struct MeshPeerQueue *next;
3031 peer_info = peer_info_get_short (neighbor_id);
3032 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3035 if (pq->tunnel == t)
3037 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
3038 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
3039 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
3041 // Should have been removed on destroy children
3044 queue_destroy (pq, GNUNET_YES);
3047 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3049 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3050 peer_info->core_transmit = NULL;
3055 * Destroy the tunnel and free any allocated resources linked to it.
3057 * @param t the tunnel to destroy
3059 * @return GNUNET_OK on success
3062 tunnel_destroy (struct MeshTunnel *t)
3064 struct MeshClient *c;
3065 struct GNUNET_HashCode hash;
3076 struct GNUNET_PeerIdentity id;
3078 GNUNET_PEER_resolve (t->id.oid, &id);
3079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3080 GNUNET_i2s (&id), t->id.tid);
3082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3086 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3087 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3095 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3097 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3104 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3105 for (i = 0; i < t->nclients; i++)
3109 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3115 for (i = 0; i < t->nignore; i++)
3119 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3126 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
3127 GNUNET_free_non_null (t->clients);
3128 GNUNET_free_non_null (t->ignore);
3129 GNUNET_free_non_null (t->clients_fc);
3131 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3132 tree_destroy (t->tree);
3134 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3135 GNUNET_SCHEDULER_cancel (t->timeout_task);
3136 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3137 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3140 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3145 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
3148 * Tunnel is empty: destroy it.
3150 * @param cls Closure (Tunnel).
3151 * @param tc TaskContext.
3154 tunnel_destroy_empty_delayed (void *cls,
3155 const struct GNUNET_SCHEDULER_TaskContext *tc)
3157 struct MeshTunnel *t = cls;
3159 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
3160 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3163 if (0 != t->nclients ||
3164 0 != tree_count_children (t->tree))
3169 struct GNUNET_PeerIdentity id;
3171 GNUNET_PEER_resolve (t->id.oid, &id);
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3173 "executing destruction of empty tunnel %s [%X]\n",
3174 GNUNET_i2s (&id), t->id.tid);
3178 tunnel_send_destroy (t, 0);
3179 if (0 == t->pending_messages)
3182 t->destroy = GNUNET_YES;
3187 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
3189 * @param t Tunnel to destroy if empty.
3192 tunnel_destroy_empty (struct MeshTunnel *t)
3194 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
3196 0 != tree_count_children (t->tree))
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3200 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
3206 struct GNUNET_PeerIdentity id;
3208 GNUNET_PEER_resolve (t->id.oid, &id);
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 "scheduling destruction of empty tunnel %s [%X]\n",
3211 GNUNET_i2s (&id), t->id.tid);
3215 t->delayed_destroy =
3216 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
3217 &tunnel_destroy_empty_delayed,
3223 * Create a new tunnel
3225 * @param owner Who is the owner of the tunnel (short ID).
3226 * @param tid Tunnel Number of the tunnel.
3227 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3228 * @param local Tunnel Number for the tunnel, for the client point of view.
3230 * @return A new initialized tunnel. NULL on error.
3232 static struct MeshTunnel *
3233 tunnel_new (GNUNET_PEER_Id owner,
3234 MESH_TunnelNumber tid,
3235 struct MeshClient *client,
3236 MESH_TunnelNumber local)
3238 struct MeshTunnel *t;
3239 struct GNUNET_HashCode hash;
3241 if (n_tunnels >= max_tunnels && NULL == client)
3244 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3247 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
3248 t->bck_queue_max = t->fwd_queue_max;
3249 t->tree = tree_new (owner);
3251 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
3252 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
3253 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
3254 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
3255 t->local_tid = local;
3257 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3259 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3261 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3262 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3269 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3276 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3278 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3279 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3283 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3294 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3295 * client when the client disconnects. If the client is not the owner, the
3296 * owner will get notified if no more clients are in the tunnel and the client
3297 * get removed from the tunnel's list.
3299 * @param cls closure (client that is disconnecting)
3300 * @param key the hash of the local tunnel id (used to access the hashmap)
3301 * @param value the value stored at the key (tunnel to destroy)
3303 * @return GNUNET_OK, keep iterating.
3306 tunnel_destroy_iterator (void *cls,
3307 const struct GNUNET_HashCode * key,
3310 struct MeshTunnel *t = value;
3311 struct MeshClient *c = cls;
3313 send_client_tunnel_disconnect (t, c);
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
3317 tunnel_delete_client (t, c);
3318 client_delete_tunnel (c, t);
3319 tunnel_destroy_empty (t);
3322 tunnel_send_destroy (t, 0);
3324 t->destroy = GNUNET_YES;
3331 * Timeout function, destroys tunnel if called
3333 * @param cls Closure (tunnel to destroy).
3334 * @param tc TaskContext
3337 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3339 struct MeshTunnel *t = cls;
3340 struct GNUNET_PeerIdentity id;
3342 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3343 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3345 GNUNET_PEER_resolve(t->id.oid, &id);
3346 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3347 "Tunnel %s [%X] timed out. Destroying.\n",
3348 GNUNET_i2s(&id), t->id.tid);
3349 send_clients_tunnel_destroy (t);
3354 * Resets the tunnel timeout. Starts it if no timeout was running.
3356 * @param t Tunnel whose timeout to reset.
3358 * TODO use heap to improve efficiency of scheduler.
3361 tunnel_reset_timeout (struct MeshTunnel *t)
3363 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3364 GNUNET_SCHEDULER_cancel (t->timeout_task);
3366 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3367 (refresh_path_time, 4), &tunnel_timeout, t);
3371 /******************************************************************************/
3372 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3373 /******************************************************************************/
3376 * Function to send a create path packet to a peer.
3378 * @param cls closure
3379 * @param size number of bytes available in buf
3380 * @param buf where the callee should write the message
3381 * @return number of bytes written to buf
3384 send_core_path_create (void *cls, size_t size, void *buf)
3386 struct MeshPathInfo *info = cls;
3387 struct GNUNET_MESH_ManipulatePath *msg;
3388 struct GNUNET_PeerIdentity *peer_ptr;
3389 struct MeshTunnel *t = info->t;
3390 struct MeshPeerPath *p = info->path;
3395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3397 sizeof (struct GNUNET_MESH_ManipulatePath) +
3398 p->length * sizeof (struct GNUNET_PeerIdentity);
3400 if (size < size_needed || NULL == buf)
3405 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3406 msg->header.size = htons (size_needed);
3407 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3408 msg->tid = ntohl (t->id.tid);
3411 if (GNUNET_YES == t->nobuffer)
3412 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3413 msg->opt = htonl(opt);
3416 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3417 for (i = 0; i < p->length; i++)
3419 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3426 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3432 * Fill the core buffer
3434 * @param cls closure (data itself)
3435 * @param size number of bytes available in buf
3436 * @param buf where the callee should write the message
3438 * @return number of bytes written to buf
3441 send_core_data_multicast (void *cls, size_t size, void *buf)
3443 struct MeshTransmissionDescriptor *info = cls;
3446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3447 GNUNET_assert (NULL != info);
3448 GNUNET_assert (NULL != info->peer);
3449 total_size = info->mesh_data->data_len;
3450 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3452 if (total_size > size)
3457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3458 memcpy (buf, info->mesh_data->data, total_size);
3461 struct GNUNET_MessageHeader *mh;
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
3465 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
3468 data_descriptor_decrement_rc (info->mesh_data);
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3477 * Creates a path ack message in buf and frees all unused resources.
3479 * @param cls closure (MeshTransmissionDescriptor)
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_ack (void *cls, size_t size, void *buf)
3487 struct MeshTransmissionDescriptor *info = cls;
3488 struct GNUNET_MESH_PathACK *msg = buf;
3490 GNUNET_assert (NULL != info);
3491 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3496 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3497 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3498 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3499 msg->tid = htonl (info->origin->tid);
3500 msg->peer_id = my_full_id;
3503 /* TODO add signature */
3505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3506 return sizeof (struct GNUNET_MESH_PathACK);
3511 * Free a transmission that was already queued with all resources
3512 * associated to the request.
3514 * @param queue Queue handler to cancel.
3515 * @param clear_cls Is it necessary to free associated cls?
3518 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3520 struct MeshTransmissionDescriptor *dd;
3521 struct MeshPathInfo *path_info;
3522 // unsigned int max;
3524 if (GNUNET_YES == clear_cls)
3526 switch (queue->type)
3528 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3529 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3530 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3532 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3533 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3534 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3535 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3536 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3538 " prebuilt message\n");
3539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3541 GNUNET_MESH_DEBUG_M2S(queue->type));
3543 data_descriptor_decrement_rc (dd->mesh_data);
3545 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3547 path_info = queue->cls;
3548 path_destroy (path_info->path);
3552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3553 " type %s unknown!\n",
3554 GNUNET_MESH_DEBUG_M2S(queue->type));
3556 GNUNET_free_non_null (queue->cls);
3558 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3559 queue->peer->queue_tail,
3562 /* Delete from child_fc in the appropiate tunnel */
3563 // max = queue->tunnel->fwd_queue_max;
3564 // GNUNET_PEER_resolve (queue->peer->id, &id);
3565 // if (NULL != cinfo)
3567 // for (i = 0; i < cinfo->send_buffer_n; i++)
3569 // i2 = (cinfo->send_buffer_start + i) % max;
3570 // if (cinfo->send_buffer[i2] == queue)
3572 // /* Found corresponding entry in the send_buffer. Move all others back. */
3576 // for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
3578 // j2 = (cinfo->send_buffer_start + j) % max;
3579 // j3 = (cinfo->send_buffer_start + j + 1) % max;
3580 // cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
3583 // cinfo->send_buffer[j3] = NULL;
3584 // cinfo->send_buffer_n--;
3589 GNUNET_free (queue);
3594 * @brief Get the next transmittable message from the queue.
3596 * This will be the head, except in the case of being a data packet
3597 * not allowed by the destination peer.
3599 * @param peer Destination peer.
3601 * @return The next viable MeshPeerQueue element to send to that peer.
3602 * NULL when there are no transmittable messages.
3604 struct MeshPeerQueue *
3605 queue_get_next (const struct MeshPeerInfo *peer)
3607 struct MeshPeerQueue *q;
3609 struct MeshTransmissionDescriptor *info;
3610 // struct GNUNET_MESH_Unicast *ucast;
3611 // struct GNUNET_MESH_ToOrigin *to_orig;
3612 struct GNUNET_PeerIdentity id;
3614 // uint32_t ack; FIXME
3616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
3617 for (q = peer->queue_head; NULL != q; q = q->next)
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3623 GNUNET_MESH_DEBUG_M2S(q->type));
3626 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3627 // ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
3628 // pid = ntohl (ucast->pid);
3629 GNUNET_PEER_resolve (info->peer->id, &id);
3630 // ack = cinfo->fwd_ack;
3632 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3633 // to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
3634 // pid = ntohl (to_orig->pid);
3635 // ack = t->bck_ack;
3638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3642 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3643 // "********* ACK: %u, PID: %u\n",
3645 // if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
3647 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 // "********* OK!\n");
3653 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3654 // "********* NEXT!\n");
3657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3658 "********* nothing found\n");
3664 * Core callback to write a queued packet to core buffer
3666 * @param cls Closure (peer info).
3667 * @param size Number of bytes available in buf.
3668 * @param buf Where the to write the message.
3670 * @return number of bytes written to buf
3673 queue_send (void *cls, size_t size, void *buf)
3675 struct MeshPeerInfo *peer = cls;
3676 struct GNUNET_MessageHeader *msg;
3677 struct MeshPeerQueue *queue;
3678 struct MeshTunnel *t;
3679 struct GNUNET_PeerIdentity dst_id;
3682 peer->core_transmit = NULL;
3684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3685 queue = queue_get_next (peer);
3687 /* Queue has no internal mesh traffic nor sendable payload */
3690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
3691 if (NULL == peer->queue_head)
3692 GNUNET_break (0); // Should've been canceled
3695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3697 GNUNET_PEER_resolve (peer->id, &dst_id);
3698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3699 "********* towards %s\n",
3700 GNUNET_i2s(&dst_id));
3701 /* Check if buffer size is enough for the message */
3702 if (queue->size > size)
3704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3705 "********* not enough room, reissue\n");
3706 peer->core_transmit =
3707 GNUNET_CORE_notify_transmit_ready (core_handle,
3710 GNUNET_TIME_UNIT_FOREVER_REL,
3717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3720 GNUNET_assert (0 < t->pending_messages);
3721 t->pending_messages--;
3722 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
3725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3726 "********* unicast: t->q (%u/%u)\n",
3727 t->fwd_queue_n, t->fwd_queue_max);
3729 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
3732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
3736 switch (queue->type)
3739 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3740 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3741 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3742 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3743 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3745 "********* raw: %s\n",
3746 GNUNET_MESH_DEBUG_M2S (queue->type));
3748 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3749 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3750 data_size = send_core_data_raw (queue->cls, size, buf);
3751 msg = (struct GNUNET_MessageHeader *) buf;
3752 switch (ntohs (msg->type)) // Type of preconstructed message
3754 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3755 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3757 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3758 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3764 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3766 data_size = send_core_path_create (queue->cls, size, buf);
3768 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3770 data_size = send_core_path_ack (queue->cls, size, buf);
3772 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
3774 data_size = send_core_data_multicast (queue->cls, size, buf);
3778 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3779 "********* type unknown: %u\n",
3783 switch (queue->type)
3785 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3786 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3787 // if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
3789 // GNUNET_break (0);
3790 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3791 // "at pos %u (%p) != %p\n",
3792 // cinfo->send_buffer_start,
3793 // cinfo->send_buffer[cinfo->send_buffer_start],
3796 // if (cinfo->send_buffer_n > 0)
3798 // cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
3799 // cinfo->send_buffer_n--;
3800 // cinfo->send_buffer_start++;
3801 // cinfo->send_buffer_start %= t->fwd_queue_max;
3805 // GNUNET_break (0);
3812 /* Free queue, but cls was freed by send_core_* */
3813 queue_destroy (queue, GNUNET_NO);
3815 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3821 /* If more data in queue, send next */
3822 queue = queue_get_next(peer);
3825 struct GNUNET_PeerIdentity id;
3827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3828 GNUNET_PEER_resolve (peer->id, &id);
3829 peer->core_transmit =
3830 GNUNET_CORE_notify_transmit_ready(core_handle,
3833 GNUNET_TIME_UNIT_FOREVER_REL,
3841 if (NULL != peer->queue_head)
3843 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3844 "********* %s stalled\n",
3845 GNUNET_i2s(&my_full_id));
3846 // if (NULL == cinfo) FIXME
3847 // cinfo = tunnel_get_neighbor_fc (t, &dst_id);
3848 // // FIXME unify bck/fwd structures, bck does not have cinfo right now
3849 // if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
3851 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3852 // &tunnel_poll, cinfo);
3856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3862 * @brief Queue and pass message to core when possible.
3864 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
3865 * and accounts for it. In case the queue is full, the message is dropped and
3868 * Otherwise, message is treated as internal and allowed to go regardless of
3871 * @param cls Closure (@c type dependant). It will be used by queue_send to
3872 * build the message to be sent if not already prebuilt.
3873 * @param type Type of the message, 0 for a raw message.
3874 * @param size Size of the message.
3875 * @param dst Neighbor to send message to.
3876 * @param t Tunnel this message belongs to.
3879 queue_add (void *cls, uint16_t type, size_t size,
3880 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3882 struct MeshPeerQueue *queue;
3883 struct GNUNET_PeerIdentity id;
3888 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
3889 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
3891 n = &t->fwd_queue_n;
3892 max = &t->fwd_queue_max;
3894 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3896 n = &t->bck_queue_n;
3897 max = &t->bck_queue_max;
3904 GNUNET_STATISTICS_update(stats,
3905 "# messages dropped (buffer full)",
3907 return; // Drop message
3911 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3917 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3918 GNUNET_PEER_resolve (dst->id, &id);
3919 if (NULL == dst->core_transmit)
3921 dst->core_transmit =
3922 GNUNET_CORE_notify_transmit_ready (core_handle,
3925 GNUNET_TIME_UNIT_FOREVER_REL,
3931 t->pending_messages++;
3932 if (NULL == n) // Is this internal mesh traffic?
3935 // It's payload, keep track of buffer per peer. FIXME
3936 // i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
3937 // if (NULL != cinfo->send_buffer[i])
3939 // GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
3940 // queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
3941 // cinfo->send_buffer_start++;
3942 // cinfo->send_buffer_start %= t->fwd_queue_max;
3946 // cinfo->send_buffer_n++;
3948 // cinfo->send_buffer[i] = queue;
3949 // if (cinfo->send_buffer_n > t->fwd_queue_max)
3951 // GNUNET_break (0);
3952 // cinfo->send_buffer_n = t->fwd_queue_max;
3957 /******************************************************************************/
3958 /******************** MESH NETWORK HANDLERS **************************/
3959 /******************************************************************************/
3963 * Core handler for path creation
3965 * @param cls closure
3966 * @param message message
3967 * @param peer peer identity this notification is about
3969 * @return GNUNET_OK to keep the connection open,
3970 * GNUNET_SYSERR to close it (signal serious error)
3973 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3974 const struct GNUNET_MessageHeader *message)
3976 unsigned int own_pos;
3979 MESH_TunnelNumber tid;
3980 struct GNUNET_MESH_ManipulatePath *msg;
3981 struct GNUNET_PeerIdentity *pi;
3982 struct GNUNET_HashCode hash;
3983 struct MeshPeerPath *path;
3984 struct MeshPeerInfo *dest_peer_info;
3985 struct MeshPeerInfo *orig_peer_info;
3986 struct MeshTunnel *t;
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3989 "Received a path create msg [%s]\n",
3990 GNUNET_i2s (&my_full_id));
3991 size = ntohs (message->size);
3992 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3994 GNUNET_break_op (0);
3998 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3999 if (size % sizeof (struct GNUNET_PeerIdentity))
4001 GNUNET_break_op (0);
4004 size /= sizeof (struct GNUNET_PeerIdentity);
4007 GNUNET_break_op (0);
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4011 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4013 tid = ntohl (msg->tid);
4014 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4016 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4017 t = tunnel_get (pi, tid);
4018 if (NULL == t) // FIXME only for INCOMING tunnels?
4022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4023 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4026 // FIXME notify failure
4029 opt = ntohl (msg->opt);
4030 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
4032 t->nobuffer = GNUNET_YES;
4033 t->last_fwd_ack = t->fwd_pid + 1;
4035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
4037 if (GNUNET_YES == t->nobuffer)
4039 t->bck_queue_max = 1;
4040 t->fwd_queue_max = 1;
4043 // FIXME only assign a local tid if a local client is interested (on demand)
4044 while (NULL != tunnel_get_incoming (next_local_tid))
4045 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4046 t->local_tid_dest = next_local_tid++;
4047 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4050 tunnel_reset_timeout (t);
4051 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4053 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4054 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4062 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4063 if (NULL == dest_peer_info)
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4066 " Creating PeerInfo for destination.\n");
4067 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4068 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4069 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4071 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4073 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4074 if (NULL == orig_peer_info)
4076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4077 " Creating PeerInfo for origin.\n");
4078 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4079 orig_peer_info->id = GNUNET_PEER_intern (pi);
4080 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4081 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4084 path = path_new (size);
4086 for (i = 0; i < size; i++)
4088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4089 GNUNET_i2s (&pi[i]));
4090 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4091 if (path->peers[i] == myid)
4094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4097 /* cannot be self, must be 'not found' */
4098 /* create path: self not found in path through self */
4099 GNUNET_break_op (0);
4100 path_destroy (path);
4104 path_add_to_peers (path, GNUNET_NO);
4105 tunnel_add_path (t, path, own_pos);
4106 if (own_pos == size - 1)
4108 /* It is for us! Send ack. */
4109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4110 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4116 struct MeshPeerPath *path2;
4118 /* It's for somebody else! Retransmit. */
4119 path2 = path_duplicate (path);
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4121 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4122 path2 = path_duplicate (path);
4123 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4124 send_create_path (dest_peer_info, path, t);
4131 * Core handler for path destruction
4133 * @param cls closure
4134 * @param message message
4135 * @param peer peer identity this notification is about
4137 * @return GNUNET_OK to keep the connection open,
4138 * GNUNET_SYSERR to close it (signal serious error)
4141 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4142 const struct GNUNET_MessageHeader *message)
4144 struct GNUNET_MESH_ManipulatePath *msg;
4145 struct GNUNET_PeerIdentity *pi;
4146 struct MeshPeerPath *path;
4147 struct MeshTunnel *t;
4148 unsigned int own_pos;
4152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4153 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4154 size = ntohs (message->size);
4155 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4157 GNUNET_break_op (0);
4161 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4162 if (size % sizeof (struct GNUNET_PeerIdentity))
4164 GNUNET_break_op (0);
4167 size /= sizeof (struct GNUNET_PeerIdentity);
4170 GNUNET_break_op (0);
4173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4175 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4176 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4178 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4180 t = tunnel_get (pi, ntohl (msg->tid));
4183 /* TODO notify back: we don't know this tunnel */
4184 GNUNET_break_op (0);
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4188 path = path_new (size);
4190 for (i = 0; i < size; i++)
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4193 GNUNET_i2s (&pi[i]));
4194 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4195 if (path->peers[i] == myid)
4198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4199 if (own_pos < path->length - 1)
4200 send_prebuilt_message (message, &pi[own_pos + 1], t);
4202 send_client_tunnel_disconnect(t, NULL);
4204 // tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
4205 path_destroy (path);
4211 * Core handler for notifications of broken paths
4213 * @param cls closure
4214 * @param message message
4215 * @param peer peer identity this notification is about
4217 * @return GNUNET_OK to keep the connection open,
4218 * GNUNET_SYSERR to close it (signal serious error)
4221 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4222 const struct GNUNET_MessageHeader *message)
4224 struct GNUNET_MESH_PathBroken *msg;
4225 struct MeshTunnel *t;
4227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4228 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4229 msg = (struct GNUNET_MESH_PathBroken *) message;
4230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4231 GNUNET_i2s (&msg->peer1));
4232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4233 GNUNET_i2s (&msg->peer2));
4234 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4237 GNUNET_break_op (0);
4240 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4241 GNUNET_PEER_search (&msg->peer2));
4248 * Core handler for tunnel destruction
4250 * @param cls closure
4251 * @param message message
4252 * @param peer peer identity this notification is about
4254 * @return GNUNET_OK to keep the connection open,
4255 * GNUNET_SYSERR to close it (signal serious error)
4258 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4259 const struct GNUNET_MessageHeader *message)
4261 struct GNUNET_MESH_TunnelDestroy *msg;
4262 struct MeshTunnel *t;
4263 GNUNET_PEER_Id parent;
4266 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4268 "Got a TUNNEL DESTROY packet from %s\n",
4270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4271 " for tunnel %s [%u]\n",
4272 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4273 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4274 /* Check signature */
4277 /* Probably already got the message from another path,
4278 * destroyed the tunnel and retransmitted to children.
4281 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4285 parent = tree_get_predecessor (t->tree);
4286 pid = GNUNET_PEER_search (peer);
4291 // tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
4292 nc = tree_count_children (t->tree);
4293 if (nc > 0 || NULL != t->owner || t->nclients > 0)
4295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4296 "still in use: %u cl, %u ch\n",
4301 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4303 /* Tunnel was incoming, notify clients */
4304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4305 t->local_tid, t->local_tid_dest);
4306 send_clients_tunnel_destroy (t);
4308 tunnel_send_destroy (t, parent);
4309 t->destroy = GNUNET_YES;
4310 // TODO: add timeout to destroy the tunnel anyway
4316 * Core handler for mesh network traffic going from the origin to a peer
4318 * @param cls closure
4319 * @param peer peer identity this notification is about
4320 * @param message message
4321 * @return GNUNET_OK to keep the connection open,
4322 * GNUNET_SYSERR to close it (signal serious error)
4325 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4326 const struct GNUNET_MessageHeader *message)
4328 struct GNUNET_MESH_Unicast *msg;
4329 struct GNUNET_PeerIdentity *neighbor;
4330 struct MeshTunnel *t;
4331 GNUNET_PEER_Id dest_id;
4336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4339 size = ntohs (message->size);
4341 sizeof (struct GNUNET_MESH_Unicast) +
4342 sizeof (struct GNUNET_MessageHeader))
4347 msg = (struct GNUNET_MESH_Unicast *) message;
4348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4349 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4351 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4354 /* TODO notify back: we don't know this tunnel */
4355 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4356 GNUNET_break_op (0);
4359 pid = ntohl (msg->pid);
4360 if (t->fwd_pid == pid)
4362 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4364 " Already seen pid %u, DROPPING!\n", pid);
4369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4370 " pid %u not seen yet, forwarding\n", pid);
4375 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4377 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4378 GNUNET_break_op (0);
4379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4380 "Received PID %u, ACK %u\n",
4381 pid, t->last_fwd_ack);
4385 tunnel_reset_timeout (t);
4387 if (dest_id == myid)
4389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4390 " it's for us! sending to clients...\n");
4391 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4392 send_subscribed_clients (message, &msg[1].header, t);
4393 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4396 ttl = ntohl (msg->ttl);
4397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4400 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4401 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4402 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4406 " not for us, retransmitting...\n");
4408 neighbor = tree_get_first_hop (t->tree, dest_id);
4410 /* cinfo->fwd_pid = pid; FIXME
4412 if (GNUNET_YES == t->nobuffer &&
4413 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
4415 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4416 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
4417 GNUNET_break_op (0);
4420 send_prebuilt_message (message, neighbor, t);
4421 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4427 * Core handler for mesh network traffic toward the owner of a tunnel
4429 * @param cls closure
4430 * @param message message
4431 * @param peer peer identity this notification is about
4433 * @return GNUNET_OK to keep the connection open,
4434 * GNUNET_SYSERR to close it (signal serious error)
4437 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4438 const struct GNUNET_MessageHeader *message)
4440 struct GNUNET_MESH_ToOrigin *msg;
4441 struct GNUNET_PeerIdentity id;
4442 struct MeshPeerInfo *peer_info;
4443 struct MeshTunnel *t;
4444 GNUNET_PEER_Id predecessor;
4448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4450 size = ntohs (message->size);
4451 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4452 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4454 GNUNET_break_op (0);
4457 msg = (struct GNUNET_MESH_ToOrigin *) message;
4458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4459 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4460 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4461 pid = ntohl (msg->pid);
4465 /* TODO notify that we dont know this tunnel (whom)? */
4466 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4468 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4469 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4474 // if (cinfo->bck_pid == pid) FIXME
4476 // /* already seen this packet, drop */
4477 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4478 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4479 // " Already seen pid %u, DROPPING!\n", pid);
4480 // tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4481 // return GNUNET_OK;
4484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4485 " pid %u not seen yet, forwarding\n", pid);
4486 // cinfo->bck_pid = pid; FIXME
4488 if (NULL != t->owner)
4491 struct GNUNET_MESH_ToOrigin *copy;
4493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4494 " it's for us! sending to clients...\n");
4495 /* TODO signature verification */
4496 memcpy (cbuf, message, size);
4497 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4498 copy->tid = htonl (t->local_tid);
4500 copy->pid = htonl (t->bck_pid);
4501 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4502 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4503 ©->header, GNUNET_NO);
4504 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4508 " not for us, retransmitting...\n");
4510 peer_info = peer_info_get (&msg->oid);
4511 if (NULL == peer_info)
4513 /* unknown origin of tunnel */
4517 predecessor = tree_get_predecessor (t->tree);
4518 if (0 == predecessor)
4520 if (GNUNET_YES == t->destroy)
4522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4523 "to orig received on a dying tunnel %s [%X]\n",
4524 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4527 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4528 "unknown to origin at %s\n",
4529 GNUNET_i2s (&my_full_id));
4530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4533 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4534 "for tunnel %s [%X]\n",
4535 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4538 tree_debug (t->tree);
4541 GNUNET_PEER_resolve (predecessor, &id);
4542 send_prebuilt_message (message, &id, t);
4543 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4550 * Core handler for mesh network traffic point-to-point acks.
4552 * @param cls closure
4553 * @param message message
4554 * @param peer peer identity this notification is about
4556 * @return GNUNET_OK to keep the connection open,
4557 * GNUNET_SYSERR to close it (signal serious error)
4560 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4561 const struct GNUNET_MessageHeader *message)
4563 struct GNUNET_MESH_ACK *msg;
4564 struct MeshTunnel *t;
4567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4569 msg = (struct GNUNET_MESH_ACK *) message;
4571 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4575 /* TODO notify that we dont know this tunnel (whom)? */
4576 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4579 ack = ntohl (msg->pid);
4580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4582 /* Is this a forward or backward ACK? */
4583 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
4587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4588 // cinfo->fwd_ack = ack; FIXME
4589 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4590 tunnel_unlock_fwd_queues (t);
4591 // if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll) FIXME
4593 // GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
4594 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
4595 // cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
4600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4602 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4603 tunnel_unlock_bck_queue (t);
4610 * Core handler for mesh network traffic point-to-point ack polls.
4612 * @param cls closure
4613 * @param message message
4614 * @param peer peer identity this notification is about
4616 * @return GNUNET_OK to keep the connection open,
4617 * GNUNET_SYSERR to close it (signal serious error)
4620 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4621 const struct GNUNET_MessageHeader *message)
4623 struct GNUNET_MESH_Poll *msg;
4624 struct MeshTunnel *t;
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4629 msg = (struct GNUNET_MESH_Poll *) message;
4631 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4635 /* TODO notify that we dont know this tunnel (whom)? */
4636 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4637 GNUNET_break_op (0);
4641 /* Is this a forward or backward ACK? */
4642 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4645 /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
4646 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4651 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4658 * Core handler for path ACKs
4660 * @param cls closure
4661 * @param message message
4662 * @param peer peer identity this notification is about
4664 * @return GNUNET_OK to keep the connection open,
4665 * GNUNET_SYSERR to close it (signal serious error)
4668 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4669 const struct GNUNET_MessageHeader *message)
4671 struct GNUNET_MESH_PathACK *msg;
4672 struct GNUNET_PeerIdentity id;
4673 struct MeshPeerInfo *peer_info;
4674 struct MeshPeerPath *p;
4675 struct MeshTunnel *t;
4677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4678 GNUNET_i2s (&my_full_id));
4679 msg = (struct GNUNET_MESH_PathACK *) message;
4680 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4683 /* TODO notify that we don't know the tunnel */
4684 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4686 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4690 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4692 peer_info = peer_info_get (&msg->peer_id);
4693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4694 GNUNET_i2s (&msg->peer_id));
4695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4698 /* Add paths to peers? */
4699 p = tree_get_path_to_peer (t->tree, peer_info->id);
4702 path_add_to_peers (p, GNUNET_YES);
4710 /* Message for us? */
4711 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4714 if (NULL == t->owner)
4716 GNUNET_break_op (0);
4719 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4721 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4722 // send_client_peer_connected (t, peer_info->id);
4724 if (NULL != peer_info->dhtget)
4726 GNUNET_DHT_get_stop (peer_info->dhtget);
4727 peer_info->dhtget = NULL;
4732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4733 " not for us, retransmitting...\n");
4734 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4735 peer_info = peer_info_get (&msg->oid);
4736 send_prebuilt_message (message, &id, t);
4742 * Core handler for mesh keepalives.
4744 * @param cls closure
4745 * @param message message
4746 * @param peer peer identity this notification is about
4747 * @return GNUNET_OK to keep the connection open,
4748 * GNUNET_SYSERR to close it (signal serious error)
4750 * TODO: Check who we got this from, to validate route.
4753 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4754 const struct GNUNET_MessageHeader *message)
4756 struct GNUNET_MESH_TunnelKeepAlive *msg;
4757 struct MeshTunnel *t;
4759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4762 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4763 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4767 /* TODO notify that we dont know that tunnel */
4768 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4773 tunnel_reset_timeout (t);
4775 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4776 /* FIXME tunnel_send_multicast (t, message); */
4783 * Functions to handle messages from core
4785 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4786 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4787 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4788 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4789 sizeof (struct GNUNET_MESH_PathBroken)},
4790 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4791 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4792 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4793 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4794 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4795 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4796 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4797 sizeof (struct GNUNET_MESH_ACK)},
4798 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4799 sizeof (struct GNUNET_MESH_Poll)},
4800 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4801 sizeof (struct GNUNET_MESH_PathACK)},
4807 /******************************************************************************/
4808 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4809 /******************************************************************************/
4814 * notify_client_connection_failure: notify a client that the connection to the
4815 * requested remote peer is not possible (for instance, no route found)
4816 * Function called when the socket is ready to queue more data. "buf" will be
4817 * NULL and "size" zero if the socket was closed for writing in the meantime.
4819 * @param cls closure
4820 * @param size number of bytes available in buf
4821 * @param buf where the callee should write the message
4822 * @return number of bytes written to buf
4825 notify_client_connection_failure (void *cls, size_t size, void *buf)
4828 struct MeshPeerInfo *peer_info;
4829 struct GNUNET_MESH_PeerControl *msg;
4830 struct GNUNET_PeerIdentity id;
4832 if (0 == size && NULL == buf)
4834 // TODO retry? cancel?
4838 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4839 peer_info = (struct MeshPeerInfo *) cls;
4840 msg = (struct GNUNET_MESH_PeerControl *) buf;
4841 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4842 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4843 // msg->tunnel_id = htonl(peer_info->t->tid);
4844 GNUNET_PEER_resolve (peer_info->id, &id);
4845 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4853 * Send keepalive packets for a peer
4855 * @param cls Closure (tunnel for which to send the keepalive).
4856 * @param tc Notification context.
4859 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4861 struct MeshTunnel *t = cls;
4862 struct GNUNET_MESH_TunnelKeepAlive *msg;
4863 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4866 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4867 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4873 "sending keepalive for tunnel %d\n", t->id.tid);
4875 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4876 msg->header.size = htons (size);
4877 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4878 msg->oid = my_full_id;
4879 msg->tid = htonl (t->id.tid);
4880 /* FIXME tunnel_send_multicast (t, &msg->header); */
4882 t->path_refresh_task =
4883 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4884 tunnel_reset_timeout(t);
4889 * Function to process paths received for a new peer addition. The recorded
4890 * paths form the initial tunnel, which can be optimized later.
4891 * Called on each result obtained for the DHT search.
4893 * @param cls closure
4894 * @param exp when will this value expire
4895 * @param key key of the result
4896 * @param get_path path of the get request
4897 * @param get_path_length lenght of get_path
4898 * @param put_path path of the put request
4899 * @param put_path_length length of the put_path
4900 * @param type type of the result
4901 * @param size number of bytes in data
4902 * @param data pointer to the result data
4904 * TODO: re-issue the request after certain time? cancel after X results?
4907 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4908 const struct GNUNET_HashCode * key,
4909 const struct GNUNET_PeerIdentity *get_path,
4910 unsigned int get_path_length,
4911 const struct GNUNET_PeerIdentity *put_path,
4912 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4913 size_t size, const void *data)
4915 struct MeshPathInfo *path_info = cls;
4916 struct MeshPeerPath *p;
4917 struct GNUNET_PeerIdentity pi;
4920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4921 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4924 p = path_build_from_dht (get_path, get_path_length,
4925 put_path, put_path_length);
4926 path_add_to_peers (p, GNUNET_NO);
4928 for (i = 0; i < path_info->peer->ntunnels; i++)
4930 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4931 peer_info_connect (path_info->peer, path_info->t);
4938 /******************************************************************************/
4939 /********************* MESH LOCAL HANDLES **************************/
4940 /******************************************************************************/
4944 * Handler for client disconnection
4946 * @param cls closure
4947 * @param client identification of the client; NULL
4948 * for the last call when the server is destroyed
4951 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4953 struct MeshClient *c;
4954 struct MeshClient *next;
4957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4967 if (c->handle != client)
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4975 GNUNET_SERVER_client_drop (c->handle);
4976 c->shutting_down = GNUNET_YES;
4977 GNUNET_assert (NULL != c->own_tunnels);
4978 GNUNET_assert (NULL != c->incoming_tunnels);
4979 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4980 &tunnel_destroy_iterator, c);
4981 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4982 &tunnel_destroy_iterator, c);
4983 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4984 &tunnel_destroy_iterator, c);
4985 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4986 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4987 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4989 if (NULL != c->types)
4990 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4992 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
4993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4995 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5004 * Handler for new clients
5006 * @param cls closure
5007 * @param client identification of the client
5008 * @param message the actual message, which includes messages the client wants
5011 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5012 const struct GNUNET_MessageHeader *message)
5014 struct GNUNET_MESH_ClientConnect *cc_msg;
5015 struct MeshClient *c;
5021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5022 /* Check data sanity */
5023 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5024 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5025 ntypes = ntohs (cc_msg->types);
5026 if (size != ntypes * sizeof (uint16_t))
5029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5033 /* Create new client structure */
5034 c = GNUNET_malloc (sizeof (struct MeshClient));
5035 c->id = next_client_id++;
5036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5038 GNUNET_SERVER_client_keep (client);
5042 struct GNUNET_HashCode hc;
5044 t = (uint16_t *) &cc_msg[1];
5045 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
5046 for (i = 0; i < ntypes; i++)
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5050 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5052 /* store in clients hashmap */
5053 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5054 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5055 /* store in global hashmap */
5056 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5057 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
5062 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5063 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5064 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5065 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5066 GNUNET_SERVER_notification_context_add (nc, client);
5067 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5069 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5075 * Handler for requests of new tunnels
5077 * @param cls closure
5078 * @param client identification of the client
5079 * @param message the actual message
5082 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5083 const struct GNUNET_MessageHeader *message)
5085 struct GNUNET_MESH_TunnelMessage *t_msg;
5086 struct MeshTunnel *t;
5087 struct MeshClient *c;
5088 MESH_TunnelNumber tid;
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5092 /* Sanity check for client registration */
5093 if (NULL == (c = client_get (client)))
5096 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5101 /* Message sanity check */
5102 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5105 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5109 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5110 /* Sanity check for tunnel numbering */
5111 tid = ntohl (t_msg->tunnel_id);
5112 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5115 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5118 /* Sanity check for duplicate tunnel IDs */
5119 if (NULL != tunnel_get_by_local_id (c, tid))
5122 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5126 while (NULL != tunnel_get_by_pi (myid, next_tid))
5127 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5128 t = tunnel_new (myid, next_tid++, c, tid);
5131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5133 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5136 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5138 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5141 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5147 * Handler for requests of deleting tunnels
5149 * @param cls closure
5150 * @param client identification of the client
5151 * @param message the actual message
5154 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5155 const struct GNUNET_MessageHeader *message)
5157 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5158 struct MeshClient *c;
5159 struct MeshTunnel *t;
5160 MESH_TunnelNumber tid;
5162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5163 "Got a DESTROY TUNNEL from client!\n");
5165 /* Sanity check for client registration */
5166 if (NULL == (c = client_get (client)))
5169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5174 /* Message sanity check */
5175 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5178 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5182 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5184 /* Retrieve tunnel */
5185 tid = ntohl (tunnel_msg->tunnel_id);
5186 t = tunnel_get_by_local_id(c, tid);
5189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5191 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5194 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5196 client_ignore_tunnel (c, t);
5197 tunnel_destroy_empty (t);
5198 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5201 send_client_tunnel_disconnect (t, c);
5202 client_delete_tunnel (c, t);
5204 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5206 tunnel_send_destroy (t, 0);
5207 t->destroy = GNUNET_YES;
5208 /* The tunnel will be destroyed when the last message is transmitted. */
5209 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5215 * Handler for requests of seeting tunnel's buffering policy.
5217 * @param cls Closure (unused).
5218 * @param client Identification of the client.
5219 * @param message The actual message.
5222 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5223 const struct GNUNET_MessageHeader *message)
5225 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5226 struct MeshClient *c;
5227 struct MeshTunnel *t;
5228 MESH_TunnelNumber tid;
5230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5231 "Got a BUFFER request from client!\n");
5233 /* Sanity check for client registration */
5234 if (NULL == (c = client_get (client)))
5237 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5242 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5244 /* Retrieve tunnel */
5245 tid = ntohl (tunnel_msg->tunnel_id);
5246 t = tunnel_get_by_local_id(c, tid);
5249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5251 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5255 switch (ntohs(message->type))
5257 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5258 t->nobuffer = GNUNET_NO;
5260 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5261 t->nobuffer = GNUNET_YES;
5267 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5272 * Handler for client traffic directed to one peer
5274 * @param cls closure
5275 * @param client identification of the client
5276 * @param message the actual message
5279 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5280 const struct GNUNET_MessageHeader *message)
5282 struct MeshClient *c;
5283 struct MeshTunnel *t;
5284 struct GNUNET_MESH_Unicast *data_msg;
5285 MESH_TunnelNumber tid;
5288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5289 "Got a unicast request from a client!\n");
5291 /* Sanity check for client registration */
5292 if (NULL == (c = client_get (client)))
5295 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5300 data_msg = (struct GNUNET_MESH_Unicast *) message;
5302 /* Sanity check for message size */
5303 size = ntohs (message->size);
5304 if (sizeof (struct GNUNET_MESH_Unicast) +
5305 sizeof (struct GNUNET_MessageHeader) > size)
5308 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5312 /* Tunnel exists? */
5313 tid = ntohl (data_msg->tid);
5314 t = tunnel_get_by_local_id (c, tid);
5318 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5322 /* Is it a local tunnel? Then, does client own the tunnel? */
5323 if (t->owner->handle != client)
5326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5330 /* PID should be as expected */
5331 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
5334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5335 "Unicast PID, expected %u, got %u\n",
5336 t->fwd_pid + 1, ntohl (data_msg->pid));
5337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5341 /* Ok, everything is correct, send the message
5342 * (pretend we got it from a mesh peer)
5345 /* Work around const limitation */
5346 char buf[ntohs (message->size)] GNUNET_ALIGN;
5347 struct GNUNET_MESH_Unicast *copy;
5349 copy = (struct GNUNET_MESH_Unicast *) buf;
5350 memcpy (buf, data_msg, size);
5351 copy->oid = my_full_id;
5352 copy->tid = htonl (t->id.tid);
5353 copy->ttl = htonl (default_ttl);
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5355 " calling generic handler...\n");
5356 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
5358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5359 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5366 * Handler for client traffic directed to the origin
5368 * @param cls closure
5369 * @param client identification of the client
5370 * @param message the actual message
5373 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5374 const struct GNUNET_MessageHeader *message)
5376 struct GNUNET_MESH_ToOrigin *data_msg;
5377 struct MeshTunnelClientInfo *clinfo;
5378 struct MeshClient *c;
5379 struct MeshTunnel *t;
5380 MESH_TunnelNumber tid;
5383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5384 "Got a ToOrigin request from a client!\n");
5385 /* Sanity check for client registration */
5386 if (NULL == (c = client_get (client)))
5389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5394 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5396 /* Sanity check for message size */
5397 size = ntohs (message->size);
5398 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5399 sizeof (struct GNUNET_MessageHeader) > size)
5402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5406 /* Tunnel exists? */
5407 tid = ntohl (data_msg->tid);
5408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5409 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5415 t = tunnel_get_by_local_id (c, tid);
5418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5421 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5425 /* It should be sent by someone who has this as incoming tunnel. */
5426 if (GNUNET_NO == client_knows_tunnel (c, t))
5429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5433 /* PID should be as expected */
5434 clinfo = tunnel_get_client_fc (t, c);
5435 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
5438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5439 "To Origin PID, expected %u, got %u\n",
5440 clinfo->bck_pid + 1,
5441 ntohl (data_msg->pid));
5442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5447 /* Ok, everything is correct, send the message
5448 * (pretend we got it from a mesh peer)
5451 char buf[ntohs (message->size)] GNUNET_ALIGN;
5452 struct GNUNET_MESH_ToOrigin *copy;
5454 /* Work around const limitation */
5455 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5456 memcpy (buf, data_msg, size);
5457 GNUNET_PEER_resolve (t->id.oid, ©->oid);
5458 copy->tid = htonl (t->id.tid);
5459 copy->ttl = htonl (default_ttl);
5460 copy->pid = htonl (t->bck_pid + 1);
5462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5463 " calling generic handler...\n");
5464 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
5466 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5473 * Handler for client's ACKs for payload traffic.
5475 * @param cls Closure (unused).
5476 * @param client Identification of the client.
5477 * @param message The actual message.
5480 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5481 const struct GNUNET_MessageHeader *message)
5483 struct GNUNET_MESH_LocalAck *msg;
5484 struct MeshTunnel *t;
5485 struct MeshClient *c;
5486 MESH_TunnelNumber tid;
5489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5490 /* Sanity check for client registration */
5491 if (NULL == (c = client_get (client)))
5494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5499 msg = (struct GNUNET_MESH_LocalAck *) message;
5501 /* Tunnel exists? */
5502 tid = ntohl (msg->tunnel_id);
5503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5504 t = tunnel_get_by_local_id (c, tid);
5508 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5514 ack = ntohl (msg->max_pid);
5515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
5517 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5518 if (NULL != t->owner && t->owner->handle == client)
5520 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5522 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5526 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5527 tunnel_set_client_fwd_ack (t, c, ack);
5528 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5531 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5539 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5541 * @param cls Closure (client handle).
5542 * @param key Key (hashed tunnel ID, unused).
5543 * @param value Tunnel info.
5545 * @return GNUNET_YES, to keep iterating.
5548 monitor_all_tunnels_iterator (void *cls,
5549 const struct GNUNET_HashCode * key,
5552 struct GNUNET_SERVER_Client *client = cls;
5553 struct MeshTunnel *t = value;
5554 struct GNUNET_MESH_LocalMonitor *msg;
5556 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5557 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5558 msg->tunnel_id = htonl (t->id.tid);
5559 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5560 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5561 GNUNET_PEER_resolve (t->peer, &msg->destination);
5563 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5564 "* sending info about tunnel %s [%u]\n",
5565 GNUNET_i2s (&msg->owner), t->id.tid);
5567 GNUNET_SERVER_notification_context_unicast (nc, client,
5568 &msg->header, GNUNET_NO);
5574 * Handler for client's MONITOR request.
5576 * @param cls Closure (unused).
5577 * @param client Identification of the client.
5578 * @param message The actual message.
5581 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5582 const struct GNUNET_MessageHeader *message)
5584 struct MeshClient *c;
5586 /* Sanity check for client registration */
5587 if (NULL == (c = client_get (client)))
5590 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5594 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5595 "Received get tunnels request from client %u\n",
5597 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5598 monitor_all_tunnels_iterator,
5600 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5601 "Get tunnels request from client %u completed\n",
5603 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5608 * Handler for client's MONITOR_TUNNEL request.
5610 * @param cls Closure (unused).
5611 * @param client Identification of the client.
5612 * @param message The actual message.
5615 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5616 const struct GNUNET_MessageHeader *message)
5618 const struct GNUNET_MESH_LocalMonitor *msg;
5619 struct GNUNET_MESH_LocalMonitor *resp;
5620 struct MeshClient *c;
5621 struct MeshTunnel *t;
5623 /* Sanity check for client registration */
5624 if (NULL == (c = client_get (client)))
5627 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5631 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5632 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5633 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5636 ntohl (msg->tunnel_id));
5637 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5640 /* We don't know the tunnel FIXME */
5641 struct GNUNET_MESH_LocalMonitor warn;
5644 GNUNET_SERVER_notification_context_unicast (nc, client,
5647 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5651 /* Initialize context */
5652 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5654 GNUNET_PEER_resolve (t->peer, &resp->destination);
5655 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5656 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5657 &resp->header, GNUNET_NO);
5660 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5661 "Monitor tunnel request from client %u completed\n",
5663 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5668 * Functions to handle messages from clients
5670 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5671 {&handle_local_new_client, NULL,
5672 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5673 {&handle_local_tunnel_create, NULL,
5674 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5675 sizeof (struct GNUNET_MESH_TunnelMessage)},
5676 {&handle_local_tunnel_destroy, NULL,
5677 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5678 sizeof (struct GNUNET_MESH_TunnelMessage)},
5679 {&handle_local_tunnel_buffer, NULL,
5680 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
5681 sizeof (struct GNUNET_MESH_TunnelMessage)},
5682 {&handle_local_tunnel_buffer, NULL,
5683 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
5684 sizeof (struct GNUNET_MESH_TunnelMessage)},
5685 {&handle_local_unicast, NULL,
5686 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5687 {&handle_local_to_origin, NULL,
5688 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5689 {&handle_local_ack, NULL,
5690 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5691 sizeof (struct GNUNET_MESH_LocalAck)},
5692 {&handle_local_get_tunnels, NULL,
5693 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5694 sizeof (struct GNUNET_MessageHeader)},
5695 {&handle_local_show_tunnel, NULL,
5696 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5697 sizeof (struct GNUNET_MESH_LocalMonitor)},
5703 * To be called on core init/fail.
5705 * @param cls service closure
5706 * @param server handle to the server for this service
5707 * @param identity the public identity of this peer
5710 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5711 const struct GNUNET_PeerIdentity *identity)
5714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5715 core_handle = server;
5716 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5719 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5720 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5722 GNUNET_i2s (identity));
5723 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5725 GNUNET_i2s (&my_full_id));
5726 GNUNET_SCHEDULER_shutdown (); // Try gracefully
5728 GNUNET_abort(); // Try harder
5735 * Method called whenever a given peer connects.
5737 * @param cls closure
5738 * @param peer peer identity this notification is about
5741 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5743 struct MeshPeerInfo *peer_info;
5744 struct MeshPeerPath *path;
5746 DEBUG_CONN ("Peer connected\n");
5747 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5748 peer_info = peer_info_get (peer);
5749 if (myid == peer_info->id)
5751 DEBUG_CONN (" (self)\n");
5756 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5758 path = path_new (2);
5759 path->peers[0] = myid;
5760 path->peers[1] = peer_info->id;
5761 GNUNET_PEER_change_rc (myid, 1);
5762 GNUNET_PEER_change_rc (peer_info->id, 1);
5763 peer_info_add_path (peer_info, path, GNUNET_YES);
5764 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5770 * Method called whenever a peer disconnects.
5772 * @param cls closure
5773 * @param peer peer identity this notification is about
5776 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5778 struct MeshPeerInfo *pi;
5779 struct MeshPeerQueue *q;
5780 struct MeshPeerQueue *n;
5782 DEBUG_CONN ("Peer disconnected\n");
5783 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5793 /* TODO try to reroute this traffic instead */
5794 queue_destroy(q, GNUNET_YES);
5797 if (NULL != pi->core_transmit)
5799 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5800 pi->core_transmit = NULL;
5802 peer_info_remove_path (pi, pi->id, myid);
5805 DEBUG_CONN (" (self)\n");
5807 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5812 /******************************************************************************/
5813 /************************ MAIN FUNCTIONS ****************************/
5814 /******************************************************************************/
5817 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5819 * @param cls closure
5820 * @param key current key code
5821 * @param value value in the hash map
5822 * @return GNUNET_YES if we should continue to iterate,
5826 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5828 struct MeshTunnel *t = value;
5835 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5837 * @param cls closure
5838 * @param key current key code
5839 * @param value value in the hash map
5840 * @return GNUNET_YES if we should continue to iterate,
5844 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5846 struct MeshPeerInfo *p = value;
5847 struct MeshPeerQueue *q;
5848 struct MeshPeerQueue *n;
5856 queue_destroy(q, GNUNET_YES);
5860 peer_info_destroy (p);
5866 * Task run during shutdown.
5872 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5876 if (core_handle != NULL)
5878 GNUNET_CORE_disconnect (core_handle);
5883 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
5886 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5887 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5888 if (dht_handle != NULL)
5890 GNUNET_DHT_disconnect (dht_handle);
5895 GNUNET_SERVER_notification_context_destroy (nc);
5898 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5900 GNUNET_SCHEDULER_cancel (announce_id_task);
5901 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5908 * Callback for hostkey read/generation.
5910 * @param cls Closure (Configuration handle).
5911 * @param pk The ECC private key.
5912 * @param emsg Error message, if any.
5915 key_generation_cb (void *cls,
5916 struct GNUNET_CRYPTO_EccPrivateKey *pk,
5919 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5920 struct MeshPeerInfo *peer;
5921 struct MeshPeerPath *p;
5926 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5927 _("Mesh service could not access hostkey: %s. Exiting.\n"),
5929 GNUNET_SCHEDULER_shutdown ();
5932 my_private_key = pk;
5933 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5934 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5935 &my_full_id.hashPubKey);
5936 myid = GNUNET_PEER_intern (&my_full_id);
5937 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5938 "Mesh for peer [%s] starting\n",
5939 GNUNET_i2s(&my_full_id));
5941 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5942 NULL, /* Closure passed to MESH functions */
5943 &core_init, /* Call core_init once connected */
5944 &core_connect, /* Handle connects */
5945 &core_disconnect, /* remove peers on disconnects */
5946 NULL, /* Don't notify about all incoming messages */
5947 GNUNET_NO, /* For header only in notification */
5948 NULL, /* Don't notify about all outbound messages */
5949 GNUNET_NO, /* For header-only out notification */
5950 core_handlers); /* Register these handlers */
5952 if (core_handle == NULL)
5955 GNUNET_SCHEDULER_shutdown ();
5960 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5963 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5964 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5965 GNUNET_SERVER_disconnect_notify (server_handle,
5966 &handle_local_client_disconnect, NULL);
5970 clients_tail = NULL;
5973 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5975 /* Create a peer_info for the local peer */
5976 peer = peer_info_get (&my_full_id);
5979 GNUNET_PEER_change_rc (myid, 1);
5980 peer_info_add_path (peer, p, GNUNET_YES);
5981 GNUNET_SERVER_resume (server_handle);
5982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5987 * Process mesh requests.
5989 * @param cls closure
5990 * @param server the initialized server
5991 * @param c configuration to use
5994 run (void *cls, struct GNUNET_SERVER_Handle *server,
5995 const struct GNUNET_CONFIGURATION_Handle *c)
5999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6000 server_handle = server;
6003 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6008 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6009 "mesh", "peer/privatekey");
6010 GNUNET_SCHEDULER_shutdown ();
6015 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6016 &refresh_path_time))
6018 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6020 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6021 "mesh", "refresh path time");
6022 GNUNET_SCHEDULER_shutdown ();
6027 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6028 &app_announce_time))
6030 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6032 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6033 "mesh", "app announce time");
6034 GNUNET_SCHEDULER_shutdown ();
6037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6038 "APP_ANNOUNCE_TIME %llu ms\n",
6039 app_announce_time.rel_value);
6041 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6044 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6046 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6047 "mesh", "id announce time");
6048 GNUNET_SCHEDULER_shutdown ();
6053 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6058 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6059 "mesh", "connect timeout");
6060 GNUNET_SCHEDULER_shutdown ();
6065 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6070 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6071 "mesh", "max msgs queue");
6072 GNUNET_SCHEDULER_shutdown ();
6077 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6080 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6082 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6083 "mesh", "max tunnels");
6084 GNUNET_SCHEDULER_shutdown ();
6089 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6092 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6094 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6095 "mesh", "default ttl", 64);
6100 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6104 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6105 "mesh", "max peers", 1000);
6110 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6111 &dht_replication_level))
6113 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6115 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6116 "mesh", "dht replication level", 3);
6117 dht_replication_level = 3;
6120 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6121 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6122 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6123 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6125 dht_handle = GNUNET_DHT_connect (c, 64);
6126 if (NULL == dht_handle)
6130 stats = GNUNET_STATISTICS_create ("mesh", c);
6132 GNUNET_SERVER_suspend (server_handle);
6133 /* Scheduled the task to clean up when shutdown is called */
6134 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6136 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
6139 GNUNET_free (keyfile);
6144 * The main function for the mesh service.
6146 * @param argc number of arguments from the command line
6147 * @param argv command line arguments
6148 * @return 0 ok, 1 on error
6151 main (int argc, char *const *argv)
6156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6157 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6159 ret = (GNUNET_OK == r) ? 0 : 1;
6160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
6164 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6165 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
6166 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);