2 This file is part of GNUnet.
3 (C) 2001-2013 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
27 * - keep queues until receiving ACK
30 * - relay corking down to core
31 * - set ttl relative to path length
39 #include "mesh_protocol.h"
40 #include "mesh_path.h"
41 #include "block_mesh.h"
42 #include "gnunet_dht_service.h"
43 #include "gnunet_statistics_service.h"
45 #define MESH_BLOOM_SIZE 128
47 #define MESH_DEBUG_DHT GNUNET_NO
48 #define MESH_DEBUG_CONNECTION GNUNET_NO
49 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
51 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
52 GNUNET_TIME_UNIT_MINUTES,\
54 #define MESH_RETRANSMIT_TIME GNUNET_TIME_relative_multiply (\
55 GNUNET_TIME_UNIT_SECONDS,\
58 #if MESH_DEBUG_CONNECTION
59 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
61 #define DEBUG_CONN(...)
65 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
67 #define DEBUG_DHT(...)
74 struct timespec __mesh_start;
75 struct timespec __mesh_end;
76 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
77 #define INTERVAL_END \
79 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
80 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
81 if (__diff < 0) __diff += 1000000000;\
85 #define INTERVAL_SHOW \
87 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
89 #define INTERVAL_START
94 /******************************************************************************/
95 /************************ DATA STRUCTURES ****************************/
96 /******************************************************************************/
98 /** FWD declaration */
104 * Struct containing info about a queued transmission to this peer
111 struct MeshPeerQueue *next;
116 struct MeshPeerQueue *prev;
119 * Peer this transmission is directed to.
121 struct MeshPeerInfo *peer;
124 * Tunnel this message belongs to.
126 struct MeshTunnel *tunnel;
129 * Pointer to info stucture used as cls.
139 * Size of the message
146 * Struct containing all information regarding a given peer
156 * Last time we heard from this peer
158 struct GNUNET_TIME_Absolute last_contact;
161 * Number of attempts to reconnect so far
163 int n_reconnect_attempts;
166 * Paths to reach the peer, ordered by ascending hop count
168 struct MeshPeerPath *path_head;
171 * Paths to reach the peer, ordered by ascending hop count
173 struct MeshPeerPath *path_tail;
176 * Handle to stop the DHT search for a path to this peer
178 struct GNUNET_DHT_GetHandle *dhtget;
181 * Array of tunnels this peer is the target of.
182 * Most probably a small amount, therefore not a hashmap.
183 * When the path to the peer changes, notify these tunnels to let them
184 * re-adjust their path trees.
186 struct MeshTunnel **tunnels;
189 * Number of tunnels this peers participates in
191 unsigned int ntunnels;
194 * Transmission queue to core DLL head
196 struct MeshPeerQueue *queue_head;
199 * Transmission queue to core DLL tail
201 struct MeshPeerQueue *queue_tail;
204 * How many messages are in the queue to this peer.
206 unsigned int queue_n;
209 * Handle for queued transmissions
211 struct GNUNET_CORE_TransmitHandle *core_transmit;
216 * Struct to encapsulate all the Flow Control information to a peer in the
217 * context of a tunnel: Same peer in different tunnels will have independent
218 * flow control structures, allowing to choke/free tunnels according to its
221 struct MeshFlowControl
224 * ID of the last packet sent towards the peer.
226 uint32_t last_pid_sent;
229 * ID of the last packet received from the peer.
231 uint32_t last_pid_recv;
234 * Last ACK sent to the peer (peer can't send more than this PID).
236 uint32_t last_ack_sent;
239 * Last ACK sent towards the origin (for traffic towards leaf node).
241 uint32_t last_ack_recv;
244 * How many payload messages are in the queue towards this peer.
249 * Task to poll the peer in case of a lost ACK causes stall.
251 GNUNET_SCHEDULER_TaskIdentifier poll_task;
254 * How frequently to poll for ACKs.
256 struct GNUNET_TIME_Relative poll_time;
259 * On which tunnel to poll.
260 * Using an explicit poll_ctx would not help memory wise,
261 * since the allocated context would have to be stored in the
262 * fc struct in order to free it upon cancelling poll_task.
264 struct MeshTunnel *t;
269 * Globally unique tunnel identification (owner + number)
270 * DO NOT USE OVER THE NETWORK
275 * Node that owns the tunnel
280 * Tunnel number to differentiate all the tunnels owned by the node oid
281 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
283 MESH_TunnelNumber tid;
288 * Info needed to retry a message in case it gets lost.
290 struct MeshSentMessage
293 * Double linked list, FIFO style
295 struct MeshSentMessage *next;
296 struct MeshSentMessage *prev;
299 * Tunnel this message is in.
301 struct MeshTunnel *t;
304 * ID of the message (ACK needed to free)
309 * Task to resend/poll in case no ACK is received.
311 GNUNET_SCHEDULER_TaskIdentifier retry_task; // FIXME move to per tunnel timer?
314 * Counter for exponential backoff.
316 struct GNUNET_TIME_Relative retry_timer;
319 * Is this a forward or backward going message?
323 /* struct GNUNET_MESH_Data with payload */
327 * Struct containing all information regarding a tunnel
328 * For an intermediate node the improtant info used will be:
329 * - id Tunnel unique identification
330 * - paths[0] To know where to send it next
331 * - metainfo: ready, speeds, accounting
338 struct MESH_TunnelID id;
341 * Port of the tunnel.
346 * State of the tunnel.
348 enum MeshTunnelState state;
351 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
353 MESH_TunnelNumber local_tid;
356 * Local tunnel number for local destination clients (incoming number)
357 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
360 MESH_TunnelNumber local_tid_dest;
363 * Is the tunnel bufferless (minimum latency)?
368 * Is the tunnel reliable?
373 * Force sending ACK? Flag to allow duplicate ACK on POLL.
378 * How many messages do we accept in the forward queue.
380 unsigned int queue_max;
383 * Last time the tunnel was used
385 struct GNUNET_TIME_Absolute timestamp;
388 * Destination of the tunnel.
393 * Next hop in the tunnel. If 0, @c client must be set.
395 GNUNET_PEER_Id next_hop;
398 * Previous hop in the tunnel. If 0, @c owner must be set.
400 GNUNET_PEER_Id prev_hop;
403 * Flow control information about @c next_hop or @c client.
405 struct MeshFlowControl next_fc;
408 * Flow control information about @c prev_hop or @c owner.
410 struct MeshFlowControl prev_fc;
413 * Client owner of the tunnel, if any
415 struct MeshClient *owner;
418 * Client destination of the tunnel, if any.
420 struct MeshClient *client;
423 * Task to keep the used paths alive at the owner,
424 * time tunnel out on all the other peers.
426 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
429 * Path being used for the tunnel.
431 struct MeshPeerPath *path;
434 * Flag to signal the destruction of the tunnel.
435 * If this is set GNUNET_YES the tunnel will be destroyed
436 * when the queue is empty.
441 * Total messages pending for this tunnels, payload or not.
443 unsigned int pending_messages;
446 * Messages sent and not yet ACK'd.
447 * Only present (non-NULL) at the owner of a tunnel.
449 struct MeshSentMessage *fwd_head;
450 struct MeshSentMessage *fwd_tail;
453 * Messages sent and not yet ACK'd.
454 * Only present (non-NULL) at the destination of a tunnel.
456 struct MeshSentMessage *bck_head;
457 struct MeshSentMessage *bck_tail;
462 * Struct containing information about a client of the service
464 * TODO: add a list of 'waiting' ports
471 struct MeshClient *next;
476 struct MeshClient *prev;
479 * Tunnels that belong to this client, indexed by local id
481 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
484 * Tunnels this client has accepted, indexed by incoming local id
486 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
489 * Handle to communicate with the client
491 struct GNUNET_SERVER_Client *handle;
494 * Ports that this client has declared interest in.
495 * Indexed by port, contains *Client.
497 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
500 * Whether the client is active or shutting down (don't send confirmations
501 * to a client that is shutting down.
506 * ID of the client, mainly for debug messages
513 /******************************************************************************/
514 /************************ DEBUG FUNCTIONS ****************************/
515 /******************************************************************************/
519 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
520 * @param cls string to print
521 * @param success GNUNET_OK if the PUT was transmitted,
522 * GNUNET_NO on timeout,
523 * GNUNET_SYSERR on disconnect from service
524 * after the PUT message was transmitted
525 * (so we don't know if it was received or not)
530 mesh_debug (void *cls, int success)
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
539 unsigned int debug_fwd_ack;
540 unsigned int debug_bck_ack;
544 /******************************************************************************/
545 /*********************** GLOBAL VARIABLES ****************************/
546 /******************************************************************************/
548 /************************** Configuration parameters **************************/
551 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
553 static struct GNUNET_TIME_Relative refresh_path_time;
556 * How often to PUT own ID in the DHT.
558 static struct GNUNET_TIME_Relative id_announce_time;
561 * Maximum time allowed to connect to a peer found by string.
563 static struct GNUNET_TIME_Relative connect_timeout;
566 * Default TTL for payload packets.
568 static unsigned long long default_ttl;
571 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
573 static unsigned long long dht_replication_level;
576 * How many tunnels are we willing to maintain.
577 * Local tunnels are always allowed, even if there are more tunnels than max.
579 static unsigned long long max_tunnels;
582 * How many messages *in total* are we willing to queue, divided by number of
583 * tunnels to get tunnel queue size.
585 static unsigned long long max_msgs_queue;
588 * How many peers do we want to remember?
590 static unsigned long long max_peers;
593 * Percentage of messages that will be dropped (for test purposes only).
595 static unsigned long long drop_percent;
597 /*************************** Static global variables **************************/
600 * DLL with all the clients, head.
602 static struct MeshClient *clients_head;
605 * DLL with all the clients, tail.
607 static struct MeshClient *clients_tail;
610 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
612 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
615 * Number of tunnels known.
617 static unsigned long long n_tunnels;
620 * Tunnels incoming, indexed by MESH_TunnelNumber
621 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
623 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
626 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
628 static struct GNUNET_CONTAINER_MultiHashMap *peers;
631 * Handle to communicate with transport
633 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
636 * Handle to communicate with core.
638 static struct GNUNET_CORE_Handle *core_handle;
643 static struct GNUNET_DHT_Handle *dht_handle;
648 static struct GNUNET_SERVER_Handle *server_handle;
651 * Handle to the statistics service.
653 static struct GNUNET_STATISTICS_Handle *stats;
656 * Notification context, to send messages to local clients.
658 static struct GNUNET_SERVER_NotificationContext *nc;
661 * Local peer own ID (memory efficient handle).
663 static GNUNET_PEER_Id myid;
666 * Local peer own ID (full value).
668 static struct GNUNET_PeerIdentity my_full_id;
673 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
678 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
681 * Tunnel ID for the next created tunnel (global tunnel number).
683 static MESH_TunnelNumber next_tid;
686 * Tunnel ID for the next incoming tunnel (local tunnel number).
688 static MESH_TunnelNumber next_local_tid;
691 * All ports clients of this peer have opened.
693 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
696 * Task to periodically announce itself in the network.
698 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
701 * Next ID to assign to a client.
703 unsigned int next_client_id;
706 /******************************************************************************/
707 /*********************** DECLARATIONS **************************/
708 /******************************************************************************/
711 * Function to process paths received for a new peer addition. The recorded
712 * paths form the initial tunnel, which can be optimized later.
713 * Called on each result obtained for the DHT search.
716 * @param exp when will this value expire
717 * @param key key of the result
718 * @param type type of the result
719 * @param size number of bytes in data
720 * @param data pointer to the result data
723 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
724 const struct GNUNET_HashCode * key,
725 const struct GNUNET_PeerIdentity *get_path,
726 unsigned int get_path_length,
727 const struct GNUNET_PeerIdentity *put_path,
728 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
729 size_t size, const void *data);
733 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
734 * and insert it in the appropriate structures if the peer is not known yet.
736 * @param peer Full identity of the peer.
738 * @return Existing or newly created peer info.
740 static struct MeshPeerInfo *
741 peer_get (const struct GNUNET_PeerIdentity *peer);
745 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
746 * and insert it in the appropriate structures if the peer is not known yet.
748 * @param peer Short identity of the peer.
750 * @return Existing or newly created peer info.
752 static struct MeshPeerInfo *
753 peer_get_short (const GNUNET_PEER_Id peer);
757 * Build a PeerPath from the paths returned from the DHT, reversing the paths
758 * to obtain a local peer -> destination path and interning the peer ids.
760 * @return Newly allocated and created path
762 static struct MeshPeerPath *
763 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
764 unsigned int get_path_length,
765 const struct GNUNET_PeerIdentity *put_path,
766 unsigned int put_path_length);
770 * Adds a path to the peer_infos of all the peers in the path
772 * @param p Path to process.
773 * @param confirmed Whether we know if the path works or not.
776 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
781 * Search for a tunnel by global ID using full PeerIdentities.
783 * @param oid owner of the tunnel.
784 * @param tid global tunnel number.
786 * @return tunnel handler, NULL if doesn't exist.
788 static struct MeshTunnel *
789 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
793 * Notify a tunnel that a connection has broken that affects at least
796 * @param t Tunnel affected.
797 * @param p1 Peer that got disconnected from p2.
798 * @param p2 Peer that got disconnected from p1.
800 * @return Short ID of the peer disconnected (either p1 or p2).
801 * 0 if the tunnel remained unaffected.
803 static GNUNET_PEER_Id
804 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
809 * @brief Use the given path for the tunnel.
810 * Update the next and prev hops (and RCs).
811 * (Re)start the path refresh in case the tunnel is locally owned.
813 * @param t Tunnel to update.
814 * @param p Path to use.
817 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
820 * Tunnel is empty: destroy it.
822 * Notifies all participants (peers, cleints) about the destruction.
824 * @param t Tunnel to destroy.
827 tunnel_destroy_empty (struct MeshTunnel *t);
830 * @brief Queue and pass message to core when possible.
832 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
833 * and accounts for it. In case the queue is full, the message is dropped and
836 * Otherwise, message is treated as internal and allowed to go regardless of
839 * @param cls Closure (@c type dependant). It will be used by queue_send to
840 * build the message to be sent if not already prebuilt.
841 * @param type Type of the message, 0 for a raw message.
842 * @param size Size of the message.
843 * @param dst Neighbor to send message to.
844 * @param t Tunnel this message belongs to.
847 queue_add (void *cls, uint16_t type, size_t size,
848 struct MeshPeerInfo *dst, struct MeshTunnel *t);
852 * Free a transmission that was already queued with all resources
853 * associated to the request.
855 * @param queue Queue handler to cancel.
856 * @param clear_cls Is it necessary to free associated cls?
859 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
863 * @brief Get the next transmittable message from the queue.
865 * This will be the head, except in the case of being a data packet
866 * not allowed by the destination peer.
868 * @param peer Destination peer.
870 * @return The next viable MeshPeerQueue element to send to that peer.
871 * NULL when there are no transmittable messages.
873 struct MeshPeerQueue *
874 queue_get_next (const struct MeshPeerInfo *peer);
878 * Core callback to write a queued packet to core buffer
880 * @param cls Closure (peer info).
881 * @param size Number of bytes available in buf.
882 * @param buf Where the to write the message.
884 * @return number of bytes written to buf
887 queue_send (void *cls, size_t size, void *buf);
890 /******************************************************************************/
891 /************************ PERIODIC FUNCTIONS ****************************/
892 /******************************************************************************/
895 * Periodically announce self id in the DHT
898 * @param tc task context
901 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
905 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
907 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
911 * - Set data expiration in function of X
914 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
916 block.id = my_full_id;
917 GNUNET_DHT_put (dht_handle, /* DHT handle */
918 &my_full_id.hashPubKey, /* Key to use */
919 dht_replication_level, /* Replication level */
920 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
921 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
922 sizeof (block), /* Size of the data */
923 (const char *) &block, /* Data itself */
924 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
925 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
926 NULL, /* Continuation */
927 NULL); /* Continuation closure */
929 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
933 /******************************************************************************/
934 /****************** GENERAL HELPER FUNCTIONS ************************/
935 /******************************************************************************/
939 * Check if client has registered with the service and has not disconnected
941 * @param client the client to check
943 * @return non-NULL if client exists in the global DLL
945 static struct MeshClient *
946 client_get (struct GNUNET_SERVER_Client *client)
948 struct MeshClient *c;
953 if (c->handle == client)
962 * Deletes a tunnel from a client (either owner or destination). To be used on
965 * @param c Client whose tunnel to delete.
966 * @param t Tunnel which should be deleted.
969 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
973 GNUNET_assert (GNUNET_YES ==
974 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
980 GNUNET_assert (GNUNET_YES ==
981 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
988 * Notify the appropriate client that a new incoming tunnel was created.
990 * @param t Tunnel that was created.
993 send_client_tunnel_create (struct MeshTunnel *t)
995 struct GNUNET_MESH_TunnelMessage msg;
997 if (NULL == t->client)
999 msg.header.size = htons (sizeof (msg));
1000 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1001 msg.tunnel_id = htonl (t->local_tid_dest);
1002 msg.port = htonl (t->port);
1004 msg.options |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1005 msg.options |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1006 msg.options = htonl (msg.options);
1007 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1008 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1009 &msg.header, GNUNET_NO);
1014 * Notify dest client that the incoming tunnel is no longer valid.
1016 * @param c Client to notify..
1017 * @param t Tunnel that is destroyed.
1020 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
1022 struct GNUNET_MESH_TunnelMessage msg;
1029 if (c != t->client && c != t->owner)
1034 msg.header.size = htons (sizeof (msg));
1035 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1036 msg.tunnel_id = htonl (t->local_tid_dest);
1037 msg.port = htonl (0);
1038 memset(&msg.peer, 0, sizeof (msg.peer));
1039 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1040 &msg.header, GNUNET_NO);
1045 * Iterator over all the peers to remove the oldest not-used entry.
1047 * @param cls Closure (unsued).
1048 * @param key ID of the peer.
1049 * @param value Peer_Info of the peer.
1054 peer_info_timeout (void *cls,
1055 const struct GNUNET_HashCode *key,
1062 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1063 * and insert it in the appropriate structures if the peer is not known yet.
1065 * @param peer Full identity of the peer.
1067 * @return Existing or newly created peer info.
1069 static struct MeshPeerInfo *
1070 peer_get (const struct GNUNET_PeerIdentity *peer)
1072 struct MeshPeerInfo *peer_info;
1074 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1075 if (NULL == peer_info)
1078 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1079 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1081 GNUNET_CONTAINER_multihashmap_iterate (peers,
1085 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1086 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1087 peer_info->id = GNUNET_PEER_intern (peer);
1089 peer_info->last_contact = GNUNET_TIME_absolute_get();
1096 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1097 * and insert it in the appropriate structures if the peer is not known yet.
1099 * @param peer Short identity of the peer.
1101 * @return Existing or newly created peer info.
1103 static struct MeshPeerInfo *
1104 peer_get_short (const GNUNET_PEER_Id peer)
1106 struct GNUNET_PeerIdentity id;
1108 GNUNET_PEER_resolve (peer, &id);
1109 return peer_get (&id);
1114 * Choose the best path towards a peer considering the tunnel properties.
1116 * @param peer The destination peer.
1117 * @param t The tunnel the path is for.
1119 * @return Best current known path towards the peer, if any.
1121 static struct MeshPeerPath *
1122 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1124 struct MeshPeerPath *best_p;
1125 struct MeshPeerPath *p;
1126 unsigned int best_cost;
1129 best_p = p = peer->path_head;
1130 best_cost = cost = p->length;
1133 if ((cost = p->length) < best_cost)
1144 * Core callback to write a pre-constructed data packet to core buffer
1146 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1147 * @param size Number of bytes available in buf.
1148 * @param buf Where the to write the message.
1150 * @return number of bytes written to buf
1153 send_core_data_raw (void *cls, size_t size, void *buf)
1155 struct GNUNET_MessageHeader *msg = cls;
1158 GNUNET_assert (NULL != msg);
1159 total_size = ntohs (msg->size);
1161 if (total_size > size)
1166 memcpy (buf, msg, total_size);
1173 * Sends an already built message to a peer, properly registrating
1174 * all used resources.
1176 * @param message Message to send. Function makes a copy of it.
1177 * @param peer Short ID of the neighbor whom to send the message.
1178 * @param t Tunnel on which this message is transmitted.
1181 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1182 GNUNET_PEER_Id peer,
1183 struct MeshTunnel *t)
1185 struct GNUNET_PeerIdentity id;
1186 struct MeshPeerInfo *neighbor;
1187 struct MeshPeerPath *p;
1192 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1197 size = ntohs (message->size);
1198 data = GNUNET_malloc (size);
1199 memcpy (data, message, size);
1200 type = ntohs(message->type);
1201 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1202 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1204 struct GNUNET_MESH_Data *u;
1206 u = (struct GNUNET_MESH_Data *) data;
1207 u->ttl = htonl (ntohl (u->ttl) - 1);
1209 GNUNET_PEER_resolve (peer, &id);
1210 neighbor = peer_get (&id);
1211 for (p = neighbor->path_head; NULL != p; p = p->next)
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1222 " %s IS NOT DIRECTLY CONNECTED\n",
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 for (p = neighbor->path_head; NULL != p; p = p->next)
1229 struct GNUNET_PeerIdentity debug_id;
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 " path with %u hops through:\n",
1235 for (i = 0; i < p->length; i++)
1237 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1240 i, GNUNET_i2s(&debug_id));
1244 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1245 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1246 " no direct connection to %s\n",
1251 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1262 * Sends a CREATE PATH message for a path to a peer, properly registrating
1263 * all used resources.
1265 * @param t Tunnel for which the path is created.
1268 send_create_path (struct MeshTunnel *t)
1270 struct MeshPeerInfo *neighbor;
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1273 neighbor = peer_get_short (t->next_hop);
1275 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1276 sizeof (struct GNUNET_MESH_CreateTunnel) +
1277 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1280 t->state = MESH_TUNNEL_WAITING;
1285 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1287 * @param t Tunnel which to confirm.
1290 send_path_ack (struct MeshTunnel *t)
1292 struct MeshPeerInfo *peer;
1294 peer = peer_get_short (t->prev_hop);
1297 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1298 sizeof (struct GNUNET_MESH_PathACK),
1305 * Try to establish a new connection to this peer in the given tunnel.
1306 * If the peer doesn't have any path to it yet, try to get one.
1307 * If the peer already has some path, send a CREATE PATH towards it.
1309 * @param peer PeerInfo of the peer.
1310 * @param t Tunnel for which to create the path, if possible.
1313 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1315 struct MeshPeerPath *p;
1317 if (NULL != peer->path_head)
1319 p = peer_get_best_path (peer, t);
1320 tunnel_use_path (t, p);
1321 send_create_path (t);
1323 else if (NULL == peer->dhtget)
1325 struct GNUNET_PeerIdentity id;
1327 GNUNET_PEER_resolve (peer->id, &id);
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1330 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1331 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1332 &id.hashPubKey, /* key to search */
1333 dht_replication_level, /* replication level */
1334 GNUNET_DHT_RO_RECORD_ROUTE |
1335 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1337 0, /* xquery bits */
1338 &dht_get_id_handler, peer);
1339 t->state = MESH_TUNNEL_SEARCHING;
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1344 "There is no path but the DHT GET is already started.\n");
1350 * Destroy the peer_info and free any allocated resources linked to it
1352 * @param pi The peer_info to destroy.
1354 * @return GNUNET_OK on success
1357 peer_info_destroy (struct MeshPeerInfo *pi)
1359 struct GNUNET_PeerIdentity id;
1360 struct MeshPeerPath *p;
1361 struct MeshPeerPath *nextp;
1364 GNUNET_PEER_resolve (pi->id, &id);
1365 GNUNET_PEER_change_rc (pi->id, -1);
1368 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1372 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1374 if (NULL != pi->dhtget)
1376 GNUNET_DHT_get_stop (pi->dhtget);
1382 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1386 for (i = 0; i < pi->ntunnels; i++)
1387 tunnel_destroy_empty (pi->tunnels[i]);
1388 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1395 * Remove all paths that rely on a direct connection between p1 and p2
1396 * from the peer itself and notify all tunnels about it.
1398 * @param peer PeerInfo of affected peer.
1399 * @param p1 GNUNET_PEER_Id of one peer.
1400 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1403 * TODO: optimize (see below)
1406 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1409 struct MeshPeerPath *p;
1410 struct MeshPeerPath *next;
1411 struct MeshPeerInfo *peer_d;
1413 unsigned int destroyed;
1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1418 for (p = peer->path_head; NULL != p; p = next)
1421 for (i = 0; i < (p->length - 1); i++)
1423 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1424 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1426 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1436 for (i = 0; i < peer->ntunnels; i++)
1438 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1442 peer_d = peer_get_short (d);
1443 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1444 tunnel_use_path (peer->tunnels[i], next);
1445 peer_connect (peer_d, peer->tunnels[i]);
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1452 * Add the path to the peer and update the path used to reach it in case this
1455 * @param peer_info Destination peer to add the path to.
1456 * @param path New path to add. Last peer must be the peer in arg 1.
1457 * Path will be either used of freed if already known.
1458 * @param trusted Do we trust that this path is real?
1461 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1464 struct MeshPeerPath *aux;
1468 if ((NULL == peer_info) || (NULL == path))
1471 path_destroy (path);
1474 if (path->peers[path->length - 1] != peer_info->id)
1477 path_destroy (path);
1480 if (2 >= path->length && GNUNET_NO == trusted)
1482 /* Only allow CORE to tell us about direct paths */
1483 path_destroy (path);
1486 for (l = 1; l < path->length; l++)
1488 if (path->peers[l] == myid)
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1491 for (l2 = 0; l2 < path->length - l; l2++)
1493 path->peers[l2] = path->peers[l + l2];
1498 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1503 struct GNUNET_PeerIdentity id;
1505 GNUNET_PEER_resolve (peer_info->id, &id);
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1507 path->length, GNUNET_i2s (&id));
1510 l = path_get_length (path);
1513 path_destroy (path);
1517 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1518 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1520 l2 = path_get_length (aux);
1523 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1524 peer_info->path_tail, aux, path);
1529 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1531 path_destroy (path);
1536 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1543 * Add the path to the origin peer and update the path used to reach it in case
1544 * this is the shortest.
1545 * The path is given in peer_info -> destination, therefore we turn the path
1546 * upside down first.
1548 * @param peer_info Peer to add the path to, being the origin of the path.
1549 * @param path New path to add after being inversed.
1550 * Path will be either used or freed.
1551 * @param trusted Do we trust that this path is real?
1554 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1555 struct MeshPeerPath *path, int trusted)
1558 peer_info_add_path (peer_info, path, trusted);
1563 * Add a tunnel to the list of tunnels a peer participates in.
1564 * Update the tunnel's destination.
1566 * @param p Peer to add to.
1567 * @param t Tunnel to add.
1570 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1574 GNUNET_break (t->dest == p->id);
1578 GNUNET_PEER_change_rc (t->dest, 1);
1579 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1584 * Remove a tunnel from the list of tunnels a peer participates in.
1585 * Free the tunnel's destination.
1587 * @param p Peer to clean.
1588 * @param t Tunnel to remove.
1591 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1595 if (t->dest == p->id)
1597 GNUNET_PEER_change_rc (t->dest, -1);
1600 for (i = 0; i < p->ntunnels; i++)
1602 if (p->tunnels[i] == t)
1604 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1605 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1613 * Function called if the connection to the peer has been stalled for a while,
1614 * possibly due to a missed ACK. Poll the peer about its ACK status.
1616 * @param cls Closure (poll ctx).
1617 * @param tc TaskContext.
1620 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1622 struct MeshFlowControl *fc = cls;
1623 struct GNUNET_MESH_Poll msg;
1624 struct MeshTunnel *t = fc->t;
1625 GNUNET_PEER_Id peer;
1627 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1628 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1635 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1636 msg.header.size = htons (sizeof (msg));
1637 msg.tid = htonl (t->id.tid);
1638 msg.pid = htonl (fc->last_pid_sent);
1639 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1641 if (fc == &t->prev_fc)
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1646 else if (fc == &t->next_fc)
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1656 send_prebuilt_message (&msg.header, peer, t);
1657 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1658 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1664 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1665 * to obtain a local peer -> destination path and interning the peer ids.
1667 * @return Newly allocated and created path
1669 static struct MeshPeerPath *
1670 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1671 unsigned int get_path_length,
1672 const struct GNUNET_PeerIdentity *put_path,
1673 unsigned int put_path_length)
1675 struct MeshPeerPath *p;
1681 GNUNET_PEER_change_rc (myid, 1);
1682 i = get_path_length;
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1684 for (i--; i >= 0; i--)
1686 id = GNUNET_PEER_intern (&get_path[i]);
1687 if (p->length > 0 && id == p->peers[p->length - 1])
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1690 GNUNET_PEER_change_rc (id, -1);
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1695 GNUNET_i2s (&get_path[i]));
1697 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1698 p->peers[p->length - 1] = id;
1701 i = put_path_length;
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1703 for (i--; i >= 0; i--)
1705 id = GNUNET_PEER_intern (&put_path[i]);
1708 /* PUT path went through us, so discard the path up until now and start
1709 * from here to get a much shorter (and loop-free) path.
1714 if (p->length > 0 && id == p->peers[p->length - 1])
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1717 GNUNET_PEER_change_rc (id, -1);
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1722 GNUNET_i2s (&put_path[i]));
1724 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1725 p->peers[p->length - 1] = id;
1729 if (get_path_length > 0)
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1731 GNUNET_i2s (&get_path[0]));
1732 if (put_path_length > 0)
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1734 GNUNET_i2s (&put_path[0]));
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1737 for (i = 0; i < p->length; i++)
1739 struct GNUNET_PeerIdentity peer_id;
1741 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1743 GNUNET_i2s (&peer_id));
1751 * Adds a path to the peer_infos of all the peers in the path
1753 * @param p Path to process.
1754 * @param confirmed Whether we know if the path works or not.
1757 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1761 /* TODO: invert and add */
1762 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1763 for (i++; i < p->length; i++)
1765 struct MeshPeerInfo *aux;
1766 struct MeshPeerPath *copy;
1768 aux = peer_get_short (p->peers[i]);
1769 copy = path_duplicate (p);
1770 copy->length = i + 1;
1771 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1777 * Send keepalive packets for a peer
1779 * @param cls Closure (tunnel for which to send the keepalive).
1780 * @param tc Notification context.
1783 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1787 * Search for a tunnel among the incoming tunnels
1789 * @param tid the local id of the tunnel
1791 * @return tunnel handler, NULL if doesn't exist
1793 static struct MeshTunnel *
1794 tunnel_get_incoming (MESH_TunnelNumber tid)
1796 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1797 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1802 * Search for a tunnel among the tunnels for a client
1804 * @param c the client whose tunnels to search in
1805 * @param tid the local id of the tunnel
1807 * @return tunnel handler, NULL if doesn't exist
1809 static struct MeshTunnel *
1810 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1812 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1814 return tunnel_get_incoming (tid);
1818 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1819 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1825 * Search for a tunnel by global ID using PEER_ID
1827 * @param pi owner of the tunnel
1828 * @param tid global tunnel number
1830 * @return tunnel handler, NULL if doesn't exist
1832 static struct MeshTunnel *
1833 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1835 struct MESH_TunnelID id;
1836 struct GNUNET_HashCode hash;
1841 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1842 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1847 * Search for a tunnel by global ID using full PeerIdentities
1849 * @param oid owner of the tunnel
1850 * @param tid global tunnel number
1852 * @return tunnel handler, NULL if doesn't exist
1854 static struct MeshTunnel *
1855 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1857 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1862 * Add a client to a tunnel, initializing all needed data structures.
1864 * @param t Tunnel to which add the client.
1865 * @param c Client which to add to the tunnel.
1868 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1870 if (NULL != t->client)
1876 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1877 t->local_tid_dest, t,
1878 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1884 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1885 t->local_tid_dest, t,
1886 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1896 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1898 unsigned int own_pos;
1900 for (own_pos = 0; own_pos < p->length; own_pos++)
1902 if (p->peers[own_pos] == myid)
1905 if (own_pos > p->length - 1)
1911 if (own_pos < p->length - 1)
1912 t->next_hop = p->peers[own_pos + 1];
1914 t->next_hop = p->peers[own_pos];
1915 GNUNET_PEER_change_rc (t->next_hop, 1);
1917 t->prev_hop = p->peers[own_pos - 1];
1919 t->prev_hop = p->peers[0];
1920 GNUNET_PEER_change_rc (t->prev_hop, 1);
1922 if (NULL != t->path)
1923 path_destroy (t->path);
1924 t->path = path_duplicate (p);
1927 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1928 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1929 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1936 * Notifies a tunnel that a connection has broken that affects at least
1937 * some of its peers. Sends a notification towards the root of the tree.
1938 * In case the peer is the owner of the tree, notifies the client that owns
1939 * the tunnel and tries to reconnect.
1941 * @param t Tunnel affected.
1942 * @param p1 Peer that got disconnected from p2.
1943 * @param p2 Peer that got disconnected from p1.
1945 * @return Short ID of the peer disconnected (either p1 or p2).
1946 * 0 if the tunnel remained unaffected.
1948 static GNUNET_PEER_Id
1949 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1952 // if (myid != p1 && myid != p2) FIXME
1957 // if (tree_get_predecessor (t->tree) != 0)
1959 // /* We are the peer still connected, notify owner of the disconnection. */
1960 // struct GNUNET_MESH_PathBroken msg;
1961 // struct GNUNET_PeerIdentity neighbor;
1963 // msg.header.size = htons (sizeof (msg));
1964 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1965 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1966 // msg.tid = htonl (t->id.tid);
1967 // msg.peer1 = my_full_id;
1968 // GNUNET_PEER_resolve (pid, &msg.peer2);
1969 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1970 // send_prebuilt_message (&msg.header, &neighbor, t);
1977 * Build a local ACK message and send it to a local client.
1979 * @param t Tunnel on which to send the ACK.
1980 * @param c Client to whom send the ACK.
1981 * @param ack Value of the ACK.
1982 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1985 send_local_ack (struct MeshTunnel *t,
1986 struct MeshClient *c,
1989 struct GNUNET_MESH_LocalAck msg;
1991 msg.header.size = htons (sizeof (msg));
1992 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1993 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
1994 GNUNET_SERVER_notification_context_unicast (nc,
2001 * Build an ACK message and queue it to send to the given peer.
2003 * @param t Tunnel on which to send the ACK.
2004 * @param peer Peer to whom send the ACK.
2005 * @param ack Value of the ACK.
2008 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2010 struct GNUNET_MESH_ACK msg;
2012 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2013 msg.header.size = htons (sizeof (msg));
2014 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2015 msg.pid = htonl (ack);
2016 msg.tid = htonl (t->id.tid);
2018 send_prebuilt_message (&msg.header, peer, t);
2023 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2025 * @param t Tunnel this is about.
2028 tunnel_send_fwd_data_ack (struct MeshTunnel *t)
2030 struct GNUNET_MESH_DataACK msg;
2032 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
2033 msg.header.size = htons (sizeof (msg));
2034 msg.tid = htonl (t->id.tid);
2035 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2036 msg.pid = htonl (t->prev_fc.last_pid_recv);
2037 msg.futures = 0; // FIXME set bits of other newer messages received
2039 send_prebuilt_message (&msg.header, t->prev_hop, t);
2044 * Send an end-to-end BCK ACK message for the most recent in-sequence payload.
2046 * @param t Tunnel this is about.
2049 tunnel_send_bck_data_ack (struct MeshTunnel *t)
2051 struct GNUNET_MESH_DataACK msg;
2053 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2054 msg.header.size = htons (sizeof (msg));
2055 msg.tid = htonl (t->id.tid);
2056 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2057 msg.pid = htonl (t->next_fc.last_pid_recv);
2058 msg.futures = 0; // FIXME set bits of other newer messages received
2060 send_prebuilt_message (&msg.header, t->next_hop, t);
2065 * Send an ACK informing the predecessor about the available buffer space.
2066 * In case there is no predecessor, inform the owning client.
2067 * If buffering is off, send only on behalf of children or self if endpoint.
2068 * If buffering is on, send when sent to children and buffer space is free.
2069 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2070 * the ACK itself goes "back" (towards root).
2072 * @param t Tunnel on which to send the ACK.
2073 * @param type Type of message that triggered the ACK transmission.
2076 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2080 /* Is it after unicast retransmission? */
2083 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2085 "ACK due to FWD DATA retransmission\n");
2086 if (GNUNET_YES == t->nobuffer)
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2091 if (GNUNET_YES == t->reliable && NULL != t->client)
2092 tunnel_send_fwd_data_ack (t);
2094 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2095 if (NULL != t->owner && GNUNET_YES == t->reliable)
2097 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2099 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2100 tunnel_send_fwd_data_ack (t);
2102 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2103 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2104 t->force_ack = GNUNET_YES;
2110 /* Check if we need to transmit the ACK */
2111 if (NULL == t->owner &&
2112 t->queue_max > t->next_fc.queue_n * 4 &&
2113 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2114 GNUNET_NO == t->force_ack)
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, buffer free\n");
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2118 " t->qmax: %u, t->qn: %u\n",
2119 t->queue_max, t->next_fc.queue_n);
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121 " t->pid: %u, t->ack: %u\n",
2122 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2126 /* Ok, ACK might be necessary, what PID to ACK? */
2127 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK %u\n", ack);
2129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2130 " last %u, qmax %u, q %u\n",
2131 t->prev_fc.last_pid_recv, t->queue_max, t->next_fc.queue_n);
2132 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2138 t->prev_fc.last_ack_sent = ack;
2139 if (NULL != t->owner)
2140 send_local_ack (t, t->owner, GNUNET_YES);
2141 else if (0 != t->prev_hop)
2142 send_ack (t, t->prev_hop, ack);
2146 t->force_ack = GNUNET_NO;
2151 * Send an ACK informing the children node/client about the available
2153 * If buffering is off, send only on behalf of root (can be self).
2154 * If buffering is on, send when sent to predecessor and buffer space is free.
2155 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2156 * the ACK itself goes "forward" (towards children/clients).
2158 * @param t Tunnel on which to send the ACK.
2159 * @param type Type of message that triggered the ACK transmission.
2162 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2167 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2168 /* Is it after data to_origin retransmission? */
2171 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2172 if (GNUNET_YES == t->nobuffer)
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 " Not sending ACK, nobuffer + traffic\n");
2179 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2180 if (NULL != t->client && GNUNET_YES == t->reliable)
2182 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2184 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2185 tunnel_send_bck_data_ack (t);
2187 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2188 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2189 t->force_ack = GNUNET_YES;
2195 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2197 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2199 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2202 " Not sending ACK, not needed, last ack sent was %u\n",
2203 t->next_fc.last_ack_sent);
2206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2207 " Sending BCK ACK %u (last sent: %u)\n",
2208 ack, t->next_fc.last_ack_sent);
2209 t->next_fc.last_ack_sent = ack;
2211 if (NULL != t->client)
2212 send_local_ack (t, t->client, GNUNET_NO);
2213 else if (0 != t->next_hop)
2214 send_ack (t, t->next_hop, ack);
2217 t->force_ack = GNUNET_NO;
2222 * Modify the mesh message TID from global to local and send to client.
2224 * @param t Tunnel on which to send the message.
2225 * @param msg Message to modify and send.
2226 * @param c Client to send to.
2227 * @param tid Tunnel ID to use (c can be both owner and client).
2230 tunnel_send_client_data (struct MeshTunnel *t,
2231 const struct GNUNET_MESH_Data *msg,
2232 struct MeshClient *c, MESH_TunnelNumber tid)
2234 struct GNUNET_MESH_LocalData *copy;
2235 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2236 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2238 if (size < sizeof (struct GNUNET_MessageHeader))
2240 GNUNET_break_op (0);
2248 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2249 memcpy (©[1], &msg[1], size);
2250 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2251 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2252 copy->tid = htonl (tid);
2253 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2254 ©->header, GNUNET_NO);
2258 * Modify the unicast message TID from global to local and send to client.
2260 * @param t Tunnel on which to send the message.
2261 * @param msg Message to modify and send.
2264 tunnel_send_client_ucast (struct MeshTunnel *t,
2265 const struct GNUNET_MESH_Data *msg)
2267 tunnel_send_client_data (t, msg, t->client, t->local_tid_dest);
2272 * Modify the to_origin message TID from global to local and send to client.
2274 * @param t Tunnel on which to send the message.
2275 * @param msg Message to modify and send.
2278 tunnel_send_client_to_orig (struct MeshTunnel *t,
2279 const struct GNUNET_MESH_Data *msg)
2281 tunnel_send_client_data (t, msg, t->owner, t->local_tid);
2286 * We haven't received an ACK after a certain time: restransmit the message.
2288 * @param cls Closure (MeshSentMessage with the message to restransmit)
2289 * @param tc TaskContext.
2292 tunnel_retransmit_message (void *cls,
2293 const struct GNUNET_SCHEDULER_TaskContext *tc)
2295 struct MeshSentMessage *copy = cls;
2296 struct GNUNET_MESH_Data *payload;
2299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Retransmit \n");
2300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->id);
2301 copy->retry_task = GNUNET_SCHEDULER_NO_TASK;
2302 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2305 payload = (struct GNUNET_MESH_Data *) ©[1];
2306 hop = copy->is_forward ? copy->t->next_hop : copy->t->prev_hop;
2307 send_prebuilt_message (&payload->header, hop, copy->t);
2308 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2309 copy->retry_timer = GNUNET_TIME_STD_BACKOFF (copy->retry_timer);
2310 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
2311 &tunnel_retransmit_message,
2317 * @brief Re-initiate traffic to this peer if necessary.
2319 * Check if there is traffic queued towards this peer
2320 * and the core transmit handle is NULL (traffic was stalled).
2321 * If so, call core tmt rdy.
2323 * @param peer_id Short ID of peer to which initiate traffic.
2326 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2328 struct MeshPeerInfo *peer;
2329 struct GNUNET_PeerIdentity id;
2330 struct MeshPeerQueue *q;
2333 peer = peer_get_short (peer_id);
2334 if (NULL != peer->core_transmit)
2337 q = queue_get_next (peer);
2340 /* Might br multicast traffic already sent to this particular peer but
2341 * not to other children in this tunnel.
2342 * This way t->queue_n would be > 0 but the queue of this particular peer
2348 GNUNET_PEER_resolve (peer->id, &id);
2349 peer->core_transmit =
2350 GNUNET_CORE_notify_transmit_ready(core_handle,
2353 GNUNET_TIME_UNIT_FOREVER_REL,
2363 * Send a message to all peers and clients in this tunnel that the tunnel
2364 * is no longer valid. If some peer or client should not receive the message,
2365 * should be zero'ed out before calling this function.
2367 * @param t The tunnel whose peers and clients to notify.
2370 tunnel_send_destroy (struct MeshTunnel *t)
2372 struct GNUNET_MESH_TunnelDestroy msg;
2373 struct GNUNET_PeerIdentity id;
2375 msg.header.size = htons (sizeof (msg));
2376 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2377 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2378 msg.tid = htonl (t->id.tid);
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 " sending tunnel destroy for tunnel: %s [%X]\n",
2381 GNUNET_i2s (&msg.oid), t->id.tid);
2383 if (NULL == t->client && 0 != t->next_hop)
2385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2386 GNUNET_PEER_resolve (t->next_hop, &id);
2387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2388 " sending forward to %s\n",
2390 send_prebuilt_message (&msg.header, t->next_hop, t);
2392 if (NULL == t->owner && 0 != t->prev_hop)
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2395 GNUNET_PEER_resolve (t->prev_hop, &id);
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2397 " sending back to %s\n",
2399 send_prebuilt_message (&msg.header, t->prev_hop, t);
2401 if (NULL != t->owner)
2403 send_client_tunnel_destroy (t->owner, t);
2405 if (NULL != t->client)
2407 send_client_tunnel_destroy (t->client, t);
2413 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2415 * @param t Tunnel which to cancel.
2416 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2419 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2421 struct MeshPeerInfo *peer_info;
2422 struct MeshPeerQueue *pq;
2423 struct MeshPeerQueue *next;
2426 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2427 peer_info = peer_get_short (neighbor);
2428 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2431 if (pq->tunnel == t)
2433 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2434 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2436 /* Should have been removed on destroy children */
2439 queue_destroy (pq, GNUNET_YES);
2442 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2444 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2445 peer_info->core_transmit = NULL;
2451 * Destroy the tunnel.
2453 * This function does not generate any warning traffic to clients or peers.
2456 * Remove the tunnel from peer_info's and clients' hashmaps.
2457 * Cancel messages belonging to this tunnel queued to neighbors.
2458 * Free any allocated resources linked to the tunnel.
2460 * @param t the tunnel to destroy
2462 * @return GNUNET_OK on success
2465 tunnel_destroy (struct MeshTunnel *t)
2467 struct MeshClient *c;
2468 struct GNUNET_HashCode hash;
2478 struct GNUNET_PeerIdentity id;
2480 GNUNET_PEER_resolve (t->id.oid, &id);
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2482 GNUNET_i2s (&id), t->id.tid);
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2488 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2489 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2498 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2505 if (NULL != t->client)
2509 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2510 t->local_tid_dest, t))
2516 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2517 t->local_tid_dest, t))
2524 if (0 != t->prev_hop)
2526 peer_cancel_queues (t->prev_hop, t);
2527 GNUNET_PEER_change_rc (t->prev_hop, -1);
2529 if (0 != t->next_hop)
2531 peer_cancel_queues (t->next_hop, t);
2532 GNUNET_PEER_change_rc (t->next_hop, -1);
2535 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2538 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2539 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2542 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2543 path_destroy (t->path);
2549 * Tunnel is empty: destroy it.
2551 * Notifies all participants (peers, cleints) about the destruction.
2553 * @param t Tunnel to destroy.
2556 tunnel_destroy_empty (struct MeshTunnel *t)
2560 struct GNUNET_PeerIdentity id;
2562 GNUNET_PEER_resolve (t->id.oid, &id);
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2564 "executing destruction of empty tunnel %s [%X]\n",
2565 GNUNET_i2s (&id), t->id.tid);
2569 if (GNUNET_NO == t->destroy)
2570 tunnel_send_destroy (t);
2571 if (0 == t->pending_messages)
2574 t->destroy = GNUNET_YES;
2578 * Initialize a Flow Control structure to the initial state.
2580 * @param fc Flow Control structure to initialize.
2583 fc_init (struct MeshFlowControl *fc)
2585 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2586 fc->last_pid_recv = (uint32_t) -1;
2587 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2588 fc->last_ack_recv = (uint32_t) -1;
2589 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2590 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2595 * Create a new tunnel
2597 * @param owner Who is the owner of the tunnel (short ID).
2598 * @param tid Tunnel Number of the tunnel.
2599 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2600 * @param local Tunnel Number for the tunnel, for the client point of view.
2602 * @return A new initialized tunnel. NULL on error.
2604 static struct MeshTunnel *
2605 tunnel_new (GNUNET_PEER_Id owner,
2606 MESH_TunnelNumber tid,
2607 struct MeshClient *client,
2608 MESH_TunnelNumber local)
2610 struct MeshTunnel *t;
2611 struct GNUNET_HashCode hash;
2613 if (n_tunnels >= max_tunnels && NULL == client)
2616 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2619 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2621 fc_init (&t->next_fc);
2622 fc_init (&t->prev_fc);
2623 t->local_tid = local;
2625 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2627 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2629 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2630 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2637 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2645 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2647 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2651 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2661 * Set options in a tunnel, extracted from a bit flag field
2663 * @param t Tunnel to set options to.
2664 * @param options Bit array in host byte order.
2667 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2669 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
2670 GNUNET_YES : GNUNET_NO;
2671 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
2672 GNUNET_YES : GNUNET_NO;
2677 * Iterator for deleting each tunnel whose client endpoint disconnected.
2679 * @param cls Closure (client that has disconnected).
2680 * @param key The local tunnel id (used to access the hashmap).
2681 * @param value The value stored at the key (tunnel to destroy).
2683 * @return GNUNET_OK, keep iterating.
2686 tunnel_destroy_iterator (void *cls,
2690 struct MeshTunnel *t = value;
2691 struct MeshClient *c = cls;
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2694 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2695 t->local_tid, t->local_tid_dest, c->id);
2696 client_delete_tunnel (c, t);
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2701 if (0 != t->next_hop) { /* destroy could come before a path is used */
2702 GNUNET_PEER_change_rc (t->next_hop, -1);
2708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2710 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2711 GNUNET_PEER_change_rc (t->prev_hop, -1);
2716 tunnel_destroy_empty (t);
2723 * Timeout function, destroys tunnel if called
2725 * @param cls Closure (tunnel to destroy).
2726 * @param tc TaskContext
2729 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2731 struct MeshTunnel *t = cls;
2732 struct GNUNET_PeerIdentity id;
2734 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2735 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2737 GNUNET_PEER_resolve(t->id.oid, &id);
2738 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2739 "Tunnel %s [%X] timed out. Destroying.\n",
2740 GNUNET_i2s(&id), t->id.tid);
2741 if (NULL != t->client)
2742 send_client_tunnel_destroy (t->client, t);
2743 tunnel_destroy (t); /* Do not notify other */
2748 * Resets the tunnel timeout. Starts it if no timeout was running.
2750 * @param t Tunnel whose timeout to reset.
2752 * TODO use heap to improve efficiency of scheduler.
2755 tunnel_reset_timeout (struct MeshTunnel *t)
2757 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2759 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2760 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2761 t->maintenance_task =
2762 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2763 (refresh_path_time, 4), &tunnel_timeout, t);
2767 /******************************************************************************/
2768 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2769 /******************************************************************************/
2772 * Function to send a create path packet to a peer.
2774 * @param cls closure
2775 * @param size number of bytes available in buf
2776 * @param buf where the callee should write the message
2777 * @return number of bytes written to buf
2780 send_core_path_create (void *cls, size_t size, void *buf)
2782 struct MeshTunnel *t = cls;
2783 struct GNUNET_MESH_CreateTunnel *msg;
2784 struct GNUNET_PeerIdentity *peer_ptr;
2785 struct MeshPeerPath *p = t->path;
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2792 sizeof (struct GNUNET_MESH_CreateTunnel) +
2793 p->length * sizeof (struct GNUNET_PeerIdentity);
2795 if (size < size_needed || NULL == buf)
2800 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2801 msg->header.size = htons (size_needed);
2802 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2803 msg->tid = ntohl (t->id.tid);
2806 if (GNUNET_YES == t->nobuffer)
2807 opt |= GNUNET_MESH_OPTION_NOBUFFER;
2808 if (GNUNET_YES == t->reliable)
2809 opt |= GNUNET_MESH_OPTION_RELIABLE;
2810 msg->opt = htonl (opt);
2811 msg->port = htonl (t->port);
2813 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2814 for (i = 0; i < p->length; i++)
2816 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2820 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2826 * Creates a path ack message in buf and frees all unused resources.
2828 * @param cls closure (MeshTransmissionDescriptor)
2829 * @param size number of bytes available in buf
2830 * @param buf where the callee should write the message
2831 * @return number of bytes written to buf
2834 send_core_path_ack (void *cls, size_t size, void *buf)
2836 struct MeshTunnel *t = cls;
2837 struct GNUNET_MESH_PathACK *msg = buf;
2839 GNUNET_assert (NULL != t);
2840 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2845 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2846 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2847 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2848 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2849 msg->tid = htonl (t->id.tid);
2850 msg->peer_id = my_full_id;
2851 msg->ack = htonl (t->prev_fc.last_ack_sent);
2853 /* TODO add signature */
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2856 return sizeof (struct GNUNET_MESH_PathACK);
2861 * Free a transmission that was already queued with all resources
2862 * associated to the request.
2864 * @param queue Queue handler to cancel.
2865 * @param clear_cls Is it necessary to free associated cls?
2868 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2870 struct MeshFlowControl *fc;
2872 if (GNUNET_YES == clear_cls)
2874 switch (queue->type)
2876 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2877 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2878 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2880 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2881 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2882 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2883 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2884 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2886 " prebuilt message\n");
2887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2889 GNUNET_MESH_DEBUG_M2S (queue->type));
2891 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2896 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2897 " type %s unknown!\n",
2898 GNUNET_MESH_DEBUG_M2S (queue->type));
2900 GNUNET_free_non_null (queue->cls);
2902 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2903 queue->peer->queue_tail,
2906 /* Delete from appropriate fc in the tunnel */
2907 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
2908 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
2910 if (queue->peer->id == queue->tunnel->prev_hop)
2911 fc = &queue->tunnel->prev_fc;
2912 else if (queue->peer->id == queue->tunnel->next_hop)
2913 fc = &queue->tunnel->next_fc;
2921 GNUNET_free (queue);
2926 * @brief Get the next transmittable message from the queue.
2928 * This will be the head, except in the case of being a data packet
2929 * not allowed by the destination peer.
2931 * @param peer Destination peer.
2933 * @return The next viable MeshPeerQueue element to send to that peer.
2934 * NULL when there are no transmittable messages.
2936 struct MeshPeerQueue *
2937 queue_get_next (const struct MeshPeerInfo *peer)
2939 struct MeshPeerQueue *q;
2941 struct GNUNET_MESH_Data *dmsg;
2942 struct MeshTunnel* t;
2946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2947 for (q = peer->queue_head; NULL != q; q = q->next)
2950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2952 GNUNET_MESH_DEBUG_M2S (q->type));
2953 dmsg = (struct GNUNET_MESH_Data *) q->cls;
2954 pid = ntohl (dmsg->pid);
2957 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2958 ack = t->next_fc.last_ack_recv;
2960 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2961 ack = t->prev_fc.last_ack_recv;
2964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2969 "* ACK: %u, PID: %u\n",
2971 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2984 "* nothing found\n");
2990 queue_send (void *cls, size_t size, void *buf)
2992 struct MeshPeerInfo *peer = cls;
2993 struct GNUNET_MessageHeader *msg;
2994 struct MeshPeerQueue *queue;
2995 struct MeshTunnel *t;
2996 struct GNUNET_PeerIdentity dst_id;
2997 struct MeshFlowControl *fc;
3002 peer->core_transmit = NULL;
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3005 queue = queue_get_next (peer);
3007 /* Queue has no internal mesh traffic nor sendable payload */
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3011 if (NULL == peer->queue_head)
3012 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3017 GNUNET_PEER_resolve (peer->id, &dst_id);
3018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3020 GNUNET_i2s (&dst_id));
3021 /* Check if buffer size is enough for the message */
3022 if (queue->size > size)
3024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3025 "* not enough room, reissue\n");
3026 peer->core_transmit =
3027 GNUNET_CORE_notify_transmit_ready (core_handle,
3030 GNUNET_TIME_UNIT_FOREVER_REL,
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3040 GNUNET_assert (0 < t->pending_messages);
3041 t->pending_messages--;
3045 switch (queue->type)
3048 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3049 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3050 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3051 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3052 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3053 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3054 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3055 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3058 GNUNET_MESH_DEBUG_M2S (queue->type));
3060 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3061 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3062 data_size = send_core_data_raw (queue->cls, size, buf);
3063 msg = (struct GNUNET_MessageHeader *) buf;
3064 type = ntohs (msg->type);
3066 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3068 data_size = send_core_path_create (queue->cls, size, buf);
3070 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3072 data_size = send_core_path_ack (queue->cls, size, buf);
3076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3077 "* type unknown: %u\n",
3082 if (0 < drop_percent &&
3083 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3085 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3086 "Dropping message of type %s\n",
3087 GNUNET_MESH_DEBUG_M2S(queue->type));
3090 /* Free queue, but cls was freed by send_core_* */
3091 queue_destroy (queue, GNUNET_NO);
3093 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3094 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3098 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3099 t->next_fc.last_pid_sent = pid;
3100 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3102 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3103 t->prev_fc.last_pid_sent = pid;
3104 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3110 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3116 /* If more data in queue, send next */
3117 queue = queue_get_next (peer);
3120 struct GNUNET_PeerIdentity id;
3122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3123 GNUNET_PEER_resolve (peer->id, &id);
3124 peer->core_transmit =
3125 GNUNET_CORE_notify_transmit_ready(core_handle,
3128 GNUNET_TIME_UNIT_FOREVER_REL,
3134 else if (NULL != peer->queue_head)
3136 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3138 GNUNET_i2s (&my_full_id));
3139 if (peer->id == t->next_hop)
3141 else if (peer->id == t->prev_hop)
3148 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3151 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3161 * @brief Queue and pass message to core when possible.
3163 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3164 * accounts for it. In case the queue is full, the message is dropped and
3167 * Otherwise, message is treated as internal and allowed to go regardless of
3170 * @param cls Closure (@c type dependant). It will be used by queue_send to
3171 * build the message to be sent if not already prebuilt.
3172 * @param type Type of the message, 0 for a raw message.
3173 * @param size Size of the message.
3174 * @param dst Neighbor to send message to.
3175 * @param t Tunnel this message belongs to.
3178 queue_add (void *cls, uint16_t type, size_t size,
3179 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3181 struct MeshPeerQueue *queue;
3182 struct GNUNET_PeerIdentity id;
3186 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3188 n = &t->next_fc.queue_n;
3190 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3192 n = &t->prev_fc.queue_n;
3196 if (*n >= t->queue_max)
3199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3200 "queue full: %u/%u\n",
3202 GNUNET_STATISTICS_update(stats,
3203 "# messages dropped (buffer full)",
3205 return; /* Drop message */
3209 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3215 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3216 if (NULL == dst->core_transmit)
3218 GNUNET_PEER_resolve (dst->id, &id);
3219 dst->core_transmit =
3220 GNUNET_CORE_notify_transmit_ready (core_handle,
3223 GNUNET_TIME_UNIT_FOREVER_REL,
3229 t->pending_messages++;
3233 /******************************************************************************/
3234 /******************** MESH NETWORK HANDLERS **************************/
3235 /******************************************************************************/
3239 * Core handler for path creation
3241 * @param cls closure
3242 * @param message message
3243 * @param peer peer identity this notification is about
3245 * @return GNUNET_OK to keep the connection open,
3246 * GNUNET_SYSERR to close it (signal serious error)
3249 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3250 const struct GNUNET_MessageHeader *message)
3252 unsigned int own_pos;
3255 MESH_TunnelNumber tid;
3256 struct GNUNET_MESH_CreateTunnel *msg;
3257 struct GNUNET_PeerIdentity *pi;
3258 struct MeshPeerPath *path;
3259 struct MeshPeerInfo *dest_peer_info;
3260 struct MeshPeerInfo *orig_peer_info;
3261 struct MeshTunnel *t;
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3264 "Received a path create msg [%s]\n",
3265 GNUNET_i2s (&my_full_id));
3266 size = ntohs (message->size);
3267 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3269 GNUNET_break_op (0);
3273 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3274 if (size % sizeof (struct GNUNET_PeerIdentity))
3276 GNUNET_break_op (0);
3279 size /= sizeof (struct GNUNET_PeerIdentity);
3282 GNUNET_break_op (0);
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3286 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3288 tid = ntohl (msg->tid);
3289 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3291 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3292 t = tunnel_get (pi, tid);
3293 if (NULL == t) /* might be a local tunnel */
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3298 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3304 t->port = ntohl (msg->port);
3305 opt = ntohl (msg->opt);
3306 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3308 t->nobuffer = GNUNET_YES;
3311 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3313 t->reliable = GNUNET_YES;
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3317 tunnel_reset_timeout (t);
3319 t->state = MESH_TUNNEL_WAITING;
3321 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3322 if (NULL == dest_peer_info)
3324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3325 " Creating PeerInfo for destination.\n");
3326 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3327 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3328 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3330 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3332 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3333 if (NULL == orig_peer_info)
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3336 " Creating PeerInfo for origin.\n");
3337 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3338 orig_peer_info->id = GNUNET_PEER_intern (pi);
3339 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3340 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3343 path = path_new (size);
3345 for (i = 0; i < size; i++)
3347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3348 GNUNET_i2s (&pi[i]));
3349 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3350 if (path->peers[i] == myid)
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3354 if (own_pos == 0 && path->peers[own_pos] != myid)
3356 /* create path: self not found in path through self */
3357 GNUNET_break_op (0);
3358 path_destroy (path);
3362 path_add_to_peers (path, GNUNET_NO);
3363 tunnel_use_path (t, path);
3365 peer_info_add_tunnel (dest_peer_info, t);
3367 if (own_pos == size - 1)
3369 struct MeshClient *c;
3371 /* Find target client */
3372 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3375 /* TODO send reject */
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3380 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3382 /* Assign local tid */
3383 while (NULL != tunnel_get_incoming (next_local_tid))
3384 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3385 t->local_tid_dest = next_local_tid++;
3386 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3388 if (GNUNET_YES == t->reliable)
3389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3391 tunnel_add_client (t, c);
3392 send_client_tunnel_create (t);
3397 struct MeshPeerPath *path2;
3399 t->next_hop = path->peers[own_pos + 1];
3400 GNUNET_PEER_change_rc(t->next_hop, 1);
3402 /* It's for somebody else! Retransmit. */
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3404 path2 = path_duplicate (path);
3405 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3406 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3407 send_create_path (t);
3415 * Core handler for path ACKs
3417 * @param cls closure
3418 * @param message message
3419 * @param peer peer identity this notification is about
3421 * @return GNUNET_OK to keep the connection open,
3422 * GNUNET_SYSERR to close it (signal serious error)
3425 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3426 const struct GNUNET_MessageHeader *message)
3428 struct GNUNET_MESH_PathACK *msg;
3429 struct MeshPeerInfo *peer_info;
3430 struct MeshPeerPath *p;
3431 struct MeshTunnel *t;
3433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3434 GNUNET_i2s (&my_full_id));
3435 msg = (struct GNUNET_MESH_PathACK *) message;
3436 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3439 /* TODO notify that we don't know the tunnel */
3440 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3442 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3446 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3448 peer_info = peer_get (&msg->peer_id);
3449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3450 GNUNET_i2s (&msg->peer_id));
3451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3454 /* Add path to peers? */
3458 path_add_to_peers (p, GNUNET_YES);
3464 t->state = MESH_TUNNEL_READY;
3465 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3466 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3468 /* Message for us? */
3469 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3472 if (NULL == t->owner)
3474 GNUNET_break_op (0);
3477 if (NULL != peer_info->dhtget)
3479 GNUNET_DHT_get_stop (peer_info->dhtget);
3480 peer_info->dhtget = NULL;
3482 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3483 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3488 " not for us, retransmitting...\n");
3489 peer_info = peer_get (&msg->oid);
3490 send_prebuilt_message (message, t->prev_hop, t);
3496 * Core handler for notifications of broken paths
3498 * @param cls closure
3499 * @param message message
3500 * @param peer peer identity this notification is about
3502 * @return GNUNET_OK to keep the connection open,
3503 * GNUNET_SYSERR to close it (signal serious error)
3506 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3507 const struct GNUNET_MessageHeader *message)
3509 struct GNUNET_MESH_PathBroken *msg;
3510 struct MeshTunnel *t;
3512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3513 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3514 msg = (struct GNUNET_MESH_PathBroken *) message;
3515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3516 GNUNET_i2s (&msg->peer1));
3517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3518 GNUNET_i2s (&msg->peer2));
3519 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3522 GNUNET_break_op (0);
3525 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3526 GNUNET_PEER_search (&msg->peer2));
3533 * Core handler for tunnel destruction
3535 * @param cls closure
3536 * @param message message
3537 * @param peer peer identity this notification is about
3539 * @return GNUNET_OK to keep the connection open,
3540 * GNUNET_SYSERR to close it (signal serious error)
3543 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3544 const struct GNUNET_MessageHeader *message)
3546 struct GNUNET_MESH_TunnelDestroy *msg;
3547 struct MeshTunnel *t;
3549 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3551 "Got a TUNNEL DESTROY packet from %s\n",
3553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3554 " for tunnel %s [%u]\n",
3555 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3556 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3559 /* Probably already got the message from another path,
3560 * destroyed the tunnel and retransmitted to children.
3563 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3567 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3570 t->local_tid, t->local_tid_dest);
3572 if (GNUNET_PEER_search (peer) == t->prev_hop)
3574 // TODO check owner's signature
3575 // TODO add owner's signatue to tunnel for retransmission
3576 peer_cancel_queues (t->prev_hop, t);
3577 GNUNET_PEER_change_rc (t->prev_hop, -1);
3580 else if (GNUNET_PEER_search (peer) == t->next_hop)
3582 // TODO check dest's signature
3583 // TODO add dest's signatue to tunnel for retransmission
3584 peer_cancel_queues (t->next_hop, t);
3585 GNUNET_PEER_change_rc (t->next_hop, -1);
3590 GNUNET_break_op (0);
3591 // TODO check both owner AND destination's signature to see which matches
3592 // TODO restransmit in appropriate direction
3595 tunnel_destroy_empty (t);
3597 // TODO: add timeout to destroy the tunnel anyway
3603 * Core handler for mesh network traffic going from the origin to a peer
3605 * @param cls closure
3606 * @param peer peer identity this notification is about
3607 * @param message message
3608 * @return GNUNET_OK to keep the connection open,
3609 * GNUNET_SYSERR to close it (signal serious error)
3612 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3613 const struct GNUNET_MessageHeader *message)
3615 struct GNUNET_MESH_Data *msg;
3616 struct MeshTunnel *t;
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3624 size = ntohs (message->size);
3626 sizeof (struct GNUNET_MESH_Data) +
3627 sizeof (struct GNUNET_MessageHeader))
3632 msg = (struct GNUNET_MESH_Data *) message;
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3634 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3636 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3639 /* TODO notify back: we don't know this tunnel */
3640 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3641 GNUNET_break_op (0);
3644 pid = ntohl (msg->pid);
3645 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3647 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3648 GNUNET_break_op (0);
3649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3650 "Received PID %u, ACK %u\n",
3651 pid, t->prev_fc.last_ack_sent);
3652 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3656 tunnel_reset_timeout (t);
3657 if (t->dest == myid)
3659 /* TODO signature verification */
3660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3661 " it's for us! sending to clients...\n");
3662 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3663 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
3664 if ( (GNUNET_NO == t->reliable &&
3665 GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv))
3667 (GNUNET_YES == t->reliable &&
3668 pid == t->prev_fc.last_pid_recv + 1) )
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3671 " pid %u not seen yet, forwarding\n", pid);
3672 t->prev_fc.last_pid_recv = pid;
3673 tunnel_send_client_ucast (t, msg);
3677 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
3678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3679 " Pid %u not expected (%u), sending FWD ACK!\n",
3680 pid, t->prev_fc.last_pid_recv + 1);
3682 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
3685 t->prev_fc.last_pid_recv = pid;
3686 if (0 == t->next_hop)
3691 ttl = ntohl (msg->ttl);
3692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3695 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3696 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3697 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3701 " not for us, retransmitting...\n");
3703 send_prebuilt_message (message, t->next_hop, t);
3704 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3710 * Core handler for mesh network traffic toward the owner of a tunnel
3712 * @param cls closure
3713 * @param message message
3714 * @param peer peer identity this notification is about
3716 * @return GNUNET_OK to keep the connection open,
3717 * GNUNET_SYSERR to close it (signal serious error)
3720 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3721 const struct GNUNET_MessageHeader *message)
3723 struct GNUNET_MESH_Data *msg;
3724 struct MeshTunnel *t;
3729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3731 size = ntohs (message->size);
3732 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
3733 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3735 GNUNET_break_op (0);
3738 msg = (struct GNUNET_MESH_Data *) message;
3739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3740 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3741 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3742 pid = ntohl (msg->pid);
3745 /* TODO notify that we dont know this tunnel (whom)? */
3746 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3748 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3749 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3753 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3755 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3756 GNUNET_break_op (0);
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3758 "Received PID %u, ACK %u\n",
3759 pid, t->next_fc.last_ack_sent);
3760 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3764 if (myid == t->id.oid)
3766 /* TODO signature verification */
3767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3768 " it's for us! sending to clients...\n");
3769 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3770 if ( (GNUNET_NO == t->reliable &&
3771 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
3773 (GNUNET_YES == t->reliable &&
3774 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
3776 t->next_fc.last_pid_recv = pid;
3777 tunnel_send_client_to_orig (t, msg);
3781 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3782 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3783 " Pid %u not expected, sending FWD ACK!\n", pid);
3785 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
3788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3789 " not for us, retransmitting...\n");
3790 t->next_fc.last_pid_recv = pid;
3791 if (0 == t->prev_hop) /* No owner AND no prev hop */
3793 if (GNUNET_YES == t->destroy)
3795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3796 "to orig received on a dying tunnel %s [%X]\n",
3797 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3801 "unknown to origin at %s\n",
3802 GNUNET_i2s (&my_full_id));
3803 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3806 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3807 "on tunnel %s [%X]\n",
3808 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3811 ttl = ntohl (msg->ttl);
3812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3815 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3816 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3817 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3820 send_prebuilt_message (message, t->prev_hop, t);
3821 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3828 * Core handler for mesh network traffic end-to-end ACKs.
3830 * @param cls Closure.
3831 * @param message Message.
3832 * @param peer Peer identity this notification is about.
3834 * @return GNUNET_OK to keep the connection open,
3835 * GNUNET_SYSERR to close it (signal serious error)
3838 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3839 const struct GNUNET_MessageHeader *message)
3841 struct GNUNET_MESH_DataACK *msg;
3842 struct MeshSentMessage *copy;
3843 struct MeshSentMessage *next;
3844 struct MeshSentMessage *head;
3845 struct MeshTunnel *t;
3850 type = ntohs (message->type);
3851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
3852 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
3853 msg = (struct GNUNET_MESH_DataACK *) message;
3855 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3858 /* TODO notify that we dont know this tunnel (whom)? */
3859 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3862 ack = ntohl (msg->pid);
3863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3865 /* Is this a forward or backward ACK? */
3866 id = GNUNET_PEER_search (peer);
3867 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
3869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3870 if (NULL == t->owner)
3872 send_prebuilt_message (message, t->prev_hop, t);
3876 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3878 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
3880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3881 if (NULL == t->client)
3883 send_prebuilt_message (message, t->next_hop, t);
3887 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3891 GNUNET_break_op (0);
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK \n");
3896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ack %u\n", ack);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %p\n", head);
3898 for (copy = head; copy != NULL; copy = next)
3902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head is %u, out!\n", copy->id);
3906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->id);
3909 /* This CANNOT use the variable 'head', as the macro must modify 't'*/
3910 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
3911 GNUNET_CONTAINER_DLL_remove (t->fwd_head, t->fwd_tail, copy);
3913 GNUNET_CONTAINER_DLL_remove (t->bck_head, t->bck_tail, copy);
3915 if (GNUNET_SCHEDULER_NO_TASK != copy->retry_task)
3916 GNUNET_SCHEDULER_cancel (copy->retry_task);
3926 * Core handler for mesh network traffic point-to-point acks.
3928 * @param cls closure
3929 * @param message message
3930 * @param peer peer identity this notification is about
3932 * @return GNUNET_OK to keep the connection open,
3933 * GNUNET_SYSERR to close it (signal serious error)
3936 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3937 const struct GNUNET_MessageHeader *message)
3939 struct GNUNET_MESH_ACK *msg;
3940 struct MeshTunnel *t;
3941 struct MeshFlowControl *fc;
3945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3947 msg = (struct GNUNET_MESH_ACK *) message;
3949 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3953 /* TODO notify that we dont know this tunnel (whom)? */
3954 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3957 ack = ntohl (msg->pid);
3958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3960 /* Is this a forward or backward ACK? */
3961 id = GNUNET_PEER_search (peer);
3962 if (t->next_hop == id)
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3968 else if (t->prev_hop == id)
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3976 GNUNET_break_op (0);
3980 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
3981 GMC_is_pid_bigger (ack, fc->last_ack_recv))
3983 GNUNET_SCHEDULER_cancel (fc->poll_task);
3984 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3985 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3987 fc->last_ack_recv = ack;
3988 peer_unlock_queue (id);
3990 if (t->next_hop == id)
3991 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3993 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4000 * Core handler for mesh network traffic point-to-point ack polls.
4002 * @param cls closure
4003 * @param message message
4004 * @param peer peer identity this notification is about
4006 * @return GNUNET_OK to keep the connection open,
4007 * GNUNET_SYSERR to close it (signal serious error)
4010 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4011 const struct GNUNET_MessageHeader *message)
4013 struct GNUNET_MESH_Poll *msg;
4014 struct MeshTunnel *t;
4017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4020 msg = (struct GNUNET_MESH_Poll *) message;
4022 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4026 /* TODO notify that we dont know this tunnel (whom)? */
4027 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4028 GNUNET_break_op (0);
4032 /* Is this a forward or backward ACK? */
4033 id = GNUNET_PEER_search(peer);
4034 if (t->next_hop == id)
4036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4037 t->next_fc.last_pid_recv = ntohl (msg->pid);
4038 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4040 else if (t->prev_hop == id)
4042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4043 t->prev_fc.last_pid_recv = ntohl (msg->pid);
4044 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4054 * Core handler for mesh keepalives.
4056 * @param cls closure
4057 * @param message message
4058 * @param peer peer identity this notification is about
4059 * @return GNUNET_OK to keep the connection open,
4060 * GNUNET_SYSERR to close it (signal serious error)
4062 * TODO: Check who we got this from, to validate route.
4065 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4066 const struct GNUNET_MessageHeader *message)
4068 struct GNUNET_MESH_TunnelKeepAlive *msg;
4069 struct MeshTunnel *t;
4071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4074 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4075 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4079 /* TODO notify that we dont know that tunnel */
4080 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4085 tunnel_reset_timeout (t);
4086 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4089 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4090 send_prebuilt_message (message, t->next_hop, t);
4097 * Functions to handle messages from core
4099 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4100 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4101 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4102 sizeof (struct GNUNET_MESH_PathBroken)},
4103 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4104 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4105 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4106 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4107 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4108 sizeof (struct GNUNET_MESH_DataACK)},
4109 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4110 sizeof (struct GNUNET_MESH_DataACK)},
4111 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4112 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4113 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4114 sizeof (struct GNUNET_MESH_ACK)},
4115 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4116 sizeof (struct GNUNET_MESH_Poll)},
4117 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4118 sizeof (struct GNUNET_MESH_PathACK)},
4124 /******************************************************************************/
4125 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4126 /******************************************************************************/
4131 * notify_client_connection_failure: notify a client that the connection to the
4132 * requested remote peer is not possible (for instance, no route found)
4133 * Function called when the socket is ready to queue more data. "buf" will be
4134 * NULL and "size" zero if the socket was closed for writing in the meantime.
4136 * @param cls closure
4137 * @param size number of bytes available in buf
4138 * @param buf where the callee should write the message
4139 * @return number of bytes written to buf
4142 notify_client_connection_failure (void *cls, size_t size, void *buf)
4145 struct MeshPeerInfo *peer_info;
4146 struct GNUNET_MESH_PeerControl *msg;
4147 struct GNUNET_PeerIdentity id;
4149 if (0 == size && NULL == buf)
4151 // TODO retry? cancel?
4155 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4156 peer_info = (struct MeshPeerInfo *) cls;
4157 msg = (struct GNUNET_MESH_PeerControl *) buf;
4158 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4159 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4160 // msg->tunnel_id = htonl(peer_info->t->tid);
4161 GNUNET_PEER_resolve (peer_info->id, &id);
4162 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4170 * Send keepalive packets for a tunnel.
4172 * @param cls Closure (tunnel for which to send the keepalive).
4173 * @param tc Notification context.
4175 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4178 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4180 struct MeshTunnel *t = cls;
4181 struct GNUNET_MESH_TunnelKeepAlive *msg;
4182 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4185 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4186 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4187 NULL == t->owner || 0 == t->local_tid)
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4193 "sending keepalive for tunnel %d\n", t->id.tid);
4195 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4196 msg->header.size = htons (size);
4197 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4198 msg->oid = my_full_id;
4199 msg->tid = htonl (t->id.tid);
4200 send_prebuilt_message (&msg->header, t->next_hop, t);
4202 t->maintenance_task =
4203 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4208 * Function to process paths received for a new peer addition. The recorded
4209 * paths form the initial tunnel, which can be optimized later.
4210 * Called on each result obtained for the DHT search.
4212 * @param cls closure
4213 * @param exp when will this value expire
4214 * @param key key of the result
4215 * @param get_path path of the get request
4216 * @param get_path_length lenght of get_path
4217 * @param put_path path of the put request
4218 * @param put_path_length length of the put_path
4219 * @param type type of the result
4220 * @param size number of bytes in data
4221 * @param data pointer to the result data
4224 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4225 const struct GNUNET_HashCode * key,
4226 const struct GNUNET_PeerIdentity *get_path,
4227 unsigned int get_path_length,
4228 const struct GNUNET_PeerIdentity *put_path,
4229 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4230 size_t size, const void *data)
4232 struct MeshPeerInfo *peer = cls;
4233 struct MeshPeerPath *p;
4234 struct GNUNET_PeerIdentity pi;
4237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4238 GNUNET_PEER_resolve (peer->id, &pi);
4239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4241 p = path_build_from_dht (get_path, get_path_length,
4242 put_path, put_path_length);
4243 path_add_to_peers (p, GNUNET_NO);
4245 for (i = 0; i < peer->ntunnels; i++)
4247 struct GNUNET_PeerIdentity id;
4249 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4251 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4252 peer->tunnels[i]->local_tid,
4253 peer->tunnels[i]->local_tid_dest);
4254 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4257 peer_connect (peer, peer->tunnels[i]);
4265 /******************************************************************************/
4266 /********************* MESH LOCAL HANDLES **************************/
4267 /******************************************************************************/
4271 * Handler for client disconnection
4273 * @param cls closure
4274 * @param client identification of the client; NULL
4275 * for the last call when the server is destroyed
4278 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4280 struct MeshClient *c;
4281 struct MeshClient *next;
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4293 if (c->handle != client)
4295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4296 " ... searching %p (%u)\n",
4301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4303 GNUNET_SERVER_client_drop (c->handle);
4304 c->shutting_down = GNUNET_YES;
4305 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4306 &tunnel_destroy_iterator, c);
4307 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4308 &tunnel_destroy_iterator, c);
4309 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4310 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4312 if (NULL != c->ports)
4313 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4315 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4318 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4327 * Handler for new clients
4329 * @param cls closure
4330 * @param client identification of the client
4331 * @param message the actual message, which includes messages the client wants
4334 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4335 const struct GNUNET_MessageHeader *message)
4337 struct GNUNET_MESH_ClientConnect *cc_msg;
4338 struct MeshClient *c;
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4345 /* Check data sanity */
4346 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4347 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4348 if (0 != (size % sizeof (uint32_t)))
4351 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4354 size /= sizeof (uint32_t);
4356 /* Create new client structure */
4357 c = GNUNET_malloc (sizeof (struct MeshClient));
4358 c->id = next_client_id++; /* overflow not important: just for debug */
4359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4362 GNUNET_SERVER_client_keep (client);
4367 p = (uint32_t *) &cc_msg[1];
4368 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4369 for (i = 0; i < size; i++)
4372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4374 /* store in client's hashmap */
4375 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4376 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4377 /* store in global hashmap */
4378 /* FIXME only allow one client to have the port open,
4379 * have a backup hashmap with waiting clients */
4380 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4385 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4386 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4387 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4388 GNUNET_SERVER_notification_context_add (nc, client);
4389 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4391 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4397 * Handler for requests of new tunnels
4399 * @param cls Closure.
4400 * @param client Identification of the client.
4401 * @param message The actual message.
4404 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4405 const struct GNUNET_MessageHeader *message)
4407 struct GNUNET_MESH_TunnelMessage *t_msg;
4408 struct MeshPeerInfo *peer_info;
4409 struct MeshTunnel *t;
4410 struct MeshClient *c;
4411 MESH_TunnelNumber tid;
4413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4415 /* Sanity check for client registration */
4416 if (NULL == (c = client_get (client)))
4419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4424 /* Message size sanity check */
4425 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4432 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4434 GNUNET_i2s (&t_msg->peer));
4435 /* Sanity check for tunnel numbering */
4436 tid = ntohl (t_msg->tunnel_id);
4437 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4440 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4443 /* Sanity check for duplicate tunnel IDs */
4444 if (NULL != tunnel_get_by_local_id (c, tid))
4447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4452 while (NULL != tunnel_get_by_pi (myid, next_tid))
4453 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4454 t = tunnel_new (myid, next_tid, c, tid);
4455 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4462 t->port = ntohl (t_msg->port);
4463 tunnel_set_options (t, ntohl (t_msg->options));
4464 if (GNUNET_YES == t->reliable)
4465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4468 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4470 peer_info = peer_get (&t_msg->peer);
4471 peer_info_add_tunnel (peer_info, t);
4472 peer_connect (peer_info, t);
4473 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4479 * Handler for requests of deleting tunnels
4481 * @param cls closure
4482 * @param client identification of the client
4483 * @param message the actual message
4486 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4487 const struct GNUNET_MessageHeader *message)
4489 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4490 struct MeshClient *c;
4491 struct MeshTunnel *t;
4492 MESH_TunnelNumber tid;
4494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4495 "Got a DESTROY TUNNEL from client!\n");
4497 /* Sanity check for client registration */
4498 if (NULL == (c = client_get (client)))
4501 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4506 /* Message sanity check */
4507 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4514 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4516 /* Retrieve tunnel */
4517 tid = ntohl (tunnel_msg->tunnel_id);
4518 t = tunnel_get_by_local_id(c, tid);
4521 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4523 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4527 /* Cleanup after the tunnel */
4528 client_delete_tunnel (c, t);
4535 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4539 /* The tunnel will be destroyed when the last message is transmitted. */
4540 tunnel_destroy_empty (t);
4542 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4548 * Handler for client traffic
4550 * @param cls closure
4551 * @param client identification of the client
4552 * @param message the actual message
4555 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4556 const struct GNUNET_MessageHeader *message)
4558 struct GNUNET_MESH_LocalData *data_msg;
4559 struct MeshClient *c;
4560 struct MeshTunnel *t;
4561 struct MeshFlowControl *fc;
4562 MESH_TunnelNumber tid;
4565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4566 "Got data from a client!\n");
4568 /* Sanity check for client registration */
4569 if (NULL == (c = client_get (client)))
4572 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4577 data_msg = (struct GNUNET_MESH_LocalData *) message;
4579 /* Sanity check for message size */
4580 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4581 if (size < sizeof (struct GNUNET_MessageHeader))
4584 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4588 /* Tunnel exists? */
4589 tid = ntohl (data_msg->tid);
4590 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4593 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4596 t = tunnel_get_by_local_id (c, tid);
4600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4604 /* Is the client in the tunnel? */
4605 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4607 t->owner->handle == client)
4609 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4611 t->client->handle == client) ) )
4614 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4618 /* Ok, everything is correct, send the message
4619 * (pretend we got it from a mesh peer)
4622 struct GNUNET_MESH_Data *payload;
4623 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
4625 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
4626 if (GNUNET_YES == t->reliable)
4628 struct MeshSentMessage *copy;
4630 copy = GNUNET_malloc (sizeof (struct MeshSentMessage)
4631 + sizeof(struct GNUNET_MESH_Data)
4634 copy->id = fc->last_pid_recv + 1;
4635 copy->is_forward = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
4636 copy->retry_timer = MESH_RETRANSMIT_TIME;
4638 GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
4639 &tunnel_retransmit_message,
4641 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4643 GNUNET_CONTAINER_DLL_insert_tail (t->fwd_head, t->fwd_tail, copy);
4647 GNUNET_CONTAINER_DLL_insert_tail (t->bck_head, t->bck_tail, copy);
4649 payload = (struct GNUNET_MESH_Data *) ©[1];
4653 payload = (struct GNUNET_MESH_Data *) cbuf;
4655 memcpy (&payload[1], &data_msg[1], size);
4656 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
4657 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
4658 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
4659 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4660 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
4661 payload->tid = htonl (t->id.tid);
4662 payload->ttl = htonl (default_ttl);
4663 payload->pid = htonl (fc->last_pid_recv + 1);
4664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4665 " calling generic handler...\n");
4666 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4667 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
4669 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
4671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4672 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4679 * Handler for client's ACKs for payload traffic.
4681 * @param cls Closure (unused).
4682 * @param client Identification of the client.
4683 * @param message The actual message.
4686 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4687 const struct GNUNET_MessageHeader *message)
4689 struct GNUNET_MESH_LocalAck *msg;
4690 struct MeshTunnel *t;
4691 struct MeshClient *c;
4692 MESH_TunnelNumber tid;
4694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4695 /* Sanity check for client registration */
4696 if (NULL == (c = client_get (client)))
4699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4704 msg = (struct GNUNET_MESH_LocalAck *) message;
4706 /* Tunnel exists? */
4707 tid = ntohl (msg->tunnel_id);
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4709 t = tunnel_get_by_local_id (c, tid);
4713 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4714 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4719 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4722 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4723 t->prev_fc.last_ack_recv++;
4724 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4728 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4729 t->next_fc.last_ack_recv++;
4730 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4733 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4741 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4743 * @param cls Closure (client handle).
4744 * @param key Key (hashed tunnel ID, unused).
4745 * @param value Tunnel info.
4747 * @return GNUNET_YES, to keep iterating.
4750 monitor_all_tunnels_iterator (void *cls,
4751 const struct GNUNET_HashCode * key,
4754 struct GNUNET_SERVER_Client *client = cls;
4755 struct MeshTunnel *t = value;
4756 struct GNUNET_MESH_LocalMonitor *msg;
4758 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4759 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4760 msg->tunnel_id = htonl (t->id.tid);
4761 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4762 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4763 GNUNET_PEER_resolve (t->dest, &msg->destination);
4765 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4766 "* sending info about tunnel %s [%u]\n",
4767 GNUNET_i2s (&msg->owner), t->id.tid);
4769 GNUNET_SERVER_notification_context_unicast (nc, client,
4770 &msg->header, GNUNET_NO);
4776 * Handler for client's MONITOR request.
4778 * @param cls Closure (unused).
4779 * @param client Identification of the client.
4780 * @param message The actual message.
4783 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4784 const struct GNUNET_MessageHeader *message)
4786 struct MeshClient *c;
4788 /* Sanity check for client registration */
4789 if (NULL == (c = client_get (client)))
4792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4796 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4797 "Received get tunnels request from client %u\n",
4799 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4800 monitor_all_tunnels_iterator,
4802 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4803 "Get tunnels request from client %u completed\n",
4805 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4810 * Handler for client's MONITOR_TUNNEL request.
4812 * @param cls Closure (unused).
4813 * @param client Identification of the client.
4814 * @param message The actual message.
4817 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4818 const struct GNUNET_MessageHeader *message)
4820 const struct GNUNET_MESH_LocalMonitor *msg;
4821 struct GNUNET_MESH_LocalMonitor *resp;
4822 struct MeshClient *c;
4823 struct MeshTunnel *t;
4825 /* Sanity check for client registration */
4826 if (NULL == (c = client_get (client)))
4829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4833 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4834 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4835 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4838 ntohl (msg->tunnel_id));
4839 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4842 /* We don't know the tunnel FIXME */
4843 struct GNUNET_MESH_LocalMonitor warn;
4846 GNUNET_SERVER_notification_context_unicast (nc, client,
4849 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4853 /* Initialize context */
4854 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4856 GNUNET_PEER_resolve (t->dest, &resp->destination);
4857 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4858 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4859 &resp->header, GNUNET_NO);
4862 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4863 "Monitor tunnel request from client %u completed\n",
4865 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4870 * Functions to handle messages from clients
4872 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4873 {&handle_local_new_client, NULL,
4874 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4875 {&handle_local_tunnel_create, NULL,
4876 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4877 sizeof (struct GNUNET_MESH_TunnelMessage)},
4878 {&handle_local_tunnel_destroy, NULL,
4879 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4880 sizeof (struct GNUNET_MESH_TunnelMessage)},
4881 {&handle_local_data, NULL,
4882 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
4883 {&handle_local_ack, NULL,
4884 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4885 sizeof (struct GNUNET_MESH_LocalAck)},
4886 {&handle_local_get_tunnels, NULL,
4887 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4888 sizeof (struct GNUNET_MessageHeader)},
4889 {&handle_local_show_tunnel, NULL,
4890 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4891 sizeof (struct GNUNET_MESH_LocalMonitor)},
4897 * Method called whenever a given peer connects.
4899 * @param cls closure
4900 * @param peer peer identity this notification is about
4903 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4905 struct MeshPeerInfo *peer_info;
4906 struct MeshPeerPath *path;
4908 DEBUG_CONN ("Peer connected\n");
4909 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4910 peer_info = peer_get (peer);
4911 if (myid == peer_info->id)
4913 DEBUG_CONN (" (self)\n");
4914 path = path_new (1);
4918 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4919 path = path_new (2);
4920 path->peers[1] = peer_info->id;
4921 GNUNET_PEER_change_rc (peer_info->id, 1);
4922 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4924 path->peers[0] = myid;
4925 GNUNET_PEER_change_rc (myid, 1);
4926 peer_info_add_path (peer_info, path, GNUNET_YES);
4932 * Method called whenever a peer disconnects.
4934 * @param cls closure
4935 * @param peer peer identity this notification is about
4938 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4940 struct MeshPeerInfo *pi;
4941 struct MeshPeerQueue *q;
4942 struct MeshPeerQueue *n;
4944 DEBUG_CONN ("Peer disconnected\n");
4945 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4955 /* TODO try to reroute this traffic instead */
4956 queue_destroy(q, GNUNET_YES);
4959 if (NULL != pi->core_transmit)
4961 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4962 pi->core_transmit = NULL;
4964 peer_remove_path (pi, pi->id, myid);
4967 DEBUG_CONN (" (self)\n");
4969 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4975 * Install server (service) handlers and start listening to clients.
4980 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4981 GNUNET_SERVER_disconnect_notify (server_handle,
4982 &handle_local_client_disconnect, NULL);
4983 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4985 clients_head = NULL;
4986 clients_tail = NULL;
4988 GNUNET_SERVER_resume (server_handle);
4993 * To be called on core init/fail.
4995 * @param cls Closure (config)
4996 * @param server handle to the server for this service
4997 * @param identity the public identity of this peer
5000 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5001 const struct GNUNET_PeerIdentity *identity)
5003 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5007 GNUNET_break (core_handle == server);
5008 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5011 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5012 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5014 GNUNET_i2s (identity));
5015 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5017 GNUNET_i2s (&my_full_id));
5018 GNUNET_CORE_disconnect (core_handle);
5019 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5020 NULL, /* Closure passed to MESH functions */
5021 &core_init, /* Call core_init once connected */
5022 &core_connect, /* Handle connects */
5023 &core_disconnect, /* remove peers on disconnects */
5024 NULL, /* Don't notify about all incoming messages */
5025 GNUNET_NO, /* For header only in notification */
5026 NULL, /* Don't notify about all outbound messages */
5027 GNUNET_NO, /* For header-only out notification */
5028 core_handlers); /* Register these handlers */
5037 /******************************************************************************/
5038 /************************ MAIN FUNCTIONS ****************************/
5039 /******************************************************************************/
5042 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5044 * @param cls closure
5045 * @param key current key code
5046 * @param value value in the hash map
5047 * @return GNUNET_YES if we should continue to iterate,
5051 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5053 struct MeshTunnel *t = value;
5060 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5062 * @param cls closure
5063 * @param key current key code
5064 * @param value value in the hash map
5065 * @return GNUNET_YES if we should continue to iterate,
5069 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5071 struct MeshPeerInfo *p = value;
5072 struct MeshPeerQueue *q;
5073 struct MeshPeerQueue *n;
5081 queue_destroy(q, GNUNET_YES);
5085 peer_info_destroy (p);
5091 * Task run during shutdown.
5097 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5101 if (core_handle != NULL)
5103 GNUNET_CORE_disconnect (core_handle);
5106 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5107 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5108 if (dht_handle != NULL)
5110 GNUNET_DHT_disconnect (dht_handle);
5115 GNUNET_SERVER_notification_context_destroy (nc);
5118 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5120 GNUNET_SCHEDULER_cancel (announce_id_task);
5121 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5128 * Process mesh requests.
5130 * @param cls closure
5131 * @param server the initialized server
5132 * @param c configuration to use
5135 run (void *cls, struct GNUNET_SERVER_Handle *server,
5136 const struct GNUNET_CONFIGURATION_Handle *c)
5139 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5142 server_handle = server;
5143 GNUNET_SERVER_suspend (server_handle);
5146 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5151 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5152 "mesh", "peer/privatekey");
5153 GNUNET_SCHEDULER_shutdown ();
5158 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5159 &refresh_path_time))
5161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5163 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5164 "mesh", "refresh path time");
5165 GNUNET_SCHEDULER_shutdown ();
5170 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5175 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5176 "mesh", "id announce time");
5177 GNUNET_SCHEDULER_shutdown ();
5182 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5187 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5188 "mesh", "connect timeout");
5189 GNUNET_SCHEDULER_shutdown ();
5194 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5199 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5200 "mesh", "max msgs queue");
5201 GNUNET_SCHEDULER_shutdown ();
5206 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5211 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5212 "mesh", "max tunnels");
5213 GNUNET_SCHEDULER_shutdown ();
5218 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5221 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5223 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5224 "mesh", "default ttl", 64);
5229 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5232 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5233 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5234 "mesh", "max peers", 1000);
5239 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5246 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5247 "Mesh is running with drop mode enabled. "
5248 "This is NOT a good idea! "
5249 "Remove the DROP_PERCENT option from your configuration.\n");
5253 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5254 &dht_replication_level))
5256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5258 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5259 "mesh", "dht replication level", 3);
5260 dht_replication_level = 3;
5263 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5264 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5265 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5266 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5268 dht_handle = GNUNET_DHT_connect (c, 64);
5269 if (NULL == dht_handle)
5273 stats = GNUNET_STATISTICS_create ("mesh", c);
5275 /* Scheduled the task to clean up when shutdown is called */
5276 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5278 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5279 GNUNET_free (keyfile);
5280 GNUNET_assert (NULL != pk);
5281 my_private_key = pk;
5282 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5283 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5284 &my_full_id.hashPubKey);
5285 myid = GNUNET_PEER_intern (&my_full_id);
5286 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5287 "Mesh for peer [%s] starting\n",
5288 GNUNET_i2s(&my_full_id));
5290 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5291 NULL, /* Closure passed to MESH functions */
5292 &core_init, /* Call core_init once connected */
5293 &core_connect, /* Handle connects */
5294 &core_disconnect, /* remove peers on disconnects */
5295 NULL, /* Don't notify about all incoming messages */
5296 GNUNET_NO, /* For header only in notification */
5297 NULL, /* Don't notify about all outbound messages */
5298 GNUNET_NO, /* For header-only out notification */
5299 core_handlers); /* Register these handlers */
5300 if (NULL == core_handle)
5303 GNUNET_SCHEDULER_shutdown ();
5307 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5308 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5314 * The main function for the mesh service.
5316 * @param argc number of arguments from the command line
5317 * @param argv command line arguments
5318 * @return 0 ok, 1 on error
5321 main (int argc, char *const *argv)
5326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5327 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5329 ret = (GNUNET_OK == r) ? 0 : 1;
5330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5334 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5335 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5336 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);