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 * Next hop in the tunnel.
389 GNUNET_PEER_Id next_hop;
392 * Previous hop in the tunnel.
394 GNUNET_PEER_Id prev_hop;
397 * Client owner of the tunnel, if any
399 struct MeshClient *owner;
402 * Clients that have been informed about and want to stay in the tunnel.
404 struct MeshClient **clients;
407 * Flow control info for each client.
409 struct MeshTunnelClientInfo *clients_fc;
412 * Number of elements in clients/clients_fc
414 unsigned int nclients;
417 * Clients that have been informed but requested to leave the tunnel.
419 struct MeshClient **ignore;
422 * Number of elements in clients
424 unsigned int nignore;
427 * Bloomfilter (for peer identities) to stop circular routes
429 char bloomfilter[MESH_BLOOM_SIZE];
435 struct MeshTunnelTree *tree;
438 * Task to keep the used paths alive
440 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
443 * Task to destroy the tunnel after timeout
445 * FIXME: merge the two? a tunnel will have either
446 * a path refresh OR a timeout, never both!
448 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
451 * Flag to signal the destruction of the tunnel.
452 * If this is set GNUNET_YES the tunnel will be destroyed
453 * when the queue is empty.
458 * Total messages pending for this tunnels, payload or not.
460 unsigned int pending_messages;
463 * If the tunnel is empty, destoy it.
465 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
470 * Info about a leaf client of a tunnel, needed to perform flow control.
472 struct MeshTunnelClientInfo
475 * PID of the last packet sent to the client (FWD).
480 * PID of the last packet received from the client (BCK).
485 * Maximum PID allowed (FWD ACK received).
490 * Last ACK sent to that child (BCK ACK).
497 * Info needed to work with tunnel paths and peers
504 struct MeshTunnel *t;
507 * Neighbouring peer to whom we send the packet to
509 struct MeshPeerInfo *peer;
514 struct MeshPeerPath *path;
519 * Struct containing information about a client of the service
526 struct MeshClient *next;
531 struct MeshClient *prev;
534 * Tunnels that belong to this client, indexed by local id
536 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
539 * Tunnels this client has accepted, indexed by incoming local id
541 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
544 * Tunnels this client has rejected, indexed by incoming local id
546 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
548 * Handle to communicate with the client
550 struct GNUNET_SERVER_Client *handle;
553 * Messages that this client has declared interest in
555 struct GNUNET_CONTAINER_MultiHashMap *types;
558 * Whether the client is active or shutting down (don't send confirmations
559 * to a client that is shutting down.
564 * ID of the client, mainly for debug messages
571 /******************************************************************************/
572 /************************ DEBUG FUNCTIONS ****************************/
573 /******************************************************************************/
577 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
578 * @param cls string to print
579 * @param success GNUNET_OK if the PUT was transmitted,
580 * GNUNET_NO on timeout,
581 * GNUNET_SYSERR on disconnect from service
582 * after the PUT message was transmitted
583 * (so we don't know if it was received or not)
588 mesh_debug (void *cls, int success)
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
596 unsigned int debug_fwd_ack;
597 unsigned int debug_bck_ack;
601 /******************************************************************************/
602 /*********************** GLOBAL VARIABLES ****************************/
603 /******************************************************************************/
605 /************************** Configuration parameters **************************/
608 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
610 static struct GNUNET_TIME_Relative refresh_path_time;
613 * How often to PUT own ID in the DHT.
615 static struct GNUNET_TIME_Relative id_announce_time;
618 * Maximum time allowed to connect to a peer found by string.
620 static struct GNUNET_TIME_Relative connect_timeout;
623 * Default TTL for payload packets.
625 static unsigned long long default_ttl;
628 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
630 static unsigned long long dht_replication_level;
633 * How many tunnels are we willing to maintain.
634 * Local tunnels are always allowed, even if there are more tunnels than max.
636 static unsigned long long max_tunnels;
639 * How many messages *in total* are we willing to queue, divided by number of
640 * tunnels to get tunnel queue size.
642 static unsigned long long max_msgs_queue;
645 * How many peers do we want to remember?
647 static unsigned long long max_peers;
650 /*************************** Static global variables **************************/
653 * Hostkey generation context
655 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
658 * DLL with all the clients, head.
660 static struct MeshClient *clients_head;
663 * DLL with all the clients, tail.
665 static struct MeshClient *clients_tail;
668 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
670 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
673 * Number of tunnels known.
675 static unsigned long long n_tunnels;
678 * Tunnels incoming, indexed by MESH_TunnelNumber
679 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
681 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
684 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
686 static struct GNUNET_CONTAINER_MultiHashMap *peers;
689 * Handle to communicate with transport
691 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
694 * Handle to communicate with core.
696 static struct GNUNET_CORE_Handle *core_handle;
701 static struct GNUNET_DHT_Handle *dht_handle;
706 static struct GNUNET_SERVER_Handle *server_handle;
709 * Handle to the statistics service.
711 static struct GNUNET_STATISTICS_Handle *stats;
714 * Notification context, to send messages to local clients.
716 static struct GNUNET_SERVER_NotificationContext *nc;
719 * Local peer own ID (memory efficient handle).
721 static GNUNET_PEER_Id myid;
724 * Local peer own ID (full value).
726 static struct GNUNET_PeerIdentity my_full_id;
731 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
736 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
739 * Tunnel ID for the next created tunnel (global tunnel number).
741 static MESH_TunnelNumber next_tid;
744 * Tunnel ID for the next incoming tunnel (local tunnel number).
746 static MESH_TunnelNumber next_local_tid;
749 * All message types clients of this peer are interested in.
751 static struct GNUNET_CONTAINER_MultiHashMap *types;
754 * Task to periodically announce itself in the network.
756 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
759 * Next ID to assign to a client.
761 unsigned int next_client_id;
764 /******************************************************************************/
765 /*********************** DECLARATIONS **************************/
766 /******************************************************************************/
769 * Function to process paths received for a new peer addition. The recorded
770 * paths form the initial tunnel, which can be optimized later.
771 * Called on each result obtained for the DHT search.
774 * @param exp when will this value expire
775 * @param key key of the result
776 * @param type type of the result
777 * @param size number of bytes in data
778 * @param data pointer to the result data
781 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
782 const struct GNUNET_HashCode * key,
783 const struct GNUNET_PeerIdentity *get_path,
784 unsigned int get_path_length,
785 const struct GNUNET_PeerIdentity *put_path,
786 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
787 size_t size, const void *data);
791 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
792 * and insert it in the appropiate structures if the peer is not known yet.
794 * @param peer Full identity of the peer.
796 * @return Existing or newly created peer info.
798 static struct MeshPeerInfo *
799 peer_info_get (const struct GNUNET_PeerIdentity *peer);
803 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
804 * and insert it in the appropiate structures if the peer is not known yet.
806 * @param peer Short identity of the peer.
808 * @return Existing or newly created peer info.
810 static struct MeshPeerInfo *
811 peer_info_get_short (const GNUNET_PEER_Id peer);
815 * Try to establish a new connection to this peer.
816 * Use the best path for the given tunnel.
817 * If the peer doesn't have any path to it yet, try to get one.
818 * If the peer already has some path, send a CREATE PATH towards it.
820 * @param peer PeerInfo of the peer.
821 * @param t Tunnel for which to create the path, if possible.
824 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
828 * Build a PeerPath from the paths returned from the DHT, reversing the paths
829 * to obtain a local peer -> destination path and interning the peer ids.
831 * @return Newly allocated and created path
833 static struct MeshPeerPath *
834 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
835 unsigned int get_path_length,
836 const struct GNUNET_PeerIdentity *put_path,
837 unsigned int put_path_length);
841 * Adds a path to the peer_infos of all the peers in the path
843 * @param p Path to process.
844 * @param confirmed Whether we know if the path works or not.
847 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
852 * Search for a tunnel by global ID using full PeerIdentities.
854 * @param oid owner of the tunnel.
855 * @param tid global tunnel number.
857 * @return tunnel handler, NULL if doesn't exist.
859 static struct MeshTunnel *
860 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
864 * Delete an active client from the tunnel.
870 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
873 * Notify a tunnel that a connection has broken that affects at least
876 * @param t Tunnel affected.
877 * @param p1 Peer that got disconnected from p2.
878 * @param p2 Peer that got disconnected from p1.
880 * @return Short ID of the peer disconnected (either p1 or p2).
881 * 0 if the tunnel remained unaffected.
883 static GNUNET_PEER_Id
884 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
889 * Get the current ack value for a tunnel, for data going from root to leaves,
890 * taking in account the tunnel mode and the status of all children and clients.
894 * @return Maximum PID allowed.
897 tunnel_get_fwd_ack (struct MeshTunnel *t);
901 * Add a client to a tunnel, initializing all needed data structures.
903 * @param t Tunnel to which add the client.
904 * @param c Client which to add to the tunnel.
907 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
911 * @brief Queue and pass message to core when possible.
913 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
914 * and accounts for it. In case the queue is full, the message is dropped and
917 * Otherwise, message is treated as internal and allowed to go regardless of
920 * @param cls Closure (@c type dependant). It will be used by queue_send to
921 * build the message to be sent if not already prebuilt.
922 * @param type Type of the message, 0 for a raw message.
923 * @param size Size of the message.
924 * @param dst Neighbor to send message to.
925 * @param t Tunnel this message belongs to.
928 queue_add (void *cls, uint16_t type, size_t size,
929 struct MeshPeerInfo *dst, struct MeshTunnel *t);
933 * Free a transmission that was already queued with all resources
934 * associated to the request.
936 * @param queue Queue handler to cancel.
937 * @param clear_cls Is it necessary to free associated cls?
940 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
944 * @brief Get the next transmittable message from the queue.
946 * This will be the head, except in the case of being a data packet
947 * not allowed by the destination peer.
949 * @param peer Destination peer.
951 * @return The next viable MeshPeerQueue element to send to that peer.
952 * NULL when there are no transmittable messages.
954 struct MeshPeerQueue *
955 queue_get_next (const struct MeshPeerInfo *peer);
959 * Core callback to write a queued packet to core buffer
961 * @param cls Closure (peer info).
962 * @param size Number of bytes available in buf.
963 * @param buf Where the to write the message.
965 * @return number of bytes written to buf
968 queue_send (void *cls, size_t size, void *buf);
971 /******************************************************************************/
972 /************************ PERIODIC FUNCTIONS ****************************/
973 /******************************************************************************/
976 * Periodically announce self id in the DHT
979 * @param tc task context
982 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
986 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
988 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
992 * - Set data expiration in function of X
995 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
997 block.id = my_full_id;
998 block.type = htonl (0);
999 GNUNET_DHT_put (dht_handle, /* DHT handle */
1000 &my_full_id.hashPubKey, /* Key to use */
1001 dht_replication_level, /* Replication level */
1002 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1003 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1004 sizeof (block), /* Size of the data */
1005 (const char *) &block, /* Data itself */
1006 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1007 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1008 NULL, /* Continuation */
1009 NULL); /* Continuation closure */
1011 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1015 /******************************************************************************/
1016 /****************** GENERAL HELPER FUNCTIONS ************************/
1017 /******************************************************************************/
1020 * Decrements the reference counter and frees all resources if needed
1022 * @param mesh_data Data Descriptor used in a multicast message.
1023 * Freed no longer needed (last message).
1026 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1028 if (0 == --(mesh_data->reference_counter))
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1031 GNUNET_free (mesh_data->data);
1032 GNUNET_free (mesh_data);
1038 * Check if client has registered with the service and has not disconnected
1040 * @param client the client to check
1042 * @return non-NULL if client exists in the global DLL
1044 static struct MeshClient *
1045 client_get (struct GNUNET_SERVER_Client *client)
1047 struct MeshClient *c;
1052 if (c->handle == client)
1061 * Checks if a given client has subscribed to certain message type
1063 * @param message_type Type of message to check
1064 * @param c Client to check
1066 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1068 * FIXME: use of crypto_hash slows it down
1069 * The hash function alone takes 8-10us out of the ~55us for the whole
1070 * process of retransmitting the message from one local client to another.
1071 * Find faster implementation!
1074 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1076 struct GNUNET_HashCode hc;
1078 if (NULL == c->types)
1081 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1082 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1087 * Check whether client wants traffic from a tunnel.
1089 * @param c Client to check.
1090 * @param t Tunnel to be found.
1092 * @return GNUNET_YES if client knows tunnel.
1094 * TODO look in client hashmap
1097 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1101 for (i = 0; i < t->nclients; i++)
1102 if (t->clients[i] == c)
1109 * Check whether client has been informed about a tunnel.
1111 * @param c Client to check.
1112 * @param t Tunnel to be found.
1114 * @return GNUNET_YES if client knows tunnel.
1116 * TODO look in client hashmap
1119 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1123 for (i = 0; i < t->nignore; i++)
1124 if (t->ignore[i] == c)
1126 return client_wants_tunnel(c, t);
1131 * Marks a client as uninterested in traffic from the tunnel, updating both
1132 * client and tunnel to reflect this.
1134 * @param c Client that doesn't want traffic anymore.
1135 * @param t Tunnel which should be ignored.
1137 * FIXME when to delete an incoming tunnel?
1140 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1142 struct GNUNET_HashCode hash;
1144 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1145 GNUNET_break (GNUNET_YES ==
1146 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1148 GNUNET_break (GNUNET_YES ==
1149 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1150 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1151 tunnel_delete_active_client (t, c);
1152 GNUNET_array_append (t->ignore, t->nignore, c);
1157 * Deletes a tunnel from a client (either owner or destination). To be used on
1158 * tunnel destroy, otherwise, use client_ignore_tunnel.
1160 * @param c Client whose tunnel to delete.
1161 * @param t Tunnel which should be deleted.
1164 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1166 struct GNUNET_HashCode hash;
1170 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1171 GNUNET_assert (GNUNET_YES ==
1172 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1178 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1180 GNUNET_assert (GNUNET_YES ==
1181 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1185 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1193 * Send the message to all clients that have subscribed to its type
1195 * @param msg Pointer to the message itself
1196 * @param payload Pointer to the payload of the message.
1197 * @param t The tunnel to whose clients this message goes.
1199 * @return number of clients this message was sent to
1202 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1203 const struct GNUNET_MessageHeader *payload,
1204 struct MeshTunnel *t)
1206 struct MeshClient *c;
1207 MESH_TunnelNumber *tid;
1210 char cbuf[htons (msg->size)];
1212 type = ntohs (payload->type);
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1215 GNUNET_MESH_DEBUG_M2S (type));
1217 memcpy (cbuf, msg, sizeof (cbuf));
1218 switch (htons (msg->type))
1220 struct GNUNET_MESH_Unicast *uc;
1221 struct GNUNET_MESH_ToOrigin *to;
1223 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1224 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1227 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1228 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1236 for (count = 0, c = clients_head; c != NULL; c = c->next)
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1239 if (client_is_subscribed (type, c))
1241 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1245 *tid = htonl (t->local_tid);
1249 if (GNUNET_NO == client_knows_tunnel (c, t))
1251 /* This client doesn't know the tunnel */
1252 struct GNUNET_MESH_TunnelNotification tmsg;
1253 struct GNUNET_HashCode hash;
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1256 tmsg.header.size = htons (sizeof (tmsg));
1257 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1258 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1259 tmsg.tunnel_id = htonl (t->local_tid_dest);
1261 if (GNUNET_YES == t->nobuffer)
1262 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1263 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1264 &tmsg.header, GNUNET_NO);
1265 tunnel_add_client (t, c);
1266 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1268 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1269 c->incoming_tunnels, &hash, t,
1270 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1272 *tid = htonl (t->local_tid_dest);
1275 /* Check if the client wants to get traffic from the tunnel */
1276 if (GNUNET_NO == client_wants_tunnel(c, t))
1279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1280 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1281 (struct GNUNET_MessageHeader
1282 *) cbuf, GNUNET_NO);
1291 * Notify all clients (not depending on registration status) that the incoming
1292 * tunnel is no longer valid.
1294 * @param t Tunnel that was destroyed.
1297 send_clients_tunnel_destroy (struct MeshTunnel *t)
1299 struct GNUNET_MESH_TunnelMessage msg;
1301 msg.header.size = htons (sizeof (msg));
1302 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1303 msg.tunnel_id = htonl (t->local_tid_dest);
1304 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1309 * Notify clients of tunnel disconnections, if needed.
1310 * In case the origin disconnects, the destination clients get a tunnel destroy
1311 * notification. If the last destination disconnects (only one remaining client
1312 * in tunnel), the origin gets a (local ID) peer disconnected.
1313 * Note that the function must be called BEFORE removing the client from
1316 * @param t Tunnel that was destroyed.
1317 * @param c Client that disconnected.
1320 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1326 struct GNUNET_MESH_TunnelMessage msg;
1328 msg.header.size = htons (sizeof (msg));
1329 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1330 msg.tunnel_id = htonl (t->local_tid_dest);
1331 for (i = 0; i < t->nclients; i++)
1332 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1333 &msg.header, GNUNET_NO);
1335 // FIXME when to disconnect an incoming tunnel?
1340 * Iterator over all the peers to remove the oldest not-used entry.
1342 * @param cls Closure (unsued).
1343 * @param key ID of the peer.
1344 * @param value Peer_Info of the peer.
1349 peer_info_timeout (void *cls,
1350 const struct GNUNET_HashCode *key,
1357 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1358 * and insert it in the appropiate structures if the peer is not known yet.
1360 * @param peer Full identity of the peer.
1362 * @return Existing or newly created peer info.
1364 static struct MeshPeerInfo *
1365 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1367 struct MeshPeerInfo *peer_info;
1369 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1370 if (NULL == peer_info)
1373 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1374 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1376 GNUNET_CONTAINER_multihashmap_iterate (peers,
1380 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1382 peer_info->id = GNUNET_PEER_intern (peer);
1384 peer_info->last_contact = GNUNET_TIME_absolute_get();
1391 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1392 * and insert it in the appropiate structures if the peer is not known yet.
1394 * @param peer Short identity of the peer.
1396 * @return Existing or newly created peer info.
1398 static struct MeshPeerInfo *
1399 peer_info_get_short (const GNUNET_PEER_Id peer)
1401 struct GNUNET_PeerIdentity id;
1403 GNUNET_PEER_resolve (peer, &id);
1404 return peer_info_get (&id);
1409 * Iterator to remove the tunnel from the list of tunnels a peer participates
1412 * @param cls Closure (tunnel info)
1413 * @param key GNUNET_PeerIdentity of the peer (unused)
1414 * @param value PeerInfo of the peer
1416 * @return always GNUNET_YES, to keep iterating
1419 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1421 struct MeshTunnel *t = cls;
1422 struct MeshPeerInfo *peer = value;
1425 for (i = 0; i < peer->ntunnels; i++)
1428 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1431 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1433 GNUNET_realloc (peer->tunnels,
1434 peer->ntunnels * sizeof(struct MeshTunnel *));
1443 * Core callback to write a pre-constructed data packet to core buffer
1445 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1446 * @param size Number of bytes available in buf.
1447 * @param buf Where the to write the message.
1449 * @return number of bytes written to buf
1452 send_core_data_raw (void *cls, size_t size, void *buf)
1454 struct MeshTransmissionDescriptor *info = cls;
1455 struct GNUNET_MessageHeader *msg;
1458 GNUNET_assert (NULL != info);
1459 GNUNET_assert (NULL != info->mesh_data);
1460 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1461 total_size = ntohs (msg->size);
1463 if (total_size > size)
1468 memcpy (buf, msg, total_size);
1469 data_descriptor_decrement_rc (info->mesh_data);
1476 * Sends an already built non-multicast message to a peer,
1477 * properly registrating all used resources.
1479 * @param message Message to send. Function makes a copy of it.
1480 * @param peer Short ID of the neighbor whom to send the message.
1481 * @param t Tunnel on which this message is transmitted.
1484 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1485 const struct GNUNET_PeerIdentity *peer,
1486 struct MeshTunnel *t)
1488 struct MeshTransmissionDescriptor *info;
1489 struct MeshPeerInfo *neighbor;
1490 struct MeshPeerPath *p;
1494 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1496 size = ntohs (message->size);
1497 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1498 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1499 info->mesh_data->data = GNUNET_malloc (size);
1500 memcpy (info->mesh_data->data, message, size);
1501 type = ntohs(message->type);
1504 struct GNUNET_MESH_Unicast *m;
1505 struct GNUNET_MESH_ToOrigin *to;
1507 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1508 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
1509 m->ttl = htonl (ntohl (m->ttl) - 1);
1511 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1512 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
1514 to->pid = htonl(t->bck_pid);
1516 info->mesh_data->data_len = size;
1517 info->mesh_data->reference_counter = 1;
1518 info->mesh_data->total_out = 1;
1519 neighbor = peer_info_get (peer);
1520 for (p = neighbor->path_head; NULL != p; p = p->next)
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 " %s IS NOT DIRECTLY CONNECTED\n",
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1536 for (p = neighbor->path_head; NULL != p; p = p->next)
1538 struct GNUNET_PeerIdentity debug_id;
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 " path with %u hops through:\n",
1544 for (i = 0; i < p->length; i++)
1546 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 i, GNUNET_i2s(&debug_id));
1553 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1555 " no direct connection to %s\n",
1557 GNUNET_free (info->mesh_data->data);
1558 GNUNET_free (info->mesh_data);
1562 info->peer = neighbor;
1563 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1574 * Sends a CREATE PATH message for a path to a peer, properly registrating
1575 * all used resources.
1577 * @param peer PeerInfo of the final peer for whom this path is being created.
1578 * @param p Path itself.
1579 * @param t Tunnel for which the path is created.
1582 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1583 struct MeshTunnel *t)
1585 struct GNUNET_PeerIdentity id;
1586 struct MeshPathInfo *path_info;
1587 struct MeshPeerInfo *neighbor;
1593 p = tree_get_path_to_peer (t->tree, peer->id);
1600 for (i = 0; i < p->length; i++)
1602 if (p->peers[i] == myid)
1605 if (i >= p->length - 1)
1611 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1613 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1614 path_info->path = p;
1616 neighbor = peer_info_get (&id);
1617 path_info->peer = neighbor;
1618 queue_add (path_info,
1619 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1620 sizeof (struct GNUNET_MESH_ManipulatePath) +
1621 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1628 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1630 * @param t Tunnel whose path to destroy.
1631 * @param destination Short ID of the peer to whom the path to destroy.
1634 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1636 struct MeshPeerPath *p;
1639 p = tree_get_path_to_peer (t->tree, destination);
1645 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1646 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1648 struct GNUNET_MESH_ManipulatePath *msg;
1649 struct GNUNET_PeerIdentity *pi;
1653 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1654 msg->header.size = htons (size);
1655 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1656 msg->tid = htonl (t->id.tid);
1657 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1658 for (i = 0; i < p->length; i++)
1660 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1662 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1669 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1671 * @param t Tunnel which to confirm.
1674 send_path_ack (struct MeshTunnel *t)
1676 struct MeshTransmissionDescriptor *info;
1677 struct GNUNET_PeerIdentity id;
1678 GNUNET_PEER_Id peer;
1680 peer = tree_get_predecessor (t->tree);
1681 GNUNET_PEER_resolve (peer, &id);
1682 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1683 info->origin = &t->id;
1684 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1685 GNUNET_assert (NULL != info->peer);
1688 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1689 sizeof (struct GNUNET_MESH_PathACK),
1696 * Try to establish a new connection to this peer.
1697 * Use the best path for the given tunnel.
1698 * If the peer doesn't have any path to it yet, try to get one.
1699 * If the peer already has some path, send a CREATE PATH towards it.
1701 * @param peer PeerInfo of the peer.
1702 * @param t Tunnel for which to create the path, if possible.
1705 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1707 struct MeshPeerPath *p;
1708 struct MeshPathInfo *path_info;
1710 if (NULL != peer->path_head)
1712 p = tree_get_path_to_peer (t->tree, peer->id);
1719 // FIXME always send create path to self
1722 send_create_path (peer, p, t);
1726 struct GNUNET_HashCode hash;
1729 t->local_tid_dest = next_local_tid++;
1730 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1733 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1734 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1741 else if (NULL == peer->dhtget)
1743 struct GNUNET_PeerIdentity id;
1745 GNUNET_PEER_resolve (peer->id, &id);
1746 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1747 path_info->peer = peer;
1749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1750 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1751 peer->dhtgetcls = path_info;
1752 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1753 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1754 &id.hashPubKey, /* key to search */
1755 dht_replication_level, /* replication level */
1756 GNUNET_DHT_RO_RECORD_ROUTE |
1757 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1758 NULL, /* xquery */ // FIXME BLOOMFILTER
1759 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1760 &dht_get_id_handler, path_info);
1762 /* Otherwise, there is no path but the DHT get is already started. */
1767 * Task to delay the connection of a peer
1769 * @param cls Closure (path info with tunnel and peer to connect).
1770 * Will be free'd on exection.
1771 * @param tc TaskContext
1774 peer_info_connect_task (void *cls,
1775 const struct GNUNET_SCHEDULER_TaskContext *tc)
1777 struct MeshPathInfo *path_info = cls;
1779 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
1781 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1786 peer_info_connect (path_info->peer, path_info->t);
1792 * Destroy the peer_info and free any allocated resources linked to it
1794 * @param pi The peer_info to destroy.
1796 * @return GNUNET_OK on success
1799 peer_info_destroy (struct MeshPeerInfo *pi)
1801 struct GNUNET_PeerIdentity id;
1802 struct MeshPeerPath *p;
1803 struct MeshPeerPath *nextp;
1805 GNUNET_PEER_resolve (pi->id, &id);
1806 GNUNET_PEER_change_rc (pi->id, -1);
1809 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1812 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1813 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1815 if (NULL != pi->dhtget)
1817 GNUNET_DHT_get_stop (pi->dhtget);
1818 GNUNET_free (pi->dhtgetcls);
1824 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1828 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
1830 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
1838 * Remove all paths that rely on a direct connection between p1 and p2
1839 * from the peer itself and notify all tunnels about it.
1841 * @param peer PeerInfo of affected peer.
1842 * @param p1 GNUNET_PEER_Id of one peer.
1843 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1846 * TODO: optimize (see below)
1849 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1852 struct MeshPeerPath *p;
1853 struct MeshPeerPath *aux;
1854 struct MeshPeerInfo *peer_d;
1856 unsigned int destroyed;
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1863 p = peer->path_head;
1867 for (i = 0; i < (p->length - 1); i++)
1869 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1870 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1872 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1883 for (i = 0; i < peer->ntunnels; i++)
1885 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1889 * Problem: one or more peers have been deleted from the tunnel tree.
1890 * We don't know who they are to try to add them again.
1891 * We need to try to find a new path for each of the disconnected peers.
1892 * Some of them might already have a path to reach them that does not
1893 * involve p1 and p2. Adding all anew might render in a better tree than
1894 * the trivial immediate fix.
1896 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1897 * its children will be reachable trough him.
1899 peer_d = peer_info_get_short (d);
1902 for (p = peer_d->path_head; NULL != p; p = p->next)
1904 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1912 /* No callback, as peer will be already disconnected and a connection
1913 * scheduled by tunnel_notify_connection_broken.
1915 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1919 peer_info_connect (peer_d, peer->tunnels[i]);
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1927 * Add the path to the peer and update the path used to reach it in case this
1930 * @param peer_info Destination peer to add the path to.
1931 * @param path New path to add. Last peer must be the peer in arg 1.
1932 * Path will be either used of freed if already known.
1933 * @param trusted Do we trust that this path is real?
1936 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1939 struct MeshPeerPath *aux;
1943 if ((NULL == peer_info) || (NULL == path))
1946 path_destroy (path);
1949 if (path->peers[path->length - 1] != peer_info->id)
1952 path_destroy (path);
1955 if (path->length <= 2 && GNUNET_NO == trusted)
1957 /* Only allow CORE to tell us about direct paths */
1958 path_destroy (path);
1961 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1962 for (l = 1; l < path->length; l++)
1964 if (path->peers[l] == myid)
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1967 for (l2 = 0; l2 < path->length - l; l2++)
1969 path->peers[l2] = path->peers[l + l2];
1974 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1979 struct GNUNET_PeerIdentity id;
1981 GNUNET_PEER_resolve (peer_info->id, &id);
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1983 path->length, GNUNET_i2s (&id));
1986 l = path_get_length (path);
1993 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1994 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1996 l2 = path_get_length (aux);
1999 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2000 peer_info->path_tail, aux, path);
2005 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2007 path_destroy (path);
2012 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2019 * Add the path to the origin peer and update the path used to reach it in case
2020 * this is the shortest.
2021 * The path is given in peer_info -> destination, therefore we turn the path
2022 * upside down first.
2024 * @param peer_info Peer to add the path to, being the origin of the path.
2025 * @param path New path to add after being inversed.
2026 * @param trusted Do we trust that this path is real?
2029 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2030 struct MeshPeerPath *path, int trusted)
2033 peer_info_add_path (peer_info, path, trusted);
2038 * Function called if the connection to the peer has been stalled for a while,
2039 * possibly due to a missed ACK. Poll the peer about its ACK status.
2041 * @param cls Closure (cinfo).
2042 * @param tc TaskContext.
2045 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2047 // struct GNUNET_MESH_Poll msg;
2048 // struct GNUNET_PeerIdentity id;
2049 // struct MeshTunnel *t;
2051 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2052 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2058 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2059 // msg.header.size = htons (sizeof (msg));
2060 // msg.tid = htonl (t->id.tid);
2061 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2062 // msg.last_ack = htonl (cinfo->fwd_ack);
2064 // GNUNET_PEER_resolve (cinfo->id, &id);
2065 // send_prebuilt_message (&msg.header, &id, cinfo->t);
2066 // cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2067 // MESH_MAX_POLL_TIME,
2068 // GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2069 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2070 // &tunnel_poll, cinfo);
2075 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2076 * to obtain a local peer -> destination path and interning the peer ids.
2078 * @return Newly allocated and created path
2080 static struct MeshPeerPath *
2081 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2082 unsigned int get_path_length,
2083 const struct GNUNET_PeerIdentity *put_path,
2084 unsigned int put_path_length)
2086 struct MeshPeerPath *p;
2092 GNUNET_PEER_change_rc (myid, 1);
2093 i = get_path_length;
2094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2095 for (i--; i >= 0; i--)
2097 id = GNUNET_PEER_intern (&get_path[i]);
2098 if (p->length > 0 && id == p->peers[p->length - 1])
2100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2101 GNUNET_PEER_change_rc (id, -1);
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2106 GNUNET_i2s (&get_path[i]));
2108 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2109 p->peers[p->length - 1] = id;
2112 i = put_path_length;
2113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2114 for (i--; i >= 0; i--)
2116 id = GNUNET_PEER_intern (&put_path[i]);
2119 /* PUT path went through us, so discard the path up until now and start
2120 * from here to get a much shorter (and loop-free) path.
2125 if (p->length > 0 && id == p->peers[p->length - 1])
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2128 GNUNET_PEER_change_rc (id, -1);
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2133 GNUNET_i2s (&put_path[i]));
2135 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2136 p->peers[p->length - 1] = id;
2140 if (get_path_length > 0)
2141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2142 GNUNET_i2s (&get_path[0]));
2143 if (put_path_length > 0)
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2145 GNUNET_i2s (&put_path[0]));
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2148 for (i = 0; i < p->length; i++)
2150 struct GNUNET_PeerIdentity peer_id;
2152 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2154 GNUNET_i2s (&peer_id));
2162 * Adds a path to the peer_infos of all the peers in the path
2164 * @param p Path to process.
2165 * @param confirmed Whether we know if the path works or not.
2168 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2172 /* TODO: invert and add */
2173 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2174 for (i++; i < p->length; i++)
2176 struct MeshPeerInfo *aux;
2177 struct MeshPeerPath *copy;
2179 aux = peer_info_get_short (p->peers[i]);
2180 copy = path_duplicate (p);
2181 copy->length = i + 1;
2182 peer_info_add_path (aux, copy, GNUNET_NO);
2188 * Send keepalive packets for a peer
2190 * @param cls Closure (tunnel for which to send the keepalive).
2191 * @param tc Notification context.
2193 * TODO: implement explicit multicast keepalive?
2196 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2200 * Search for a tunnel among the incoming tunnels
2202 * @param tid the local id of the tunnel
2204 * @return tunnel handler, NULL if doesn't exist
2206 static struct MeshTunnel *
2207 tunnel_get_incoming (MESH_TunnelNumber tid)
2209 struct GNUNET_HashCode hash;
2211 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2212 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2213 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2218 * Search for a tunnel among the tunnels for a client
2220 * @param c the client whose tunnels to search in
2221 * @param tid the local id of the tunnel
2223 * @return tunnel handler, NULL if doesn't exist
2225 static struct MeshTunnel *
2226 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2228 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2230 return tunnel_get_incoming (tid);
2234 struct GNUNET_HashCode hash;
2236 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2237 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2243 * Search for a tunnel by global ID using PEER_ID
2245 * @param pi owner of the tunnel
2246 * @param tid global tunnel number
2248 * @return tunnel handler, NULL if doesn't exist
2250 static struct MeshTunnel *
2251 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2253 struct MESH_TunnelID id;
2254 struct GNUNET_HashCode hash;
2259 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2260 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2265 * Search for a tunnel by global ID using full PeerIdentities
2267 * @param oid owner of the tunnel
2268 * @param tid global tunnel number
2270 * @return tunnel handler, NULL if doesn't exist
2272 static struct MeshTunnel *
2273 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2275 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2280 * Delete an active client from the tunnel.
2286 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2290 for (i = 0; i < t->nclients; i++)
2292 if (t->clients[i] == c)
2294 t->clients[i] = t->clients[t->nclients - 1];
2295 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2296 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2298 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2306 * Delete an ignored client from the tunnel.
2312 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2316 for (i = 0; i < t->nignore; i++)
2318 if (t->ignore[i] == c)
2320 t->ignore[i] = t->ignore[t->nignore - 1];
2321 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2329 * Delete a client from the tunnel. It should be only done on
2330 * client disconnection, otherwise use client_ignore_tunnel.
2336 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2338 tunnel_delete_ignored_client (t, c);
2339 tunnel_delete_active_client (t, c);
2344 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2345 * needed rescources.
2346 * If peer already exists, reevaluate shortest path and change if different.
2348 * @param t Tunnel we want to add a new peer to
2349 * @param peer PeerInfo of the peer being added
2353 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2355 struct MeshPeerPath *best_p;
2356 struct MeshPeerPath *p;
2357 unsigned int best_cost;
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2362 if (t->peer != peer->id)
2364 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2369 if (NULL != (p = peer->path_head))
2372 best_cost = tree_get_path_cost (t->tree, p);
2375 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2382 tree_add_path (t->tree, best_p, NULL, NULL); // FIXME
2383 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2384 t->path_refresh_task =
2385 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2389 /* Start a DHT get */
2390 peer_info_connect (peer, t);
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2397 * Add a client to a tunnel, initializing all needed data structures.
2399 * @param t Tunnel to which add the client.
2400 * @param c Client which to add to the tunnel.
2403 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2405 struct MeshTunnelClientInfo clinfo;
2407 GNUNET_array_append (t->clients, t->nclients, c);
2408 clinfo.fwd_ack = t->fwd_pid + 1;
2409 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
2410 clinfo.fwd_pid = t->fwd_pid;
2411 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
2413 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
2418 * Notifies a tunnel that a connection has broken that affects at least
2419 * some of its peers. Sends a notification towards the root of the tree.
2420 * In case the peer is the owner of the tree, notifies the client that owns
2421 * the tunnel and tries to reconnect.
2423 * @param t Tunnel affected.
2424 * @param p1 Peer that got disconnected from p2.
2425 * @param p2 Peer that got disconnected from p1.
2427 * @return Short ID of the peer disconnected (either p1 or p2).
2428 * 0 if the tunnel remained unaffected.
2430 static GNUNET_PEER_Id
2431 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2434 // if (myid != p1 && myid != p2) FIXME
2439 // if (tree_get_predecessor (t->tree) != 0)
2441 // /* We are the peer still connected, notify owner of the disconnection. */
2442 // struct GNUNET_MESH_PathBroken msg;
2443 // struct GNUNET_PeerIdentity neighbor;
2445 // msg.header.size = htons (sizeof (msg));
2446 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2447 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2448 // msg.tid = htonl (t->id.tid);
2449 // msg.peer1 = my_full_id;
2450 // GNUNET_PEER_resolve (pid, &msg.peer2);
2451 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2452 // send_prebuilt_message (&msg.header, &neighbor, t);
2459 * Get the Flow Control info of a client.
2461 * @param t Tunnel on which to look.
2462 * @param c Client whose ACK to get.
2464 * @return ACK value.
2466 static struct MeshTunnelClientInfo *
2467 tunnel_get_client_fc (struct MeshTunnel *t,
2468 struct MeshClient *c)
2472 for (i = 0; i < t->nclients; i++)
2474 if (t->clients[i] != c)
2476 return &t->clients_fc[i];
2479 return NULL; // avoid compiler / coverity complaints
2484 * Set the FWD ACK value of a client in a particular tunnel.
2486 * @param t Tunnel affected.
2487 * @param c Client whose ACK to set.
2488 * @param ack ACK value.
2491 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
2492 struct MeshClient *c,
2497 for (i = 0; i < t->nclients; i++)
2499 if (t->clients[i] != c)
2501 t->clients_fc[i].fwd_ack = ack;
2509 * Get the highest ACK value of all clients in a particular tunnel,
2510 * according to the buffering/speed settings.
2512 * @param t Tunnel on which to look.
2514 * @return Corresponding ACK value (max uint32_t).
2515 * If no clients are suscribed, -1LL.
2518 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
2523 if (0 == t->nclients)
2525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2526 " tunnel has no clients, no FWD ACK\n");
2530 for (ack = -1LL, i = 0; i < t->nclients; i++)
2533 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack))
2535 ack = t->clients_fc[i].fwd_ack;
2539 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
2540 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
2542 return (uint32_t) ack;
2547 * Get the current fwd ack value for a tunnel, taking in account the tunnel
2548 * mode and the status of all children nodes.
2552 * @return Maximum PID allowed.
2555 tunnel_get_fwd_ack (struct MeshTunnel *t)
2559 uint32_t buffer_free;
2563 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
2564 client_ack = tunnel_get_clients_fwd_ack (t);
2565 if (GNUNET_YES == t->nobuffer)
2571 ack = count + buffer_free; // Overflow? OK!
2573 if (-1LL == client_ack)
2578 ack = GMC_max_pid ((uint32_t) client_ack, ack);
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2581 "c %u, bf %u, cl %lld, ACK: %u\n",
2582 count, buffer_free, client_ack, ack);
2588 * Build a local ACK message and send it to a local client.
2590 * @param t Tunnel on which to send the ACK.
2591 * @param c Client to whom send the ACK.
2592 * @param ack Value of the ACK.
2595 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
2597 struct GNUNET_MESH_LocalAck msg;
2599 msg.header.size = htons (sizeof (msg));
2600 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2601 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
2602 msg.max_pid = htonl (ack);
2603 GNUNET_SERVER_notification_context_unicast(nc,
2610 * Build an ACK message and queue it to send to the given peer.
2612 * @param t Tunnel on which to send the ACK.
2613 * @param peer Peer to whom send the ACK.
2614 * @param ack Value of the ACK.
2617 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
2619 struct GNUNET_MESH_ACK msg;
2621 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2622 msg.header.size = htons (sizeof (msg));
2623 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2624 msg.pid = htonl (ack);
2625 msg.tid = htonl (t->id.tid);
2627 send_prebuilt_message (&msg.header, peer, t);
2632 * Notify a the owner of a tunnel about how many more
2633 * payload packages will we accept on a given tunnel.
2635 * @param t Tunnel on which to send the ACK.
2638 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
2642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2643 "Sending client FWD ACK on tunnel %X\n",
2646 ack = tunnel_get_fwd_ack (t);
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2649 if (t->last_fwd_ack == ack)
2651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2656 t->last_fwd_ack = ack;
2657 send_local_ack (t, t->owner, ack);
2662 * Send an ACK informing the predecessor about the available buffer space.
2663 * In case there is no predecessor, inform the owning client.
2664 * If buffering is off, send only on behalf of children or self if endpoint.
2665 * If buffering is on, send when sent to children and buffer space is free.
2666 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2667 * the ACK itself goes "back" (towards root).
2669 * @param t Tunnel on which to send the ACK.
2670 * @param type Type of message that triggered the ACK transmission.
2673 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2675 struct GNUNET_PeerIdentity id;
2678 if (NULL != t->owner)
2680 tunnel_send_client_fwd_ack (t);
2683 /* Is it after unicast / multicast retransmission? */
2686 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2687 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2689 "ACK due to FWD DATA retransmission\n");
2690 if (GNUNET_YES == t->nobuffer)
2692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2696 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2697 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2699 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2700 t->force_ack = GNUNET_YES;
2706 /* Check if we need to transmit the ACK */
2707 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
2708 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
2709 GNUNET_NO == t->force_ack)
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2713 " t->qmax: %u, t->qn: %u\n",
2714 t->fwd_queue_max, t->fwd_queue_n);
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716 " t->pid: %u, t->ack: %u\n",
2717 t->fwd_pid, t->last_fwd_ack);
2721 /* Ok, ACK might be necessary, what PID to ACK? */
2722 ack = tunnel_get_fwd_ack (t);
2724 /* If speed_min and not all children have ack'd, dont send yet */
2725 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
2731 t->last_fwd_ack = ack;
2732 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
2733 send_ack (t, &id, ack);
2735 t->force_ack = GNUNET_NO;
2740 * Iterator to send a child node a BCK ACK to allow him to send more
2743 * @param cls Closure (tunnel).
2744 * @param id Id of the child node.
2747 tunnel_send_child_bck_ack (void *cls,
2750 struct MeshTunnel *t = cls;
2751 struct GNUNET_PeerIdentity peer;
2752 uint32_t ack = 0; // FIXME
2754 GNUNET_PEER_resolve (id, &peer);
2755 // ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
2757 // if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
2759 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2760 // " Not sending ACK, not needed\n");
2763 // cinfo->bck_ack = ack;
2765 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2766 // " Sending BCK ACK %u (last sent: %u)\n",
2767 // ack, cinfo->bck_ack);
2768 send_ack (t, &peer, ack);
2773 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
2775 * Iterates over all clients and sends BCK ACKs to the ones that need it.
2777 * FIXME fc: what happens if we have 2 clients but q_size is 1?
2778 * - implement a size 1 buffer in each client_fc AND children_fc
2779 * to hold at least 1 message per "child".
2780 * problem: violates no buffer policy
2781 * - ack 0 and make "children" poll for transmission slots
2782 * problem: big overhead, extra latency even in low traffic
2785 * @param t Tunnel on which to send the BCK ACKs.
2788 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
2791 unsigned int tunnel_delta;
2793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
2795 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
2798 /* Find client whom to allow to send to origin (with lowest buffer space) */
2799 for (i = 0; i < t->nclients; i++)
2801 struct MeshTunnelClientInfo *clinfo;
2804 clinfo = &t->clients_fc[i];
2805 delta = clinfo->bck_ack - clinfo->bck_pid;
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
2807 t->clients[i]->id, delta);
2809 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
2810 (GNUNET_YES == t->nobuffer && 0 == delta))
2814 ack = clinfo->bck_pid;
2815 ack += t->nobuffer ? 1 : tunnel_delta;
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2817 " sending ack to client %u: %u\n",
2818 t->clients[i]->id, ack);
2819 send_local_ack (t, t->clients[i], ack);
2820 clinfo->bck_ack = ack;
2824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2825 " not sending ack to client %u (td %u, d %u)\n",
2826 t->clients[i]->id, tunnel_delta, delta);
2833 * Send an ACK informing the children nodes and destination clients about
2834 * the available buffer space.
2835 * If buffering is off, send only on behalf of root (can be self).
2836 * If buffering is on, send when sent to predecessor and buffer space is free.
2837 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2838 * the ACK itself goes "forward" (towards children/clients).
2840 * @param t Tunnel on which to send the ACK.
2841 * @param type Type of message that triggered the ACK transmission.
2844 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2847 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2848 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2849 /* Is it after data to_origin retransmission? */
2852 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2853 if (GNUNET_YES == t->nobuffer)
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2856 " Not sending ACK, nobuffer\n");
2860 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2861 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2863 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2864 t->force_ack = GNUNET_YES;
2870 tunnel_send_clients_bck_ack (t);
2871 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
2872 t->force_ack = GNUNET_NO;
2877 * @brief Re-initiate traffic to this peer if necessary.
2879 * Check if there is traffic queued towards this peer
2880 * and the core transmit handle is NULL (traffic was stalled).
2881 * If so, call core tmt rdy.
2883 * @param cls Closure (unused)
2884 * @param peer_id Short ID of peer to which initiate traffic.
2887 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
2889 struct MeshPeerInfo *peer;
2890 struct GNUNET_PeerIdentity id;
2891 struct MeshPeerQueue *q;
2894 peer = peer_info_get_short(peer_id);
2895 if (NULL != peer->core_transmit)
2898 q = queue_get_next(peer);
2901 /* Might br multicast traffic already sent to this particular peer but
2902 * not to other children in this tunnel.
2903 * This way t->queue_n would be > 0 but the queue of this particular peer
2909 GNUNET_PEER_resolve (peer->id, &id);
2910 peer->core_transmit =
2911 GNUNET_CORE_notify_transmit_ready(core_handle,
2914 GNUNET_TIME_UNIT_FOREVER_REL,
2924 * @brief Allow transmission of FWD traffic on this tunnel
2926 * Check if there is traffic queued towards any children
2927 * and the core transmit handle is NULL, and if so, call core tmt rdy.
2929 * @param t Tunnel on which to unlock FWD traffic.
2932 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
2934 if (0 == t->fwd_queue_n)
2937 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
2942 * @brief Allow transmission of BCK traffic on this tunnel
2944 * Check if there is traffic queued towards the root of the tree
2945 * and the core transmit handle is NULL, and if so, call core tmt rdy.
2947 * @param t Tunnel on which to unlock BCK traffic.
2950 tunnel_unlock_bck_queue (struct MeshTunnel *t)
2952 if (0 == t->bck_queue_n)
2955 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
2960 * Send a message to all peers in this tunnel that the tunnel is no longer
2963 * @param t The tunnel whose peers to notify.
2964 * @param parent ID of the parent, in case the tree is already destroyed.
2967 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
2969 struct GNUNET_MESH_TunnelDestroy msg;
2970 struct GNUNET_PeerIdentity id;
2972 msg.header.size = htons (sizeof (msg));
2973 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2974 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2975 msg.tid = htonl (t->id.tid);
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2977 " sending tunnel destroy for tunnel: %s [%X]\n",
2978 GNUNET_i2s (&msg.oid), t->id.tid);
2980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to child\n");
2981 /* FIXME tunnel_send_multicast (t, &msg.header); */
2984 parent = tree_get_predecessor (t->tree);
2985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
2989 GNUNET_PEER_resolve (parent, &id);
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2991 " sending back to %s\n",
2993 send_prebuilt_message (&msg.header, &id, t);
2998 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3000 * @param cls Closure (Tunnel which to cancel).
3001 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3004 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3006 struct MeshTunnel *t = cls;
3007 struct MeshPeerInfo *peer_info;
3008 struct MeshPeerQueue *pq;
3009 struct MeshPeerQueue *next;
3011 peer_info = peer_info_get_short (neighbor_id);
3012 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3015 if (pq->tunnel == t)
3017 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
3018 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
3019 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
3021 // Should have been removed on destroy children
3024 queue_destroy (pq, GNUNET_YES);
3027 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3029 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3030 peer_info->core_transmit = NULL;
3035 * Destroy the tunnel and free any allocated resources linked to it.
3037 * @param t the tunnel to destroy
3039 * @return GNUNET_OK on success
3042 tunnel_destroy (struct MeshTunnel *t)
3044 struct MeshClient *c;
3045 struct GNUNET_HashCode hash;
3056 struct GNUNET_PeerIdentity id;
3058 GNUNET_PEER_resolve (t->id.oid, &id);
3059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3060 GNUNET_i2s (&id), t->id.tid);
3062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3066 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3067 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3075 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3077 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3084 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3085 for (i = 0; i < t->nclients; i++)
3089 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3095 for (i = 0; i < t->nignore; i++)
3099 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3106 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
3107 GNUNET_free_non_null (t->clients);
3108 GNUNET_free_non_null (t->ignore);
3109 GNUNET_free_non_null (t->clients_fc);
3111 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3112 tree_destroy (t->tree);
3114 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3115 GNUNET_SCHEDULER_cancel (t->timeout_task);
3116 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3117 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3120 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3125 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
3128 * Tunnel is empty: destroy it.
3130 * @param cls Closure (Tunnel).
3131 * @param tc TaskContext.
3134 tunnel_destroy_empty_delayed (void *cls,
3135 const struct GNUNET_SCHEDULER_TaskContext *tc)
3137 struct MeshTunnel *t = cls;
3139 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
3140 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3143 if (0 != t->nclients ||
3144 0 != tree_count_children (t->tree))
3149 struct GNUNET_PeerIdentity id;
3151 GNUNET_PEER_resolve (t->id.oid, &id);
3152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3153 "executing destruction of empty tunnel %s [%X]\n",
3154 GNUNET_i2s (&id), t->id.tid);
3158 tunnel_send_destroy (t, 0);
3159 if (0 == t->pending_messages)
3162 t->destroy = GNUNET_YES;
3167 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
3169 * @param t Tunnel to destroy if empty.
3172 tunnel_destroy_empty (struct MeshTunnel *t)
3174 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
3176 0 != tree_count_children (t->tree))
3178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3180 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
3186 struct GNUNET_PeerIdentity id;
3188 GNUNET_PEER_resolve (t->id.oid, &id);
3189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3190 "scheduling destruction of empty tunnel %s [%X]\n",
3191 GNUNET_i2s (&id), t->id.tid);
3195 t->delayed_destroy =
3196 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
3197 &tunnel_destroy_empty_delayed,
3203 * Create a new tunnel
3205 * @param owner Who is the owner of the tunnel (short ID).
3206 * @param tid Tunnel Number of the tunnel.
3207 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3208 * @param local Tunnel Number for the tunnel, for the client point of view.
3210 * @return A new initialized tunnel. NULL on error.
3212 static struct MeshTunnel *
3213 tunnel_new (GNUNET_PEER_Id owner,
3214 MESH_TunnelNumber tid,
3215 struct MeshClient *client,
3216 MESH_TunnelNumber local)
3218 struct MeshTunnel *t;
3219 struct GNUNET_HashCode hash;
3221 if (n_tunnels >= max_tunnels && NULL == client)
3224 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3227 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
3228 t->bck_queue_max = t->fwd_queue_max;
3229 t->tree = tree_new (owner);
3231 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
3232 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
3233 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
3234 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
3235 t->local_tid = local;
3237 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3239 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3241 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3242 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3249 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3256 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3258 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3259 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3263 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3274 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3275 * client when the client disconnects. If the client is not the owner, the
3276 * owner will get notified if no more clients are in the tunnel and the client
3277 * get removed from the tunnel's list.
3279 * @param cls closure (client that is disconnecting)
3280 * @param key the hash of the local tunnel id (used to access the hashmap)
3281 * @param value the value stored at the key (tunnel to destroy)
3283 * @return GNUNET_OK, keep iterating.
3286 tunnel_destroy_iterator (void *cls,
3287 const struct GNUNET_HashCode * key,
3290 struct MeshTunnel *t = value;
3291 struct MeshClient *c = cls;
3293 send_client_tunnel_disconnect (t, c);
3296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
3297 tunnel_delete_client (t, c);
3298 client_delete_tunnel (c, t);
3299 tunnel_destroy_empty (t);
3302 tunnel_send_destroy (t, 0);
3304 t->destroy = GNUNET_YES;
3311 * Timeout function, destroys tunnel if called
3313 * @param cls Closure (tunnel to destroy).
3314 * @param tc TaskContext
3317 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3319 struct MeshTunnel *t = cls;
3320 struct GNUNET_PeerIdentity id;
3322 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3323 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3325 GNUNET_PEER_resolve(t->id.oid, &id);
3326 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3327 "Tunnel %s [%X] timed out. Destroying.\n",
3328 GNUNET_i2s(&id), t->id.tid);
3329 send_clients_tunnel_destroy (t);
3334 * Resets the tunnel timeout. Starts it if no timeout was running.
3336 * @param t Tunnel whose timeout to reset.
3338 * TODO use heap to improve efficiency of scheduler.
3341 tunnel_reset_timeout (struct MeshTunnel *t)
3343 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3344 GNUNET_SCHEDULER_cancel (t->timeout_task);
3346 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3347 (refresh_path_time, 4), &tunnel_timeout, t);
3351 /******************************************************************************/
3352 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3353 /******************************************************************************/
3356 * Function to send a create path packet to a peer.
3358 * @param cls closure
3359 * @param size number of bytes available in buf
3360 * @param buf where the callee should write the message
3361 * @return number of bytes written to buf
3364 send_core_path_create (void *cls, size_t size, void *buf)
3366 struct MeshPathInfo *info = cls;
3367 struct GNUNET_MESH_ManipulatePath *msg;
3368 struct GNUNET_PeerIdentity *peer_ptr;
3369 struct MeshTunnel *t = info->t;
3370 struct MeshPeerPath *p = info->path;
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3377 sizeof (struct GNUNET_MESH_ManipulatePath) +
3378 p->length * sizeof (struct GNUNET_PeerIdentity);
3380 if (size < size_needed || NULL == buf)
3385 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3386 msg->header.size = htons (size_needed);
3387 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3388 msg->tid = ntohl (t->id.tid);
3391 if (GNUNET_YES == t->nobuffer)
3392 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3393 msg->opt = htonl(opt);
3396 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3397 for (i = 0; i < p->length; i++)
3399 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3406 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3412 * Fill the core buffer
3414 * @param cls closure (data itself)
3415 * @param size number of bytes available in buf
3416 * @param buf where the callee should write the message
3418 * @return number of bytes written to buf
3421 send_core_data_multicast (void *cls, size_t size, void *buf)
3423 struct MeshTransmissionDescriptor *info = cls;
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3427 GNUNET_assert (NULL != info);
3428 GNUNET_assert (NULL != info->peer);
3429 total_size = info->mesh_data->data_len;
3430 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3432 if (total_size > size)
3437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3438 memcpy (buf, info->mesh_data->data, total_size);
3441 struct GNUNET_MessageHeader *mh;
3444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
3445 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
3448 data_descriptor_decrement_rc (info->mesh_data);
3449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3457 * Creates a path ack message in buf and frees all unused resources.
3459 * @param cls closure (MeshTransmissionDescriptor)
3460 * @param size number of bytes available in buf
3461 * @param buf where the callee should write the message
3462 * @return number of bytes written to buf
3465 send_core_path_ack (void *cls, size_t size, void *buf)
3467 struct MeshTransmissionDescriptor *info = cls;
3468 struct GNUNET_MESH_PathACK *msg = buf;
3470 GNUNET_assert (NULL != info);
3471 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3476 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3477 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3478 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3479 msg->tid = htonl (info->origin->tid);
3480 msg->peer_id = my_full_id;
3483 /* TODO add signature */
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3486 return sizeof (struct GNUNET_MESH_PathACK);
3491 * Free a transmission that was already queued with all resources
3492 * associated to the request.
3494 * @param queue Queue handler to cancel.
3495 * @param clear_cls Is it necessary to free associated cls?
3498 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3500 struct MeshTransmissionDescriptor *dd;
3501 struct MeshPathInfo *path_info;
3502 // unsigned int max;
3504 if (GNUNET_YES == clear_cls)
3506 switch (queue->type)
3508 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3509 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3510 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3512 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3513 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3514 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3515 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3516 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3518 " prebuilt message\n");
3519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3521 GNUNET_MESH_DEBUG_M2S(queue->type));
3523 data_descriptor_decrement_rc (dd->mesh_data);
3525 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3527 path_info = queue->cls;
3528 path_destroy (path_info->path);
3532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3533 " type %s unknown!\n",
3534 GNUNET_MESH_DEBUG_M2S(queue->type));
3536 GNUNET_free_non_null (queue->cls);
3538 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3539 queue->peer->queue_tail,
3542 /* Delete from child_fc in the appropiate tunnel */
3543 // max = queue->tunnel->fwd_queue_max;
3544 // GNUNET_PEER_resolve (queue->peer->id, &id);
3545 // if (NULL != cinfo)
3547 // for (i = 0; i < cinfo->send_buffer_n; i++)
3549 // i2 = (cinfo->send_buffer_start + i) % max;
3550 // if (cinfo->send_buffer[i2] == queue)
3552 // /* Found corresponding entry in the send_buffer. Move all others back. */
3556 // for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
3558 // j2 = (cinfo->send_buffer_start + j) % max;
3559 // j3 = (cinfo->send_buffer_start + j + 1) % max;
3560 // cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
3563 // cinfo->send_buffer[j3] = NULL;
3564 // cinfo->send_buffer_n--;
3569 GNUNET_free (queue);
3574 * @brief Get the next transmittable message from the queue.
3576 * This will be the head, except in the case of being a data packet
3577 * not allowed by the destination peer.
3579 * @param peer Destination peer.
3581 * @return The next viable MeshPeerQueue element to send to that peer.
3582 * NULL when there are no transmittable messages.
3584 struct MeshPeerQueue *
3585 queue_get_next (const struct MeshPeerInfo *peer)
3587 struct MeshPeerQueue *q;
3589 struct MeshTransmissionDescriptor *info;
3590 // struct GNUNET_MESH_Unicast *ucast;
3591 // struct GNUNET_MESH_ToOrigin *to_orig;
3592 struct GNUNET_PeerIdentity id;
3594 // uint32_t ack; FIXME
3596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
3597 for (q = peer->queue_head; NULL != q; q = q->next)
3601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3603 GNUNET_MESH_DEBUG_M2S(q->type));
3606 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3607 // ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
3608 // pid = ntohl (ucast->pid);
3609 GNUNET_PEER_resolve (info->peer->id, &id);
3610 // ack = cinfo->fwd_ack;
3612 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3613 // to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
3614 // pid = ntohl (to_orig->pid);
3615 // ack = t->bck_ack;
3618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3622 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3623 // "********* ACK: %u, PID: %u\n",
3625 // if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
3627 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3628 // "********* OK!\n");
3633 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3634 // "********* NEXT!\n");
3637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3638 "********* nothing found\n");
3644 * Core callback to write a queued packet to core buffer
3646 * @param cls Closure (peer info).
3647 * @param size Number of bytes available in buf.
3648 * @param buf Where the to write the message.
3650 * @return number of bytes written to buf
3653 queue_send (void *cls, size_t size, void *buf)
3655 struct MeshPeerInfo *peer = cls;
3656 struct GNUNET_MessageHeader *msg;
3657 struct MeshPeerQueue *queue;
3658 struct MeshTunnel *t;
3659 struct GNUNET_PeerIdentity dst_id;
3662 peer->core_transmit = NULL;
3664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3665 queue = queue_get_next (peer);
3667 /* Queue has no internal mesh traffic nor sendable payload */
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
3671 if (NULL == peer->queue_head)
3672 GNUNET_break (0); // Should've been canceled
3675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3677 GNUNET_PEER_resolve (peer->id, &dst_id);
3678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3679 "********* towards %s\n",
3680 GNUNET_i2s(&dst_id));
3681 /* Check if buffer size is enough for the message */
3682 if (queue->size > size)
3684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3685 "********* not enough room, reissue\n");
3686 peer->core_transmit =
3687 GNUNET_CORE_notify_transmit_ready (core_handle,
3690 GNUNET_TIME_UNIT_FOREVER_REL,
3697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3700 GNUNET_assert (0 < t->pending_messages);
3701 t->pending_messages--;
3702 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3706 "********* unicast: t->q (%u/%u)\n",
3707 t->fwd_queue_n, t->fwd_queue_max);
3709 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
3712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
3716 switch (queue->type)
3719 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3720 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3721 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3722 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3723 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3725 "********* raw: %s\n",
3726 GNUNET_MESH_DEBUG_M2S (queue->type));
3728 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3729 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3730 data_size = send_core_data_raw (queue->cls, size, buf);
3731 msg = (struct GNUNET_MessageHeader *) buf;
3732 switch (ntohs (msg->type)) // Type of preconstructed message
3734 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3735 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3737 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3738 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3744 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3746 data_size = send_core_path_create (queue->cls, size, buf);
3748 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3750 data_size = send_core_path_ack (queue->cls, size, buf);
3752 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
3754 data_size = send_core_data_multicast (queue->cls, size, buf);
3758 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3759 "********* type unknown: %u\n",
3763 switch (queue->type)
3765 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3766 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3767 // if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
3769 // GNUNET_break (0);
3770 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3771 // "at pos %u (%p) != %p\n",
3772 // cinfo->send_buffer_start,
3773 // cinfo->send_buffer[cinfo->send_buffer_start],
3776 // if (cinfo->send_buffer_n > 0)
3778 // cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
3779 // cinfo->send_buffer_n--;
3780 // cinfo->send_buffer_start++;
3781 // cinfo->send_buffer_start %= t->fwd_queue_max;
3785 // GNUNET_break (0);
3792 /* Free queue, but cls was freed by send_core_* */
3793 queue_destroy (queue, GNUNET_NO);
3795 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3801 /* If more data in queue, send next */
3802 queue = queue_get_next(peer);
3805 struct GNUNET_PeerIdentity id;
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3808 GNUNET_PEER_resolve (peer->id, &id);
3809 peer->core_transmit =
3810 GNUNET_CORE_notify_transmit_ready(core_handle,
3813 GNUNET_TIME_UNIT_FOREVER_REL,
3821 if (NULL != peer->queue_head)
3823 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3824 "********* %s stalled\n",
3825 GNUNET_i2s(&my_full_id));
3826 // if (NULL == cinfo) FIXME
3827 // cinfo = tunnel_get_neighbor_fc (t, &dst_id);
3828 // // FIXME unify bck/fwd structures, bck does not have cinfo right now
3829 // if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
3831 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3832 // &tunnel_poll, cinfo);
3836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3842 * @brief Queue and pass message to core when possible.
3844 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
3845 * and accounts for it. In case the queue is full, the message is dropped and
3848 * Otherwise, message is treated as internal and allowed to go regardless of
3851 * @param cls Closure (@c type dependant). It will be used by queue_send to
3852 * build the message to be sent if not already prebuilt.
3853 * @param type Type of the message, 0 for a raw message.
3854 * @param size Size of the message.
3855 * @param dst Neighbor to send message to.
3856 * @param t Tunnel this message belongs to.
3859 queue_add (void *cls, uint16_t type, size_t size,
3860 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3862 struct MeshPeerQueue *queue;
3863 struct GNUNET_PeerIdentity id;
3868 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
3869 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
3871 n = &t->fwd_queue_n;
3872 max = &t->fwd_queue_max;
3874 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3876 n = &t->bck_queue_n;
3877 max = &t->bck_queue_max;
3884 GNUNET_STATISTICS_update(stats,
3885 "# messages dropped (buffer full)",
3887 return; // Drop message
3891 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3897 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3898 GNUNET_PEER_resolve (dst->id, &id);
3899 if (NULL == dst->core_transmit)
3901 dst->core_transmit =
3902 GNUNET_CORE_notify_transmit_ready (core_handle,
3905 GNUNET_TIME_UNIT_FOREVER_REL,
3911 t->pending_messages++;
3912 if (NULL == n) // Is this internal mesh traffic?
3915 // It's payload, keep track of buffer per peer. FIXME
3916 // i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
3917 // if (NULL != cinfo->send_buffer[i])
3919 // GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
3920 // queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
3921 // cinfo->send_buffer_start++;
3922 // cinfo->send_buffer_start %= t->fwd_queue_max;
3926 // cinfo->send_buffer_n++;
3928 // cinfo->send_buffer[i] = queue;
3929 // if (cinfo->send_buffer_n > t->fwd_queue_max)
3931 // GNUNET_break (0);
3932 // cinfo->send_buffer_n = t->fwd_queue_max;
3937 /******************************************************************************/
3938 /******************** MESH NETWORK HANDLERS **************************/
3939 /******************************************************************************/
3943 * Core handler for path creation
3945 * @param cls closure
3946 * @param message message
3947 * @param peer peer identity this notification is about
3949 * @return GNUNET_OK to keep the connection open,
3950 * GNUNET_SYSERR to close it (signal serious error)
3953 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3954 const struct GNUNET_MessageHeader *message)
3956 unsigned int own_pos;
3959 MESH_TunnelNumber tid;
3960 struct GNUNET_MESH_ManipulatePath *msg;
3961 struct GNUNET_PeerIdentity *pi;
3962 struct GNUNET_HashCode hash;
3963 struct MeshPeerPath *path;
3964 struct MeshPeerInfo *dest_peer_info;
3965 struct MeshPeerInfo *orig_peer_info;
3966 struct MeshTunnel *t;
3968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3969 "Received a path create msg [%s]\n",
3970 GNUNET_i2s (&my_full_id));
3971 size = ntohs (message->size);
3972 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3974 GNUNET_break_op (0);
3978 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3979 if (size % sizeof (struct GNUNET_PeerIdentity))
3981 GNUNET_break_op (0);
3984 size /= sizeof (struct GNUNET_PeerIdentity);
3987 GNUNET_break_op (0);
3990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3991 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3993 tid = ntohl (msg->tid);
3994 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3996 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3997 t = tunnel_get (pi, tid);
3998 if (NULL == t) // FIXME only for INCOMING tunnels?
4002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4003 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4006 // FIXME notify failure
4009 opt = ntohl (msg->opt);
4010 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
4012 t->nobuffer = GNUNET_YES;
4013 t->last_fwd_ack = t->fwd_pid + 1;
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
4017 if (GNUNET_YES == t->nobuffer)
4019 t->bck_queue_max = 1;
4020 t->fwd_queue_max = 1;
4023 // FIXME only assign a local tid if a local client is interested (on demand)
4024 while (NULL != tunnel_get_incoming (next_local_tid))
4025 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4026 t->local_tid_dest = next_local_tid++;
4027 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4030 tunnel_reset_timeout (t);
4031 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4033 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4034 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4042 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4043 if (NULL == dest_peer_info)
4045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4046 " Creating PeerInfo for destination.\n");
4047 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4048 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4049 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4053 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4054 if (NULL == orig_peer_info)
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4057 " Creating PeerInfo for origin.\n");
4058 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4059 orig_peer_info->id = GNUNET_PEER_intern (pi);
4060 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4061 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4064 path = path_new (size);
4066 for (i = 0; i < size; i++)
4068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4069 GNUNET_i2s (&pi[i]));
4070 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4071 if (path->peers[i] == myid)
4074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4077 /* cannot be self, must be 'not found' */
4078 /* create path: self not found in path through self */
4079 GNUNET_break_op (0);
4080 path_destroy (path);
4084 path_add_to_peers (path, GNUNET_NO);
4085 t->prev_hop = path->peers[own_pos - 1];
4086 GNUNET_PEER_change_rc (t->prev_hop, 1);
4087 if (own_pos == size - 1)
4089 /* It is for us! Send ack. */
4090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4091 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4092 t->peer = path->peers[0];
4097 struct MeshPeerPath *path2;
4099 t->next_hop = path->peers[own_pos + 1];
4100 GNUNET_PEER_change_rc(t->next_hop, 1);
4102 /* It's for somebody else! Retransmit. */
4103 path2 = path_duplicate (path);
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4105 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4106 path2 = path_duplicate (path);
4107 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4108 send_create_path (dest_peer_info, path, t);
4115 * Core handler for path destruction
4117 * @param cls closure
4118 * @param message message
4119 * @param peer peer identity this notification is about
4121 * @return GNUNET_OK to keep the connection open,
4122 * GNUNET_SYSERR to close it (signal serious error)
4125 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4126 const struct GNUNET_MessageHeader *message)
4128 struct GNUNET_MESH_ManipulatePath *msg;
4129 struct GNUNET_PeerIdentity *pi;
4130 struct MeshPeerPath *path;
4131 struct MeshTunnel *t;
4132 unsigned int own_pos;
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4137 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4138 size = ntohs (message->size);
4139 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4141 GNUNET_break_op (0);
4145 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4146 if (size % sizeof (struct GNUNET_PeerIdentity))
4148 GNUNET_break_op (0);
4151 size /= sizeof (struct GNUNET_PeerIdentity);
4154 GNUNET_break_op (0);
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4159 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4160 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4162 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4164 t = tunnel_get (pi, ntohl (msg->tid));
4167 /* TODO notify back: we don't know this tunnel */
4168 GNUNET_break_op (0);
4171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4172 path = path_new (size);
4174 for (i = 0; i < size; i++)
4176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4177 GNUNET_i2s (&pi[i]));
4178 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4179 if (path->peers[i] == myid)
4182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4183 if (own_pos < path->length - 1)
4184 send_prebuilt_message (message, &pi[own_pos + 1], t);
4186 send_client_tunnel_disconnect(t, NULL);
4188 // tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
4189 path_destroy (path);
4195 * Core handler for notifications of broken paths
4197 * @param cls closure
4198 * @param message message
4199 * @param peer peer identity this notification is about
4201 * @return GNUNET_OK to keep the connection open,
4202 * GNUNET_SYSERR to close it (signal serious error)
4205 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4206 const struct GNUNET_MessageHeader *message)
4208 struct GNUNET_MESH_PathBroken *msg;
4209 struct MeshTunnel *t;
4211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4212 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4213 msg = (struct GNUNET_MESH_PathBroken *) message;
4214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4215 GNUNET_i2s (&msg->peer1));
4216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4217 GNUNET_i2s (&msg->peer2));
4218 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4221 GNUNET_break_op (0);
4224 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4225 GNUNET_PEER_search (&msg->peer2));
4232 * Core handler for tunnel destruction
4234 * @param cls closure
4235 * @param message message
4236 * @param peer peer identity this notification is about
4238 * @return GNUNET_OK to keep the connection open,
4239 * GNUNET_SYSERR to close it (signal serious error)
4242 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4243 const struct GNUNET_MessageHeader *message)
4245 struct GNUNET_MESH_TunnelDestroy *msg;
4246 struct MeshTunnel *t;
4247 GNUNET_PEER_Id parent;
4250 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4252 "Got a TUNNEL DESTROY packet from %s\n",
4254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4255 " for tunnel %s [%u]\n",
4256 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4257 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4258 /* Check signature */
4261 /* Probably already got the message from another path,
4262 * destroyed the tunnel and retransmitted to children.
4265 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4269 parent = tree_get_predecessor (t->tree);
4270 pid = GNUNET_PEER_search (peer);
4275 // tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
4276 nc = tree_count_children (t->tree);
4277 if (nc > 0 || NULL != t->owner || t->nclients > 0)
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4280 "still in use: %u cl, %u ch\n",
4285 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4287 /* Tunnel was incoming, notify clients */
4288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4289 t->local_tid, t->local_tid_dest);
4290 send_clients_tunnel_destroy (t);
4292 tunnel_send_destroy (t, parent);
4293 t->destroy = GNUNET_YES;
4294 // TODO: add timeout to destroy the tunnel anyway
4300 * Core handler for mesh network traffic going from the origin to a peer
4302 * @param cls closure
4303 * @param peer peer identity this notification is about
4304 * @param message message
4305 * @return GNUNET_OK to keep the connection open,
4306 * GNUNET_SYSERR to close it (signal serious error)
4309 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4310 const struct GNUNET_MessageHeader *message)
4312 struct GNUNET_MESH_Unicast *msg;
4313 struct GNUNET_PeerIdentity *neighbor;
4314 struct MeshTunnel *t;
4315 GNUNET_PEER_Id dest_id;
4320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4323 size = ntohs (message->size);
4325 sizeof (struct GNUNET_MESH_Unicast) +
4326 sizeof (struct GNUNET_MessageHeader))
4331 msg = (struct GNUNET_MESH_Unicast *) message;
4332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4333 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4335 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4338 /* TODO notify back: we don't know this tunnel */
4339 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4340 GNUNET_break_op (0);
4343 pid = ntohl (msg->pid);
4344 if (t->fwd_pid == pid)
4346 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4348 " Already seen pid %u, DROPPING!\n", pid);
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4354 " pid %u not seen yet, forwarding\n", pid);
4359 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4361 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4362 GNUNET_break_op (0);
4363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4364 "Received PID %u, ACK %u\n",
4365 pid, t->last_fwd_ack);
4369 tunnel_reset_timeout (t);
4371 if (dest_id == myid)
4373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4374 " it's for us! sending to clients...\n");
4375 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4376 send_subscribed_clients (message, &msg[1].header, t);
4377 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4380 ttl = ntohl (msg->ttl);
4381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4384 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4385 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4386 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4390 " not for us, retransmitting...\n");
4392 neighbor = tree_get_first_hop (t->tree, dest_id);
4394 /* cinfo->fwd_pid = pid; FIXME
4396 if (GNUNET_YES == t->nobuffer &&
4397 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
4399 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4400 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
4401 GNUNET_break_op (0);
4404 send_prebuilt_message (message, neighbor, t);
4405 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4411 * Core handler for mesh network traffic toward the owner of a tunnel
4413 * @param cls closure
4414 * @param message message
4415 * @param peer peer identity this notification is about
4417 * @return GNUNET_OK to keep the connection open,
4418 * GNUNET_SYSERR to close it (signal serious error)
4421 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4422 const struct GNUNET_MessageHeader *message)
4424 struct GNUNET_MESH_ToOrigin *msg;
4425 struct GNUNET_PeerIdentity id;
4426 struct MeshPeerInfo *peer_info;
4427 struct MeshTunnel *t;
4428 GNUNET_PEER_Id predecessor;
4432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4434 size = ntohs (message->size);
4435 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4436 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4438 GNUNET_break_op (0);
4441 msg = (struct GNUNET_MESH_ToOrigin *) message;
4442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4443 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4444 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4445 pid = ntohl (msg->pid);
4449 /* TODO notify that we dont know this tunnel (whom)? */
4450 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4452 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4453 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4458 // if (cinfo->bck_pid == pid) FIXME
4460 // /* already seen this packet, drop */
4461 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4462 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4463 // " Already seen pid %u, DROPPING!\n", pid);
4464 // tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4465 // return GNUNET_OK;
4468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4469 " pid %u not seen yet, forwarding\n", pid);
4470 // cinfo->bck_pid = pid; FIXME
4472 if (NULL != t->owner)
4475 struct GNUNET_MESH_ToOrigin *copy;
4477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4478 " it's for us! sending to clients...\n");
4479 /* TODO signature verification */
4480 memcpy (cbuf, message, size);
4481 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4482 copy->tid = htonl (t->local_tid);
4484 copy->pid = htonl (t->bck_pid);
4485 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4486 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4487 ©->header, GNUNET_NO);
4488 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4492 " not for us, retransmitting...\n");
4494 peer_info = peer_info_get (&msg->oid);
4495 if (NULL == peer_info)
4497 /* unknown origin of tunnel */
4501 predecessor = tree_get_predecessor (t->tree);
4502 if (0 == predecessor)
4504 if (GNUNET_YES == t->destroy)
4506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4507 "to orig received on a dying tunnel %s [%X]\n",
4508 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4511 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4512 "unknown to origin at %s\n",
4513 GNUNET_i2s (&my_full_id));
4514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4518 "for tunnel %s [%X]\n",
4519 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4522 tree_debug (t->tree);
4525 GNUNET_PEER_resolve (predecessor, &id);
4526 send_prebuilt_message (message, &id, t);
4527 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4534 * Core handler for mesh network traffic point-to-point acks.
4536 * @param cls closure
4537 * @param message message
4538 * @param peer peer identity this notification is about
4540 * @return GNUNET_OK to keep the connection open,
4541 * GNUNET_SYSERR to close it (signal serious error)
4544 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4545 const struct GNUNET_MessageHeader *message)
4547 struct GNUNET_MESH_ACK *msg;
4548 struct MeshTunnel *t;
4551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4553 msg = (struct GNUNET_MESH_ACK *) message;
4555 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4559 /* TODO notify that we dont know this tunnel (whom)? */
4560 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4563 ack = ntohl (msg->pid);
4564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4566 /* Is this a forward or backward ACK? */
4567 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
4571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4572 // cinfo->fwd_ack = ack; FIXME
4573 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4574 tunnel_unlock_fwd_queues (t);
4575 // if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll) FIXME
4577 // GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
4578 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
4579 // cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4586 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4587 tunnel_unlock_bck_queue (t);
4594 * Core handler for mesh network traffic point-to-point ack polls.
4596 * @param cls closure
4597 * @param message message
4598 * @param peer peer identity this notification is about
4600 * @return GNUNET_OK to keep the connection open,
4601 * GNUNET_SYSERR to close it (signal serious error)
4604 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4605 const struct GNUNET_MessageHeader *message)
4607 struct GNUNET_MESH_Poll *msg;
4608 struct MeshTunnel *t;
4610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4613 msg = (struct GNUNET_MESH_Poll *) message;
4615 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4619 /* TODO notify that we dont know this tunnel (whom)? */
4620 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4621 GNUNET_break_op (0);
4625 /* Is this a forward or backward ACK? */
4626 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4629 /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
4630 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4635 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4642 * Core handler for path ACKs
4644 * @param cls closure
4645 * @param message message
4646 * @param peer peer identity this notification is about
4648 * @return GNUNET_OK to keep the connection open,
4649 * GNUNET_SYSERR to close it (signal serious error)
4652 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4653 const struct GNUNET_MessageHeader *message)
4655 struct GNUNET_MESH_PathACK *msg;
4656 struct GNUNET_PeerIdentity id;
4657 struct MeshPeerInfo *peer_info;
4658 struct MeshPeerPath *p;
4659 struct MeshTunnel *t;
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4662 GNUNET_i2s (&my_full_id));
4663 msg = (struct GNUNET_MESH_PathACK *) message;
4664 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4667 /* TODO notify that we don't know the tunnel */
4668 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4670 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4674 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4676 peer_info = peer_info_get (&msg->peer_id);
4677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4678 GNUNET_i2s (&msg->peer_id));
4679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4682 /* Add paths to peers? */
4683 p = tree_get_path_to_peer (t->tree, peer_info->id);
4686 path_add_to_peers (p, GNUNET_YES);
4694 /* Message for us? */
4695 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4698 if (NULL == t->owner)
4700 GNUNET_break_op (0);
4703 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
4705 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
4706 // send_client_peer_connected (t, peer_info->id);
4708 if (NULL != peer_info->dhtget)
4710 GNUNET_DHT_get_stop (peer_info->dhtget);
4711 peer_info->dhtget = NULL;
4716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4717 " not for us, retransmitting...\n");
4718 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4719 peer_info = peer_info_get (&msg->oid);
4720 send_prebuilt_message (message, &id, t);
4726 * Core handler for mesh keepalives.
4728 * @param cls closure
4729 * @param message message
4730 * @param peer peer identity this notification is about
4731 * @return GNUNET_OK to keep the connection open,
4732 * GNUNET_SYSERR to close it (signal serious error)
4734 * TODO: Check who we got this from, to validate route.
4737 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4738 const struct GNUNET_MessageHeader *message)
4740 struct GNUNET_MESH_TunnelKeepAlive *msg;
4741 struct MeshTunnel *t;
4743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4746 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4747 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4751 /* TODO notify that we dont know that tunnel */
4752 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4757 tunnel_reset_timeout (t);
4759 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4760 /* FIXME tunnel_send_multicast (t, message); */
4767 * Functions to handle messages from core
4769 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4770 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4771 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4772 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4773 sizeof (struct GNUNET_MESH_PathBroken)},
4774 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4775 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4776 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4777 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4778 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4779 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4780 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4781 sizeof (struct GNUNET_MESH_ACK)},
4782 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4783 sizeof (struct GNUNET_MESH_Poll)},
4784 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4785 sizeof (struct GNUNET_MESH_PathACK)},
4791 /******************************************************************************/
4792 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4793 /******************************************************************************/
4798 * notify_client_connection_failure: notify a client that the connection to the
4799 * requested remote peer is not possible (for instance, no route found)
4800 * Function called when the socket is ready to queue more data. "buf" will be
4801 * NULL and "size" zero if the socket was closed for writing in the meantime.
4803 * @param cls closure
4804 * @param size number of bytes available in buf
4805 * @param buf where the callee should write the message
4806 * @return number of bytes written to buf
4809 notify_client_connection_failure (void *cls, size_t size, void *buf)
4812 struct MeshPeerInfo *peer_info;
4813 struct GNUNET_MESH_PeerControl *msg;
4814 struct GNUNET_PeerIdentity id;
4816 if (0 == size && NULL == buf)
4818 // TODO retry? cancel?
4822 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4823 peer_info = (struct MeshPeerInfo *) cls;
4824 msg = (struct GNUNET_MESH_PeerControl *) buf;
4825 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4826 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4827 // msg->tunnel_id = htonl(peer_info->t->tid);
4828 GNUNET_PEER_resolve (peer_info->id, &id);
4829 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4837 * Send keepalive packets for a peer
4839 * @param cls Closure (tunnel for which to send the keepalive).
4840 * @param tc Notification context.
4843 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4845 struct MeshTunnel *t = cls;
4846 struct GNUNET_MESH_TunnelKeepAlive *msg;
4847 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4850 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4851 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4857 "sending keepalive for tunnel %d\n", t->id.tid);
4859 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4860 msg->header.size = htons (size);
4861 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4862 msg->oid = my_full_id;
4863 msg->tid = htonl (t->id.tid);
4864 /* FIXME tunnel_send_multicast (t, &msg->header); */
4866 t->path_refresh_task =
4867 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4868 tunnel_reset_timeout(t);
4873 * Function to process paths received for a new peer addition. The recorded
4874 * paths form the initial tunnel, which can be optimized later.
4875 * Called on each result obtained for the DHT search.
4877 * @param cls closure
4878 * @param exp when will this value expire
4879 * @param key key of the result
4880 * @param get_path path of the get request
4881 * @param get_path_length lenght of get_path
4882 * @param put_path path of the put request
4883 * @param put_path_length length of the put_path
4884 * @param type type of the result
4885 * @param size number of bytes in data
4886 * @param data pointer to the result data
4888 * TODO: re-issue the request after certain time? cancel after X results?
4891 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4892 const struct GNUNET_HashCode * key,
4893 const struct GNUNET_PeerIdentity *get_path,
4894 unsigned int get_path_length,
4895 const struct GNUNET_PeerIdentity *put_path,
4896 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4897 size_t size, const void *data)
4899 struct MeshPathInfo *path_info = cls;
4900 struct MeshPeerPath *p;
4901 struct GNUNET_PeerIdentity pi;
4904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4905 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4908 p = path_build_from_dht (get_path, get_path_length,
4909 put_path, put_path_length);
4910 path_add_to_peers (p, GNUNET_NO);
4912 for (i = 0; i < path_info->peer->ntunnels; i++)
4914 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
4915 peer_info_connect (path_info->peer, path_info->t);
4922 /******************************************************************************/
4923 /********************* MESH LOCAL HANDLES **************************/
4924 /******************************************************************************/
4928 * Handler for client disconnection
4930 * @param cls closure
4931 * @param client identification of the client; NULL
4932 * for the last call when the server is destroyed
4935 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4937 struct MeshClient *c;
4938 struct MeshClient *next;
4940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4950 if (c->handle != client)
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4958 GNUNET_SERVER_client_drop (c->handle);
4959 c->shutting_down = GNUNET_YES;
4960 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4961 &tunnel_destroy_iterator, c);
4962 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4963 &tunnel_destroy_iterator, c);
4964 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4965 &tunnel_destroy_iterator, c);
4966 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4967 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4968 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4970 if (NULL != c->types)
4971 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4973 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4976 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4985 * Handler for new clients
4987 * @param cls closure
4988 * @param client identification of the client
4989 * @param message the actual message, which includes messages the client wants
4992 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4993 const struct GNUNET_MessageHeader *message)
4995 struct GNUNET_MESH_ClientConnect *cc_msg;
4996 struct MeshClient *c;
5002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5004 /* Check data sanity */
5005 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5006 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5007 ntypes = ntohs (cc_msg->types);
5008 if (size != ntypes * sizeof (uint16_t))
5011 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5015 /* Create new client structure */
5016 c = GNUNET_malloc (sizeof (struct MeshClient));
5017 c->id = next_client_id++; // overflow not important: just for debug
5018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
5021 GNUNET_SERVER_client_keep (client);
5025 struct GNUNET_HashCode hc;
5027 t = (uint16_t *) &cc_msg[1];
5028 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
5029 for (i = 0; i < ntypes; i++)
5032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5033 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc); // FIXME pseudo hash
5035 /* store in clients hashmap */
5036 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5037 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5038 /* store in global hashmap */
5039 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5040 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5044 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
5045 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5046 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5047 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5048 GNUNET_SERVER_notification_context_add (nc, client);
5049 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5051 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5057 * Handler for requests of new tunnels
5059 * @param cls closure
5060 * @param client identification of the client
5061 * @param message the actual message
5064 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5065 const struct GNUNET_MessageHeader *message)
5067 struct GNUNET_MESH_TunnelMessage *t_msg;
5068 struct MeshPeerInfo *peer_info;
5069 struct MeshTunnel *t;
5070 struct MeshClient *c;
5071 MESH_TunnelNumber tid;
5073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5075 /* Sanity check for client registration */
5076 if (NULL == (c = client_get (client)))
5079 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5084 /* Message sanity check */
5085 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5088 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5092 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
5094 GNUNET_i2s (&t_msg->peer));
5095 /* Sanity check for tunnel numbering */
5096 tid = ntohl (t_msg->tunnel_id);
5097 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5103 /* Sanity check for duplicate tunnel IDs */
5104 if (NULL != tunnel_get_by_local_id (c, tid))
5107 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5111 while (NULL != tunnel_get_by_pi (myid, next_tid))
5112 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5113 t = tunnel_new (myid, next_tid++, c, tid);
5117 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5120 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5122 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5125 peer_info = peer_info_get (&t_msg->peer);
5126 tunnel_add_peer (t, peer_info);
5127 peer_info_connect (peer_info, t);
5128 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5134 * Handler for requests of deleting tunnels
5136 * @param cls closure
5137 * @param client identification of the client
5138 * @param message the actual message
5141 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5142 const struct GNUNET_MessageHeader *message)
5144 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5145 struct MeshClient *c;
5146 struct MeshTunnel *t;
5147 MESH_TunnelNumber tid;
5149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5150 "Got a DESTROY TUNNEL from client!\n");
5152 /* Sanity check for client registration */
5153 if (NULL == (c = client_get (client)))
5156 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5161 /* Message sanity check */
5162 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5169 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5171 /* Retrieve tunnel */
5172 tid = ntohl (tunnel_msg->tunnel_id);
5173 t = tunnel_get_by_local_id(c, tid);
5176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5178 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5181 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5183 client_ignore_tunnel (c, t);
5184 tunnel_destroy_empty (t);
5185 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5188 send_client_tunnel_disconnect (t, c);
5189 client_delete_tunnel (c, t);
5191 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5193 tunnel_send_destroy (t, 0);
5194 t->destroy = GNUNET_YES;
5195 /* The tunnel will be destroyed when the last message is transmitted. */
5196 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5202 * Handler for requests of seeting tunnel's buffering policy.
5204 * @param cls Closure (unused).
5205 * @param client Identification of the client.
5206 * @param message The actual message.
5209 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5210 const struct GNUNET_MessageHeader *message)
5212 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5213 struct MeshClient *c;
5214 struct MeshTunnel *t;
5215 MESH_TunnelNumber tid;
5217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5218 "Got a BUFFER request from client!\n");
5220 /* Sanity check for client registration */
5221 if (NULL == (c = client_get (client)))
5224 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5229 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5231 /* Retrieve tunnel */
5232 tid = ntohl (tunnel_msg->tunnel_id);
5233 t = tunnel_get_by_local_id(c, tid);
5236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5238 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5242 switch (ntohs(message->type))
5244 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5245 t->nobuffer = GNUNET_NO;
5247 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5248 t->nobuffer = GNUNET_YES;
5254 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5259 * Handler for client traffic directed to one peer
5261 * @param cls closure
5262 * @param client identification of the client
5263 * @param message the actual message
5266 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5267 const struct GNUNET_MessageHeader *message)
5269 struct MeshClient *c;
5270 struct MeshTunnel *t;
5271 struct GNUNET_MESH_Unicast *data_msg;
5272 MESH_TunnelNumber tid;
5275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5276 "Got a unicast request from a client!\n");
5278 /* Sanity check for client registration */
5279 if (NULL == (c = client_get (client)))
5282 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5287 data_msg = (struct GNUNET_MESH_Unicast *) message;
5289 /* Sanity check for message size */
5290 size = ntohs (message->size);
5291 if (sizeof (struct GNUNET_MESH_Unicast) +
5292 sizeof (struct GNUNET_MessageHeader) > size)
5295 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5299 /* Tunnel exists? */
5300 tid = ntohl (data_msg->tid);
5301 t = tunnel_get_by_local_id (c, tid);
5305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5309 /* Is it a local tunnel? Then, does client own the tunnel? */
5310 if (t->owner->handle != client)
5313 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5317 /* PID should be as expected */
5318 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
5321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5322 "Unicast PID, expected %u, got %u\n",
5323 t->fwd_pid + 1, ntohl (data_msg->pid));
5324 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5328 /* Ok, everything is correct, send the message
5329 * (pretend we got it from a mesh peer)
5332 /* Work around const limitation */
5333 char buf[ntohs (message->size)] GNUNET_ALIGN;
5334 struct GNUNET_MESH_Unicast *copy;
5336 copy = (struct GNUNET_MESH_Unicast *) buf;
5337 memcpy (buf, data_msg, size);
5338 copy->oid = my_full_id;
5339 copy->tid = htonl (t->id.tid);
5340 copy->ttl = htonl (default_ttl);
5341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5342 " calling generic handler...\n");
5343 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
5345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5346 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5353 * Handler for client traffic directed to the origin
5355 * @param cls closure
5356 * @param client identification of the client
5357 * @param message the actual message
5360 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5361 const struct GNUNET_MessageHeader *message)
5363 struct GNUNET_MESH_ToOrigin *data_msg;
5364 struct MeshTunnelClientInfo *clinfo;
5365 struct MeshClient *c;
5366 struct MeshTunnel *t;
5367 MESH_TunnelNumber tid;
5370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5371 "Got a ToOrigin request from a client!\n");
5372 /* Sanity check for client registration */
5373 if (NULL == (c = client_get (client)))
5376 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5381 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5383 /* Sanity check for message size */
5384 size = ntohs (message->size);
5385 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5386 sizeof (struct GNUNET_MessageHeader) > size)
5389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5393 /* Tunnel exists? */
5394 tid = ntohl (data_msg->tid);
5395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5396 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5399 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5402 t = tunnel_get_by_local_id (c, tid);
5405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5412 /* It should be sent by someone who has this as incoming tunnel. */
5413 if (GNUNET_NO == client_knows_tunnel (c, t))
5416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5420 /* PID should be as expected */
5421 clinfo = tunnel_get_client_fc (t, c);
5422 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
5425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5426 "To Origin PID, expected %u, got %u\n",
5427 clinfo->bck_pid + 1,
5428 ntohl (data_msg->pid));
5429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5434 /* Ok, everything is correct, send the message
5435 * (pretend we got it from a mesh peer)
5438 char buf[ntohs (message->size)] GNUNET_ALIGN;
5439 struct GNUNET_MESH_ToOrigin *copy;
5441 /* Work around const limitation */
5442 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5443 memcpy (buf, data_msg, size);
5444 GNUNET_PEER_resolve (t->id.oid, ©->oid);
5445 copy->tid = htonl (t->id.tid);
5446 copy->ttl = htonl (default_ttl);
5447 copy->pid = htonl (t->bck_pid + 1);
5449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5450 " calling generic handler...\n");
5451 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
5453 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5460 * Handler for client's ACKs for payload traffic.
5462 * @param cls Closure (unused).
5463 * @param client Identification of the client.
5464 * @param message The actual message.
5467 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5468 const struct GNUNET_MessageHeader *message)
5470 struct GNUNET_MESH_LocalAck *msg;
5471 struct MeshTunnel *t;
5472 struct MeshClient *c;
5473 MESH_TunnelNumber tid;
5476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5477 /* Sanity check for client registration */
5478 if (NULL == (c = client_get (client)))
5481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5486 msg = (struct GNUNET_MESH_LocalAck *) message;
5488 /* Tunnel exists? */
5489 tid = ntohl (msg->tunnel_id);
5490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5491 t = tunnel_get_by_local_id (c, tid);
5495 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5496 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5501 ack = ntohl (msg->max_pid);
5502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
5504 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5505 if (NULL != t->owner && t->owner->handle == client)
5507 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5509 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5513 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5514 tunnel_set_client_fwd_ack (t, c, ack);
5515 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5518 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5526 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5528 * @param cls Closure (client handle).
5529 * @param key Key (hashed tunnel ID, unused).
5530 * @param value Tunnel info.
5532 * @return GNUNET_YES, to keep iterating.
5535 monitor_all_tunnels_iterator (void *cls,
5536 const struct GNUNET_HashCode * key,
5539 struct GNUNET_SERVER_Client *client = cls;
5540 struct MeshTunnel *t = value;
5541 struct GNUNET_MESH_LocalMonitor *msg;
5543 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5544 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5545 msg->tunnel_id = htonl (t->id.tid);
5546 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5547 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5548 GNUNET_PEER_resolve (t->peer, &msg->destination);
5550 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5551 "* sending info about tunnel %s [%u]\n",
5552 GNUNET_i2s (&msg->owner), t->id.tid);
5554 GNUNET_SERVER_notification_context_unicast (nc, client,
5555 &msg->header, GNUNET_NO);
5561 * Handler for client's MONITOR request.
5563 * @param cls Closure (unused).
5564 * @param client Identification of the client.
5565 * @param message The actual message.
5568 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5569 const struct GNUNET_MessageHeader *message)
5571 struct MeshClient *c;
5573 /* Sanity check for client registration */
5574 if (NULL == (c = client_get (client)))
5577 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5581 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5582 "Received get tunnels request from client %u\n",
5584 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5585 monitor_all_tunnels_iterator,
5587 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5588 "Get tunnels request from client %u completed\n",
5590 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5595 * Handler for client's MONITOR_TUNNEL request.
5597 * @param cls Closure (unused).
5598 * @param client Identification of the client.
5599 * @param message The actual message.
5602 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5603 const struct GNUNET_MessageHeader *message)
5605 const struct GNUNET_MESH_LocalMonitor *msg;
5606 struct GNUNET_MESH_LocalMonitor *resp;
5607 struct MeshClient *c;
5608 struct MeshTunnel *t;
5610 /* Sanity check for client registration */
5611 if (NULL == (c = client_get (client)))
5614 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5618 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5619 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5620 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5623 ntohl (msg->tunnel_id));
5624 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5627 /* We don't know the tunnel FIXME */
5628 struct GNUNET_MESH_LocalMonitor warn;
5631 GNUNET_SERVER_notification_context_unicast (nc, client,
5634 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5638 /* Initialize context */
5639 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5641 GNUNET_PEER_resolve (t->peer, &resp->destination);
5642 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5643 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5644 &resp->header, GNUNET_NO);
5647 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5648 "Monitor tunnel request from client %u completed\n",
5650 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5655 * Functions to handle messages from clients
5657 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5658 {&handle_local_new_client, NULL,
5659 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5660 {&handle_local_tunnel_create, NULL,
5661 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5662 sizeof (struct GNUNET_MESH_TunnelMessage)},
5663 {&handle_local_tunnel_destroy, NULL,
5664 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5665 sizeof (struct GNUNET_MESH_TunnelMessage)},
5666 {&handle_local_tunnel_buffer, NULL,
5667 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
5668 sizeof (struct GNUNET_MESH_TunnelMessage)},
5669 {&handle_local_tunnel_buffer, NULL,
5670 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
5671 sizeof (struct GNUNET_MESH_TunnelMessage)},
5672 {&handle_local_unicast, NULL,
5673 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5674 {&handle_local_to_origin, NULL,
5675 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5676 {&handle_local_ack, NULL,
5677 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5678 sizeof (struct GNUNET_MESH_LocalAck)},
5679 {&handle_local_get_tunnels, NULL,
5680 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5681 sizeof (struct GNUNET_MessageHeader)},
5682 {&handle_local_show_tunnel, NULL,
5683 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5684 sizeof (struct GNUNET_MESH_LocalMonitor)},
5690 * To be called on core init/fail.
5692 * @param cls service closure
5693 * @param server handle to the server for this service
5694 * @param identity the public identity of this peer
5697 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5698 const struct GNUNET_PeerIdentity *identity)
5701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5702 core_handle = server;
5703 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5709 GNUNET_i2s (identity));
5710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5712 GNUNET_i2s (&my_full_id));
5713 GNUNET_SCHEDULER_shutdown (); // Try gracefully
5715 GNUNET_abort(); // Try harder
5722 * Method called whenever a given peer connects.
5724 * @param cls closure
5725 * @param peer peer identity this notification is about
5728 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5730 struct MeshPeerInfo *peer_info;
5731 struct MeshPeerPath *path;
5733 DEBUG_CONN ("Peer connected\n");
5734 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5735 peer_info = peer_info_get (peer);
5736 if (myid == peer_info->id)
5738 DEBUG_CONN (" (self)\n");
5743 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5745 path = path_new (2);
5746 path->peers[0] = myid;
5747 path->peers[1] = peer_info->id;
5748 GNUNET_PEER_change_rc (myid, 1);
5749 GNUNET_PEER_change_rc (peer_info->id, 1);
5750 peer_info_add_path (peer_info, path, GNUNET_YES);
5751 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5757 * Method called whenever a peer disconnects.
5759 * @param cls closure
5760 * @param peer peer identity this notification is about
5763 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5765 struct MeshPeerInfo *pi;
5766 struct MeshPeerQueue *q;
5767 struct MeshPeerQueue *n;
5769 DEBUG_CONN ("Peer disconnected\n");
5770 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5780 /* TODO try to reroute this traffic instead */
5781 queue_destroy(q, GNUNET_YES);
5784 if (NULL != pi->core_transmit)
5786 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5787 pi->core_transmit = NULL;
5789 peer_info_remove_path (pi, pi->id, myid);
5792 DEBUG_CONN (" (self)\n");
5794 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5799 /******************************************************************************/
5800 /************************ MAIN FUNCTIONS ****************************/
5801 /******************************************************************************/
5804 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5806 * @param cls closure
5807 * @param key current key code
5808 * @param value value in the hash map
5809 * @return GNUNET_YES if we should continue to iterate,
5813 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5815 struct MeshTunnel *t = value;
5822 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5824 * @param cls closure
5825 * @param key current key code
5826 * @param value value in the hash map
5827 * @return GNUNET_YES if we should continue to iterate,
5831 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5833 struct MeshPeerInfo *p = value;
5834 struct MeshPeerQueue *q;
5835 struct MeshPeerQueue *n;
5843 queue_destroy(q, GNUNET_YES);
5847 peer_info_destroy (p);
5853 * Task run during shutdown.
5859 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5863 if (core_handle != NULL)
5865 GNUNET_CORE_disconnect (core_handle);
5870 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
5873 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5874 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5875 if (dht_handle != NULL)
5877 GNUNET_DHT_disconnect (dht_handle);
5882 GNUNET_SERVER_notification_context_destroy (nc);
5885 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5887 GNUNET_SCHEDULER_cancel (announce_id_task);
5888 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5895 * Callback for hostkey read/generation.
5897 * @param cls Closure (Configuration handle).
5898 * @param pk The ECC private key.
5899 * @param emsg Error message, if any.
5902 key_generation_cb (void *cls,
5903 struct GNUNET_CRYPTO_EccPrivateKey *pk,
5906 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5907 struct MeshPeerInfo *peer;
5908 struct MeshPeerPath *p;
5913 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5914 _("Mesh service could not access hostkey: %s. Exiting.\n"),
5916 GNUNET_SCHEDULER_shutdown ();
5919 my_private_key = pk;
5920 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5921 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5922 &my_full_id.hashPubKey);
5923 myid = GNUNET_PEER_intern (&my_full_id);
5924 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5925 "Mesh for peer [%s] starting\n",
5926 GNUNET_i2s(&my_full_id));
5928 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5929 NULL, /* Closure passed to MESH functions */
5930 &core_init, /* Call core_init once connected */
5931 &core_connect, /* Handle connects */
5932 &core_disconnect, /* remove peers on disconnects */
5933 NULL, /* Don't notify about all incoming messages */
5934 GNUNET_NO, /* For header only in notification */
5935 NULL, /* Don't notify about all outbound messages */
5936 GNUNET_NO, /* For header-only out notification */
5937 core_handlers); /* Register these handlers */
5939 if (core_handle == NULL)
5942 GNUNET_SCHEDULER_shutdown ();
5947 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5950 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5951 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5952 GNUNET_SERVER_disconnect_notify (server_handle,
5953 &handle_local_client_disconnect, NULL);
5956 clients_head = NULL;
5957 clients_tail = NULL;
5960 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5962 /* Create a peer_info for the local peer */
5963 peer = peer_info_get (&my_full_id);
5966 GNUNET_PEER_change_rc (myid, 1);
5967 peer_info_add_path (peer, p, GNUNET_YES);
5968 GNUNET_SERVER_resume (server_handle);
5969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5974 * Process mesh requests.
5976 * @param cls closure
5977 * @param server the initialized server
5978 * @param c configuration to use
5981 run (void *cls, struct GNUNET_SERVER_Handle *server,
5982 const struct GNUNET_CONFIGURATION_Handle *c)
5986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5987 server_handle = server;
5990 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5993 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5995 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5996 "mesh", "peer/privatekey");
5997 GNUNET_SCHEDULER_shutdown ();
6002 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6003 &refresh_path_time))
6005 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6007 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6008 "mesh", "refresh path time");
6009 GNUNET_SCHEDULER_shutdown ();
6014 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6017 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6019 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6020 "mesh", "id announce time");
6021 GNUNET_SCHEDULER_shutdown ();
6026 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6029 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6031 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6032 "mesh", "connect timeout");
6033 GNUNET_SCHEDULER_shutdown ();
6038 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6041 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6043 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6044 "mesh", "max msgs queue");
6045 GNUNET_SCHEDULER_shutdown ();
6050 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6053 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6055 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6056 "mesh", "max tunnels");
6057 GNUNET_SCHEDULER_shutdown ();
6062 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6065 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6067 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6068 "mesh", "default ttl", 64);
6073 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6077 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6078 "mesh", "max peers", 1000);
6083 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6084 &dht_replication_level))
6086 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6088 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6089 "mesh", "dht replication level", 3);
6090 dht_replication_level = 3;
6093 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6094 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6095 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6096 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6098 dht_handle = GNUNET_DHT_connect (c, 64);
6099 if (NULL == dht_handle)
6103 stats = GNUNET_STATISTICS_create ("mesh", c);
6105 GNUNET_SERVER_suspend (server_handle);
6106 /* Scheduled the task to clean up when shutdown is called */
6107 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6109 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
6112 GNUNET_free (keyfile);
6117 * The main function for the mesh service.
6119 * @param argc number of arguments from the command line
6120 * @param argv command line arguments
6121 * @return 0 ok, 1 on error
6124 main (int argc, char *const *argv)
6129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6130 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6132 ret = (GNUNET_OK == r) ? 0 : 1;
6133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
6137 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6138 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
6139 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);