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;
2658 t->reliable = options & GNUNET_MESH_OPTION_RELIABLE;
2663 * Iterator for deleting each tunnel whose client endpoint disconnected.
2665 * @param cls Closure (client that has disconnected).
2666 * @param key The local tunnel id (used to access the hashmap).
2667 * @param value The value stored at the key (tunnel to destroy).
2669 * @return GNUNET_OK, keep iterating.
2672 tunnel_destroy_iterator (void *cls,
2676 struct MeshTunnel *t = value;
2677 struct MeshClient *c = cls;
2679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2680 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2681 t->local_tid, t->local_tid_dest, c->id);
2682 client_delete_tunnel (c, t);
2685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2687 if (0 != t->next_hop) { /* destroy could come before a path is used */
2688 GNUNET_PEER_change_rc (t->next_hop, -1);
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2696 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2697 GNUNET_PEER_change_rc (t->prev_hop, -1);
2702 tunnel_destroy_empty (t);
2709 * Timeout function, destroys tunnel if called
2711 * @param cls Closure (tunnel to destroy).
2712 * @param tc TaskContext
2715 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2717 struct MeshTunnel *t = cls;
2718 struct GNUNET_PeerIdentity id;
2720 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2721 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2723 GNUNET_PEER_resolve(t->id.oid, &id);
2724 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2725 "Tunnel %s [%X] timed out. Destroying.\n",
2726 GNUNET_i2s(&id), t->id.tid);
2727 if (NULL != t->client)
2728 send_client_tunnel_destroy (t->client, t);
2729 tunnel_destroy (t); /* Do not notify other */
2734 * Resets the tunnel timeout. Starts it if no timeout was running.
2736 * @param t Tunnel whose timeout to reset.
2738 * TODO use heap to improve efficiency of scheduler.
2741 tunnel_reset_timeout (struct MeshTunnel *t)
2743 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2745 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2746 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2747 t->maintenance_task =
2748 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2749 (refresh_path_time, 4), &tunnel_timeout, t);
2753 /******************************************************************************/
2754 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2755 /******************************************************************************/
2758 * Function to send a create path packet to a peer.
2760 * @param cls closure
2761 * @param size number of bytes available in buf
2762 * @param buf where the callee should write the message
2763 * @return number of bytes written to buf
2766 send_core_path_create (void *cls, size_t size, void *buf)
2768 struct MeshTunnel *t = cls;
2769 struct GNUNET_MESH_CreateTunnel *msg;
2770 struct GNUNET_PeerIdentity *peer_ptr;
2771 struct MeshPeerPath *p = t->path;
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2778 sizeof (struct GNUNET_MESH_CreateTunnel) +
2779 p->length * sizeof (struct GNUNET_PeerIdentity);
2781 if (size < size_needed || NULL == buf)
2786 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2787 msg->header.size = htons (size_needed);
2788 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2789 msg->tid = ntohl (t->id.tid);
2792 if (GNUNET_YES == t->nobuffer)
2793 opt |= GNUNET_MESH_OPTION_NOBUFFER;
2794 if (GNUNET_YES == t->reliable)
2795 opt |= GNUNET_MESH_OPTION_RELIABLE;
2796 msg->opt = htonl (opt);
2797 msg->port = htonl (t->port);
2799 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2800 for (i = 0; i < p->length; i++)
2802 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2806 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2812 * Creates a path ack message in buf and frees all unused resources.
2814 * @param cls closure (MeshTransmissionDescriptor)
2815 * @param size number of bytes available in buf
2816 * @param buf where the callee should write the message
2817 * @return number of bytes written to buf
2820 send_core_path_ack (void *cls, size_t size, void *buf)
2822 struct MeshTunnel *t = cls;
2823 struct GNUNET_MESH_PathACK *msg = buf;
2825 GNUNET_assert (NULL != t);
2826 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2831 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2832 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2833 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2834 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2835 msg->tid = htonl (t->id.tid);
2836 msg->peer_id = my_full_id;
2837 msg->ack = htonl (t->prev_fc.last_ack_sent);
2839 /* TODO add signature */
2841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2842 return sizeof (struct GNUNET_MESH_PathACK);
2847 * Free a transmission that was already queued with all resources
2848 * associated to the request.
2850 * @param queue Queue handler to cancel.
2851 * @param clear_cls Is it necessary to free associated cls?
2854 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2856 struct MeshFlowControl *fc;
2858 if (GNUNET_YES == clear_cls)
2860 switch (queue->type)
2862 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2863 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2864 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2866 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2867 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2868 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2869 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2870 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2872 " prebuilt message\n");
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2875 GNUNET_MESH_DEBUG_M2S (queue->type));
2877 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2883 " type %s unknown!\n",
2884 GNUNET_MESH_DEBUG_M2S (queue->type));
2886 GNUNET_free_non_null (queue->cls);
2888 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2889 queue->peer->queue_tail,
2892 /* Delete from appropriate fc in the tunnel */
2893 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
2894 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
2896 if (queue->peer->id == queue->tunnel->prev_hop)
2897 fc = &queue->tunnel->prev_fc;
2898 else if (queue->peer->id == queue->tunnel->next_hop)
2899 fc = &queue->tunnel->next_fc;
2907 GNUNET_free (queue);
2912 * @brief Get the next transmittable message from the queue.
2914 * This will be the head, except in the case of being a data packet
2915 * not allowed by the destination peer.
2917 * @param peer Destination peer.
2919 * @return The next viable MeshPeerQueue element to send to that peer.
2920 * NULL when there are no transmittable messages.
2922 struct MeshPeerQueue *
2923 queue_get_next (const struct MeshPeerInfo *peer)
2925 struct MeshPeerQueue *q;
2927 struct GNUNET_MESH_Data *dmsg;
2928 struct MeshTunnel* t;
2932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2933 for (q = peer->queue_head; NULL != q; q = q->next)
2936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2938 GNUNET_MESH_DEBUG_M2S (q->type));
2939 dmsg = (struct GNUNET_MESH_Data *) q->cls;
2940 pid = ntohl (dmsg->pid);
2943 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2944 ack = t->next_fc.last_ack_recv;
2946 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2947 ack = t->prev_fc.last_ack_recv;
2950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2955 "* ACK: %u, PID: %u\n",
2957 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2970 "* nothing found\n");
2976 queue_send (void *cls, size_t size, void *buf)
2978 struct MeshPeerInfo *peer = cls;
2979 struct GNUNET_MessageHeader *msg;
2980 struct MeshPeerQueue *queue;
2981 struct MeshTunnel *t;
2982 struct GNUNET_PeerIdentity dst_id;
2983 struct MeshFlowControl *fc;
2988 peer->core_transmit = NULL;
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
2991 queue = queue_get_next (peer);
2993 /* Queue has no internal mesh traffic nor sendable payload */
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
2997 if (NULL == peer->queue_head)
2998 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3003 GNUNET_PEER_resolve (peer->id, &dst_id);
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3006 GNUNET_i2s (&dst_id));
3007 /* Check if buffer size is enough for the message */
3008 if (queue->size > size)
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3011 "* not enough room, reissue\n");
3012 peer->core_transmit =
3013 GNUNET_CORE_notify_transmit_ready (core_handle,
3016 GNUNET_TIME_UNIT_FOREVER_REL,
3023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3026 GNUNET_assert (0 < t->pending_messages);
3027 t->pending_messages--;
3031 switch (queue->type)
3034 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3035 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3036 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3037 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3038 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3039 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3042 GNUNET_MESH_DEBUG_M2S (queue->type));
3044 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3045 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3046 data_size = send_core_data_raw (queue->cls, size, buf);
3047 msg = (struct GNUNET_MessageHeader *) buf;
3048 type = ntohs (msg->type);
3050 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3052 data_size = send_core_path_create (queue->cls, size, buf);
3054 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3056 data_size = send_core_path_ack (queue->cls, size, buf);
3060 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3061 "* type unknown: %u\n",
3066 if (0 < drop_percent &&
3067 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3070 "Dropping message of type %s\n",
3071 GNUNET_MESH_DEBUG_M2S(queue->type));
3074 /* Free queue, but cls was freed by send_core_* */
3075 queue_destroy (queue, GNUNET_NO);
3077 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3078 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3082 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3083 t->next_fc.last_pid_sent = pid;
3084 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3086 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3087 t->prev_fc.last_pid_sent = pid;
3088 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3094 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3100 /* If more data in queue, send next */
3101 queue = queue_get_next (peer);
3104 struct GNUNET_PeerIdentity id;
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3107 GNUNET_PEER_resolve (peer->id, &id);
3108 peer->core_transmit =
3109 GNUNET_CORE_notify_transmit_ready(core_handle,
3112 GNUNET_TIME_UNIT_FOREVER_REL,
3118 else if (NULL != peer->queue_head)
3120 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3122 GNUNET_i2s (&my_full_id));
3123 if (peer->id == t->next_hop)
3125 else if (peer->id == t->prev_hop)
3132 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3135 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3145 * @brief Queue and pass message to core when possible.
3147 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3148 * accounts for it. In case the queue is full, the message is dropped and
3151 * Otherwise, message is treated as internal and allowed to go regardless of
3154 * @param cls Closure (@c type dependant). It will be used by queue_send to
3155 * build the message to be sent if not already prebuilt.
3156 * @param type Type of the message, 0 for a raw message.
3157 * @param size Size of the message.
3158 * @param dst Neighbor to send message to.
3159 * @param t Tunnel this message belongs to.
3162 queue_add (void *cls, uint16_t type, size_t size,
3163 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3165 struct MeshPeerQueue *queue;
3166 struct GNUNET_PeerIdentity id;
3170 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3172 n = &t->next_fc.queue_n;
3174 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3176 n = &t->prev_fc.queue_n;
3180 if (*n >= t->queue_max)
3183 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3184 "queue full: %u/%u\n",
3186 GNUNET_STATISTICS_update(stats,
3187 "# messages dropped (buffer full)",
3189 return; /* Drop message */
3193 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3199 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3200 if (NULL == dst->core_transmit)
3202 GNUNET_PEER_resolve (dst->id, &id);
3203 dst->core_transmit =
3204 GNUNET_CORE_notify_transmit_ready (core_handle,
3207 GNUNET_TIME_UNIT_FOREVER_REL,
3213 t->pending_messages++;
3217 /******************************************************************************/
3218 /******************** MESH NETWORK HANDLERS **************************/
3219 /******************************************************************************/
3223 * Core handler for path creation
3225 * @param cls closure
3226 * @param message message
3227 * @param peer peer identity this notification is about
3229 * @return GNUNET_OK to keep the connection open,
3230 * GNUNET_SYSERR to close it (signal serious error)
3233 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3234 const struct GNUNET_MessageHeader *message)
3236 unsigned int own_pos;
3239 MESH_TunnelNumber tid;
3240 struct GNUNET_MESH_CreateTunnel *msg;
3241 struct GNUNET_PeerIdentity *pi;
3242 struct MeshPeerPath *path;
3243 struct MeshPeerInfo *dest_peer_info;
3244 struct MeshPeerInfo *orig_peer_info;
3245 struct MeshTunnel *t;
3247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3248 "Received a path create msg [%s]\n",
3249 GNUNET_i2s (&my_full_id));
3250 size = ntohs (message->size);
3251 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3253 GNUNET_break_op (0);
3257 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3258 if (size % sizeof (struct GNUNET_PeerIdentity))
3260 GNUNET_break_op (0);
3263 size /= sizeof (struct GNUNET_PeerIdentity);
3266 GNUNET_break_op (0);
3269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3270 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3272 tid = ntohl (msg->tid);
3273 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3275 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3276 t = tunnel_get (pi, tid);
3277 if (NULL == t) /* might be a local tunnel */
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3282 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3288 t->port = ntohl (msg->port);
3289 opt = ntohl (msg->opt);
3290 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3292 t->nobuffer = GNUNET_YES;
3295 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3297 t->reliable = GNUNET_YES;
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3301 tunnel_reset_timeout (t);
3303 t->state = MESH_TUNNEL_WAITING;
3305 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3306 if (NULL == dest_peer_info)
3308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3309 " Creating PeerInfo for destination.\n");
3310 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3311 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3312 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3316 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3317 if (NULL == orig_peer_info)
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3320 " Creating PeerInfo for origin.\n");
3321 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3322 orig_peer_info->id = GNUNET_PEER_intern (pi);
3323 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3324 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3327 path = path_new (size);
3329 for (i = 0; i < size; i++)
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3332 GNUNET_i2s (&pi[i]));
3333 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3334 if (path->peers[i] == myid)
3337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3338 if (own_pos == 0 && path->peers[own_pos] != myid)
3340 /* create path: self not found in path through self */
3341 GNUNET_break_op (0);
3342 path_destroy (path);
3346 path_add_to_peers (path, GNUNET_NO);
3347 tunnel_use_path (t, path);
3349 peer_info_add_tunnel (dest_peer_info, t);
3351 if (own_pos == size - 1)
3353 struct MeshClient *c;
3355 /* Find target client */
3356 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3359 /* TODO send reject */
3363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3364 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3366 /* Assign local tid */
3367 while (NULL != tunnel_get_incoming (next_local_tid))
3368 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3369 t->local_tid_dest = next_local_tid++;
3370 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3372 if (GNUNET_YES == t->reliable)
3373 t->sent_messages_bck =
3374 GNUNET_CONTAINER_multihashmap32_create (t->queue_max);
3376 tunnel_add_client (t, c);
3377 send_client_tunnel_create (t);
3382 struct MeshPeerPath *path2;
3384 t->next_hop = path->peers[own_pos + 1];
3385 GNUNET_PEER_change_rc(t->next_hop, 1);
3387 /* It's for somebody else! Retransmit. */
3388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3389 path2 = path_duplicate (path);
3390 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3391 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3392 send_create_path (t);
3400 * Core handler for path ACKs
3402 * @param cls closure
3403 * @param message message
3404 * @param peer peer identity this notification is about
3406 * @return GNUNET_OK to keep the connection open,
3407 * GNUNET_SYSERR to close it (signal serious error)
3410 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3411 const struct GNUNET_MessageHeader *message)
3413 struct GNUNET_MESH_PathACK *msg;
3414 struct MeshPeerInfo *peer_info;
3415 struct MeshPeerPath *p;
3416 struct MeshTunnel *t;
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3419 GNUNET_i2s (&my_full_id));
3420 msg = (struct GNUNET_MESH_PathACK *) message;
3421 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3424 /* TODO notify that we don't know the tunnel */
3425 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3427 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3431 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3433 peer_info = peer_get (&msg->peer_id);
3434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3435 GNUNET_i2s (&msg->peer_id));
3436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3439 /* Add path to peers? */
3443 path_add_to_peers (p, GNUNET_YES);
3449 t->state = MESH_TUNNEL_READY;
3450 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3451 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3453 /* Message for us? */
3454 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3457 if (NULL == t->owner)
3459 GNUNET_break_op (0);
3462 if (NULL != peer_info->dhtget)
3464 GNUNET_DHT_get_stop (peer_info->dhtget);
3465 peer_info->dhtget = NULL;
3467 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3468 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3473 " not for us, retransmitting...\n");
3474 peer_info = peer_get (&msg->oid);
3475 send_prebuilt_message (message, t->prev_hop, t);
3481 * Core handler for notifications of broken paths
3483 * @param cls closure
3484 * @param message message
3485 * @param peer peer identity this notification is about
3487 * @return GNUNET_OK to keep the connection open,
3488 * GNUNET_SYSERR to close it (signal serious error)
3491 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3492 const struct GNUNET_MessageHeader *message)
3494 struct GNUNET_MESH_PathBroken *msg;
3495 struct MeshTunnel *t;
3497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3498 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3499 msg = (struct GNUNET_MESH_PathBroken *) message;
3500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3501 GNUNET_i2s (&msg->peer1));
3502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3503 GNUNET_i2s (&msg->peer2));
3504 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3507 GNUNET_break_op (0);
3510 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3511 GNUNET_PEER_search (&msg->peer2));
3518 * Core handler for tunnel destruction
3520 * @param cls closure
3521 * @param message message
3522 * @param peer peer identity this notification is about
3524 * @return GNUNET_OK to keep the connection open,
3525 * GNUNET_SYSERR to close it (signal serious error)
3528 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3529 const struct GNUNET_MessageHeader *message)
3531 struct GNUNET_MESH_TunnelDestroy *msg;
3532 struct MeshTunnel *t;
3534 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3536 "Got a TUNNEL DESTROY packet from %s\n",
3538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3539 " for tunnel %s [%u]\n",
3540 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3541 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3544 /* Probably already got the message from another path,
3545 * destroyed the tunnel and retransmitted to children.
3548 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3552 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3555 t->local_tid, t->local_tid_dest);
3557 if (GNUNET_PEER_search (peer) == t->prev_hop)
3559 // TODO check owner's signature
3560 // TODO add owner's signatue to tunnel for retransmission
3561 peer_cancel_queues (t->prev_hop, t);
3562 GNUNET_PEER_change_rc (t->prev_hop, -1);
3565 else if (GNUNET_PEER_search (peer) == t->next_hop)
3567 // TODO check dest's signature
3568 // TODO add dest's signatue to tunnel for retransmission
3569 peer_cancel_queues (t->next_hop, t);
3570 GNUNET_PEER_change_rc (t->next_hop, -1);
3575 GNUNET_break_op (0);
3576 // TODO check both owner AND destination's signature to see which matches
3577 // TODO restransmit in appropriate direction
3580 tunnel_destroy_empty (t);
3582 // TODO: add timeout to destroy the tunnel anyway
3588 * Core handler for mesh network traffic going from the origin to a peer
3590 * @param cls closure
3591 * @param peer peer identity this notification is about
3592 * @param message message
3593 * @return GNUNET_OK to keep the connection open,
3594 * GNUNET_SYSERR to close it (signal serious error)
3597 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3598 const struct GNUNET_MessageHeader *message)
3600 struct GNUNET_MESH_Data *msg;
3601 struct MeshTunnel *t;
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3609 size = ntohs (message->size);
3611 sizeof (struct GNUNET_MESH_Data) +
3612 sizeof (struct GNUNET_MessageHeader))
3617 msg = (struct GNUNET_MESH_Data *) message;
3618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3619 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3621 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3624 /* TODO notify back: we don't know this tunnel */
3625 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3626 GNUNET_break_op (0);
3629 pid = ntohl (msg->pid);
3630 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3632 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3633 GNUNET_break_op (0);
3634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3635 "Received PID %u, ACK %u\n",
3636 pid, t->prev_fc.last_ack_sent);
3637 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3641 tunnel_reset_timeout (t);
3642 if (t->dest == myid)
3644 /* TODO signature verification */
3645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3646 " it's for us! sending to clients...\n");
3647 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3648 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
3649 if ( (GNUNET_NO == t->reliable &&
3650 GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv))
3652 (GNUNET_YES == t->reliable &&
3653 pid == t->prev_fc.last_pid_recv + 1) )
3655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3656 " pid %u not seen yet, forwarding\n", pid);
3657 t->prev_fc.last_pid_recv = pid;
3658 tunnel_send_client_ucast (t, msg);
3659 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3663 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
3664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3665 " Pid %u not expected (%u), sending FWD ACK!\n",
3666 pid, t->prev_fc.last_pid_recv + 1);
3667 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3671 t->prev_fc.last_pid_recv = pid;
3672 if (0 == t->next_hop)
3677 ttl = ntohl (msg->ttl);
3678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3681 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3682 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3683 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3687 " not for us, retransmitting...\n");
3689 send_prebuilt_message (message, t->next_hop, t);
3690 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3696 * Core handler for mesh network traffic toward the owner of a tunnel
3698 * @param cls closure
3699 * @param message message
3700 * @param peer peer identity this notification is about
3702 * @return GNUNET_OK to keep the connection open,
3703 * GNUNET_SYSERR to close it (signal serious error)
3706 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3707 const struct GNUNET_MessageHeader *message)
3709 struct GNUNET_MESH_Data *msg;
3710 struct MeshTunnel *t;
3715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3717 size = ntohs (message->size);
3718 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
3719 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3721 GNUNET_break_op (0);
3724 msg = (struct GNUNET_MESH_Data *) message;
3725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3726 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3727 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3728 pid = ntohl (msg->pid);
3731 /* TODO notify that we dont know this tunnel (whom)? */
3732 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3734 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3735 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3739 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3741 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3742 GNUNET_break_op (0);
3743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3744 "Received PID %u, ACK %u\n",
3745 pid, t->next_fc.last_ack_sent);
3746 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3750 if (myid == t->id.oid)
3752 /* TODO signature verification */
3753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3754 " it's for us! sending to clients...\n");
3755 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3756 if ( (GNUNET_NO == t->reliable &&
3757 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
3759 (GNUNET_YES == t->reliable &&
3760 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
3762 t->next_fc.last_pid_recv = pid;
3763 tunnel_send_client_to_orig (t, msg);
3764 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3768 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3769 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3770 " Pid %u not expected, sending FWD ACK!\n", pid);
3771 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3776 " not for us, retransmitting...\n");
3777 t->next_fc.last_pid_recv = pid;
3778 if (0 == t->prev_hop) /* No owner AND no prev hop */
3780 if (GNUNET_YES == t->destroy)
3782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3783 "to orig received on a dying tunnel %s [%X]\n",
3784 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3787 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3788 "unknown to origin at %s\n",
3789 GNUNET_i2s (&my_full_id));
3790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3794 "on tunnel %s [%X]\n",
3795 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3798 ttl = ntohl (msg->ttl);
3799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3802 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3803 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3804 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3807 send_prebuilt_message (message, t->prev_hop, t);
3808 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3815 * Core handler for mesh network traffic end-to-end ACKs.
3817 * @param cls Closure.
3818 * @param message Message.
3819 * @param peer Peer identity this notification is about.
3821 * @return GNUNET_OK to keep the connection open,
3822 * GNUNET_SYSERR to close it (signal serious error)
3825 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3826 const struct GNUNET_MessageHeader *message)
3828 struct GNUNET_MESH_DataACK *msg;
3829 struct GNUNET_CONTAINER_MultiHashMap32 *hm;
3830 struct MeshSentMessage *copy;
3831 struct MeshTunnel *t;
3835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a DATA ACK message from %s!\n",
3837 msg = (struct GNUNET_MESH_DataACK *) message;
3839 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3842 /* TODO notify that we dont know this tunnel (whom)? */
3843 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3846 ack = ntohl (msg->pid);
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3849 /* Is this a forward or backward ACK? */
3850 id = GNUNET_PEER_search (peer);
3851 if (t->next_hop == id)
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3854 if (NULL == t->owner)
3856 send_prebuilt_message (message, t->prev_hop, t);
3859 hm = t->sent_messages_fwd;
3860 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3862 else if (t->prev_hop == id)
3864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3865 if (NULL == t->client)
3867 send_prebuilt_message (message, t->next_hop, t);
3870 hm = t->sent_messages_bck;
3871 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3874 GNUNET_break_op (0);
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK'ed \n");
3877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", ack);
3878 copy = GNUNET_CONTAINER_multihashmap32_get (hm, ack);
3881 GNUNET_break (0); // FIXME needed?
3884 GNUNET_break (GNUNET_YES ==
3885 GNUNET_CONTAINER_multihashmap32_remove (hm, ack, copy));
3886 if (GNUNET_SCHEDULER_NO_TASK != copy->retry_task)
3888 GNUNET_SCHEDULER_cancel (copy->retry_task);
3897 * Core handler for mesh network traffic point-to-point acks.
3899 * @param cls closure
3900 * @param message message
3901 * @param peer peer identity this notification is about
3903 * @return GNUNET_OK to keep the connection open,
3904 * GNUNET_SYSERR to close it (signal serious error)
3907 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3908 const struct GNUNET_MessageHeader *message)
3910 struct GNUNET_MESH_ACK *msg;
3911 struct MeshTunnel *t;
3912 struct MeshFlowControl *fc;
3916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3918 msg = (struct GNUNET_MESH_ACK *) message;
3920 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3924 /* TODO notify that we dont know this tunnel (whom)? */
3925 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3928 ack = ntohl (msg->pid);
3929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3931 /* Is this a forward or backward ACK? */
3932 id = GNUNET_PEER_search (peer);
3933 if (t->next_hop == id)
3936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3939 else if (t->prev_hop == id)
3942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3947 GNUNET_break_op (0);
3951 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
3952 GMC_is_pid_bigger (ack, fc->last_ack_recv))
3954 GNUNET_SCHEDULER_cancel (fc->poll_task);
3955 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3956 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3958 fc->last_ack_recv = ack;
3959 peer_unlock_queue (id);
3961 if (t->next_hop == id)
3962 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3964 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3971 * Core handler for mesh network traffic point-to-point ack polls.
3973 * @param cls closure
3974 * @param message message
3975 * @param peer peer identity this notification is about
3977 * @return GNUNET_OK to keep the connection open,
3978 * GNUNET_SYSERR to close it (signal serious error)
3981 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3982 const struct GNUNET_MessageHeader *message)
3984 struct GNUNET_MESH_Poll *msg;
3985 struct MeshTunnel *t;
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3991 msg = (struct GNUNET_MESH_Poll *) message;
3993 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3997 /* TODO notify that we dont know this tunnel (whom)? */
3998 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3999 GNUNET_break_op (0);
4003 /* Is this a forward or backward ACK? */
4004 id = GNUNET_PEER_search(peer);
4005 if (t->next_hop == id)
4007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4008 t->next_fc.last_pid_recv = ntohl (msg->pid);
4009 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4011 else if (t->prev_hop == id)
4013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4014 t->prev_fc.last_pid_recv = ntohl (msg->pid);
4015 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4025 * Core handler for mesh keepalives.
4027 * @param cls closure
4028 * @param message message
4029 * @param peer peer identity this notification is about
4030 * @return GNUNET_OK to keep the connection open,
4031 * GNUNET_SYSERR to close it (signal serious error)
4033 * TODO: Check who we got this from, to validate route.
4036 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4037 const struct GNUNET_MessageHeader *message)
4039 struct GNUNET_MESH_TunnelKeepAlive *msg;
4040 struct MeshTunnel *t;
4042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4045 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4046 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4050 /* TODO notify that we dont know that tunnel */
4051 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4056 tunnel_reset_timeout (t);
4057 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4060 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4061 send_prebuilt_message (message, t->next_hop, t);
4068 * Functions to handle messages from core
4070 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4071 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4072 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4073 sizeof (struct GNUNET_MESH_PathBroken)},
4074 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4075 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4076 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4077 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4078 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK,
4079 sizeof (struct GNUNET_MESH_DataACK)},
4080 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4081 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4082 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4083 sizeof (struct GNUNET_MESH_ACK)},
4084 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4085 sizeof (struct GNUNET_MESH_Poll)},
4086 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4087 sizeof (struct GNUNET_MESH_PathACK)},
4093 /******************************************************************************/
4094 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4095 /******************************************************************************/
4100 * notify_client_connection_failure: notify a client that the connection to the
4101 * requested remote peer is not possible (for instance, no route found)
4102 * Function called when the socket is ready to queue more data. "buf" will be
4103 * NULL and "size" zero if the socket was closed for writing in the meantime.
4105 * @param cls closure
4106 * @param size number of bytes available in buf
4107 * @param buf where the callee should write the message
4108 * @return number of bytes written to buf
4111 notify_client_connection_failure (void *cls, size_t size, void *buf)
4114 struct MeshPeerInfo *peer_info;
4115 struct GNUNET_MESH_PeerControl *msg;
4116 struct GNUNET_PeerIdentity id;
4118 if (0 == size && NULL == buf)
4120 // TODO retry? cancel?
4124 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4125 peer_info = (struct MeshPeerInfo *) cls;
4126 msg = (struct GNUNET_MESH_PeerControl *) buf;
4127 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4128 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4129 // msg->tunnel_id = htonl(peer_info->t->tid);
4130 GNUNET_PEER_resolve (peer_info->id, &id);
4131 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4139 * Send keepalive packets for a tunnel.
4141 * @param cls Closure (tunnel for which to send the keepalive).
4142 * @param tc Notification context.
4144 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4147 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4149 struct MeshTunnel *t = cls;
4150 struct GNUNET_MESH_TunnelKeepAlive *msg;
4151 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4154 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4155 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4156 NULL == t->owner || 0 == t->local_tid)
4161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4162 "sending keepalive for tunnel %d\n", t->id.tid);
4164 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4165 msg->header.size = htons (size);
4166 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4167 msg->oid = my_full_id;
4168 msg->tid = htonl (t->id.tid);
4169 send_prebuilt_message (&msg->header, t->next_hop, t);
4171 t->maintenance_task =
4172 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4177 * Function to process paths received for a new peer addition. The recorded
4178 * paths form the initial tunnel, which can be optimized later.
4179 * Called on each result obtained for the DHT search.
4181 * @param cls closure
4182 * @param exp when will this value expire
4183 * @param key key of the result
4184 * @param get_path path of the get request
4185 * @param get_path_length lenght of get_path
4186 * @param put_path path of the put request
4187 * @param put_path_length length of the put_path
4188 * @param type type of the result
4189 * @param size number of bytes in data
4190 * @param data pointer to the result data
4193 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4194 const struct GNUNET_HashCode * key,
4195 const struct GNUNET_PeerIdentity *get_path,
4196 unsigned int get_path_length,
4197 const struct GNUNET_PeerIdentity *put_path,
4198 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4199 size_t size, const void *data)
4201 struct MeshPeerInfo *peer = cls;
4202 struct MeshPeerPath *p;
4203 struct GNUNET_PeerIdentity pi;
4206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4207 GNUNET_PEER_resolve (peer->id, &pi);
4208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4210 p = path_build_from_dht (get_path, get_path_length,
4211 put_path, put_path_length);
4212 path_add_to_peers (p, GNUNET_NO);
4214 for (i = 0; i < peer->ntunnels; i++)
4216 struct GNUNET_PeerIdentity id;
4218 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4220 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4221 peer->tunnels[i]->local_tid,
4222 peer->tunnels[i]->local_tid_dest);
4223 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4226 peer_connect (peer, peer->tunnels[i]);
4234 /******************************************************************************/
4235 /********************* MESH LOCAL HANDLES **************************/
4236 /******************************************************************************/
4240 * Handler for client disconnection
4242 * @param cls closure
4243 * @param client identification of the client; NULL
4244 * for the last call when the server is destroyed
4247 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4249 struct MeshClient *c;
4250 struct MeshClient *next;
4252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4262 if (c->handle != client)
4264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4265 " ... searching %p (%u)\n",
4270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4272 GNUNET_SERVER_client_drop (c->handle);
4273 c->shutting_down = GNUNET_YES;
4274 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4275 &tunnel_destroy_iterator, c);
4276 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4277 &tunnel_destroy_iterator, c);
4278 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4279 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4281 if (NULL != c->ports)
4282 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4284 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4287 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4296 * Handler for new clients
4298 * @param cls closure
4299 * @param client identification of the client
4300 * @param message the actual message, which includes messages the client wants
4303 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4304 const struct GNUNET_MessageHeader *message)
4306 struct GNUNET_MESH_ClientConnect *cc_msg;
4307 struct MeshClient *c;
4312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4314 /* Check data sanity */
4315 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4316 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4317 if (0 != (size % sizeof (uint32_t)))
4320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4323 size /= sizeof (uint32_t);
4325 /* Create new client structure */
4326 c = GNUNET_malloc (sizeof (struct MeshClient));
4327 c->id = next_client_id++; /* overflow not important: just for debug */
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4331 GNUNET_SERVER_client_keep (client);
4336 p = (uint32_t *) &cc_msg[1];
4337 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4338 for (i = 0; i < size; i++)
4341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4343 /* store in client's hashmap */
4344 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4345 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4346 /* store in global hashmap */
4347 /* FIXME only allow one client to have the port open,
4348 * have a backup hashmap with waiting clients */
4349 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4350 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4354 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4355 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4356 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4357 GNUNET_SERVER_notification_context_add (nc, client);
4358 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4360 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4366 * Handler for requests of new tunnels
4368 * @param cls Closure.
4369 * @param client Identification of the client.
4370 * @param message The actual message.
4373 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4374 const struct GNUNET_MessageHeader *message)
4376 struct GNUNET_MESH_TunnelMessage *t_msg;
4377 struct MeshPeerInfo *peer_info;
4378 struct MeshTunnel *t;
4379 struct MeshClient *c;
4380 MESH_TunnelNumber tid;
4382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4384 /* Sanity check for client registration */
4385 if (NULL == (c = client_get (client)))
4388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4393 /* Message size sanity check */
4394 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4401 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4403 GNUNET_i2s (&t_msg->peer));
4404 /* Sanity check for tunnel numbering */
4405 tid = ntohl (t_msg->tunnel_id);
4406 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4412 /* Sanity check for duplicate tunnel IDs */
4413 if (NULL != tunnel_get_by_local_id (c, tid))
4416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4421 while (NULL != tunnel_get_by_pi (myid, next_tid))
4422 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4423 t = tunnel_new (myid, next_tid, c, tid);
4424 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4431 t->port = ntohl (t_msg->port);
4432 tunnel_set_options (t, ntohl (t_msg->options));
4433 if (GNUNET_YES == t->reliable)
4435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable \n");
4436 t->sent_messages_fwd =
4437 GNUNET_CONTAINER_multihashmap32_create (t->queue_max);
4439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4440 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4442 peer_info = peer_get (&t_msg->peer);
4443 peer_info_add_tunnel (peer_info, t);
4444 peer_connect (peer_info, t);
4445 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4451 * Handler for requests of deleting tunnels
4453 * @param cls closure
4454 * @param client identification of the client
4455 * @param message the actual message
4458 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4459 const struct GNUNET_MessageHeader *message)
4461 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4462 struct MeshClient *c;
4463 struct MeshTunnel *t;
4464 MESH_TunnelNumber tid;
4466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4467 "Got a DESTROY TUNNEL from client!\n");
4469 /* Sanity check for client registration */
4470 if (NULL == (c = client_get (client)))
4473 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4478 /* Message sanity check */
4479 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4486 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4488 /* Retrieve tunnel */
4489 tid = ntohl (tunnel_msg->tunnel_id);
4490 t = tunnel_get_by_local_id(c, tid);
4493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4499 /* Cleanup after the tunnel */
4500 client_delete_tunnel (c, t);
4507 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4511 /* The tunnel will be destroyed when the last message is transmitted. */
4512 tunnel_destroy_empty (t);
4514 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4520 * Handler for client traffic
4522 * @param cls closure
4523 * @param client identification of the client
4524 * @param message the actual message
4527 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4528 const struct GNUNET_MessageHeader *message)
4530 struct GNUNET_MESH_LocalData *data_msg;
4531 struct MeshClient *c;
4532 struct MeshTunnel *t;
4533 struct MeshFlowControl *fc;
4534 MESH_TunnelNumber tid;
4537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4538 "Got data from a client!\n");
4540 /* Sanity check for client registration */
4541 if (NULL == (c = client_get (client)))
4544 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4549 data_msg = (struct GNUNET_MESH_LocalData *) message;
4551 /* Sanity check for message size */
4552 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4553 if (size < sizeof (struct GNUNET_MessageHeader))
4556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4560 /* Tunnel exists? */
4561 tid = ntohl (data_msg->tid);
4562 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4565 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4568 t = tunnel_get_by_local_id (c, tid);
4572 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4576 /* Is the client in the tunnel? */
4577 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4579 t->owner->handle == client)
4581 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4583 t->client->handle == client) ) )
4586 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4590 /* Ok, everything is correct, send the message
4591 * (pretend we got it from a mesh peer)
4594 struct GNUNET_MESH_Data *payload;
4595 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
4597 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
4598 if (GNUNET_YES == t->reliable)
4600 struct MeshSentMessage *copy;
4602 copy = GNUNET_malloc (sizeof (struct MeshSentMessage)
4603 + sizeof(struct GNUNET_MESH_Data)
4606 copy->id = fc->last_pid_recv + 1;
4607 copy->is_forward = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
4608 copy->retry_timer = MESH_RETRANSMIT_TIME;
4609 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
4610 &tunnel_retransmit_message,
4613 GNUNET_CONTAINER_multihashmap32_put (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
4614 t->sent_messages_fwd : t->sent_messages_bck,
4617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4620 GNUNET_SCHEDULER_cancel (copy->retry_task);
4622 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4625 payload = (struct GNUNET_MESH_Data *) ©[1];
4629 payload = (struct GNUNET_MESH_Data *) cbuf;
4631 memcpy (&payload[1], &data_msg[1], size);
4632 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
4633 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
4634 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
4635 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4636 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
4637 payload->tid = htonl (t->id.tid);
4638 payload->ttl = htonl (default_ttl);
4639 payload->pid = htonl (fc->last_pid_recv + 1);
4640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4641 " calling generic handler...\n");
4642 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4643 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
4645 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
4647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4648 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4655 * Handler for client's ACKs for payload traffic.
4657 * @param cls Closure (unused).
4658 * @param client Identification of the client.
4659 * @param message The actual message.
4662 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4663 const struct GNUNET_MessageHeader *message)
4665 struct GNUNET_MESH_LocalAck *msg;
4666 struct MeshTunnel *t;
4667 struct MeshClient *c;
4668 MESH_TunnelNumber tid;
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4671 /* Sanity check for client registration */
4672 if (NULL == (c = client_get (client)))
4675 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4680 msg = (struct GNUNET_MESH_LocalAck *) message;
4682 /* Tunnel exists? */
4683 tid = ntohl (msg->tunnel_id);
4684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4685 t = tunnel_get_by_local_id (c, tid);
4689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4690 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4695 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4698 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4699 t->prev_fc.last_ack_recv++;
4700 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4704 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4705 t->next_fc.last_ack_recv++;
4706 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4709 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4717 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4719 * @param cls Closure (client handle).
4720 * @param key Key (hashed tunnel ID, unused).
4721 * @param value Tunnel info.
4723 * @return GNUNET_YES, to keep iterating.
4726 monitor_all_tunnels_iterator (void *cls,
4727 const struct GNUNET_HashCode * key,
4730 struct GNUNET_SERVER_Client *client = cls;
4731 struct MeshTunnel *t = value;
4732 struct GNUNET_MESH_LocalMonitor *msg;
4734 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4735 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4736 msg->tunnel_id = htonl (t->id.tid);
4737 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4738 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4739 GNUNET_PEER_resolve (t->dest, &msg->destination);
4741 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4742 "* sending info about tunnel %s [%u]\n",
4743 GNUNET_i2s (&msg->owner), t->id.tid);
4745 GNUNET_SERVER_notification_context_unicast (nc, client,
4746 &msg->header, GNUNET_NO);
4752 * Handler for client's MONITOR request.
4754 * @param cls Closure (unused).
4755 * @param client Identification of the client.
4756 * @param message The actual message.
4759 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4760 const struct GNUNET_MessageHeader *message)
4762 struct MeshClient *c;
4764 /* Sanity check for client registration */
4765 if (NULL == (c = client_get (client)))
4768 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4772 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4773 "Received get tunnels request from client %u\n",
4775 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4776 monitor_all_tunnels_iterator,
4778 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4779 "Get tunnels request from client %u completed\n",
4781 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4786 * Handler for client's MONITOR_TUNNEL request.
4788 * @param cls Closure (unused).
4789 * @param client Identification of the client.
4790 * @param message The actual message.
4793 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4794 const struct GNUNET_MessageHeader *message)
4796 const struct GNUNET_MESH_LocalMonitor *msg;
4797 struct GNUNET_MESH_LocalMonitor *resp;
4798 struct MeshClient *c;
4799 struct MeshTunnel *t;
4801 /* Sanity check for client registration */
4802 if (NULL == (c = client_get (client)))
4805 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4809 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4810 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4811 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4814 ntohl (msg->tunnel_id));
4815 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4818 /* We don't know the tunnel FIXME */
4819 struct GNUNET_MESH_LocalMonitor warn;
4822 GNUNET_SERVER_notification_context_unicast (nc, client,
4825 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4829 /* Initialize context */
4830 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4832 GNUNET_PEER_resolve (t->dest, &resp->destination);
4833 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4834 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4835 &resp->header, GNUNET_NO);
4838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4839 "Monitor tunnel request from client %u completed\n",
4841 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4846 * Functions to handle messages from clients
4848 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4849 {&handle_local_new_client, NULL,
4850 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4851 {&handle_local_tunnel_create, NULL,
4852 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4853 sizeof (struct GNUNET_MESH_TunnelMessage)},
4854 {&handle_local_tunnel_destroy, NULL,
4855 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4856 sizeof (struct GNUNET_MESH_TunnelMessage)},
4857 {&handle_local_data, NULL,
4858 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
4859 {&handle_local_ack, NULL,
4860 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4861 sizeof (struct GNUNET_MESH_LocalAck)},
4862 {&handle_local_get_tunnels, NULL,
4863 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4864 sizeof (struct GNUNET_MessageHeader)},
4865 {&handle_local_show_tunnel, NULL,
4866 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4867 sizeof (struct GNUNET_MESH_LocalMonitor)},
4873 * Method called whenever a given peer connects.
4875 * @param cls closure
4876 * @param peer peer identity this notification is about
4879 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4881 struct MeshPeerInfo *peer_info;
4882 struct MeshPeerPath *path;
4884 DEBUG_CONN ("Peer connected\n");
4885 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4886 peer_info = peer_get (peer);
4887 if (myid == peer_info->id)
4889 DEBUG_CONN (" (self)\n");
4890 path = path_new (1);
4894 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4895 path = path_new (2);
4896 path->peers[1] = peer_info->id;
4897 GNUNET_PEER_change_rc (peer_info->id, 1);
4898 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4900 path->peers[0] = myid;
4901 GNUNET_PEER_change_rc (myid, 1);
4902 peer_info_add_path (peer_info, path, GNUNET_YES);
4908 * Method called whenever a peer disconnects.
4910 * @param cls closure
4911 * @param peer peer identity this notification is about
4914 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4916 struct MeshPeerInfo *pi;
4917 struct MeshPeerQueue *q;
4918 struct MeshPeerQueue *n;
4920 DEBUG_CONN ("Peer disconnected\n");
4921 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4931 /* TODO try to reroute this traffic instead */
4932 queue_destroy(q, GNUNET_YES);
4935 if (NULL != pi->core_transmit)
4937 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4938 pi->core_transmit = NULL;
4940 peer_remove_path (pi, pi->id, myid);
4943 DEBUG_CONN (" (self)\n");
4945 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4951 * Install server (service) handlers and start listening to clients.
4956 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4957 GNUNET_SERVER_disconnect_notify (server_handle,
4958 &handle_local_client_disconnect, NULL);
4959 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4961 clients_head = NULL;
4962 clients_tail = NULL;
4964 GNUNET_SERVER_resume (server_handle);
4969 * To be called on core init/fail.
4971 * @param cls Closure (config)
4972 * @param server handle to the server for this service
4973 * @param identity the public identity of this peer
4976 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4977 const struct GNUNET_PeerIdentity *identity)
4979 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4983 GNUNET_break (core_handle == server);
4984 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4987 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4990 GNUNET_i2s (identity));
4991 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4993 GNUNET_i2s (&my_full_id));
4994 GNUNET_CORE_disconnect (core_handle);
4995 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4996 NULL, /* Closure passed to MESH functions */
4997 &core_init, /* Call core_init once connected */
4998 &core_connect, /* Handle connects */
4999 &core_disconnect, /* remove peers on disconnects */
5000 NULL, /* Don't notify about all incoming messages */
5001 GNUNET_NO, /* For header only in notification */
5002 NULL, /* Don't notify about all outbound messages */
5003 GNUNET_NO, /* For header-only out notification */
5004 core_handlers); /* Register these handlers */
5013 /******************************************************************************/
5014 /************************ MAIN FUNCTIONS ****************************/
5015 /******************************************************************************/
5018 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5020 * @param cls closure
5021 * @param key current key code
5022 * @param value value in the hash map
5023 * @return GNUNET_YES if we should continue to iterate,
5027 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5029 struct MeshTunnel *t = value;
5036 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5038 * @param cls closure
5039 * @param key current key code
5040 * @param value value in the hash map
5041 * @return GNUNET_YES if we should continue to iterate,
5045 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5047 struct MeshPeerInfo *p = value;
5048 struct MeshPeerQueue *q;
5049 struct MeshPeerQueue *n;
5057 queue_destroy(q, GNUNET_YES);
5061 peer_info_destroy (p);
5067 * Task run during shutdown.
5073 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5077 if (core_handle != NULL)
5079 GNUNET_CORE_disconnect (core_handle);
5082 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5083 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5084 if (dht_handle != NULL)
5086 GNUNET_DHT_disconnect (dht_handle);
5091 GNUNET_SERVER_notification_context_destroy (nc);
5094 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5096 GNUNET_SCHEDULER_cancel (announce_id_task);
5097 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5104 * Process mesh requests.
5106 * @param cls closure
5107 * @param server the initialized server
5108 * @param c configuration to use
5111 run (void *cls, struct GNUNET_SERVER_Handle *server,
5112 const struct GNUNET_CONFIGURATION_Handle *c)
5115 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5118 server_handle = server;
5119 GNUNET_SERVER_suspend (server_handle);
5122 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5127 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5128 "mesh", "peer/privatekey");
5129 GNUNET_SCHEDULER_shutdown ();
5134 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5135 &refresh_path_time))
5137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5139 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5140 "mesh", "refresh path time");
5141 GNUNET_SCHEDULER_shutdown ();
5146 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5151 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5152 "mesh", "id announce time");
5153 GNUNET_SCHEDULER_shutdown ();
5158 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5163 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5164 "mesh", "connect timeout");
5165 GNUNET_SCHEDULER_shutdown ();
5170 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5175 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5176 "mesh", "max msgs queue");
5177 GNUNET_SCHEDULER_shutdown ();
5182 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5187 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5188 "mesh", "max tunnels");
5189 GNUNET_SCHEDULER_shutdown ();
5194 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5197 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5199 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5200 "mesh", "default ttl", 64);
5205 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5208 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5209 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5210 "mesh", "max peers", 1000);
5215 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5222 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5223 "Mesh is running with drop mode enabled. "
5224 "This is NOT a good idea! "
5225 "Remove the DROP_PERCENT option from your configuration.\n");
5229 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5230 &dht_replication_level))
5232 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5234 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5235 "mesh", "dht replication level", 3);
5236 dht_replication_level = 3;
5239 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5240 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5241 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5242 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5244 dht_handle = GNUNET_DHT_connect (c, 64);
5245 if (NULL == dht_handle)
5249 stats = GNUNET_STATISTICS_create ("mesh", c);
5251 /* Scheduled the task to clean up when shutdown is called */
5252 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5254 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5255 GNUNET_free (keyfile);
5256 GNUNET_assert (NULL != pk);
5257 my_private_key = pk;
5258 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5259 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5260 &my_full_id.hashPubKey);
5261 myid = GNUNET_PEER_intern (&my_full_id);
5262 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5263 "Mesh for peer [%s] starting\n",
5264 GNUNET_i2s(&my_full_id));
5266 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5267 NULL, /* Closure passed to MESH functions */
5268 &core_init, /* Call core_init once connected */
5269 &core_connect, /* Handle connects */
5270 &core_disconnect, /* remove peers on disconnects */
5271 NULL, /* Don't notify about all incoming messages */
5272 GNUNET_NO, /* For header only in notification */
5273 NULL, /* Don't notify about all outbound messages */
5274 GNUNET_NO, /* For header-only out notification */
5275 core_handlers); /* Register these handlers */
5276 if (NULL == core_handle)
5279 GNUNET_SCHEDULER_shutdown ();
5283 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5284 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5290 * The main function for the mesh service.
5292 * @param argc number of arguments from the command line
5293 * @param argv command line arguments
5294 * @return 0 ok, 1 on error
5297 main (int argc, char *const *argv)
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5303 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5305 ret = (GNUNET_OK == r) ? 0 : 1;
5306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5310 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5311 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5312 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);