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 * Tunnel this message is in.
295 struct MeshTunnel *t;
298 * ID of the message (ACK needed to free)
303 * Task to resend/poll in case no ACK is received.
305 GNUNET_SCHEDULER_TaskIdentifier retry_task; // FIXME move to per tunnel timer?
308 * Counter for exponential backoff.
310 struct GNUNET_TIME_Relative retry_timer;
313 * Is this a forward or backward going message?
317 /* struct GNUNET_MESH_Data with payload */
321 * Struct containing all information regarding a tunnel
322 * For an intermediate node the improtant info used will be:
323 * - id Tunnel unique identification
324 * - paths[0] To know where to send it next
325 * - metainfo: ready, speeds, accounting
332 struct MESH_TunnelID id;
335 * Port of the tunnel.
340 * State of the tunnel.
342 enum MeshTunnelState state;
345 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
347 MESH_TunnelNumber local_tid;
350 * Local tunnel number for local destination clients (incoming number)
351 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
354 MESH_TunnelNumber local_tid_dest;
357 * Is the tunnel bufferless (minimum latency)?
362 * Is the tunnel reliable?
367 * Force sending ACK? Flag to allow duplicate ACK on POLL.
372 * How many messages do we accept in the forward queue.
374 unsigned int queue_max;
377 * Last time the tunnel was used
379 struct GNUNET_TIME_Absolute timestamp;
382 * Destination of the tunnel.
387 * Next hop in the tunnel. If 0, @c client must be set.
389 GNUNET_PEER_Id next_hop;
392 * Previous hop in the tunnel. If 0, @c owner must be set.
394 GNUNET_PEER_Id prev_hop;
397 * Flow control information about @c next_hop or @c client.
399 struct MeshFlowControl next_fc;
402 * Flow control information about @c prev_hop or @c owner.
404 struct MeshFlowControl prev_fc;
407 * Client owner of the tunnel, if any
409 struct MeshClient *owner;
412 * Client destination of the tunnel, if any.
414 struct MeshClient *client;
417 * Task to keep the used paths alive at the owner,
418 * time tunnel out on all the other peers.
420 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
423 * Path being used for the tunnel.
425 struct MeshPeerPath *path;
428 * Flag to signal the destruction of the tunnel.
429 * If this is set GNUNET_YES the tunnel will be destroyed
430 * when the queue is empty.
435 * Total messages pending for this tunnels, payload or not.
437 unsigned int pending_messages;
440 * Messages sent and not yet ACK'd.
441 * Only present (non-NULL) at the owner of a tunnel.
443 struct GNUNET_CONTAINER_MultiHashMap32 *sent_messages_fwd;
446 * Messages sent and not yet ACK'd.
447 * Only present (non-NULL) at the destination of a tunnel.
449 struct GNUNET_CONTAINER_MultiHashMap32 *sent_messages_bck;
454 * Struct containing information about a client of the service
456 * TODO: add a list of 'waiting' ports
463 struct MeshClient *next;
468 struct MeshClient *prev;
471 * Tunnels that belong to this client, indexed by local id
473 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
476 * Tunnels this client has accepted, indexed by incoming local id
478 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
481 * Handle to communicate with the client
483 struct GNUNET_SERVER_Client *handle;
486 * Ports that this client has declared interest in.
487 * Indexed by port, contains *Client.
489 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
492 * Whether the client is active or shutting down (don't send confirmations
493 * to a client that is shutting down.
498 * ID of the client, mainly for debug messages
505 /******************************************************************************/
506 /************************ DEBUG FUNCTIONS ****************************/
507 /******************************************************************************/
511 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
512 * @param cls string to print
513 * @param success GNUNET_OK if the PUT was transmitted,
514 * GNUNET_NO on timeout,
515 * GNUNET_SYSERR on disconnect from service
516 * after the PUT message was transmitted
517 * (so we don't know if it was received or not)
522 mesh_debug (void *cls, int success)
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
531 unsigned int debug_fwd_ack;
532 unsigned int debug_bck_ack;
536 /******************************************************************************/
537 /*********************** GLOBAL VARIABLES ****************************/
538 /******************************************************************************/
540 /************************** Configuration parameters **************************/
543 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
545 static struct GNUNET_TIME_Relative refresh_path_time;
548 * How often to PUT own ID in the DHT.
550 static struct GNUNET_TIME_Relative id_announce_time;
553 * Maximum time allowed to connect to a peer found by string.
555 static struct GNUNET_TIME_Relative connect_timeout;
558 * Default TTL for payload packets.
560 static unsigned long long default_ttl;
563 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
565 static unsigned long long dht_replication_level;
568 * How many tunnels are we willing to maintain.
569 * Local tunnels are always allowed, even if there are more tunnels than max.
571 static unsigned long long max_tunnels;
574 * How many messages *in total* are we willing to queue, divided by number of
575 * tunnels to get tunnel queue size.
577 static unsigned long long max_msgs_queue;
580 * How many peers do we want to remember?
582 static unsigned long long max_peers;
585 * Percentage of messages that will be dropped (for test purposes only).
587 static unsigned long long drop_percent;
589 /*************************** Static global variables **************************/
592 * DLL with all the clients, head.
594 static struct MeshClient *clients_head;
597 * DLL with all the clients, tail.
599 static struct MeshClient *clients_tail;
602 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
604 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
607 * Number of tunnels known.
609 static unsigned long long n_tunnels;
612 * Tunnels incoming, indexed by MESH_TunnelNumber
613 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
615 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
618 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
620 static struct GNUNET_CONTAINER_MultiHashMap *peers;
623 * Handle to communicate with transport
625 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
628 * Handle to communicate with core.
630 static struct GNUNET_CORE_Handle *core_handle;
635 static struct GNUNET_DHT_Handle *dht_handle;
640 static struct GNUNET_SERVER_Handle *server_handle;
643 * Handle to the statistics service.
645 static struct GNUNET_STATISTICS_Handle *stats;
648 * Notification context, to send messages to local clients.
650 static struct GNUNET_SERVER_NotificationContext *nc;
653 * Local peer own ID (memory efficient handle).
655 static GNUNET_PEER_Id myid;
658 * Local peer own ID (full value).
660 static struct GNUNET_PeerIdentity my_full_id;
665 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
670 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
673 * Tunnel ID for the next created tunnel (global tunnel number).
675 static MESH_TunnelNumber next_tid;
678 * Tunnel ID for the next incoming tunnel (local tunnel number).
680 static MESH_TunnelNumber next_local_tid;
683 * All ports clients of this peer have opened.
685 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
688 * Task to periodically announce itself in the network.
690 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
693 * Next ID to assign to a client.
695 unsigned int next_client_id;
698 /******************************************************************************/
699 /*********************** DECLARATIONS **************************/
700 /******************************************************************************/
703 * Function to process paths received for a new peer addition. The recorded
704 * paths form the initial tunnel, which can be optimized later.
705 * Called on each result obtained for the DHT search.
708 * @param exp when will this value expire
709 * @param key key of the result
710 * @param type type of the result
711 * @param size number of bytes in data
712 * @param data pointer to the result data
715 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
716 const struct GNUNET_HashCode * key,
717 const struct GNUNET_PeerIdentity *get_path,
718 unsigned int get_path_length,
719 const struct GNUNET_PeerIdentity *put_path,
720 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
721 size_t size, const void *data);
725 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
726 * and insert it in the appropriate structures if the peer is not known yet.
728 * @param peer Full identity of the peer.
730 * @return Existing or newly created peer info.
732 static struct MeshPeerInfo *
733 peer_get (const struct GNUNET_PeerIdentity *peer);
737 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
738 * and insert it in the appropriate structures if the peer is not known yet.
740 * @param peer Short identity of the peer.
742 * @return Existing or newly created peer info.
744 static struct MeshPeerInfo *
745 peer_get_short (const GNUNET_PEER_Id peer);
749 * Build a PeerPath from the paths returned from the DHT, reversing the paths
750 * to obtain a local peer -> destination path and interning the peer ids.
752 * @return Newly allocated and created path
754 static struct MeshPeerPath *
755 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
756 unsigned int get_path_length,
757 const struct GNUNET_PeerIdentity *put_path,
758 unsigned int put_path_length);
762 * Adds a path to the peer_infos of all the peers in the path
764 * @param p Path to process.
765 * @param confirmed Whether we know if the path works or not.
768 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
773 * Search for a tunnel by global ID using full PeerIdentities.
775 * @param oid owner of the tunnel.
776 * @param tid global tunnel number.
778 * @return tunnel handler, NULL if doesn't exist.
780 static struct MeshTunnel *
781 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
785 * Notify a tunnel that a connection has broken that affects at least
788 * @param t Tunnel affected.
789 * @param p1 Peer that got disconnected from p2.
790 * @param p2 Peer that got disconnected from p1.
792 * @return Short ID of the peer disconnected (either p1 or p2).
793 * 0 if the tunnel remained unaffected.
795 static GNUNET_PEER_Id
796 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
801 * @brief Use the given path for the tunnel.
802 * Update the next and prev hops (and RCs).
803 * (Re)start the path refresh in case the tunnel is locally owned.
805 * @param t Tunnel to update.
806 * @param p Path to use.
809 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
812 * Tunnel is empty: destroy it.
814 * Notifies all participants (peers, cleints) about the destruction.
816 * @param t Tunnel to destroy.
819 tunnel_destroy_empty (struct MeshTunnel *t);
822 * @brief Queue and pass message to core when possible.
824 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
825 * and accounts for it. In case the queue is full, the message is dropped and
828 * Otherwise, message is treated as internal and allowed to go regardless of
831 * @param cls Closure (@c type dependant). It will be used by queue_send to
832 * build the message to be sent if not already prebuilt.
833 * @param type Type of the message, 0 for a raw message.
834 * @param size Size of the message.
835 * @param dst Neighbor to send message to.
836 * @param t Tunnel this message belongs to.
839 queue_add (void *cls, uint16_t type, size_t size,
840 struct MeshPeerInfo *dst, struct MeshTunnel *t);
844 * Free a transmission that was already queued with all resources
845 * associated to the request.
847 * @param queue Queue handler to cancel.
848 * @param clear_cls Is it necessary to free associated cls?
851 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
855 * @brief Get the next transmittable message from the queue.
857 * This will be the head, except in the case of being a data packet
858 * not allowed by the destination peer.
860 * @param peer Destination peer.
862 * @return The next viable MeshPeerQueue element to send to that peer.
863 * NULL when there are no transmittable messages.
865 struct MeshPeerQueue *
866 queue_get_next (const struct MeshPeerInfo *peer);
870 * Core callback to write a queued packet to core buffer
872 * @param cls Closure (peer info).
873 * @param size Number of bytes available in buf.
874 * @param buf Where the to write the message.
876 * @return number of bytes written to buf
879 queue_send (void *cls, size_t size, void *buf);
882 /******************************************************************************/
883 /************************ PERIODIC FUNCTIONS ****************************/
884 /******************************************************************************/
887 * Periodically announce self id in the DHT
890 * @param tc task context
893 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
897 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
899 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
903 * - Set data expiration in function of X
906 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
908 block.id = my_full_id;
909 GNUNET_DHT_put (dht_handle, /* DHT handle */
910 &my_full_id.hashPubKey, /* Key to use */
911 dht_replication_level, /* Replication level */
912 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
913 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
914 sizeof (block), /* Size of the data */
915 (const char *) &block, /* Data itself */
916 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
917 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
918 NULL, /* Continuation */
919 NULL); /* Continuation closure */
921 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
925 /******************************************************************************/
926 /****************** GENERAL HELPER FUNCTIONS ************************/
927 /******************************************************************************/
931 * Check if client has registered with the service and has not disconnected
933 * @param client the client to check
935 * @return non-NULL if client exists in the global DLL
937 static struct MeshClient *
938 client_get (struct GNUNET_SERVER_Client *client)
940 struct MeshClient *c;
945 if (c->handle == client)
954 * Deletes a tunnel from a client (either owner or destination). To be used on
957 * @param c Client whose tunnel to delete.
958 * @param t Tunnel which should be deleted.
961 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
965 GNUNET_assert (GNUNET_YES ==
966 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
972 GNUNET_assert (GNUNET_YES ==
973 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
980 * Notify the appropriate client that a new incoming tunnel was created.
982 * @param t Tunnel that was created.
985 send_client_tunnel_create (struct MeshTunnel *t)
987 struct GNUNET_MESH_TunnelMessage msg;
989 if (NULL == t->client)
991 msg.header.size = htons (sizeof (msg));
992 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
993 msg.tunnel_id = htonl (t->local_tid_dest);
994 msg.port = htonl (t->port);
995 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
996 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
997 &msg.header, GNUNET_NO);
1002 * Notify dest client that the incoming tunnel is no longer valid.
1004 * @param c Client to notify..
1005 * @param t Tunnel that is destroyed.
1008 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
1010 struct GNUNET_MESH_TunnelMessage msg;
1017 if (c != t->client && c != t->owner)
1022 msg.header.size = htons (sizeof (msg));
1023 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1024 msg.tunnel_id = htonl (t->local_tid_dest);
1025 msg.port = htonl (0);
1026 memset(&msg.peer, 0, sizeof (msg.peer));
1027 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1028 &msg.header, GNUNET_NO);
1033 * Iterator over all the peers to remove the oldest not-used entry.
1035 * @param cls Closure (unsued).
1036 * @param key ID of the peer.
1037 * @param value Peer_Info of the peer.
1042 peer_info_timeout (void *cls,
1043 const struct GNUNET_HashCode *key,
1050 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1051 * and insert it in the appropriate structures if the peer is not known yet.
1053 * @param peer Full identity of the peer.
1055 * @return Existing or newly created peer info.
1057 static struct MeshPeerInfo *
1058 peer_get (const struct GNUNET_PeerIdentity *peer)
1060 struct MeshPeerInfo *peer_info;
1062 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1063 if (NULL == peer_info)
1066 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1067 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1069 GNUNET_CONTAINER_multihashmap_iterate (peers,
1073 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1074 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1075 peer_info->id = GNUNET_PEER_intern (peer);
1077 peer_info->last_contact = GNUNET_TIME_absolute_get();
1084 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1085 * and insert it in the appropriate structures if the peer is not known yet.
1087 * @param peer Short identity of the peer.
1089 * @return Existing or newly created peer info.
1091 static struct MeshPeerInfo *
1092 peer_get_short (const GNUNET_PEER_Id peer)
1094 struct GNUNET_PeerIdentity id;
1096 GNUNET_PEER_resolve (peer, &id);
1097 return peer_get (&id);
1102 * Choose the best path towards a peer considering the tunnel properties.
1104 * @param peer The destination peer.
1105 * @param t The tunnel the path is for.
1107 * @return Best current known path towards the peer, if any.
1109 static struct MeshPeerPath *
1110 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1112 struct MeshPeerPath *best_p;
1113 struct MeshPeerPath *p;
1114 unsigned int best_cost;
1117 best_p = p = peer->path_head;
1118 best_cost = cost = p->length;
1121 if ((cost = p->length) < best_cost)
1132 * Core callback to write a pre-constructed data packet to core buffer
1134 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1135 * @param size Number of bytes available in buf.
1136 * @param buf Where the to write the message.
1138 * @return number of bytes written to buf
1141 send_core_data_raw (void *cls, size_t size, void *buf)
1143 struct GNUNET_MessageHeader *msg = cls;
1146 GNUNET_assert (NULL != msg);
1147 total_size = ntohs (msg->size);
1149 if (total_size > size)
1154 memcpy (buf, msg, total_size);
1161 * Sends an already built message to a peer, properly registrating
1162 * all used resources.
1164 * @param message Message to send. Function makes a copy of it.
1165 * @param peer Short ID of the neighbor whom to send the message.
1166 * @param t Tunnel on which this message is transmitted.
1169 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1170 GNUNET_PEER_Id peer,
1171 struct MeshTunnel *t)
1173 struct GNUNET_PeerIdentity id;
1174 struct MeshPeerInfo *neighbor;
1175 struct MeshPeerPath *p;
1180 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1185 size = ntohs (message->size);
1186 data = GNUNET_malloc (size);
1187 memcpy (data, message, size);
1188 type = ntohs(message->type);
1189 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1190 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1192 struct GNUNET_MESH_Data *u;
1194 u = (struct GNUNET_MESH_Data *) data;
1195 u->ttl = htonl (ntohl (u->ttl) - 1);
1197 GNUNET_PEER_resolve (peer, &id);
1198 neighbor = peer_get (&id);
1199 for (p = neighbor->path_head; NULL != p; p = p->next)
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210 " %s IS NOT DIRECTLY CONNECTED\n",
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 for (p = neighbor->path_head; NULL != p; p = p->next)
1217 struct GNUNET_PeerIdentity debug_id;
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1221 " path with %u hops through:\n",
1223 for (i = 0; i < p->length; i++)
1225 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 i, GNUNET_i2s(&debug_id));
1232 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1233 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1234 " no direct connection to %s\n",
1239 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1250 * Sends a CREATE PATH message for a path to a peer, properly registrating
1251 * all used resources.
1253 * @param t Tunnel for which the path is created.
1256 send_create_path (struct MeshTunnel *t)
1258 struct MeshPeerInfo *neighbor;
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1261 neighbor = peer_get_short (t->next_hop);
1263 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1264 sizeof (struct GNUNET_MESH_CreateTunnel) +
1265 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1268 t->state = MESH_TUNNEL_WAITING;
1273 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1275 * @param t Tunnel which to confirm.
1278 send_path_ack (struct MeshTunnel *t)
1280 struct MeshPeerInfo *peer;
1282 peer = peer_get_short (t->prev_hop);
1285 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1286 sizeof (struct GNUNET_MESH_PathACK),
1293 * Try to establish a new connection to this peer in the given tunnel.
1294 * If the peer doesn't have any path to it yet, try to get one.
1295 * If the peer already has some path, send a CREATE PATH towards it.
1297 * @param peer PeerInfo of the peer.
1298 * @param t Tunnel for which to create the path, if possible.
1301 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1303 struct MeshPeerPath *p;
1305 if (NULL != peer->path_head)
1307 p = peer_get_best_path (peer, t);
1308 tunnel_use_path (t, p);
1309 send_create_path (t);
1311 else if (NULL == peer->dhtget)
1313 struct GNUNET_PeerIdentity id;
1315 GNUNET_PEER_resolve (peer->id, &id);
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1318 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1319 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1320 &id.hashPubKey, /* key to search */
1321 dht_replication_level, /* replication level */
1322 GNUNET_DHT_RO_RECORD_ROUTE |
1323 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1325 0, /* xquery bits */
1326 &dht_get_id_handler, peer);
1327 t->state = MESH_TUNNEL_SEARCHING;
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 "There is no path but the DHT GET is already started.\n");
1338 * Destroy the peer_info and free any allocated resources linked to it
1340 * @param pi The peer_info to destroy.
1342 * @return GNUNET_OK on success
1345 peer_info_destroy (struct MeshPeerInfo *pi)
1347 struct GNUNET_PeerIdentity id;
1348 struct MeshPeerPath *p;
1349 struct MeshPeerPath *nextp;
1352 GNUNET_PEER_resolve (pi->id, &id);
1353 GNUNET_PEER_change_rc (pi->id, -1);
1356 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1360 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1362 if (NULL != pi->dhtget)
1364 GNUNET_DHT_get_stop (pi->dhtget);
1370 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1374 for (i = 0; i < pi->ntunnels; i++)
1375 tunnel_destroy_empty (pi->tunnels[i]);
1376 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1383 * Remove all paths that rely on a direct connection between p1 and p2
1384 * from the peer itself and notify all tunnels about it.
1386 * @param peer PeerInfo of affected peer.
1387 * @param p1 GNUNET_PEER_Id of one peer.
1388 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1391 * TODO: optimize (see below)
1394 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1397 struct MeshPeerPath *p;
1398 struct MeshPeerPath *next;
1399 struct MeshPeerInfo *peer_d;
1401 unsigned int destroyed;
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1406 for (p = peer->path_head; NULL != p; p = next)
1409 for (i = 0; i < (p->length - 1); i++)
1411 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1412 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1414 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1424 for (i = 0; i < peer->ntunnels; i++)
1426 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1430 peer_d = peer_get_short (d);
1431 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1432 tunnel_use_path (peer->tunnels[i], next);
1433 peer_connect (peer_d, peer->tunnels[i]);
1435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1440 * Add the path to the peer and update the path used to reach it in case this
1443 * @param peer_info Destination peer to add the path to.
1444 * @param path New path to add. Last peer must be the peer in arg 1.
1445 * Path will be either used of freed if already known.
1446 * @param trusted Do we trust that this path is real?
1449 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1452 struct MeshPeerPath *aux;
1456 if ((NULL == peer_info) || (NULL == path))
1459 path_destroy (path);
1462 if (path->peers[path->length - 1] != peer_info->id)
1465 path_destroy (path);
1468 if (2 >= path->length && GNUNET_NO == trusted)
1470 /* Only allow CORE to tell us about direct paths */
1471 path_destroy (path);
1474 for (l = 1; l < path->length; l++)
1476 if (path->peers[l] == myid)
1478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1479 for (l2 = 0; l2 < path->length - l; l2++)
1481 path->peers[l2] = path->peers[l + l2];
1486 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1491 struct GNUNET_PeerIdentity id;
1493 GNUNET_PEER_resolve (peer_info->id, &id);
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1495 path->length, GNUNET_i2s (&id));
1498 l = path_get_length (path);
1501 path_destroy (path);
1505 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1506 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1508 l2 = path_get_length (aux);
1511 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1512 peer_info->path_tail, aux, path);
1517 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1519 path_destroy (path);
1524 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1531 * Add the path to the origin peer and update the path used to reach it in case
1532 * this is the shortest.
1533 * The path is given in peer_info -> destination, therefore we turn the path
1534 * upside down first.
1536 * @param peer_info Peer to add the path to, being the origin of the path.
1537 * @param path New path to add after being inversed.
1538 * Path will be either used or freed.
1539 * @param trusted Do we trust that this path is real?
1542 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1543 struct MeshPeerPath *path, int trusted)
1546 peer_info_add_path (peer_info, path, trusted);
1551 * Add a tunnel to the list of tunnels a peer participates in.
1552 * Update the tunnel's destination.
1554 * @param p Peer to add to.
1555 * @param t Tunnel to add.
1558 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1562 GNUNET_break (t->dest == p->id);
1566 GNUNET_PEER_change_rc (t->dest, 1);
1567 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1572 * Remove a tunnel from the list of tunnels a peer participates in.
1573 * Free the tunnel's destination.
1575 * @param p Peer to clean.
1576 * @param t Tunnel to remove.
1579 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1583 if (t->dest == p->id)
1585 GNUNET_PEER_change_rc (t->dest, -1);
1588 for (i = 0; i < p->ntunnels; i++)
1590 if (p->tunnels[i] == t)
1592 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1593 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1601 * Function called if the connection to the peer has been stalled for a while,
1602 * possibly due to a missed ACK. Poll the peer about its ACK status.
1604 * @param cls Closure (poll ctx).
1605 * @param tc TaskContext.
1608 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1610 struct MeshFlowControl *fc = cls;
1611 struct GNUNET_MESH_Poll msg;
1612 struct MeshTunnel *t = fc->t;
1613 GNUNET_PEER_Id peer;
1615 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1616 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1623 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1624 msg.header.size = htons (sizeof (msg));
1625 msg.tid = htonl (t->id.tid);
1626 msg.pid = htonl (fc->last_pid_sent);
1627 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1629 if (fc == &t->prev_fc)
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1634 else if (fc == &t->next_fc)
1636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1644 send_prebuilt_message (&msg.header, peer, t);
1645 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1646 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1652 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1653 * to obtain a local peer -> destination path and interning the peer ids.
1655 * @return Newly allocated and created path
1657 static struct MeshPeerPath *
1658 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1659 unsigned int get_path_length,
1660 const struct GNUNET_PeerIdentity *put_path,
1661 unsigned int put_path_length)
1663 struct MeshPeerPath *p;
1669 GNUNET_PEER_change_rc (myid, 1);
1670 i = get_path_length;
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1672 for (i--; i >= 0; i--)
1674 id = GNUNET_PEER_intern (&get_path[i]);
1675 if (p->length > 0 && id == p->peers[p->length - 1])
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1678 GNUNET_PEER_change_rc (id, -1);
1682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1683 GNUNET_i2s (&get_path[i]));
1685 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1686 p->peers[p->length - 1] = id;
1689 i = put_path_length;
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1691 for (i--; i >= 0; i--)
1693 id = GNUNET_PEER_intern (&put_path[i]);
1696 /* PUT path went through us, so discard the path up until now and start
1697 * from here to get a much shorter (and loop-free) path.
1702 if (p->length > 0 && id == p->peers[p->length - 1])
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1705 GNUNET_PEER_change_rc (id, -1);
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1710 GNUNET_i2s (&put_path[i]));
1712 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1713 p->peers[p->length - 1] = id;
1717 if (get_path_length > 0)
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1719 GNUNET_i2s (&get_path[0]));
1720 if (put_path_length > 0)
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1722 GNUNET_i2s (&put_path[0]));
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1725 for (i = 0; i < p->length; i++)
1727 struct GNUNET_PeerIdentity peer_id;
1729 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1731 GNUNET_i2s (&peer_id));
1739 * Adds a path to the peer_infos of all the peers in the path
1741 * @param p Path to process.
1742 * @param confirmed Whether we know if the path works or not.
1745 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1749 /* TODO: invert and add */
1750 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1751 for (i++; i < p->length; i++)
1753 struct MeshPeerInfo *aux;
1754 struct MeshPeerPath *copy;
1756 aux = peer_get_short (p->peers[i]);
1757 copy = path_duplicate (p);
1758 copy->length = i + 1;
1759 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1765 * Send keepalive packets for a peer
1767 * @param cls Closure (tunnel for which to send the keepalive).
1768 * @param tc Notification context.
1771 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1775 * Search for a tunnel among the incoming tunnels
1777 * @param tid the local id of the tunnel
1779 * @return tunnel handler, NULL if doesn't exist
1781 static struct MeshTunnel *
1782 tunnel_get_incoming (MESH_TunnelNumber tid)
1784 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1785 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1790 * Search for a tunnel among the tunnels for a client
1792 * @param c the client whose tunnels to search in
1793 * @param tid the local id of the tunnel
1795 * @return tunnel handler, NULL if doesn't exist
1797 static struct MeshTunnel *
1798 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1800 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1802 return tunnel_get_incoming (tid);
1806 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1807 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1813 * Search for a tunnel by global ID using PEER_ID
1815 * @param pi owner of the tunnel
1816 * @param tid global tunnel number
1818 * @return tunnel handler, NULL if doesn't exist
1820 static struct MeshTunnel *
1821 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1823 struct MESH_TunnelID id;
1824 struct GNUNET_HashCode hash;
1829 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1830 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1835 * Search for a tunnel by global ID using full PeerIdentities
1837 * @param oid owner of the tunnel
1838 * @param tid global tunnel number
1840 * @return tunnel handler, NULL if doesn't exist
1842 static struct MeshTunnel *
1843 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1845 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1850 * Add a client to a tunnel, initializing all needed data structures.
1852 * @param t Tunnel to which add the client.
1853 * @param c Client which to add to the tunnel.
1856 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1858 if (NULL != t->client)
1864 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1865 t->local_tid_dest, t,
1866 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1872 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1873 t->local_tid_dest, t,
1874 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1884 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1886 unsigned int own_pos;
1888 for (own_pos = 0; own_pos < p->length; own_pos++)
1890 if (p->peers[own_pos] == myid)
1893 if (own_pos > p->length - 1)
1899 if (own_pos < p->length - 1)
1900 t->next_hop = p->peers[own_pos + 1];
1902 t->next_hop = p->peers[own_pos];
1903 GNUNET_PEER_change_rc (t->next_hop, 1);
1905 t->prev_hop = p->peers[own_pos - 1];
1907 t->prev_hop = p->peers[0];
1908 GNUNET_PEER_change_rc (t->prev_hop, 1);
1910 if (NULL != t->path)
1911 path_destroy (t->path);
1912 t->path = path_duplicate (p);
1915 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1916 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1917 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1924 * Notifies a tunnel that a connection has broken that affects at least
1925 * some of its peers. Sends a notification towards the root of the tree.
1926 * In case the peer is the owner of the tree, notifies the client that owns
1927 * the tunnel and tries to reconnect.
1929 * @param t Tunnel affected.
1930 * @param p1 Peer that got disconnected from p2.
1931 * @param p2 Peer that got disconnected from p1.
1933 * @return Short ID of the peer disconnected (either p1 or p2).
1934 * 0 if the tunnel remained unaffected.
1936 static GNUNET_PEER_Id
1937 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1940 // if (myid != p1 && myid != p2) FIXME
1945 // if (tree_get_predecessor (t->tree) != 0)
1947 // /* We are the peer still connected, notify owner of the disconnection. */
1948 // struct GNUNET_MESH_PathBroken msg;
1949 // struct GNUNET_PeerIdentity neighbor;
1951 // msg.header.size = htons (sizeof (msg));
1952 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1953 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1954 // msg.tid = htonl (t->id.tid);
1955 // msg.peer1 = my_full_id;
1956 // GNUNET_PEER_resolve (pid, &msg.peer2);
1957 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1958 // send_prebuilt_message (&msg.header, &neighbor, t);
1965 * Build a local ACK message and send it to a local client.
1967 * @param t Tunnel on which to send the ACK.
1968 * @param c Client to whom send the ACK.
1969 * @param ack Value of the ACK.
1970 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1973 send_local_ack (struct MeshTunnel *t,
1974 struct MeshClient *c,
1977 struct GNUNET_MESH_LocalAck msg;
1979 msg.header.size = htons (sizeof (msg));
1980 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1981 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
1982 GNUNET_SERVER_notification_context_unicast (nc,
1989 * Build an ACK message and queue it to send to the given peer.
1991 * @param t Tunnel on which to send the ACK.
1992 * @param peer Peer to whom send the ACK.
1993 * @param ack Value of the ACK.
1996 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1998 struct GNUNET_MESH_ACK msg;
2000 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2001 msg.header.size = htons (sizeof (msg));
2002 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2003 msg.pid = htonl (ack);
2004 msg.tid = htonl (t->id.tid);
2006 send_prebuilt_message (&msg.header, peer, t);
2011 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2013 * @param t Tunnel this is about.
2016 tunnel_send_fwd_data_ack (struct MeshTunnel *t)
2018 struct GNUNET_MESH_DataACK msg;
2020 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
2021 msg.header.size = htons (sizeof (msg));
2022 msg.tid = htonl (t->id.tid);
2023 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2024 msg.pid = htonl (t->prev_fc.last_pid_recv);
2025 msg.futures = 0; // FIXME set bits of other newer messages received
2027 send_prebuilt_message (&msg.header, t->prev_hop, t);
2032 * Send an end-to-end BCK ACK message for the most recent in-sequence payload.
2034 * @param t Tunnel this is about.
2037 tunnel_send_bck_data_ack (struct MeshTunnel *t)
2039 struct GNUNET_MESH_DataACK msg;
2041 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
2042 msg.header.size = htons (sizeof (msg));
2043 msg.tid = htonl (t->id.tid);
2044 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2045 msg.pid = htonl (t->next_fc.last_pid_recv);
2046 msg.futures = 0; // FIXME set bits of other newer messages received
2048 send_prebuilt_message (&msg.header, t->next_hop, t);
2053 * Send an ACK informing the predecessor about the available buffer space.
2054 * In case there is no predecessor, inform the owning client.
2055 * If buffering is off, send only on behalf of children or self if endpoint.
2056 * If buffering is on, send when sent to children and buffer space is free.
2057 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2058 * the ACK itself goes "back" (towards root).
2060 * @param t Tunnel on which to send the ACK.
2061 * @param type Type of message that triggered the ACK transmission.
2064 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2068 /* Is it after unicast retransmission? */
2071 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2073 "ACK due to FWD DATA retransmission\n");
2074 if (GNUNET_YES == t->nobuffer)
2076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2079 if (GNUNET_YES == t->reliable && NULL != t->client)
2080 tunnel_send_fwd_data_ack (t);
2082 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2083 if (NULL != t->owner && GNUNET_YES == t->reliable)
2085 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2087 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2088 tunnel_send_fwd_data_ack (t);
2090 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2091 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2092 t->force_ack = GNUNET_YES;
2098 /* Check if we need to transmit the ACK */
2099 if (NULL == t->owner &&
2100 t->queue_max > t->next_fc.queue_n * 4 &&
2101 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2102 GNUNET_NO == t->force_ack)
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, buffer free\n");
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2106 " t->qmax: %u, t->qn: %u\n",
2107 t->queue_max, t->next_fc.queue_n);
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2109 " t->pid: %u, t->ack: %u\n",
2110 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2114 /* Ok, ACK might be necessary, what PID to ACK? */
2115 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK %u\n", ack);
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2118 " last %u, qmax %u, q %u\n",
2119 t->prev_fc.last_pid_recv, t->queue_max, t->next_fc.queue_n);
2120 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2126 t->prev_fc.last_ack_sent = ack;
2127 if (NULL != t->owner)
2128 send_local_ack (t, t->owner, GNUNET_YES);
2129 else if (0 != t->prev_hop)
2130 send_ack (t, t->prev_hop, ack);
2134 t->force_ack = GNUNET_NO;
2139 * Send an ACK informing the children node/client about the available
2141 * If buffering is off, send only on behalf of root (can be self).
2142 * If buffering is on, send when sent to predecessor and buffer space is free.
2143 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2144 * the ACK itself goes "forward" (towards children/clients).
2146 * @param t Tunnel on which to send the ACK.
2147 * @param type Type of message that triggered the ACK transmission.
2150 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2154 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2155 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2156 /* Is it after data to_origin retransmission? */
2159 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2160 if (GNUNET_YES == t->nobuffer)
2162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2163 " Not sending ACK, nobuffer + traffic\n");
2167 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2168 if (NULL != t->client && GNUNET_YES == t->reliable)
2170 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2172 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2173 tunnel_send_bck_data_ack (t);
2175 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2176 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2177 t->force_ack = GNUNET_YES;
2183 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2185 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2187 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 " Not sending ACK, not needed, last ack sent was %u\n",
2191 t->next_fc.last_ack_sent);
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 " Sending BCK ACK %u (last sent: %u)\n",
2196 ack, t->next_fc.last_ack_sent);
2197 t->next_fc.last_ack_sent = ack;
2199 if (NULL != t->client)
2200 send_local_ack (t, t->client, GNUNET_NO);
2201 else if (0 != t->next_hop)
2202 send_ack (t, t->next_hop, ack);
2205 t->force_ack = GNUNET_NO;
2210 * Modify the mesh message TID from global to local and send to client.
2212 * @param t Tunnel on which to send the message.
2213 * @param msg Message to modify and send.
2214 * @param c Client to send to.
2215 * @param tid Tunnel ID to use (c can be both owner and client).
2218 tunnel_send_client_data (struct MeshTunnel *t,
2219 const struct GNUNET_MESH_Data *msg,
2220 struct MeshClient *c, MESH_TunnelNumber tid)
2222 struct GNUNET_MESH_LocalData *copy;
2223 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2224 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2226 if (size < sizeof (struct GNUNET_MessageHeader))
2228 GNUNET_break_op (0);
2236 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2237 memcpy (©[1], &msg[1], size);
2238 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2239 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2240 copy->tid = htonl (tid);
2241 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2242 ©->header, GNUNET_NO);
2246 * Modify the unicast message TID from global to local and send to client.
2248 * @param t Tunnel on which to send the message.
2249 * @param msg Message to modify and send.
2252 tunnel_send_client_ucast (struct MeshTunnel *t,
2253 const struct GNUNET_MESH_Data *msg)
2255 tunnel_send_client_data (t, msg, t->client, t->local_tid_dest);
2260 * Modify the to_origin message TID from global to local and send to client.
2262 * @param t Tunnel on which to send the message.
2263 * @param msg Message to modify and send.
2266 tunnel_send_client_to_orig (struct MeshTunnel *t,
2267 const struct GNUNET_MESH_Data *msg)
2269 tunnel_send_client_data (t, msg, t->owner, t->local_tid);
2274 * We haven't received an ACK after a certain time: restransmit the message.
2276 * @param cls Closure (MeshSentMessage with the message to restransmit)
2277 * @param tc TaskContext.
2280 tunnel_retransmit_message (void *cls,
2281 const struct GNUNET_SCHEDULER_TaskContext *tc)
2283 struct MeshSentMessage *copy = cls;
2284 struct GNUNET_MESH_Data *payload;
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Retransmit \n");
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->id);
2289 copy->retry_task = GNUNET_SCHEDULER_NO_TASK;
2290 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2293 payload = (struct GNUNET_MESH_Data *) ©[1];
2294 hop = copy->is_forward ? copy->t->next_hop : copy->t->prev_hop;
2295 send_prebuilt_message (&payload->header, hop, copy->t);
2296 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2297 copy->retry_timer = GNUNET_TIME_STD_BACKOFF (copy->retry_timer);
2298 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
2299 &tunnel_retransmit_message,
2305 * @brief Re-initiate traffic to this peer if necessary.
2307 * Check if there is traffic queued towards this peer
2308 * and the core transmit handle is NULL (traffic was stalled).
2309 * If so, call core tmt rdy.
2311 * @param peer_id Short ID of peer to which initiate traffic.
2314 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2316 struct MeshPeerInfo *peer;
2317 struct GNUNET_PeerIdentity id;
2318 struct MeshPeerQueue *q;
2321 peer = peer_get_short (peer_id);
2322 if (NULL != peer->core_transmit)
2325 q = queue_get_next (peer);
2328 /* Might br multicast traffic already sent to this particular peer but
2329 * not to other children in this tunnel.
2330 * This way t->queue_n would be > 0 but the queue of this particular peer
2336 GNUNET_PEER_resolve (peer->id, &id);
2337 peer->core_transmit =
2338 GNUNET_CORE_notify_transmit_ready(core_handle,
2341 GNUNET_TIME_UNIT_FOREVER_REL,
2351 * Send a message to all peers and clients in this tunnel that the tunnel
2352 * is no longer valid. If some peer or client should not receive the message,
2353 * should be zero'ed out before calling this function.
2355 * @param t The tunnel whose peers and clients to notify.
2358 tunnel_send_destroy (struct MeshTunnel *t)
2360 struct GNUNET_MESH_TunnelDestroy msg;
2361 struct GNUNET_PeerIdentity id;
2363 msg.header.size = htons (sizeof (msg));
2364 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2365 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2366 msg.tid = htonl (t->id.tid);
2367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2368 " sending tunnel destroy for tunnel: %s [%X]\n",
2369 GNUNET_i2s (&msg.oid), t->id.tid);
2371 if (NULL == t->client && 0 != t->next_hop)
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2374 GNUNET_PEER_resolve (t->next_hop, &id);
2375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2376 " sending forward to %s\n",
2378 send_prebuilt_message (&msg.header, t->next_hop, t);
2380 if (NULL == t->owner && 0 != t->prev_hop)
2382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2383 GNUNET_PEER_resolve (t->prev_hop, &id);
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385 " sending back to %s\n",
2387 send_prebuilt_message (&msg.header, t->prev_hop, t);
2389 if (NULL != t->owner)
2391 send_client_tunnel_destroy (t->owner, t);
2393 if (NULL != t->client)
2395 send_client_tunnel_destroy (t->client, t);
2401 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2403 * @param t Tunnel which to cancel.
2404 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2407 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2409 struct MeshPeerInfo *peer_info;
2410 struct MeshPeerQueue *pq;
2411 struct MeshPeerQueue *next;
2414 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2415 peer_info = peer_get_short (neighbor);
2416 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2419 if (pq->tunnel == t)
2421 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2422 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2424 /* Should have been removed on destroy children */
2427 queue_destroy (pq, GNUNET_YES);
2430 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2432 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2433 peer_info->core_transmit = NULL;
2439 * Destroy the tunnel.
2441 * This function does not generate any warning traffic to clients or peers.
2444 * Remove the tunnel from peer_info's and clients' hashmaps.
2445 * Cancel messages belonging to this tunnel queued to neighbors.
2446 * Free any allocated resources linked to the tunnel.
2448 * @param t the tunnel to destroy
2450 * @return GNUNET_OK on success
2453 tunnel_destroy (struct MeshTunnel *t)
2455 struct MeshClient *c;
2456 struct GNUNET_HashCode hash;
2466 struct GNUNET_PeerIdentity id;
2468 GNUNET_PEER_resolve (t->id.oid, &id);
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2470 GNUNET_i2s (&id), t->id.tid);
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2476 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2477 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2486 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2493 if (NULL != t->client)
2497 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2498 t->local_tid_dest, t))
2504 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2505 t->local_tid_dest, t))
2512 if (0 != t->prev_hop)
2514 peer_cancel_queues (t->prev_hop, t);
2515 GNUNET_PEER_change_rc (t->prev_hop, -1);
2517 if (0 != t->next_hop)
2519 peer_cancel_queues (t->next_hop, t);
2520 GNUNET_PEER_change_rc (t->next_hop, -1);
2523 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2526 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2527 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2530 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2531 path_destroy (t->path);
2537 * Tunnel is empty: destroy it.
2539 * Notifies all participants (peers, cleints) about the destruction.
2541 * @param t Tunnel to destroy.
2544 tunnel_destroy_empty (struct MeshTunnel *t)
2548 struct GNUNET_PeerIdentity id;
2550 GNUNET_PEER_resolve (t->id.oid, &id);
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2552 "executing destruction of empty tunnel %s [%X]\n",
2553 GNUNET_i2s (&id), t->id.tid);
2557 if (GNUNET_NO == t->destroy)
2558 tunnel_send_destroy (t);
2559 if (0 == t->pending_messages)
2562 t->destroy = GNUNET_YES;
2566 * Initialize a Flow Control structure to the initial state.
2568 * @param fc Flow Control structure to initialize.
2571 fc_init (struct MeshFlowControl *fc)
2573 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2574 fc->last_pid_recv = (uint32_t) -1;
2575 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2576 fc->last_ack_recv = (uint32_t) -1;
2577 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2578 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2583 * Create a new tunnel
2585 * @param owner Who is the owner of the tunnel (short ID).
2586 * @param tid Tunnel Number of the tunnel.
2587 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2588 * @param local Tunnel Number for the tunnel, for the client point of view.
2590 * @return A new initialized tunnel. NULL on error.
2592 static struct MeshTunnel *
2593 tunnel_new (GNUNET_PEER_Id owner,
2594 MESH_TunnelNumber tid,
2595 struct MeshClient *client,
2596 MESH_TunnelNumber local)
2598 struct MeshTunnel *t;
2599 struct GNUNET_HashCode hash;
2601 if (n_tunnels >= max_tunnels && NULL == client)
2604 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2607 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2609 fc_init (&t->next_fc);
2610 fc_init (&t->prev_fc);
2611 t->local_tid = local;
2613 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2615 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2617 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2618 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2625 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2633 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2635 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2639 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2649 * Set options in a tunnel, extracted from a bit flag field
2651 * @param t Tunnel to set options to.
2652 * @param options Bit array in host byte order.
2655 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2657 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
2658 GNUNET_YES : GNUNET_NO;
2659 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
2660 GNUNET_YES : GNUNET_NO;
2665 * Iterator for deleting each tunnel whose client endpoint disconnected.
2667 * @param cls Closure (client that has disconnected).
2668 * @param key The local tunnel id (used to access the hashmap).
2669 * @param value The value stored at the key (tunnel to destroy).
2671 * @return GNUNET_OK, keep iterating.
2674 tunnel_destroy_iterator (void *cls,
2678 struct MeshTunnel *t = value;
2679 struct MeshClient *c = cls;
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2682 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2683 t->local_tid, t->local_tid_dest, c->id);
2684 client_delete_tunnel (c, t);
2687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2689 if (0 != t->next_hop) { /* destroy could come before a path is used */
2690 GNUNET_PEER_change_rc (t->next_hop, -1);
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2698 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2699 GNUNET_PEER_change_rc (t->prev_hop, -1);
2704 tunnel_destroy_empty (t);
2711 * Timeout function, destroys tunnel if called
2713 * @param cls Closure (tunnel to destroy).
2714 * @param tc TaskContext
2717 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2719 struct MeshTunnel *t = cls;
2720 struct GNUNET_PeerIdentity id;
2722 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2723 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2725 GNUNET_PEER_resolve(t->id.oid, &id);
2726 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2727 "Tunnel %s [%X] timed out. Destroying.\n",
2728 GNUNET_i2s(&id), t->id.tid);
2729 if (NULL != t->client)
2730 send_client_tunnel_destroy (t->client, t);
2731 tunnel_destroy (t); /* Do not notify other */
2736 * Resets the tunnel timeout. Starts it if no timeout was running.
2738 * @param t Tunnel whose timeout to reset.
2740 * TODO use heap to improve efficiency of scheduler.
2743 tunnel_reset_timeout (struct MeshTunnel *t)
2745 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2747 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2748 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2749 t->maintenance_task =
2750 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2751 (refresh_path_time, 4), &tunnel_timeout, t);
2755 /******************************************************************************/
2756 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2757 /******************************************************************************/
2760 * Function to send a create path packet to a peer.
2762 * @param cls closure
2763 * @param size number of bytes available in buf
2764 * @param buf where the callee should write the message
2765 * @return number of bytes written to buf
2768 send_core_path_create (void *cls, size_t size, void *buf)
2770 struct MeshTunnel *t = cls;
2771 struct GNUNET_MESH_CreateTunnel *msg;
2772 struct GNUNET_PeerIdentity *peer_ptr;
2773 struct MeshPeerPath *p = t->path;
2778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2780 sizeof (struct GNUNET_MESH_CreateTunnel) +
2781 p->length * sizeof (struct GNUNET_PeerIdentity);
2783 if (size < size_needed || NULL == buf)
2788 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2789 msg->header.size = htons (size_needed);
2790 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2791 msg->tid = ntohl (t->id.tid);
2794 if (GNUNET_YES == t->nobuffer)
2795 opt |= GNUNET_MESH_OPTION_NOBUFFER;
2796 if (GNUNET_YES == t->reliable)
2797 opt |= GNUNET_MESH_OPTION_RELIABLE;
2798 msg->opt = htonl (opt);
2799 msg->port = htonl (t->port);
2801 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2802 for (i = 0; i < p->length; i++)
2804 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2808 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2814 * Creates a path ack message in buf and frees all unused resources.
2816 * @param cls closure (MeshTransmissionDescriptor)
2817 * @param size number of bytes available in buf
2818 * @param buf where the callee should write the message
2819 * @return number of bytes written to buf
2822 send_core_path_ack (void *cls, size_t size, void *buf)
2824 struct MeshTunnel *t = cls;
2825 struct GNUNET_MESH_PathACK *msg = buf;
2827 GNUNET_assert (NULL != t);
2828 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2833 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2834 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2835 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2836 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2837 msg->tid = htonl (t->id.tid);
2838 msg->peer_id = my_full_id;
2839 msg->ack = htonl (t->prev_fc.last_ack_sent);
2841 /* TODO add signature */
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2844 return sizeof (struct GNUNET_MESH_PathACK);
2849 * Free a transmission that was already queued with all resources
2850 * associated to the request.
2852 * @param queue Queue handler to cancel.
2853 * @param clear_cls Is it necessary to free associated cls?
2856 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2858 struct MeshFlowControl *fc;
2860 if (GNUNET_YES == clear_cls)
2862 switch (queue->type)
2864 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2865 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2866 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2868 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2869 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2870 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2871 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2872 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2874 " prebuilt message\n");
2875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2877 GNUNET_MESH_DEBUG_M2S (queue->type));
2879 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2884 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2885 " type %s unknown!\n",
2886 GNUNET_MESH_DEBUG_M2S (queue->type));
2888 GNUNET_free_non_null (queue->cls);
2890 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2891 queue->peer->queue_tail,
2894 /* Delete from appropriate fc in the tunnel */
2895 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
2896 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
2898 if (queue->peer->id == queue->tunnel->prev_hop)
2899 fc = &queue->tunnel->prev_fc;
2900 else if (queue->peer->id == queue->tunnel->next_hop)
2901 fc = &queue->tunnel->next_fc;
2909 GNUNET_free (queue);
2914 * @brief Get the next transmittable message from the queue.
2916 * This will be the head, except in the case of being a data packet
2917 * not allowed by the destination peer.
2919 * @param peer Destination peer.
2921 * @return The next viable MeshPeerQueue element to send to that peer.
2922 * NULL when there are no transmittable messages.
2924 struct MeshPeerQueue *
2925 queue_get_next (const struct MeshPeerInfo *peer)
2927 struct MeshPeerQueue *q;
2929 struct GNUNET_MESH_Data *dmsg;
2930 struct MeshTunnel* t;
2934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2935 for (q = peer->queue_head; NULL != q; q = q->next)
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2940 GNUNET_MESH_DEBUG_M2S (q->type));
2941 dmsg = (struct GNUNET_MESH_Data *) q->cls;
2942 pid = ntohl (dmsg->pid);
2945 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2946 ack = t->next_fc.last_ack_recv;
2948 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2949 ack = t->prev_fc.last_ack_recv;
2952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2957 "* ACK: %u, PID: %u\n",
2959 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2972 "* nothing found\n");
2978 queue_send (void *cls, size_t size, void *buf)
2980 struct MeshPeerInfo *peer = cls;
2981 struct GNUNET_MessageHeader *msg;
2982 struct MeshPeerQueue *queue;
2983 struct MeshTunnel *t;
2984 struct GNUNET_PeerIdentity dst_id;
2985 struct MeshFlowControl *fc;
2990 peer->core_transmit = NULL;
2992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
2993 queue = queue_get_next (peer);
2995 /* Queue has no internal mesh traffic nor sendable payload */
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
2999 if (NULL == peer->queue_head)
3000 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3005 GNUNET_PEER_resolve (peer->id, &dst_id);
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3008 GNUNET_i2s (&dst_id));
3009 /* Check if buffer size is enough for the message */
3010 if (queue->size > size)
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3013 "* not enough room, reissue\n");
3014 peer->core_transmit =
3015 GNUNET_CORE_notify_transmit_ready (core_handle,
3018 GNUNET_TIME_UNIT_FOREVER_REL,
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3028 GNUNET_assert (0 < t->pending_messages);
3029 t->pending_messages--;
3033 switch (queue->type)
3036 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3037 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3038 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3039 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3040 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3041 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3044 GNUNET_MESH_DEBUG_M2S (queue->type));
3046 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3047 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3048 data_size = send_core_data_raw (queue->cls, size, buf);
3049 msg = (struct GNUNET_MessageHeader *) buf;
3050 type = ntohs (msg->type);
3052 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3054 data_size = send_core_path_create (queue->cls, size, buf);
3056 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3058 data_size = send_core_path_ack (queue->cls, size, buf);
3062 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3063 "* type unknown: %u\n",
3068 if (0 < drop_percent &&
3069 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3071 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3072 "Dropping message of type %s\n",
3073 GNUNET_MESH_DEBUG_M2S(queue->type));
3076 /* Free queue, but cls was freed by send_core_* */
3077 queue_destroy (queue, GNUNET_NO);
3079 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3080 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3084 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3085 t->next_fc.last_pid_sent = pid;
3086 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3088 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3089 t->prev_fc.last_pid_sent = pid;
3090 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3096 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3102 /* If more data in queue, send next */
3103 queue = queue_get_next (peer);
3106 struct GNUNET_PeerIdentity id;
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3109 GNUNET_PEER_resolve (peer->id, &id);
3110 peer->core_transmit =
3111 GNUNET_CORE_notify_transmit_ready(core_handle,
3114 GNUNET_TIME_UNIT_FOREVER_REL,
3120 else if (NULL != peer->queue_head)
3122 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3124 GNUNET_i2s (&my_full_id));
3125 if (peer->id == t->next_hop)
3127 else if (peer->id == t->prev_hop)
3134 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3137 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3147 * @brief Queue and pass message to core when possible.
3149 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3150 * accounts for it. In case the queue is full, the message is dropped and
3153 * Otherwise, message is treated as internal and allowed to go regardless of
3156 * @param cls Closure (@c type dependant). It will be used by queue_send to
3157 * build the message to be sent if not already prebuilt.
3158 * @param type Type of the message, 0 for a raw message.
3159 * @param size Size of the message.
3160 * @param dst Neighbor to send message to.
3161 * @param t Tunnel this message belongs to.
3164 queue_add (void *cls, uint16_t type, size_t size,
3165 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3167 struct MeshPeerQueue *queue;
3168 struct GNUNET_PeerIdentity id;
3172 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3174 n = &t->next_fc.queue_n;
3176 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3178 n = &t->prev_fc.queue_n;
3182 if (*n >= t->queue_max)
3185 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3186 "queue full: %u/%u\n",
3188 GNUNET_STATISTICS_update(stats,
3189 "# messages dropped (buffer full)",
3191 return; /* Drop message */
3195 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3201 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3202 if (NULL == dst->core_transmit)
3204 GNUNET_PEER_resolve (dst->id, &id);
3205 dst->core_transmit =
3206 GNUNET_CORE_notify_transmit_ready (core_handle,
3209 GNUNET_TIME_UNIT_FOREVER_REL,
3215 t->pending_messages++;
3219 /******************************************************************************/
3220 /******************** MESH NETWORK HANDLERS **************************/
3221 /******************************************************************************/
3225 * Core handler for path creation
3227 * @param cls closure
3228 * @param message message
3229 * @param peer peer identity this notification is about
3231 * @return GNUNET_OK to keep the connection open,
3232 * GNUNET_SYSERR to close it (signal serious error)
3235 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3236 const struct GNUNET_MessageHeader *message)
3238 unsigned int own_pos;
3241 MESH_TunnelNumber tid;
3242 struct GNUNET_MESH_CreateTunnel *msg;
3243 struct GNUNET_PeerIdentity *pi;
3244 struct MeshPeerPath *path;
3245 struct MeshPeerInfo *dest_peer_info;
3246 struct MeshPeerInfo *orig_peer_info;
3247 struct MeshTunnel *t;
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 "Received a path create msg [%s]\n",
3251 GNUNET_i2s (&my_full_id));
3252 size = ntohs (message->size);
3253 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3255 GNUNET_break_op (0);
3259 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3260 if (size % sizeof (struct GNUNET_PeerIdentity))
3262 GNUNET_break_op (0);
3265 size /= sizeof (struct GNUNET_PeerIdentity);
3268 GNUNET_break_op (0);
3271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3272 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3274 tid = ntohl (msg->tid);
3275 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3277 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3278 t = tunnel_get (pi, tid);
3279 if (NULL == t) /* might be a local tunnel */
3283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3284 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3290 t->port = ntohl (msg->port);
3291 opt = ntohl (msg->opt);
3292 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3294 t->nobuffer = GNUNET_YES;
3297 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3299 t->reliable = GNUNET_YES;
3301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3303 tunnel_reset_timeout (t);
3305 t->state = MESH_TUNNEL_WAITING;
3307 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3308 if (NULL == dest_peer_info)
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3311 " Creating PeerInfo for destination.\n");
3312 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3313 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3314 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3316 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3318 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3319 if (NULL == orig_peer_info)
3321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3322 " Creating PeerInfo for origin.\n");
3323 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3324 orig_peer_info->id = GNUNET_PEER_intern (pi);
3325 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3326 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3329 path = path_new (size);
3331 for (i = 0; i < size; i++)
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3334 GNUNET_i2s (&pi[i]));
3335 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3336 if (path->peers[i] == myid)
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3340 if (own_pos == 0 && path->peers[own_pos] != myid)
3342 /* create path: self not found in path through self */
3343 GNUNET_break_op (0);
3344 path_destroy (path);
3348 path_add_to_peers (path, GNUNET_NO);
3349 tunnel_use_path (t, path);
3351 peer_info_add_tunnel (dest_peer_info, t);
3353 if (own_pos == size - 1)
3355 struct MeshClient *c;
3357 /* Find target client */
3358 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3361 /* TODO send reject */
3365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3366 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3368 /* Assign local tid */
3369 while (NULL != tunnel_get_incoming (next_local_tid))
3370 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3371 t->local_tid_dest = next_local_tid++;
3372 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3374 if (GNUNET_YES == t->reliable)
3375 t->sent_messages_bck =
3376 GNUNET_CONTAINER_multihashmap32_create (t->queue_max);
3378 tunnel_add_client (t, c);
3379 send_client_tunnel_create (t);
3384 struct MeshPeerPath *path2;
3386 t->next_hop = path->peers[own_pos + 1];
3387 GNUNET_PEER_change_rc(t->next_hop, 1);
3389 /* It's for somebody else! Retransmit. */
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3391 path2 = path_duplicate (path);
3392 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3393 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3394 send_create_path (t);
3402 * Core handler for path ACKs
3404 * @param cls closure
3405 * @param message message
3406 * @param peer peer identity this notification is about
3408 * @return GNUNET_OK to keep the connection open,
3409 * GNUNET_SYSERR to close it (signal serious error)
3412 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3413 const struct GNUNET_MessageHeader *message)
3415 struct GNUNET_MESH_PathACK *msg;
3416 struct MeshPeerInfo *peer_info;
3417 struct MeshPeerPath *p;
3418 struct MeshTunnel *t;
3420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3421 GNUNET_i2s (&my_full_id));
3422 msg = (struct GNUNET_MESH_PathACK *) message;
3423 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3426 /* TODO notify that we don't know the tunnel */
3427 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3429 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3433 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3435 peer_info = peer_get (&msg->peer_id);
3436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3437 GNUNET_i2s (&msg->peer_id));
3438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3441 /* Add path to peers? */
3445 path_add_to_peers (p, GNUNET_YES);
3451 t->state = MESH_TUNNEL_READY;
3452 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3453 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3455 /* Message for us? */
3456 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3459 if (NULL == t->owner)
3461 GNUNET_break_op (0);
3464 if (NULL != peer_info->dhtget)
3466 GNUNET_DHT_get_stop (peer_info->dhtget);
3467 peer_info->dhtget = NULL;
3469 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3470 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3475 " not for us, retransmitting...\n");
3476 peer_info = peer_get (&msg->oid);
3477 send_prebuilt_message (message, t->prev_hop, t);
3483 * Core handler for notifications of broken paths
3485 * @param cls closure
3486 * @param message message
3487 * @param peer peer identity this notification is about
3489 * @return GNUNET_OK to keep the connection open,
3490 * GNUNET_SYSERR to close it (signal serious error)
3493 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3494 const struct GNUNET_MessageHeader *message)
3496 struct GNUNET_MESH_PathBroken *msg;
3497 struct MeshTunnel *t;
3499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3500 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3501 msg = (struct GNUNET_MESH_PathBroken *) message;
3502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3503 GNUNET_i2s (&msg->peer1));
3504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3505 GNUNET_i2s (&msg->peer2));
3506 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3509 GNUNET_break_op (0);
3512 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3513 GNUNET_PEER_search (&msg->peer2));
3520 * Core handler for tunnel destruction
3522 * @param cls closure
3523 * @param message message
3524 * @param peer peer identity this notification is about
3526 * @return GNUNET_OK to keep the connection open,
3527 * GNUNET_SYSERR to close it (signal serious error)
3530 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3531 const struct GNUNET_MessageHeader *message)
3533 struct GNUNET_MESH_TunnelDestroy *msg;
3534 struct MeshTunnel *t;
3536 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3538 "Got a TUNNEL DESTROY packet from %s\n",
3540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3541 " for tunnel %s [%u]\n",
3542 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3543 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3546 /* Probably already got the message from another path,
3547 * destroyed the tunnel and retransmitted to children.
3550 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3554 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3557 t->local_tid, t->local_tid_dest);
3559 if (GNUNET_PEER_search (peer) == t->prev_hop)
3561 // TODO check owner's signature
3562 // TODO add owner's signatue to tunnel for retransmission
3563 peer_cancel_queues (t->prev_hop, t);
3564 GNUNET_PEER_change_rc (t->prev_hop, -1);
3567 else if (GNUNET_PEER_search (peer) == t->next_hop)
3569 // TODO check dest's signature
3570 // TODO add dest's signatue to tunnel for retransmission
3571 peer_cancel_queues (t->next_hop, t);
3572 GNUNET_PEER_change_rc (t->next_hop, -1);
3577 GNUNET_break_op (0);
3578 // TODO check both owner AND destination's signature to see which matches
3579 // TODO restransmit in appropriate direction
3582 tunnel_destroy_empty (t);
3584 // TODO: add timeout to destroy the tunnel anyway
3590 * Core handler for mesh network traffic going from the origin to a peer
3592 * @param cls closure
3593 * @param peer peer identity this notification is about
3594 * @param message message
3595 * @return GNUNET_OK to keep the connection open,
3596 * GNUNET_SYSERR to close it (signal serious error)
3599 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3600 const struct GNUNET_MessageHeader *message)
3602 struct GNUNET_MESH_Data *msg;
3603 struct MeshTunnel *t;
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3611 size = ntohs (message->size);
3613 sizeof (struct GNUNET_MESH_Data) +
3614 sizeof (struct GNUNET_MessageHeader))
3619 msg = (struct GNUNET_MESH_Data *) message;
3620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3621 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3623 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3626 /* TODO notify back: we don't know this tunnel */
3627 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3628 GNUNET_break_op (0);
3631 pid = ntohl (msg->pid);
3632 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3634 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3635 GNUNET_break_op (0);
3636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3637 "Received PID %u, ACK %u\n",
3638 pid, t->prev_fc.last_ack_sent);
3639 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3643 tunnel_reset_timeout (t);
3644 if (t->dest == myid)
3646 /* TODO signature verification */
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 " it's for us! sending to clients...\n");
3649 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3650 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
3651 if ( (GNUNET_NO == t->reliable &&
3652 GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv))
3654 (GNUNET_YES == t->reliable &&
3655 pid == t->prev_fc.last_pid_recv + 1) )
3657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3658 " pid %u not seen yet, forwarding\n", pid);
3659 t->prev_fc.last_pid_recv = pid;
3660 tunnel_send_client_ucast (t, msg);
3661 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3665 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
3666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3667 " Pid %u not expected (%u), sending FWD ACK!\n",
3668 pid, t->prev_fc.last_pid_recv + 1);
3669 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3673 t->prev_fc.last_pid_recv = pid;
3674 if (0 == t->next_hop)
3679 ttl = ntohl (msg->ttl);
3680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3683 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3684 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3685 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3689 " not for us, retransmitting...\n");
3691 send_prebuilt_message (message, t->next_hop, t);
3692 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3698 * Core handler for mesh network traffic toward the owner of a tunnel
3700 * @param cls closure
3701 * @param message message
3702 * @param peer peer identity this notification is about
3704 * @return GNUNET_OK to keep the connection open,
3705 * GNUNET_SYSERR to close it (signal serious error)
3708 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3709 const struct GNUNET_MessageHeader *message)
3711 struct GNUNET_MESH_Data *msg;
3712 struct MeshTunnel *t;
3717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3719 size = ntohs (message->size);
3720 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
3721 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3723 GNUNET_break_op (0);
3726 msg = (struct GNUNET_MESH_Data *) message;
3727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3728 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3729 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3730 pid = ntohl (msg->pid);
3733 /* TODO notify that we dont know this tunnel (whom)? */
3734 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3736 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3737 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3741 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3743 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3744 GNUNET_break_op (0);
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3746 "Received PID %u, ACK %u\n",
3747 pid, t->next_fc.last_ack_sent);
3748 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3752 if (myid == t->id.oid)
3754 /* TODO signature verification */
3755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3756 " it's for us! sending to clients...\n");
3757 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3758 if ( (GNUNET_NO == t->reliable &&
3759 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
3761 (GNUNET_YES == t->reliable &&
3762 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
3764 t->next_fc.last_pid_recv = pid;
3765 tunnel_send_client_to_orig (t, msg);
3766 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3770 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3771 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3772 " Pid %u not expected, sending FWD ACK!\n", pid);
3773 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3778 " not for us, retransmitting...\n");
3779 t->next_fc.last_pid_recv = pid;
3780 if (0 == t->prev_hop) /* No owner AND no prev hop */
3782 if (GNUNET_YES == t->destroy)
3784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3785 "to orig received on a dying tunnel %s [%X]\n",
3786 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3789 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3790 "unknown to origin at %s\n",
3791 GNUNET_i2s (&my_full_id));
3792 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3795 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3796 "on tunnel %s [%X]\n",
3797 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3800 ttl = ntohl (msg->ttl);
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3804 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3805 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3806 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3809 send_prebuilt_message (message, t->prev_hop, t);
3810 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3817 * Core handler for mesh network traffic end-to-end ACKs.
3819 * @param cls Closure.
3820 * @param message Message.
3821 * @param peer Peer identity this notification is about.
3823 * @return GNUNET_OK to keep the connection open,
3824 * GNUNET_SYSERR to close it (signal serious error)
3827 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3828 const struct GNUNET_MessageHeader *message)
3830 struct GNUNET_MESH_DataACK *msg;
3831 struct GNUNET_CONTAINER_MultiHashMap32 *hm;
3832 struct MeshSentMessage *copy;
3833 struct MeshTunnel *t;
3837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a DATA ACK message from %s!\n",
3839 msg = (struct GNUNET_MESH_DataACK *) message;
3841 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3844 /* TODO notify that we dont know this tunnel (whom)? */
3845 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3848 ack = ntohl (msg->pid);
3849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3851 /* Is this a forward or backward ACK? */
3852 id = GNUNET_PEER_search (peer);
3853 if (t->next_hop == id)
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3856 if (NULL == t->owner)
3858 send_prebuilt_message (message, t->prev_hop, t);
3861 hm = t->sent_messages_fwd;
3862 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3864 else if (t->prev_hop == id)
3866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3867 if (NULL == t->client)
3869 send_prebuilt_message (message, t->next_hop, t);
3872 hm = t->sent_messages_bck;
3873 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3876 GNUNET_break_op (0);
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK'ed \n");
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", ack);
3880 copy = GNUNET_CONTAINER_multihashmap32_get (hm, ack);
3883 GNUNET_break (0); // FIXME needed?
3886 GNUNET_break (GNUNET_YES ==
3887 GNUNET_CONTAINER_multihashmap32_remove (hm, ack, copy));
3888 if (GNUNET_SCHEDULER_NO_TASK != copy->retry_task)
3890 GNUNET_SCHEDULER_cancel (copy->retry_task);
3899 * Core handler for mesh network traffic point-to-point acks.
3901 * @param cls closure
3902 * @param message message
3903 * @param peer peer identity this notification is about
3905 * @return GNUNET_OK to keep the connection open,
3906 * GNUNET_SYSERR to close it (signal serious error)
3909 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3910 const struct GNUNET_MessageHeader *message)
3912 struct GNUNET_MESH_ACK *msg;
3913 struct MeshTunnel *t;
3914 struct MeshFlowControl *fc;
3918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3920 msg = (struct GNUNET_MESH_ACK *) message;
3922 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3926 /* TODO notify that we dont know this tunnel (whom)? */
3927 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3930 ack = ntohl (msg->pid);
3931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3933 /* Is this a forward or backward ACK? */
3934 id = GNUNET_PEER_search (peer);
3935 if (t->next_hop == id)
3938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3941 else if (t->prev_hop == id)
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3949 GNUNET_break_op (0);
3953 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
3954 GMC_is_pid_bigger (ack, fc->last_ack_recv))
3956 GNUNET_SCHEDULER_cancel (fc->poll_task);
3957 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3958 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3960 fc->last_ack_recv = ack;
3961 peer_unlock_queue (id);
3963 if (t->next_hop == id)
3964 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3966 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3973 * Core handler for mesh network traffic point-to-point ack polls.
3975 * @param cls closure
3976 * @param message message
3977 * @param peer peer identity this notification is about
3979 * @return GNUNET_OK to keep the connection open,
3980 * GNUNET_SYSERR to close it (signal serious error)
3983 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3984 const struct GNUNET_MessageHeader *message)
3986 struct GNUNET_MESH_Poll *msg;
3987 struct MeshTunnel *t;
3990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3993 msg = (struct GNUNET_MESH_Poll *) message;
3995 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3999 /* TODO notify that we dont know this tunnel (whom)? */
4000 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4001 GNUNET_break_op (0);
4005 /* Is this a forward or backward ACK? */
4006 id = GNUNET_PEER_search(peer);
4007 if (t->next_hop == id)
4009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4010 t->next_fc.last_pid_recv = ntohl (msg->pid);
4011 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4013 else if (t->prev_hop == id)
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4016 t->prev_fc.last_pid_recv = ntohl (msg->pid);
4017 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4027 * Core handler for mesh keepalives.
4029 * @param cls closure
4030 * @param message message
4031 * @param peer peer identity this notification is about
4032 * @return GNUNET_OK to keep the connection open,
4033 * GNUNET_SYSERR to close it (signal serious error)
4035 * TODO: Check who we got this from, to validate route.
4038 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4039 const struct GNUNET_MessageHeader *message)
4041 struct GNUNET_MESH_TunnelKeepAlive *msg;
4042 struct MeshTunnel *t;
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4047 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4048 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4052 /* TODO notify that we dont know that tunnel */
4053 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4058 tunnel_reset_timeout (t);
4059 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4062 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4063 send_prebuilt_message (message, t->next_hop, t);
4070 * Functions to handle messages from core
4072 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4073 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4074 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4075 sizeof (struct GNUNET_MESH_PathBroken)},
4076 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4077 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4078 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4079 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4080 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK,
4081 sizeof (struct GNUNET_MESH_DataACK)},
4082 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4083 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4084 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4085 sizeof (struct GNUNET_MESH_ACK)},
4086 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4087 sizeof (struct GNUNET_MESH_Poll)},
4088 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4089 sizeof (struct GNUNET_MESH_PathACK)},
4095 /******************************************************************************/
4096 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4097 /******************************************************************************/
4102 * notify_client_connection_failure: notify a client that the connection to the
4103 * requested remote peer is not possible (for instance, no route found)
4104 * Function called when the socket is ready to queue more data. "buf" will be
4105 * NULL and "size" zero if the socket was closed for writing in the meantime.
4107 * @param cls closure
4108 * @param size number of bytes available in buf
4109 * @param buf where the callee should write the message
4110 * @return number of bytes written to buf
4113 notify_client_connection_failure (void *cls, size_t size, void *buf)
4116 struct MeshPeerInfo *peer_info;
4117 struct GNUNET_MESH_PeerControl *msg;
4118 struct GNUNET_PeerIdentity id;
4120 if (0 == size && NULL == buf)
4122 // TODO retry? cancel?
4126 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4127 peer_info = (struct MeshPeerInfo *) cls;
4128 msg = (struct GNUNET_MESH_PeerControl *) buf;
4129 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4130 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4131 // msg->tunnel_id = htonl(peer_info->t->tid);
4132 GNUNET_PEER_resolve (peer_info->id, &id);
4133 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4141 * Send keepalive packets for a tunnel.
4143 * @param cls Closure (tunnel for which to send the keepalive).
4144 * @param tc Notification context.
4146 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4149 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4151 struct MeshTunnel *t = cls;
4152 struct GNUNET_MESH_TunnelKeepAlive *msg;
4153 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4156 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4157 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4158 NULL == t->owner || 0 == t->local_tid)
4163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4164 "sending keepalive for tunnel %d\n", t->id.tid);
4166 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4167 msg->header.size = htons (size);
4168 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4169 msg->oid = my_full_id;
4170 msg->tid = htonl (t->id.tid);
4171 send_prebuilt_message (&msg->header, t->next_hop, t);
4173 t->maintenance_task =
4174 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4179 * Function to process paths received for a new peer addition. The recorded
4180 * paths form the initial tunnel, which can be optimized later.
4181 * Called on each result obtained for the DHT search.
4183 * @param cls closure
4184 * @param exp when will this value expire
4185 * @param key key of the result
4186 * @param get_path path of the get request
4187 * @param get_path_length lenght of get_path
4188 * @param put_path path of the put request
4189 * @param put_path_length length of the put_path
4190 * @param type type of the result
4191 * @param size number of bytes in data
4192 * @param data pointer to the result data
4195 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4196 const struct GNUNET_HashCode * key,
4197 const struct GNUNET_PeerIdentity *get_path,
4198 unsigned int get_path_length,
4199 const struct GNUNET_PeerIdentity *put_path,
4200 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4201 size_t size, const void *data)
4203 struct MeshPeerInfo *peer = cls;
4204 struct MeshPeerPath *p;
4205 struct GNUNET_PeerIdentity pi;
4208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4209 GNUNET_PEER_resolve (peer->id, &pi);
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4212 p = path_build_from_dht (get_path, get_path_length,
4213 put_path, put_path_length);
4214 path_add_to_peers (p, GNUNET_NO);
4216 for (i = 0; i < peer->ntunnels; i++)
4218 struct GNUNET_PeerIdentity id;
4220 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4222 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4223 peer->tunnels[i]->local_tid,
4224 peer->tunnels[i]->local_tid_dest);
4225 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4228 peer_connect (peer, peer->tunnels[i]);
4236 /******************************************************************************/
4237 /********************* MESH LOCAL HANDLES **************************/
4238 /******************************************************************************/
4242 * Handler for client disconnection
4244 * @param cls closure
4245 * @param client identification of the client; NULL
4246 * for the last call when the server is destroyed
4249 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4251 struct MeshClient *c;
4252 struct MeshClient *next;
4254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4264 if (c->handle != client)
4266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4267 " ... searching %p (%u)\n",
4272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4274 GNUNET_SERVER_client_drop (c->handle);
4275 c->shutting_down = GNUNET_YES;
4276 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4277 &tunnel_destroy_iterator, c);
4278 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4279 &tunnel_destroy_iterator, c);
4280 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4281 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4283 if (NULL != c->ports)
4284 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4286 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4289 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4298 * Handler for new clients
4300 * @param cls closure
4301 * @param client identification of the client
4302 * @param message the actual message, which includes messages the client wants
4305 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4306 const struct GNUNET_MessageHeader *message)
4308 struct GNUNET_MESH_ClientConnect *cc_msg;
4309 struct MeshClient *c;
4314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4316 /* Check data sanity */
4317 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4318 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4319 if (0 != (size % sizeof (uint32_t)))
4322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4325 size /= sizeof (uint32_t);
4327 /* Create new client structure */
4328 c = GNUNET_malloc (sizeof (struct MeshClient));
4329 c->id = next_client_id++; /* overflow not important: just for debug */
4330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4333 GNUNET_SERVER_client_keep (client);
4338 p = (uint32_t *) &cc_msg[1];
4339 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4340 for (i = 0; i < size; i++)
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4345 /* store in client's hashmap */
4346 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4347 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4348 /* store in global hashmap */
4349 /* FIXME only allow one client to have the port open,
4350 * have a backup hashmap with waiting clients */
4351 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4352 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4356 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4357 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4358 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4359 GNUNET_SERVER_notification_context_add (nc, client);
4360 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4362 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4368 * Handler for requests of new tunnels
4370 * @param cls Closure.
4371 * @param client Identification of the client.
4372 * @param message The actual message.
4375 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4376 const struct GNUNET_MessageHeader *message)
4378 struct GNUNET_MESH_TunnelMessage *t_msg;
4379 struct MeshPeerInfo *peer_info;
4380 struct MeshTunnel *t;
4381 struct MeshClient *c;
4382 MESH_TunnelNumber tid;
4384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4386 /* Sanity check for client registration */
4387 if (NULL == (c = client_get (client)))
4390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4395 /* Message size sanity check */
4396 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4399 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4403 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4405 GNUNET_i2s (&t_msg->peer));
4406 /* Sanity check for tunnel numbering */
4407 tid = ntohl (t_msg->tunnel_id);
4408 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4414 /* Sanity check for duplicate tunnel IDs */
4415 if (NULL != tunnel_get_by_local_id (c, tid))
4418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4423 while (NULL != tunnel_get_by_pi (myid, next_tid))
4424 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4425 t = tunnel_new (myid, next_tid, c, tid);
4426 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4433 t->port = ntohl (t_msg->port);
4434 tunnel_set_options (t, ntohl (t_msg->options));
4435 if (GNUNET_YES == t->reliable)
4437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4438 t->sent_messages_fwd =
4439 GNUNET_CONTAINER_multihashmap32_create (t->queue_max);
4441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4442 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4444 peer_info = peer_get (&t_msg->peer);
4445 peer_info_add_tunnel (peer_info, t);
4446 peer_connect (peer_info, t);
4447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4453 * Handler for requests of deleting tunnels
4455 * @param cls closure
4456 * @param client identification of the client
4457 * @param message the actual message
4460 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4461 const struct GNUNET_MessageHeader *message)
4463 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4464 struct MeshClient *c;
4465 struct MeshTunnel *t;
4466 MESH_TunnelNumber tid;
4468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4469 "Got a DESTROY TUNNEL from client!\n");
4471 /* Sanity check for client registration */
4472 if (NULL == (c = client_get (client)))
4475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4480 /* Message sanity check */
4481 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4488 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4490 /* Retrieve tunnel */
4491 tid = ntohl (tunnel_msg->tunnel_id);
4492 t = tunnel_get_by_local_id(c, tid);
4495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4501 /* Cleanup after the tunnel */
4502 client_delete_tunnel (c, t);
4509 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4513 /* The tunnel will be destroyed when the last message is transmitted. */
4514 tunnel_destroy_empty (t);
4516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4522 * Handler for client traffic
4524 * @param cls closure
4525 * @param client identification of the client
4526 * @param message the actual message
4529 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4530 const struct GNUNET_MessageHeader *message)
4532 struct GNUNET_MESH_LocalData *data_msg;
4533 struct MeshClient *c;
4534 struct MeshTunnel *t;
4535 struct MeshFlowControl *fc;
4536 MESH_TunnelNumber tid;
4539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4540 "Got data from a client!\n");
4542 /* Sanity check for client registration */
4543 if (NULL == (c = client_get (client)))
4546 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4551 data_msg = (struct GNUNET_MESH_LocalData *) message;
4553 /* Sanity check for message size */
4554 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4555 if (size < sizeof (struct GNUNET_MessageHeader))
4558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4562 /* Tunnel exists? */
4563 tid = ntohl (data_msg->tid);
4564 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4570 t = tunnel_get_by_local_id (c, tid);
4574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4578 /* Is the client in the tunnel? */
4579 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4581 t->owner->handle == client)
4583 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4585 t->client->handle == client) ) )
4588 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4592 /* Ok, everything is correct, send the message
4593 * (pretend we got it from a mesh peer)
4596 struct GNUNET_MESH_Data *payload;
4597 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
4599 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
4600 if (GNUNET_YES == t->reliable)
4602 struct MeshSentMessage *copy;
4604 copy = GNUNET_malloc (sizeof (struct MeshSentMessage)
4605 + sizeof(struct GNUNET_MESH_Data)
4608 copy->id = fc->last_pid_recv + 1;
4609 copy->is_forward = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
4610 copy->retry_timer = MESH_RETRANSMIT_TIME;
4611 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
4612 &tunnel_retransmit_message,
4615 GNUNET_CONTAINER_multihashmap32_put (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
4616 t->sent_messages_fwd : t->sent_messages_bck,
4619 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4622 GNUNET_SCHEDULER_cancel (copy->retry_task);
4624 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4627 payload = (struct GNUNET_MESH_Data *) ©[1];
4631 payload = (struct GNUNET_MESH_Data *) cbuf;
4633 memcpy (&payload[1], &data_msg[1], size);
4634 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
4635 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
4636 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
4637 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4638 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
4639 payload->tid = htonl (t->id.tid);
4640 payload->ttl = htonl (default_ttl);
4641 payload->pid = htonl (fc->last_pid_recv + 1);
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4643 " calling generic handler...\n");
4644 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4645 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
4647 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
4649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4650 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4657 * Handler for client's ACKs for payload traffic.
4659 * @param cls Closure (unused).
4660 * @param client Identification of the client.
4661 * @param message The actual message.
4664 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4665 const struct GNUNET_MessageHeader *message)
4667 struct GNUNET_MESH_LocalAck *msg;
4668 struct MeshTunnel *t;
4669 struct MeshClient *c;
4670 MESH_TunnelNumber tid;
4672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4673 /* Sanity check for client registration */
4674 if (NULL == (c = client_get (client)))
4677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4682 msg = (struct GNUNET_MESH_LocalAck *) message;
4684 /* Tunnel exists? */
4685 tid = ntohl (msg->tunnel_id);
4686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4687 t = tunnel_get_by_local_id (c, tid);
4691 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4692 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4697 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4700 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4701 t->prev_fc.last_ack_recv++;
4702 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4706 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4707 t->next_fc.last_ack_recv++;
4708 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4711 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4719 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4721 * @param cls Closure (client handle).
4722 * @param key Key (hashed tunnel ID, unused).
4723 * @param value Tunnel info.
4725 * @return GNUNET_YES, to keep iterating.
4728 monitor_all_tunnels_iterator (void *cls,
4729 const struct GNUNET_HashCode * key,
4732 struct GNUNET_SERVER_Client *client = cls;
4733 struct MeshTunnel *t = value;
4734 struct GNUNET_MESH_LocalMonitor *msg;
4736 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4737 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4738 msg->tunnel_id = htonl (t->id.tid);
4739 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4740 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4741 GNUNET_PEER_resolve (t->dest, &msg->destination);
4743 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4744 "* sending info about tunnel %s [%u]\n",
4745 GNUNET_i2s (&msg->owner), t->id.tid);
4747 GNUNET_SERVER_notification_context_unicast (nc, client,
4748 &msg->header, GNUNET_NO);
4754 * Handler for client's MONITOR request.
4756 * @param cls Closure (unused).
4757 * @param client Identification of the client.
4758 * @param message The actual message.
4761 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4762 const struct GNUNET_MessageHeader *message)
4764 struct MeshClient *c;
4766 /* Sanity check for client registration */
4767 if (NULL == (c = client_get (client)))
4770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4774 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4775 "Received get tunnels request from client %u\n",
4777 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4778 monitor_all_tunnels_iterator,
4780 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4781 "Get tunnels request from client %u completed\n",
4783 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4788 * Handler for client's MONITOR_TUNNEL request.
4790 * @param cls Closure (unused).
4791 * @param client Identification of the client.
4792 * @param message The actual message.
4795 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4796 const struct GNUNET_MessageHeader *message)
4798 const struct GNUNET_MESH_LocalMonitor *msg;
4799 struct GNUNET_MESH_LocalMonitor *resp;
4800 struct MeshClient *c;
4801 struct MeshTunnel *t;
4803 /* Sanity check for client registration */
4804 if (NULL == (c = client_get (client)))
4807 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4811 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4812 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4813 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4816 ntohl (msg->tunnel_id));
4817 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4820 /* We don't know the tunnel FIXME */
4821 struct GNUNET_MESH_LocalMonitor warn;
4824 GNUNET_SERVER_notification_context_unicast (nc, client,
4827 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4831 /* Initialize context */
4832 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4834 GNUNET_PEER_resolve (t->dest, &resp->destination);
4835 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4836 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4837 &resp->header, GNUNET_NO);
4840 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4841 "Monitor tunnel request from client %u completed\n",
4843 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4848 * Functions to handle messages from clients
4850 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4851 {&handle_local_new_client, NULL,
4852 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4853 {&handle_local_tunnel_create, NULL,
4854 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4855 sizeof (struct GNUNET_MESH_TunnelMessage)},
4856 {&handle_local_tunnel_destroy, NULL,
4857 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4858 sizeof (struct GNUNET_MESH_TunnelMessage)},
4859 {&handle_local_data, NULL,
4860 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
4861 {&handle_local_ack, NULL,
4862 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4863 sizeof (struct GNUNET_MESH_LocalAck)},
4864 {&handle_local_get_tunnels, NULL,
4865 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4866 sizeof (struct GNUNET_MessageHeader)},
4867 {&handle_local_show_tunnel, NULL,
4868 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4869 sizeof (struct GNUNET_MESH_LocalMonitor)},
4875 * Method called whenever a given peer connects.
4877 * @param cls closure
4878 * @param peer peer identity this notification is about
4881 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4883 struct MeshPeerInfo *peer_info;
4884 struct MeshPeerPath *path;
4886 DEBUG_CONN ("Peer connected\n");
4887 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4888 peer_info = peer_get (peer);
4889 if (myid == peer_info->id)
4891 DEBUG_CONN (" (self)\n");
4892 path = path_new (1);
4896 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4897 path = path_new (2);
4898 path->peers[1] = peer_info->id;
4899 GNUNET_PEER_change_rc (peer_info->id, 1);
4900 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4902 path->peers[0] = myid;
4903 GNUNET_PEER_change_rc (myid, 1);
4904 peer_info_add_path (peer_info, path, GNUNET_YES);
4910 * Method called whenever a peer disconnects.
4912 * @param cls closure
4913 * @param peer peer identity this notification is about
4916 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4918 struct MeshPeerInfo *pi;
4919 struct MeshPeerQueue *q;
4920 struct MeshPeerQueue *n;
4922 DEBUG_CONN ("Peer disconnected\n");
4923 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4933 /* TODO try to reroute this traffic instead */
4934 queue_destroy(q, GNUNET_YES);
4937 if (NULL != pi->core_transmit)
4939 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4940 pi->core_transmit = NULL;
4942 peer_remove_path (pi, pi->id, myid);
4945 DEBUG_CONN (" (self)\n");
4947 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4953 * Install server (service) handlers and start listening to clients.
4958 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4959 GNUNET_SERVER_disconnect_notify (server_handle,
4960 &handle_local_client_disconnect, NULL);
4961 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4963 clients_head = NULL;
4964 clients_tail = NULL;
4966 GNUNET_SERVER_resume (server_handle);
4971 * To be called on core init/fail.
4973 * @param cls Closure (config)
4974 * @param server handle to the server for this service
4975 * @param identity the public identity of this peer
4978 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4979 const struct GNUNET_PeerIdentity *identity)
4981 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4985 GNUNET_break (core_handle == server);
4986 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4989 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4990 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4992 GNUNET_i2s (identity));
4993 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4995 GNUNET_i2s (&my_full_id));
4996 GNUNET_CORE_disconnect (core_handle);
4997 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4998 NULL, /* Closure passed to MESH functions */
4999 &core_init, /* Call core_init once connected */
5000 &core_connect, /* Handle connects */
5001 &core_disconnect, /* remove peers on disconnects */
5002 NULL, /* Don't notify about all incoming messages */
5003 GNUNET_NO, /* For header only in notification */
5004 NULL, /* Don't notify about all outbound messages */
5005 GNUNET_NO, /* For header-only out notification */
5006 core_handlers); /* Register these handlers */
5015 /******************************************************************************/
5016 /************************ MAIN FUNCTIONS ****************************/
5017 /******************************************************************************/
5020 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5022 * @param cls closure
5023 * @param key current key code
5024 * @param value value in the hash map
5025 * @return GNUNET_YES if we should continue to iterate,
5029 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5031 struct MeshTunnel *t = value;
5038 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5040 * @param cls closure
5041 * @param key current key code
5042 * @param value value in the hash map
5043 * @return GNUNET_YES if we should continue to iterate,
5047 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5049 struct MeshPeerInfo *p = value;
5050 struct MeshPeerQueue *q;
5051 struct MeshPeerQueue *n;
5059 queue_destroy(q, GNUNET_YES);
5063 peer_info_destroy (p);
5069 * Task run during shutdown.
5075 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5079 if (core_handle != NULL)
5081 GNUNET_CORE_disconnect (core_handle);
5084 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5085 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5086 if (dht_handle != NULL)
5088 GNUNET_DHT_disconnect (dht_handle);
5093 GNUNET_SERVER_notification_context_destroy (nc);
5096 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5098 GNUNET_SCHEDULER_cancel (announce_id_task);
5099 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5106 * Process mesh requests.
5108 * @param cls closure
5109 * @param server the initialized server
5110 * @param c configuration to use
5113 run (void *cls, struct GNUNET_SERVER_Handle *server,
5114 const struct GNUNET_CONFIGURATION_Handle *c)
5117 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5120 server_handle = server;
5121 GNUNET_SERVER_suspend (server_handle);
5124 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5129 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5130 "mesh", "peer/privatekey");
5131 GNUNET_SCHEDULER_shutdown ();
5136 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5137 &refresh_path_time))
5139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5141 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5142 "mesh", "refresh path time");
5143 GNUNET_SCHEDULER_shutdown ();
5148 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5153 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5154 "mesh", "id announce time");
5155 GNUNET_SCHEDULER_shutdown ();
5160 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5165 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5166 "mesh", "connect timeout");
5167 GNUNET_SCHEDULER_shutdown ();
5172 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5177 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5178 "mesh", "max msgs queue");
5179 GNUNET_SCHEDULER_shutdown ();
5184 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5189 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5190 "mesh", "max tunnels");
5191 GNUNET_SCHEDULER_shutdown ();
5196 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5201 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5202 "mesh", "default ttl", 64);
5207 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5210 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5211 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5212 "mesh", "max peers", 1000);
5217 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5225 "Mesh is running with drop mode enabled. "
5226 "This is NOT a good idea! "
5227 "Remove the DROP_PERCENT option from your configuration.\n");
5231 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5232 &dht_replication_level))
5234 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5236 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5237 "mesh", "dht replication level", 3);
5238 dht_replication_level = 3;
5241 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5242 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5243 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5244 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5246 dht_handle = GNUNET_DHT_connect (c, 64);
5247 if (NULL == dht_handle)
5251 stats = GNUNET_STATISTICS_create ("mesh", c);
5253 /* Scheduled the task to clean up when shutdown is called */
5254 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5256 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5257 GNUNET_free (keyfile);
5258 GNUNET_assert (NULL != pk);
5259 my_private_key = pk;
5260 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5261 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5262 &my_full_id.hashPubKey);
5263 myid = GNUNET_PEER_intern (&my_full_id);
5264 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5265 "Mesh for peer [%s] starting\n",
5266 GNUNET_i2s(&my_full_id));
5268 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5269 NULL, /* Closure passed to MESH functions */
5270 &core_init, /* Call core_init once connected */
5271 &core_connect, /* Handle connects */
5272 &core_disconnect, /* remove peers on disconnects */
5273 NULL, /* Don't notify about all incoming messages */
5274 GNUNET_NO, /* For header only in notification */
5275 NULL, /* Don't notify about all outbound messages */
5276 GNUNET_NO, /* For header-only out notification */
5277 core_handlers); /* Register these handlers */
5278 if (NULL == core_handle)
5281 GNUNET_SCHEDULER_shutdown ();
5285 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5286 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5292 * The main function for the mesh service.
5294 * @param argc number of arguments from the command line
5295 * @param argv command line arguments
5296 * @return 0 ok, 1 on error
5299 main (int argc, char *const *argv)
5304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5305 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5307 ret = (GNUNET_OK == r) ? 0 : 1;
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5312 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5313 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5314 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);