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,\
55 #if MESH_DEBUG_CONNECTION
56 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
58 #define DEBUG_CONN(...)
62 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
64 #define DEBUG_DHT(...)
71 struct timespec __mesh_start;
72 struct timespec __mesh_end;
73 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
74 #define INTERVAL_END \
76 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
77 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
78 if (__diff < 0) __diff += 1000000000;\
82 #define INTERVAL_SHOW \
84 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
86 #define INTERVAL_START
91 /******************************************************************************/
92 /************************ DATA STRUCTURES ****************************/
93 /******************************************************************************/
95 /** FWD declaration */
101 * Struct containing info about a queued transmission to this peer
108 struct MeshPeerQueue *next;
113 struct MeshPeerQueue *prev;
116 * Peer this transmission is directed to.
118 struct MeshPeerInfo *peer;
121 * Tunnel this message belongs to.
123 struct MeshTunnel *tunnel;
126 * Pointer to info stucture used as cls.
136 * Size of the message
143 * Struct containing all information regarding a given peer
153 * Last time we heard from this peer
155 struct GNUNET_TIME_Absolute last_contact;
158 * Number of attempts to reconnect so far
160 int n_reconnect_attempts;
163 * Paths to reach the peer, ordered by ascending hop count
165 struct MeshPeerPath *path_head;
168 * Paths to reach the peer, ordered by ascending hop count
170 struct MeshPeerPath *path_tail;
173 * Handle to stop the DHT search for a path to this peer
175 struct GNUNET_DHT_GetHandle *dhtget;
178 * Array of tunnels this peer is the target of.
179 * Most probably a small amount, therefore not a hashmap.
180 * When the path to the peer changes, notify these tunnels to let them
181 * re-adjust their path trees.
183 struct MeshTunnel **tunnels;
186 * Number of tunnels this peers participates in
188 unsigned int ntunnels;
191 * Transmission queue to core DLL head
193 struct MeshPeerQueue *queue_head;
196 * Transmission queue to core DLL tail
198 struct MeshPeerQueue *queue_tail;
201 * How many messages are in the queue to this peer.
203 unsigned int queue_n;
206 * Handle for queued transmissions
208 struct GNUNET_CORE_TransmitHandle *core_transmit;
213 * Struct to encapsulate all the Flow Control information to a peer in the
214 * context of a tunnel: Same peer in different tunnels will have independent
215 * flow control structures, allowing to choke/free tunnels according to its
218 struct MeshFlowControl
221 * ID of the last packet sent towards the peer.
223 uint32_t last_pid_sent;
226 * ID of the last packet received from the peer.
228 uint32_t last_pid_recv;
231 * Last ACK sent to the peer (peer can't send more than this PID).
233 uint32_t last_ack_sent;
236 * Last ACK sent towards the origin (for traffic towards leaf node).
238 uint32_t last_ack_recv;
241 * How many payload messages are in the queue towards this peer.
246 * Task to poll the peer in case of a lost ACK causes stall.
248 GNUNET_SCHEDULER_TaskIdentifier poll_task;
251 * How frequently to poll for ACKs.
253 struct GNUNET_TIME_Relative poll_time;
256 * On which tunnel to poll.
257 * Using an explicit poll_ctx would not help memory wise,
258 * since the allocated context would have to be stored in the
259 * fc struct in order to free it upon cancelling poll_task.
261 struct MeshTunnel *t;
266 * Globally unique tunnel identification (owner + number)
267 * DO NOT USE OVER THE NETWORK
272 * Node that owns the tunnel
277 * Tunnel number to differentiate all the tunnels owned by the node oid
278 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
280 MESH_TunnelNumber tid;
285 * Info needed to retry a message in case it gets lost.
287 struct MeshSentMessage {
290 * Tunnel this message is in.
292 struct MeshTunnel *t;
295 * ID of the message (ACK needed to free)
300 * Task to resend/poll in case no ACK is received.
302 GNUNET_SCHEDULER_TaskIdentifier retry_task; // FIXME move to per tunnel timer?
305 * Counter for exponential backoff.
307 struct GNUNET_TIME_Relative retry_timer;
310 * Is this a forward or backward going message?
314 /* struct GNUNET_MESH_Data with payload */
318 * Struct containing all information regarding a tunnel
319 * For an intermediate node the improtant info used will be:
320 * - id Tunnel unique identification
321 * - paths[0] To know where to send it next
322 * - metainfo: ready, speeds, accounting
329 struct MESH_TunnelID id;
332 * Port of the tunnel.
337 * State of the tunnel.
339 enum MeshTunnelState state;
342 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
344 MESH_TunnelNumber local_tid;
347 * Local tunnel number for local destination clients (incoming number)
348 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
351 MESH_TunnelNumber local_tid_dest;
354 * Is the tunnel bufferless (minimum latency)?
359 * Is the tunnel reliable?
364 * Force sending ACK? Flag to allow duplicate ACK on POLL.
369 * How many messages do we accept in the forward queue.
371 unsigned int queue_max;
374 * Last time the tunnel was used
376 struct GNUNET_TIME_Absolute timestamp;
379 * Destination of the tunnel.
384 * Next hop in the tunnel. If 0, @c client must be set.
386 GNUNET_PEER_Id next_hop;
389 * Previous hop in the tunnel. If 0, @c owner must be set.
391 GNUNET_PEER_Id prev_hop;
394 * Flow control information about @c next_hop or @c client.
396 struct MeshFlowControl next_fc;
399 * Flow control information about @c prev_hop or @c owner.
401 struct MeshFlowControl prev_fc;
404 * Client owner of the tunnel, if any
406 struct MeshClient *owner;
409 * Client destination of the tunnel, if any.
411 struct MeshClient *client;
414 * Task to keep the used paths alive at the owner,
415 * time tunnel out on all the other peers.
417 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
420 * Path being used for the tunnel.
422 struct MeshPeerPath *path;
425 * Flag to signal the destruction of the tunnel.
426 * If this is set GNUNET_YES the tunnel will be destroyed
427 * when the queue is empty.
432 * Total messages pending for this tunnels, payload or not.
434 unsigned int pending_messages;
437 * Messages sent and not yet ACK'd.
438 * Only present (non-NULL) at the owner of a tunnel.
440 struct GNUNET_CONTAINER_MultiHashMap32 *sent_messages_fwd;
443 * Messages sent and not yet ACK'd.
444 * Only present (non-NULL) at the destination of a tunnel.
446 struct GNUNET_CONTAINER_MultiHashMap32 *sent_messages_bck;
451 * Struct containing information about a client of the service
453 * TODO: add a list of 'waiting' ports
460 struct MeshClient *next;
465 struct MeshClient *prev;
468 * Tunnels that belong to this client, indexed by local id
470 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
473 * Tunnels this client has accepted, indexed by incoming local id
475 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
478 * Handle to communicate with the client
480 struct GNUNET_SERVER_Client *handle;
483 * Ports that this client has declared interest in.
484 * Indexed by a GMC_hash32 (type), contains *Client.
486 struct GNUNET_CONTAINER_MultiHashMap *ports;
489 * Whether the client is active or shutting down (don't send confirmations
490 * to a client that is shutting down.
495 * ID of the client, mainly for debug messages
502 /******************************************************************************/
503 /************************ DEBUG FUNCTIONS ****************************/
504 /******************************************************************************/
508 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
509 * @param cls string to print
510 * @param success GNUNET_OK if the PUT was transmitted,
511 * GNUNET_NO on timeout,
512 * GNUNET_SYSERR on disconnect from service
513 * after the PUT message was transmitted
514 * (so we don't know if it was received or not)
519 mesh_debug (void *cls, int success)
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
528 unsigned int debug_fwd_ack;
529 unsigned int debug_bck_ack;
533 /******************************************************************************/
534 /*********************** GLOBAL VARIABLES ****************************/
535 /******************************************************************************/
537 /************************** Configuration parameters **************************/
540 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
542 static struct GNUNET_TIME_Relative refresh_path_time;
545 * How often to PUT own ID in the DHT.
547 static struct GNUNET_TIME_Relative id_announce_time;
550 * Maximum time allowed to connect to a peer found by string.
552 static struct GNUNET_TIME_Relative connect_timeout;
555 * Default TTL for payload packets.
557 static unsigned long long default_ttl;
560 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
562 static unsigned long long dht_replication_level;
565 * How many tunnels are we willing to maintain.
566 * Local tunnels are always allowed, even if there are more tunnels than max.
568 static unsigned long long max_tunnels;
571 * How many messages *in total* are we willing to queue, divided by number of
572 * tunnels to get tunnel queue size.
574 static unsigned long long max_msgs_queue;
577 * How many peers do we want to remember?
579 static unsigned long long max_peers;
582 * Percentage of messages that will be dropped (for test purposes only).
584 static unsigned long long drop_percent;
586 /*************************** Static global variables **************************/
589 * DLL with all the clients, head.
591 static struct MeshClient *clients_head;
594 * DLL with all the clients, tail.
596 static struct MeshClient *clients_tail;
599 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
601 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
604 * Number of tunnels known.
606 static unsigned long long n_tunnels;
609 * Tunnels incoming, indexed by MESH_TunnelNumber
610 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
612 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
615 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
617 static struct GNUNET_CONTAINER_MultiHashMap *peers;
620 * Handle to communicate with transport
622 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
625 * Handle to communicate with core.
627 static struct GNUNET_CORE_Handle *core_handle;
632 static struct GNUNET_DHT_Handle *dht_handle;
637 static struct GNUNET_SERVER_Handle *server_handle;
640 * Handle to the statistics service.
642 static struct GNUNET_STATISTICS_Handle *stats;
645 * Notification context, to send messages to local clients.
647 static struct GNUNET_SERVER_NotificationContext *nc;
650 * Local peer own ID (memory efficient handle).
652 static GNUNET_PEER_Id myid;
655 * Local peer own ID (full value).
657 static struct GNUNET_PeerIdentity my_full_id;
662 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
667 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
670 * Tunnel ID for the next created tunnel (global tunnel number).
672 static MESH_TunnelNumber next_tid;
675 * Tunnel ID for the next incoming tunnel (local tunnel number).
677 static MESH_TunnelNumber next_local_tid;
680 * All ports clients of this peer have opened.
682 static struct GNUNET_CONTAINER_MultiHashMap *ports;
685 * Task to periodically announce itself in the network.
687 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
690 * Next ID to assign to a client.
692 unsigned int next_client_id;
695 /******************************************************************************/
696 /*********************** DECLARATIONS **************************/
697 /******************************************************************************/
700 * Function to process paths received for a new peer addition. The recorded
701 * paths form the initial tunnel, which can be optimized later.
702 * Called on each result obtained for the DHT search.
705 * @param exp when will this value expire
706 * @param key key of the result
707 * @param type type of the result
708 * @param size number of bytes in data
709 * @param data pointer to the result data
712 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
713 const struct GNUNET_HashCode * key,
714 const struct GNUNET_PeerIdentity *get_path,
715 unsigned int get_path_length,
716 const struct GNUNET_PeerIdentity *put_path,
717 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
718 size_t size, const void *data);
722 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
723 * and insert it in the appropriate structures if the peer is not known yet.
725 * @param peer Full identity of the peer.
727 * @return Existing or newly created peer info.
729 static struct MeshPeerInfo *
730 peer_get (const struct GNUNET_PeerIdentity *peer);
734 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
735 * and insert it in the appropriate structures if the peer is not known yet.
737 * @param peer Short identity of the peer.
739 * @return Existing or newly created peer info.
741 static struct MeshPeerInfo *
742 peer_get_short (const GNUNET_PEER_Id peer);
746 * Build a PeerPath from the paths returned from the DHT, reversing the paths
747 * to obtain a local peer -> destination path and interning the peer ids.
749 * @return Newly allocated and created path
751 static struct MeshPeerPath *
752 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
753 unsigned int get_path_length,
754 const struct GNUNET_PeerIdentity *put_path,
755 unsigned int put_path_length);
759 * Adds a path to the peer_infos of all the peers in the path
761 * @param p Path to process.
762 * @param confirmed Whether we know if the path works or not.
765 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
770 * Search for a tunnel by global ID using full PeerIdentities.
772 * @param oid owner of the tunnel.
773 * @param tid global tunnel number.
775 * @return tunnel handler, NULL if doesn't exist.
777 static struct MeshTunnel *
778 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
782 * Notify a tunnel that a connection has broken that affects at least
785 * @param t Tunnel affected.
786 * @param p1 Peer that got disconnected from p2.
787 * @param p2 Peer that got disconnected from p1.
789 * @return Short ID of the peer disconnected (either p1 or p2).
790 * 0 if the tunnel remained unaffected.
792 static GNUNET_PEER_Id
793 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
798 * @brief Use the given path for the tunnel.
799 * Update the next and prev hops (and RCs).
800 * (Re)start the path refresh in case the tunnel is locally owned.
802 * @param t Tunnel to update.
803 * @param p Path to use.
806 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
809 * Tunnel is empty: destroy it.
811 * Notifies all participants (peers, cleints) about the destruction.
813 * @param t Tunnel to destroy.
816 tunnel_destroy_empty (struct MeshTunnel *t);
819 * @brief Queue and pass message to core when possible.
821 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
822 * and accounts for it. In case the queue is full, the message is dropped and
825 * Otherwise, message is treated as internal and allowed to go regardless of
828 * @param cls Closure (@c type dependant). It will be used by queue_send to
829 * build the message to be sent if not already prebuilt.
830 * @param type Type of the message, 0 for a raw message.
831 * @param size Size of the message.
832 * @param dst Neighbor to send message to.
833 * @param t Tunnel this message belongs to.
836 queue_add (void *cls, uint16_t type, size_t size,
837 struct MeshPeerInfo *dst, struct MeshTunnel *t);
841 * Free a transmission that was already queued with all resources
842 * associated to the request.
844 * @param queue Queue handler to cancel.
845 * @param clear_cls Is it necessary to free associated cls?
848 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
852 * @brief Get the next transmittable message from the queue.
854 * This will be the head, except in the case of being a data packet
855 * not allowed by the destination peer.
857 * @param peer Destination peer.
859 * @return The next viable MeshPeerQueue element to send to that peer.
860 * NULL when there are no transmittable messages.
862 struct MeshPeerQueue *
863 queue_get_next (const struct MeshPeerInfo *peer);
867 * Core callback to write a queued packet to core buffer
869 * @param cls Closure (peer info).
870 * @param size Number of bytes available in buf.
871 * @param buf Where the to write the message.
873 * @return number of bytes written to buf
876 queue_send (void *cls, size_t size, void *buf);
879 /******************************************************************************/
880 /************************ PERIODIC FUNCTIONS ****************************/
881 /******************************************************************************/
884 * Periodically announce self id in the DHT
887 * @param tc task context
890 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
894 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
896 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
900 * - Set data expiration in function of X
903 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
905 block.id = my_full_id;
906 GNUNET_DHT_put (dht_handle, /* DHT handle */
907 &my_full_id.hashPubKey, /* Key to use */
908 dht_replication_level, /* Replication level */
909 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
910 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
911 sizeof (block), /* Size of the data */
912 (const char *) &block, /* Data itself */
913 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
914 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
915 NULL, /* Continuation */
916 NULL); /* Continuation closure */
918 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
922 /******************************************************************************/
923 /****************** GENERAL HELPER FUNCTIONS ************************/
924 /******************************************************************************/
928 * Check if client has registered with the service and has not disconnected
930 * @param client the client to check
932 * @return non-NULL if client exists in the global DLL
934 static struct MeshClient *
935 client_get (struct GNUNET_SERVER_Client *client)
937 struct MeshClient *c;
942 if (c->handle == client)
951 * Deletes a tunnel from a client (either owner or destination). To be used on
954 * @param c Client whose tunnel to delete.
955 * @param t Tunnel which should be deleted.
958 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
960 struct GNUNET_HashCode hash;
964 GMC_hash32 (t->local_tid, &hash);
965 GNUNET_assert (GNUNET_YES ==
966 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
970 else if (c == t->client)
972 GMC_hash32 (t->local_tid_dest, &hash);
973 GNUNET_assert (GNUNET_YES ==
974 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
985 * Notify the appropriate client that a new incoming tunnel was created.
987 * @param t Tunnel that was created.
990 send_client_tunnel_create (struct MeshTunnel *t)
992 struct GNUNET_MESH_TunnelMessage msg;
994 if (NULL == t->client)
996 msg.header.size = htons (sizeof (msg));
997 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
998 msg.tunnel_id = htonl (t->local_tid_dest);
999 msg.port = htonl (t->port);
1000 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1001 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1002 &msg.header, GNUNET_NO);
1007 * Notify dest client that the incoming tunnel is no longer valid.
1009 * @param c Client to notify..
1010 * @param t Tunnel that is destroyed.
1013 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
1015 struct GNUNET_MESH_TunnelMessage msg;
1022 if (c != t->client && c != t->owner)
1027 msg.header.size = htons (sizeof (msg));
1028 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1029 msg.tunnel_id = htonl (t->local_tid_dest);
1030 msg.port = htonl (0);
1031 memset(&msg.peer, 0, sizeof (msg.peer));
1032 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1033 &msg.header, GNUNET_NO);
1038 * Iterator over all the peers to remove the oldest not-used entry.
1040 * @param cls Closure (unsued).
1041 * @param key ID of the peer.
1042 * @param value Peer_Info of the peer.
1047 peer_info_timeout (void *cls,
1048 const struct GNUNET_HashCode *key,
1055 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1056 * and insert it in the appropriate structures if the peer is not known yet.
1058 * @param peer Full identity of the peer.
1060 * @return Existing or newly created peer info.
1062 static struct MeshPeerInfo *
1063 peer_get (const struct GNUNET_PeerIdentity *peer)
1065 struct MeshPeerInfo *peer_info;
1067 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1068 if (NULL == peer_info)
1071 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1072 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1074 GNUNET_CONTAINER_multihashmap_iterate (peers,
1078 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1079 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1080 peer_info->id = GNUNET_PEER_intern (peer);
1082 peer_info->last_contact = GNUNET_TIME_absolute_get();
1089 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1090 * and insert it in the appropriate structures if the peer is not known yet.
1092 * @param peer Short identity of the peer.
1094 * @return Existing or newly created peer info.
1096 static struct MeshPeerInfo *
1097 peer_get_short (const GNUNET_PEER_Id peer)
1099 struct GNUNET_PeerIdentity id;
1101 GNUNET_PEER_resolve (peer, &id);
1102 return peer_get (&id);
1107 * Choose the best path towards a peer considering the tunnel properties.
1109 * @param peer The destination peer.
1110 * @param t The tunnel the path is for.
1112 * @return Best current known path towards the peer, if any.
1114 static struct MeshPeerPath *
1115 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1117 struct MeshPeerPath *best_p;
1118 struct MeshPeerPath *p;
1119 unsigned int best_cost;
1122 best_p = p = peer->path_head;
1123 best_cost = cost = p->length;
1126 if ((cost = p->length) < best_cost)
1137 * Core callback to write a pre-constructed data packet to core buffer
1139 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1140 * @param size Number of bytes available in buf.
1141 * @param buf Where the to write the message.
1143 * @return number of bytes written to buf
1146 send_core_data_raw (void *cls, size_t size, void *buf)
1148 struct GNUNET_MessageHeader *msg = cls;
1151 GNUNET_assert (NULL != msg);
1152 total_size = ntohs (msg->size);
1154 if (total_size > size)
1159 memcpy (buf, msg, total_size);
1166 * Sends an already built message to a peer, properly registrating
1167 * all used resources.
1169 * @param message Message to send. Function makes a copy of it.
1170 * @param peer Short ID of the neighbor whom to send the message.
1171 * @param t Tunnel on which this message is transmitted.
1174 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1175 GNUNET_PEER_Id peer,
1176 struct MeshTunnel *t)
1178 struct GNUNET_PeerIdentity id;
1179 struct MeshPeerInfo *neighbor;
1180 struct MeshPeerPath *p;
1185 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1190 size = ntohs (message->size);
1191 data = GNUNET_malloc (size);
1192 memcpy (data, message, size);
1193 type = ntohs(message->type);
1194 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1195 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1197 struct GNUNET_MESH_Data *u;
1199 u = (struct GNUNET_MESH_Data *) data;
1200 u->ttl = htonl (ntohl (u->ttl) - 1);
1202 GNUNET_PEER_resolve (peer, &id);
1203 neighbor = peer_get (&id);
1204 for (p = neighbor->path_head; NULL != p; p = p->next)
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 " %s IS NOT DIRECTLY CONNECTED\n",
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 for (p = neighbor->path_head; NULL != p; p = p->next)
1222 struct GNUNET_PeerIdentity debug_id;
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 " path with %u hops through:\n",
1228 for (i = 0; i < p->length; i++)
1230 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 i, GNUNET_i2s(&debug_id));
1237 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1238 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1239 " no direct connection to %s\n",
1244 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1255 * Sends a CREATE PATH message for a path to a peer, properly registrating
1256 * all used resources.
1258 * @param t Tunnel for which the path is created.
1261 send_create_path (struct MeshTunnel *t)
1263 struct MeshPeerInfo *neighbor;
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1266 neighbor = peer_get_short (t->next_hop);
1268 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1269 sizeof (struct GNUNET_MESH_CreateTunnel) +
1270 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1273 t->state = MESH_TUNNEL_WAITING;
1278 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1280 * @param t Tunnel which to confirm.
1283 send_path_ack (struct MeshTunnel *t)
1285 struct MeshPeerInfo *peer;
1287 peer = peer_get_short (t->prev_hop);
1290 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1291 sizeof (struct GNUNET_MESH_PathACK),
1298 * Try to establish a new connection to this peer in the given tunnel.
1299 * If the peer doesn't have any path to it yet, try to get one.
1300 * If the peer already has some path, send a CREATE PATH towards it.
1302 * @param peer PeerInfo of the peer.
1303 * @param t Tunnel for which to create the path, if possible.
1306 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1308 struct MeshPeerPath *p;
1310 if (NULL != peer->path_head)
1312 p = peer_get_best_path (peer, t);
1313 tunnel_use_path (t, p);
1314 send_create_path (t);
1316 else if (NULL == peer->dhtget)
1318 struct GNUNET_PeerIdentity id;
1320 GNUNET_PEER_resolve (peer->id, &id);
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1323 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1324 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1325 &id.hashPubKey, /* key to search */
1326 dht_replication_level, /* replication level */
1327 GNUNET_DHT_RO_RECORD_ROUTE |
1328 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1330 0, /* xquery bits */
1331 &dht_get_id_handler, peer);
1332 t->state = MESH_TUNNEL_SEARCHING;
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1337 "There is no path but the DHT GET is already started.\n");
1343 * Destroy the peer_info and free any allocated resources linked to it
1345 * @param pi The peer_info to destroy.
1347 * @return GNUNET_OK on success
1350 peer_info_destroy (struct MeshPeerInfo *pi)
1352 struct GNUNET_PeerIdentity id;
1353 struct MeshPeerPath *p;
1354 struct MeshPeerPath *nextp;
1357 GNUNET_PEER_resolve (pi->id, &id);
1358 GNUNET_PEER_change_rc (pi->id, -1);
1361 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1365 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1367 if (NULL != pi->dhtget)
1369 GNUNET_DHT_get_stop (pi->dhtget);
1375 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1379 for (i = 0; i < pi->ntunnels; i++)
1380 tunnel_destroy_empty (pi->tunnels[i]);
1381 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1388 * Remove all paths that rely on a direct connection between p1 and p2
1389 * from the peer itself and notify all tunnels about it.
1391 * @param peer PeerInfo of affected peer.
1392 * @param p1 GNUNET_PEER_Id of one peer.
1393 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1396 * TODO: optimize (see below)
1399 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1402 struct MeshPeerPath *p;
1403 struct MeshPeerPath *next;
1404 struct MeshPeerInfo *peer_d;
1406 unsigned int destroyed;
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1411 for (p = peer->path_head; NULL != p; p = next)
1414 for (i = 0; i < (p->length - 1); i++)
1416 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1417 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1419 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1429 for (i = 0; i < peer->ntunnels; i++)
1431 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1435 peer_d = peer_get_short (d);
1436 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1437 tunnel_use_path (peer->tunnels[i], next);
1438 peer_connect (peer_d, peer->tunnels[i]);
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1445 * Add the path to the peer and update the path used to reach it in case this
1448 * @param peer_info Destination peer to add the path to.
1449 * @param path New path to add. Last peer must be the peer in arg 1.
1450 * Path will be either used of freed if already known.
1451 * @param trusted Do we trust that this path is real?
1454 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1457 struct MeshPeerPath *aux;
1461 if ((NULL == peer_info) || (NULL == path))
1464 path_destroy (path);
1467 if (path->peers[path->length - 1] != peer_info->id)
1470 path_destroy (path);
1473 if (2 >= path->length && GNUNET_NO == trusted)
1475 /* Only allow CORE to tell us about direct paths */
1476 path_destroy (path);
1479 for (l = 1; l < path->length; l++)
1481 if (path->peers[l] == myid)
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1484 for (l2 = 0; l2 < path->length - l; l2++)
1486 path->peers[l2] = path->peers[l + l2];
1491 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1496 struct GNUNET_PeerIdentity id;
1498 GNUNET_PEER_resolve (peer_info->id, &id);
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1500 path->length, GNUNET_i2s (&id));
1503 l = path_get_length (path);
1506 path_destroy (path);
1510 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1511 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1513 l2 = path_get_length (aux);
1516 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1517 peer_info->path_tail, aux, path);
1522 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1524 path_destroy (path);
1529 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1536 * Add the path to the origin peer and update the path used to reach it in case
1537 * this is the shortest.
1538 * The path is given in peer_info -> destination, therefore we turn the path
1539 * upside down first.
1541 * @param peer_info Peer to add the path to, being the origin of the path.
1542 * @param path New path to add after being inversed.
1543 * Path will be either used or freed.
1544 * @param trusted Do we trust that this path is real?
1547 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1548 struct MeshPeerPath *path, int trusted)
1551 peer_info_add_path (peer_info, path, trusted);
1556 * Add a tunnel to the list of tunnels a peer participates in.
1557 * Update the tunnel's destination.
1559 * @param p Peer to add to.
1560 * @param t Tunnel to add.
1563 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1567 GNUNET_break (t->dest == p->id);
1571 GNUNET_PEER_change_rc (t->dest, 1);
1572 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1577 * Remove a tunnel from the list of tunnels a peer participates in.
1578 * Free the tunnel's destination.
1580 * @param p Peer to clean.
1581 * @param t Tunnel to remove.
1584 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1588 if (t->dest == p->id)
1590 GNUNET_PEER_change_rc (t->dest, -1);
1593 for (i = 0; i < p->ntunnels; i++)
1595 if (p->tunnels[i] == t)
1597 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1598 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1606 * Function called if the connection to the peer has been stalled for a while,
1607 * possibly due to a missed ACK. Poll the peer about its ACK status.
1609 * @param cls Closure (poll ctx).
1610 * @param tc TaskContext.
1613 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1615 struct MeshFlowControl *fc = cls;
1616 struct GNUNET_MESH_Poll msg;
1617 struct MeshTunnel *t = fc->t;
1618 GNUNET_PEER_Id peer;
1620 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1621 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1628 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1629 msg.header.size = htons (sizeof (msg));
1630 msg.tid = htonl (t->id.tid);
1631 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1633 if (fc == &t->prev_fc)
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1638 else if (fc == &t->next_fc)
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1648 send_prebuilt_message (&msg.header, peer, t);
1649 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1650 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1656 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1657 * to obtain a local peer -> destination path and interning the peer ids.
1659 * @return Newly allocated and created path
1661 static struct MeshPeerPath *
1662 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1663 unsigned int get_path_length,
1664 const struct GNUNET_PeerIdentity *put_path,
1665 unsigned int put_path_length)
1667 struct MeshPeerPath *p;
1673 GNUNET_PEER_change_rc (myid, 1);
1674 i = get_path_length;
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1676 for (i--; i >= 0; i--)
1678 id = GNUNET_PEER_intern (&get_path[i]);
1679 if (p->length > 0 && id == p->peers[p->length - 1])
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1682 GNUNET_PEER_change_rc (id, -1);
1686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1687 GNUNET_i2s (&get_path[i]));
1689 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1690 p->peers[p->length - 1] = id;
1693 i = put_path_length;
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1695 for (i--; i >= 0; i--)
1697 id = GNUNET_PEER_intern (&put_path[i]);
1700 /* PUT path went through us, so discard the path up until now and start
1701 * from here to get a much shorter (and loop-free) path.
1706 if (p->length > 0 && id == p->peers[p->length - 1])
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1709 GNUNET_PEER_change_rc (id, -1);
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1714 GNUNET_i2s (&put_path[i]));
1716 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1717 p->peers[p->length - 1] = id;
1721 if (get_path_length > 0)
1722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1723 GNUNET_i2s (&get_path[0]));
1724 if (put_path_length > 0)
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1726 GNUNET_i2s (&put_path[0]));
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1729 for (i = 0; i < p->length; i++)
1731 struct GNUNET_PeerIdentity peer_id;
1733 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1735 GNUNET_i2s (&peer_id));
1743 * Adds a path to the peer_infos of all the peers in the path
1745 * @param p Path to process.
1746 * @param confirmed Whether we know if the path works or not.
1749 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1753 /* TODO: invert and add */
1754 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1755 for (i++; i < p->length; i++)
1757 struct MeshPeerInfo *aux;
1758 struct MeshPeerPath *copy;
1760 aux = peer_get_short (p->peers[i]);
1761 copy = path_duplicate (p);
1762 copy->length = i + 1;
1763 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1769 * Send keepalive packets for a peer
1771 * @param cls Closure (tunnel for which to send the keepalive).
1772 * @param tc Notification context.
1775 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1779 * Search for a tunnel among the incoming tunnels
1781 * @param tid the local id of the tunnel
1783 * @return tunnel handler, NULL if doesn't exist
1785 static struct MeshTunnel *
1786 tunnel_get_incoming (MESH_TunnelNumber tid)
1788 struct GNUNET_HashCode hash;
1790 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1791 GMC_hash32 (tid, &hash);
1792 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1797 * Search for a tunnel among the tunnels for a client
1799 * @param c the client whose tunnels to search in
1800 * @param tid the local id of the tunnel
1802 * @return tunnel handler, NULL if doesn't exist
1804 static struct MeshTunnel *
1805 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1807 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1809 return tunnel_get_incoming (tid);
1813 struct GNUNET_HashCode hash;
1815 GMC_hash32 (tid, &hash);
1816 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1822 * Search for a tunnel by global ID using PEER_ID
1824 * @param pi owner of the tunnel
1825 * @param tid global tunnel number
1827 * @return tunnel handler, NULL if doesn't exist
1829 static struct MeshTunnel *
1830 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1832 struct MESH_TunnelID id;
1833 struct GNUNET_HashCode hash;
1838 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1839 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1844 * Search for a tunnel by global ID using full PeerIdentities
1846 * @param oid owner of the tunnel
1847 * @param tid global tunnel number
1849 * @return tunnel handler, NULL if doesn't exist
1851 static struct MeshTunnel *
1852 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1854 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1859 * Add a client to a tunnel, initializing all needed data structures.
1861 * @param t Tunnel to which add the client.
1862 * @param c Client which to add to the tunnel.
1865 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1867 struct GNUNET_HashCode hash;
1869 if (NULL != t->client)
1874 GMC_hash32 (t->local_tid_dest, &hash);
1876 GNUNET_CONTAINER_multihashmap_put (c->incoming_tunnels, &hash, t,
1877 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1883 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1884 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1894 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1896 unsigned int own_pos;
1898 for (own_pos = 0; own_pos < p->length; own_pos++)
1900 if (p->peers[own_pos] == myid)
1903 if (own_pos > p->length - 1)
1909 if (own_pos < p->length - 1)
1910 t->next_hop = p->peers[own_pos + 1];
1912 t->next_hop = p->peers[own_pos];
1913 GNUNET_PEER_change_rc (t->next_hop, 1);
1915 t->prev_hop = p->peers[own_pos - 1];
1917 t->prev_hop = p->peers[0];
1918 GNUNET_PEER_change_rc (t->prev_hop, 1);
1920 if (NULL != t->path)
1921 path_destroy (t->path);
1922 t->path = path_duplicate (p);
1925 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1926 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1927 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1934 * Notifies a tunnel that a connection has broken that affects at least
1935 * some of its peers. Sends a notification towards the root of the tree.
1936 * In case the peer is the owner of the tree, notifies the client that owns
1937 * the tunnel and tries to reconnect.
1939 * @param t Tunnel affected.
1940 * @param p1 Peer that got disconnected from p2.
1941 * @param p2 Peer that got disconnected from p1.
1943 * @return Short ID of the peer disconnected (either p1 or p2).
1944 * 0 if the tunnel remained unaffected.
1946 static GNUNET_PEER_Id
1947 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1950 // if (myid != p1 && myid != p2) FIXME
1955 // if (tree_get_predecessor (t->tree) != 0)
1957 // /* We are the peer still connected, notify owner of the disconnection. */
1958 // struct GNUNET_MESH_PathBroken msg;
1959 // struct GNUNET_PeerIdentity neighbor;
1961 // msg.header.size = htons (sizeof (msg));
1962 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1963 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1964 // msg.tid = htonl (t->id.tid);
1965 // msg.peer1 = my_full_id;
1966 // GNUNET_PEER_resolve (pid, &msg.peer2);
1967 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1968 // send_prebuilt_message (&msg.header, &neighbor, t);
1975 * Build a local ACK message and send it to a local client.
1977 * @param t Tunnel on which to send the ACK.
1978 * @param c Client to whom send the ACK.
1979 * @param ack Value of the ACK.
1980 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1983 send_local_ack (struct MeshTunnel *t,
1984 struct MeshClient *c,
1988 struct GNUNET_MESH_LocalAck msg;
1990 msg.header.size = htons (sizeof (msg));
1991 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1992 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
1993 msg.ack = htonl (ack);
1994 GNUNET_SERVER_notification_context_unicast(nc,
2001 * Build an ACK message and queue it to send to the given peer.
2003 * @param t Tunnel on which to send the ACK.
2004 * @param peer Peer to whom send the ACK.
2005 * @param ack Value of the ACK.
2008 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2010 struct GNUNET_MESH_ACK msg;
2012 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2013 msg.header.size = htons (sizeof (msg));
2014 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2015 msg.pid = htonl (ack);
2016 msg.tid = htonl (t->id.tid);
2018 send_prebuilt_message (&msg.header, peer, t);
2023 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2025 * @param t Tunnel this is about.
2028 tunnel_send_fwd_data_ack (struct MeshTunnel *t)
2030 struct GNUNET_MESH_DataACK msg;
2032 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
2033 msg.header.size = htons (sizeof (msg));
2034 msg.tid = htonl (t->id.tid);
2035 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2036 msg.pid = htonl (t->prev_fc.last_pid_recv);
2037 msg.futures = 0; // FIXME set bits of other newer messages received
2039 send_prebuilt_message (&msg.header, t->prev_hop, t);
2044 * Send an end-to-end BCK ACK message for the most recent in-sequence payload.
2046 * @param t Tunnel this is about.
2049 tunnel_send_bck_data_ack (struct MeshTunnel *t)
2051 struct GNUNET_MESH_DataACK msg;
2053 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
2054 msg.header.size = htons (sizeof (msg));
2055 msg.tid = htonl (t->id.tid);
2056 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2057 msg.pid = htonl (t->next_fc.last_pid_recv);
2058 msg.futures = 0; // FIXME set bits of other newer messages received
2060 send_prebuilt_message (&msg.header, t->next_hop, t);
2065 * Send an ACK informing the predecessor about the available buffer space.
2066 * In case there is no predecessor, inform the owning client.
2067 * If buffering is off, send only on behalf of children or self if endpoint.
2068 * If buffering is on, send when sent to children and buffer space is free.
2069 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2070 * the ACK itself goes "back" (towards root).
2072 * @param t Tunnel on which to send the ACK.
2073 * @param type Type of message that triggered the ACK transmission.
2076 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2080 /* Is it after unicast retransmission? */
2083 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2085 "ACK due to FWD DATA retransmission\n");
2086 if (GNUNET_YES == t->nobuffer)
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2092 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2093 if (NULL != t->owner && GNUNET_YES == t->reliable)
2095 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2097 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2098 tunnel_send_fwd_data_ack (t);
2100 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2101 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2102 t->force_ack = GNUNET_YES;
2108 /* Check if we need to transmit the ACK */
2109 if (t->queue_max > t->next_fc.queue_n * 4 &&
2110 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2111 GNUNET_NO == t->force_ack)
2113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2115 " t->qmax: %u, t->qn: %u\n",
2116 t->queue_max, t->next_fc.queue_n);
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2118 " t->pid: %u, t->ack: %u\n",
2119 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2123 /* Ok, ACK might be necessary, what PID to ACK? */
2124 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
2125 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2131 t->prev_fc.last_ack_sent = ack;
2132 if (NULL != t->owner)
2133 send_local_ack (t, t->owner, ack, GNUNET_YES);
2134 else if (0 != t->prev_hop)
2135 send_ack (t, t->prev_hop, ack);
2139 t->force_ack = GNUNET_NO;
2144 * Send an ACK informing the children node/client about the available
2146 * If buffering is off, send only on behalf of root (can be self).
2147 * If buffering is on, send when sent to predecessor and buffer space is free.
2148 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2149 * the ACK itself goes "forward" (towards children/clients).
2151 * @param t Tunnel on which to send the ACK.
2152 * @param type Type of message that triggered the ACK transmission.
2155 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2160 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2161 /* Is it after data to_origin retransmission? */
2164 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2165 if (GNUNET_YES == t->nobuffer)
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168 " Not sending ACK, nobuffer + traffic\n");
2172 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2173 if (NULL != t->client && GNUNET_YES == t->reliable)
2175 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2177 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2178 tunnel_send_bck_data_ack (t);
2180 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2181 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2182 t->force_ack = GNUNET_YES;
2188 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2190 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2192 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 " Not sending ACK, not needed, last ack sent was %u\n",
2196 t->next_fc.last_ack_sent);
2199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2200 " Sending BCK ACK %u (last sent: %u)\n",
2201 ack, t->next_fc.last_ack_sent);
2202 t->next_fc.last_ack_sent = ack;
2204 if (NULL != t->client)
2205 send_local_ack (t, t->client, ack, GNUNET_NO);
2206 else if (0 != t->next_hop)
2207 send_ack (t, t->next_hop, ack);
2210 t->force_ack = GNUNET_NO;
2215 * Modify the unicast message TID from global to local and send to client.
2217 * @param t Tunnel on which to send the message.
2218 * @param msg Message to modify and send.
2221 tunnel_send_client_ucast (struct MeshTunnel *t,
2222 const struct GNUNET_MESH_Data *msg)
2224 struct GNUNET_MESH_Data *copy;
2225 uint16_t size = ntohs (msg->header.size);
2228 if (size < sizeof (struct GNUNET_MESH_Data) +
2229 sizeof (struct GNUNET_MessageHeader))
2231 GNUNET_break_op (0);
2234 if (NULL == t->client)
2239 copy = (struct GNUNET_MESH_Data *) cbuf;
2240 memcpy (copy, msg, size);
2241 copy->tid = htonl (t->local_tid_dest);
2242 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2243 ©->header, GNUNET_NO);
2248 * Modify the to_origin message TID from global to local and send to client.
2250 * @param t Tunnel on which to send the message.
2251 * @param msg Message to modify and send.
2254 tunnel_send_client_to_orig (struct MeshTunnel *t,
2255 const struct GNUNET_MESH_Data *msg)
2257 struct GNUNET_MESH_Data *copy;
2258 uint16_t size = ntohs (msg->header.size);
2261 if (size < sizeof (struct GNUNET_MESH_Data) +
2262 sizeof (struct GNUNET_MessageHeader))
2264 GNUNET_break_op (0);
2267 if (NULL == t->owner)
2272 copy = (struct GNUNET_MESH_Data *) cbuf;
2273 memcpy (cbuf, msg, size);
2274 copy->tid = htonl (t->local_tid);
2275 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2276 ©->header, GNUNET_NO);
2281 * We haven't received an ACK after a certain time: restransmit the message.
2283 * @param cls Closure (MeshSentMessage with the message to restransmit)
2284 * @param tc TaskContext.
2287 tunnel_retransmit_message (void *cls,
2288 const struct GNUNET_SCHEDULER_TaskContext *tc)
2290 struct MeshSentMessage *copy = cls;
2291 struct GNUNET_MESH_Data *payload;
2294 copy->retry_task = GNUNET_SCHEDULER_NO_TASK;
2295 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2298 payload = (struct GNUNET_MESH_Data *) ©[1];
2299 hop = copy->is_forward ? copy->t->next_hop : copy->t->prev_hop;
2300 send_prebuilt_message (&payload->header, hop, copy->t);
2301 GNUNET_STATISTICS_update (stats, "# unicast retransmitted", 1, GNUNET_NO);
2302 copy->retry_timer = GNUNET_TIME_STD_BACKOFF (copy->retry_timer);
2303 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
2304 &tunnel_retransmit_message,
2310 * @brief Re-initiate traffic to this peer if necessary.
2312 * Check if there is traffic queued towards this peer
2313 * and the core transmit handle is NULL (traffic was stalled).
2314 * If so, call core tmt rdy.
2316 * @param peer_id Short ID of peer to which initiate traffic.
2319 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2321 struct MeshPeerInfo *peer;
2322 struct GNUNET_PeerIdentity id;
2323 struct MeshPeerQueue *q;
2326 peer = peer_get_short (peer_id);
2327 if (NULL != peer->core_transmit)
2330 q = queue_get_next (peer);
2333 /* Might br multicast traffic already sent to this particular peer but
2334 * not to other children in this tunnel.
2335 * This way t->queue_n would be > 0 but the queue of this particular peer
2341 GNUNET_PEER_resolve (peer->id, &id);
2342 peer->core_transmit =
2343 GNUNET_CORE_notify_transmit_ready(core_handle,
2346 GNUNET_TIME_UNIT_FOREVER_REL,
2356 * Send a message to all peers and clients in this tunnel that the tunnel
2357 * is no longer valid. If some peer or client should not receive the message,
2358 * should be zero'ed out before calling this function.
2360 * @param t The tunnel whose peers and clients to notify.
2363 tunnel_send_destroy (struct MeshTunnel *t)
2365 struct GNUNET_MESH_TunnelDestroy msg;
2366 struct GNUNET_PeerIdentity id;
2368 msg.header.size = htons (sizeof (msg));
2369 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2370 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2371 msg.tid = htonl (t->id.tid);
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2373 " sending tunnel destroy for tunnel: %s [%X]\n",
2374 GNUNET_i2s (&msg.oid), t->id.tid);
2376 if (NULL == t->client && 0 != t->next_hop)
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2379 GNUNET_PEER_resolve (t->next_hop, &id);
2380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2381 " sending forward to %s\n",
2383 send_prebuilt_message (&msg.header, t->next_hop, t);
2385 if (NULL == t->owner && 0 != t->prev_hop)
2387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2388 GNUNET_PEER_resolve (t->prev_hop, &id);
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2390 " sending back to %s\n",
2392 send_prebuilt_message (&msg.header, t->prev_hop, t);
2394 if (NULL != t->owner)
2396 send_client_tunnel_destroy (t->owner, t);
2398 if (NULL != t->client)
2400 send_client_tunnel_destroy (t->client, t);
2406 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2408 * @param t Tunnel which to cancel.
2409 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2412 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2414 struct MeshPeerInfo *peer_info;
2415 struct MeshPeerQueue *pq;
2416 struct MeshPeerQueue *next;
2419 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2420 peer_info = peer_get_short (neighbor);
2421 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2424 if (pq->tunnel == t)
2426 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2427 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2429 /* Should have been removed on destroy children */
2432 queue_destroy (pq, GNUNET_YES);
2435 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2437 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2438 peer_info->core_transmit = NULL;
2444 * Destroy the tunnel.
2446 * This function does not generate any warning traffic to clients or peers.
2449 * Remove the tunnel from peer_info's and clients' hashmaps.
2450 * Cancel messages belonging to this tunnel queued to neighbors.
2451 * Free any allocated resources linked to the tunnel.
2453 * @param t the tunnel to destroy
2455 * @return GNUNET_OK on success
2458 tunnel_destroy (struct MeshTunnel *t)
2460 struct MeshClient *c;
2461 struct GNUNET_HashCode hash;
2471 struct GNUNET_PeerIdentity id;
2473 GNUNET_PEER_resolve (t->id.oid, &id);
2474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2475 GNUNET_i2s (&id), t->id.tid);
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2481 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2482 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2490 GMC_hash32 (t->local_tid, &hash);
2492 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2499 if (NULL != t->client)
2502 GMC_hash32 (t->local_tid_dest, &hash);
2504 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2510 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2517 if (0 != t->prev_hop)
2519 peer_cancel_queues (t->prev_hop, t);
2520 GNUNET_PEER_change_rc (t->prev_hop, -1);
2522 if (0 != t->next_hop)
2524 peer_cancel_queues (t->next_hop, t);
2525 GNUNET_PEER_change_rc (t->next_hop, -1);
2528 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2531 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2532 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2535 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2536 path_destroy (t->path);
2542 * Tunnel is empty: destroy it.
2544 * Notifies all participants (peers, cleints) about the destruction.
2546 * @param t Tunnel to destroy.
2549 tunnel_destroy_empty (struct MeshTunnel *t)
2553 struct GNUNET_PeerIdentity id;
2555 GNUNET_PEER_resolve (t->id.oid, &id);
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2557 "executing destruction of empty tunnel %s [%X]\n",
2558 GNUNET_i2s (&id), t->id.tid);
2562 if (GNUNET_NO == t->destroy)
2563 tunnel_send_destroy (t);
2564 if (0 == t->pending_messages)
2567 t->destroy = GNUNET_YES;
2571 * Initialize a Flow Control structure to the initial state.
2573 * @param fc Flow Control structure to initialize.
2576 fc_init (struct MeshFlowControl *fc)
2578 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2579 fc->last_pid_recv = (uint32_t) -1;
2580 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2581 fc->last_ack_recv = (uint32_t) -1;
2582 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2583 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2588 * Create a new tunnel
2590 * @param owner Who is the owner of the tunnel (short ID).
2591 * @param tid Tunnel Number of the tunnel.
2592 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2593 * @param local Tunnel Number for the tunnel, for the client point of view.
2595 * @return A new initialized tunnel. NULL on error.
2597 static struct MeshTunnel *
2598 tunnel_new (GNUNET_PEER_Id owner,
2599 MESH_TunnelNumber tid,
2600 struct MeshClient *client,
2601 MESH_TunnelNumber local)
2603 struct MeshTunnel *t;
2604 struct GNUNET_HashCode hash;
2606 if (n_tunnels >= max_tunnels && NULL == client)
2609 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2612 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2614 fc_init (&t->next_fc);
2615 fc_init (&t->prev_fc);
2616 t->local_tid = local;
2618 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2620 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2622 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2623 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2630 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2637 GMC_hash32 (t->local_tid, &hash);
2639 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2640 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2644 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2654 * Set options in a tunnel, extracted from a bit flag field
2656 * @param t Tunnel to set options to.
2657 * @param options Bit array in host byte order.
2660 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2662 t->nobuffer = options & GNUNET_MESH_OPTION_NOBUFFER;
2663 t->reliable = options & GNUNET_MESH_OPTION_RELIABLE;
2668 * Iterator for deleting each tunnel whose client endpoint disconnected.
2670 * @param cls Closure (client that has disconnected).
2671 * @param key The hash of the local tunnel id (used to access the hashmap).
2672 * @param value The value stored at the key (tunnel to destroy).
2674 * @return GNUNET_OK, keep iterating.
2677 tunnel_destroy_iterator (void *cls,
2678 const struct GNUNET_HashCode * key,
2681 struct MeshTunnel *t = value;
2682 struct MeshClient *c = cls;
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2686 t->local_tid, t->local_tid_dest, c->id);
2687 client_delete_tunnel (c, t);
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2692 if (0 != t->next_hop) { /* destroy could come before a path is used */
2693 GNUNET_PEER_change_rc (t->next_hop, -1);
2697 else if (c == t->owner)
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2701 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2702 GNUNET_PEER_change_rc (t->prev_hop, -1);
2710 tunnel_destroy_empty (t);
2717 * Timeout function, destroys tunnel if called
2719 * @param cls Closure (tunnel to destroy).
2720 * @param tc TaskContext
2723 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2725 struct MeshTunnel *t = cls;
2726 struct GNUNET_PeerIdentity id;
2728 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2729 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2731 GNUNET_PEER_resolve(t->id.oid, &id);
2732 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2733 "Tunnel %s [%X] timed out. Destroying.\n",
2734 GNUNET_i2s(&id), t->id.tid);
2735 if (NULL != t->client)
2736 send_client_tunnel_destroy (t->client, t);
2737 tunnel_destroy (t); /* Do not notify other */
2742 * Resets the tunnel timeout. Starts it if no timeout was running.
2744 * @param t Tunnel whose timeout to reset.
2746 * TODO use heap to improve efficiency of scheduler.
2749 tunnel_reset_timeout (struct MeshTunnel *t)
2751 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2753 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2754 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2755 t->maintenance_task =
2756 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2757 (refresh_path_time, 4), &tunnel_timeout, t);
2761 /******************************************************************************/
2762 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2763 /******************************************************************************/
2766 * Function to send a create path packet to a peer.
2768 * @param cls closure
2769 * @param size number of bytes available in buf
2770 * @param buf where the callee should write the message
2771 * @return number of bytes written to buf
2774 send_core_path_create (void *cls, size_t size, void *buf)
2776 struct MeshTunnel *t = cls;
2777 struct GNUNET_MESH_CreateTunnel *msg;
2778 struct GNUNET_PeerIdentity *peer_ptr;
2779 struct MeshPeerPath *p = t->path;
2784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2786 sizeof (struct GNUNET_MESH_CreateTunnel) +
2787 p->length * sizeof (struct GNUNET_PeerIdentity);
2789 if (size < size_needed || NULL == buf)
2794 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2795 msg->header.size = htons (size_needed);
2796 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2797 msg->tid = ntohl (t->id.tid);
2800 if (GNUNET_YES == t->nobuffer)
2801 opt |= GNUNET_MESH_OPTION_NOBUFFER;
2802 if (GNUNET_YES == t->reliable)
2803 opt |= GNUNET_MESH_OPTION_RELIABLE;
2804 msg->opt = htonl (opt);
2805 msg->port = htonl (t->port);
2807 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2808 for (i = 0; i < p->length; i++)
2810 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2814 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2820 * Creates a path ack message in buf and frees all unused resources.
2822 * @param cls closure (MeshTransmissionDescriptor)
2823 * @param size number of bytes available in buf
2824 * @param buf where the callee should write the message
2825 * @return number of bytes written to buf
2828 send_core_path_ack (void *cls, size_t size, void *buf)
2830 struct MeshTunnel *t = cls;
2831 struct GNUNET_MESH_PathACK *msg = buf;
2833 GNUNET_assert (NULL != t);
2834 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2839 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2840 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2841 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2842 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2843 msg->tid = htonl (t->id.tid);
2844 msg->peer_id = my_full_id;
2845 msg->ack = htonl (t->prev_fc.last_ack_sent);
2847 /* TODO add signature */
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2850 return sizeof (struct GNUNET_MESH_PathACK);
2855 * Free a transmission that was already queued with all resources
2856 * associated to the request.
2858 * @param queue Queue handler to cancel.
2859 * @param clear_cls Is it necessary to free associated cls?
2862 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2864 struct MeshFlowControl *fc;
2866 if (GNUNET_YES == clear_cls)
2868 switch (queue->type)
2870 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2871 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2872 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2874 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2875 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2876 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2877 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2878 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2880 " prebuilt message\n");
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2883 GNUNET_MESH_DEBUG_M2S (queue->type));
2885 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2890 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2891 " type %s unknown!\n",
2892 GNUNET_MESH_DEBUG_M2S (queue->type));
2894 GNUNET_free_non_null (queue->cls);
2896 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2897 queue->peer->queue_tail,
2900 /* Delete from appropriate fc in the tunnel */
2901 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
2902 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
2904 if (queue->peer->id == queue->tunnel->prev_hop)
2905 fc = &queue->tunnel->prev_fc;
2906 else if (queue->peer->id == queue->tunnel->next_hop)
2907 fc = &queue->tunnel->next_fc;
2915 GNUNET_free (queue);
2920 * @brief Get the next transmittable message from the queue.
2922 * This will be the head, except in the case of being a data packet
2923 * not allowed by the destination peer.
2925 * @param peer Destination peer.
2927 * @return The next viable MeshPeerQueue element to send to that peer.
2928 * NULL when there are no transmittable messages.
2930 struct MeshPeerQueue *
2931 queue_get_next (const struct MeshPeerInfo *peer)
2933 struct MeshPeerQueue *q;
2935 struct GNUNET_MESH_Data *dmsg;
2936 struct MeshTunnel* t;
2940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2941 for (q = peer->queue_head; NULL != q; q = q->next)
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2946 GNUNET_MESH_DEBUG_M2S (q->type));
2947 dmsg = (struct GNUNET_MESH_Data *) q->cls;
2948 pid = ntohl (dmsg->pid);
2951 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2952 ack = t->next_fc.last_ack_recv;
2954 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2955 ack = t->prev_fc.last_ack_recv;
2958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2963 "* ACK: %u, PID: %u\n",
2965 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2978 "* nothing found\n");
2984 queue_send (void *cls, size_t size, void *buf)
2986 struct MeshPeerInfo *peer = cls;
2987 struct GNUNET_MessageHeader *msg;
2988 struct MeshPeerQueue *queue;
2989 struct MeshTunnel *t;
2990 struct GNUNET_PeerIdentity dst_id;
2991 struct MeshFlowControl *fc;
2996 peer->core_transmit = NULL;
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
2999 queue = queue_get_next (peer);
3001 /* Queue has no internal mesh traffic nor sendable payload */
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3005 if (NULL == peer->queue_head)
3006 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3011 GNUNET_PEER_resolve (peer->id, &dst_id);
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3014 GNUNET_i2s (&dst_id));
3015 /* Check if buffer size is enough for the message */
3016 if (queue->size > size)
3018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3019 "* not enough room, reissue\n");
3020 peer->core_transmit =
3021 GNUNET_CORE_notify_transmit_ready (core_handle,
3024 GNUNET_TIME_UNIT_FOREVER_REL,
3031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3034 GNUNET_assert (0 < t->pending_messages);
3035 t->pending_messages--;
3039 switch (queue->type)
3042 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3043 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3044 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3045 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3046 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3047 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3050 GNUNET_MESH_DEBUG_M2S (queue->type));
3052 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3053 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3054 data_size = send_core_data_raw (queue->cls, size, buf);
3055 msg = (struct GNUNET_MessageHeader *) buf;
3056 type = ntohs (msg->type);
3058 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3060 data_size = send_core_path_create (queue->cls, size, buf);
3062 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3064 data_size = send_core_path_ack (queue->cls, size, buf);
3068 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3069 "* type unknown: %u\n",
3074 if (0 < drop_percent &&
3075 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3077 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3078 "Dropping message of type %s\n",
3079 GNUNET_MESH_DEBUG_M2S(queue->type));
3082 /* Free queue, but cls was freed by send_core_* */
3083 queue_destroy (queue, GNUNET_NO);
3085 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3086 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3089 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3090 t->next_fc.last_pid_sent = pid;
3091 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3093 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3094 t->prev_fc.last_pid_sent = pid;
3095 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3101 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3107 /* If more data in queue, send next */
3108 queue = queue_get_next (peer);
3111 struct GNUNET_PeerIdentity id;
3113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3114 GNUNET_PEER_resolve (peer->id, &id);
3115 peer->core_transmit =
3116 GNUNET_CORE_notify_transmit_ready(core_handle,
3119 GNUNET_TIME_UNIT_FOREVER_REL,
3125 else if (NULL != peer->queue_head)
3127 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3129 GNUNET_i2s (&my_full_id));
3130 if (peer->id == t->next_hop)
3132 else if (peer->id == t->prev_hop)
3139 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3142 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3152 * @brief Queue and pass message to core when possible.
3154 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3155 * accounts for it. In case the queue is full, the message is dropped and
3158 * Otherwise, message is treated as internal and allowed to go regardless of
3161 * @param cls Closure (@c type dependant). It will be used by queue_send to
3162 * build the message to be sent if not already prebuilt.
3163 * @param type Type of the message, 0 for a raw message.
3164 * @param size Size of the message.
3165 * @param dst Neighbor to send message to.
3166 * @param t Tunnel this message belongs to.
3169 queue_add (void *cls, uint16_t type, size_t size,
3170 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3172 struct MeshPeerQueue *queue;
3173 struct GNUNET_PeerIdentity id;
3177 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3179 n = &t->next_fc.queue_n;
3181 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3183 n = &t->prev_fc.queue_n;
3187 if (*n >= t->queue_max)
3190 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3191 "queue full: %u/%u\n",
3193 GNUNET_STATISTICS_update(stats,
3194 "# messages dropped (buffer full)",
3196 return; /* Drop message */
3200 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3206 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3207 if (NULL == dst->core_transmit)
3209 GNUNET_PEER_resolve (dst->id, &id);
3210 dst->core_transmit =
3211 GNUNET_CORE_notify_transmit_ready (core_handle,
3214 GNUNET_TIME_UNIT_FOREVER_REL,
3220 t->pending_messages++;
3224 /******************************************************************************/
3225 /******************** MESH NETWORK HANDLERS **************************/
3226 /******************************************************************************/
3230 * Core handler for path creation
3232 * @param cls closure
3233 * @param message message
3234 * @param peer peer identity this notification is about
3236 * @return GNUNET_OK to keep the connection open,
3237 * GNUNET_SYSERR to close it (signal serious error)
3240 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3241 const struct GNUNET_MessageHeader *message)
3243 unsigned int own_pos;
3246 MESH_TunnelNumber tid;
3247 struct GNUNET_MESH_CreateTunnel *msg;
3248 struct GNUNET_PeerIdentity *pi;
3249 struct MeshPeerPath *path;
3250 struct MeshPeerInfo *dest_peer_info;
3251 struct MeshPeerInfo *orig_peer_info;
3252 struct MeshTunnel *t;
3254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3255 "Received a path create msg [%s]\n",
3256 GNUNET_i2s (&my_full_id));
3257 size = ntohs (message->size);
3258 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3260 GNUNET_break_op (0);
3264 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3265 if (size % sizeof (struct GNUNET_PeerIdentity))
3267 GNUNET_break_op (0);
3270 size /= sizeof (struct GNUNET_PeerIdentity);
3273 GNUNET_break_op (0);
3276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3277 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3279 tid = ntohl (msg->tid);
3280 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3282 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3283 t = tunnel_get (pi, tid);
3284 if (NULL == t) /* might be a local tunnel */
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3289 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3295 t->port = ntohl (msg->port);
3296 opt = ntohl (msg->opt);
3297 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3299 t->nobuffer = GNUNET_YES;
3302 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3304 t->reliable = GNUNET_YES;
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3308 tunnel_reset_timeout (t);
3310 t->state = MESH_TUNNEL_WAITING;
3312 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3313 if (NULL == dest_peer_info)
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3316 " Creating PeerInfo for destination.\n");
3317 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3318 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3319 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3321 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3323 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3324 if (NULL == orig_peer_info)
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3327 " Creating PeerInfo for origin.\n");
3328 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3329 orig_peer_info->id = GNUNET_PEER_intern (pi);
3330 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3331 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3334 path = path_new (size);
3336 for (i = 0; i < size; i++)
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3339 GNUNET_i2s (&pi[i]));
3340 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3341 if (path->peers[i] == myid)
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3345 if (own_pos == 0 && path->peers[own_pos] != myid)
3347 /* create path: self not found in path through self */
3348 GNUNET_break_op (0);
3349 path_destroy (path);
3353 path_add_to_peers (path, GNUNET_NO);
3354 tunnel_use_path (t, path);
3356 peer_info_add_tunnel (dest_peer_info, t);
3358 if (own_pos == size - 1)
3360 struct MeshClient *c;
3361 struct GNUNET_HashCode hc;
3363 /* Find target client */
3364 GMC_hash32 (t->port, &hc);
3365 c = GNUNET_CONTAINER_multihashmap_get (ports, &hc);
3368 /* TODO send reject */
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3373 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3375 /* Assign local tid */
3376 while (NULL != tunnel_get_incoming (next_local_tid))
3377 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3378 t->local_tid_dest = next_local_tid++;
3379 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3381 if (GNUNET_YES == t->reliable)
3382 t->sent_messages_bck =
3383 GNUNET_CONTAINER_multihashmap32_create (t->queue_max);
3385 tunnel_add_client (t, c);
3386 send_client_tunnel_create (t);
3391 struct MeshPeerPath *path2;
3393 t->next_hop = path->peers[own_pos + 1];
3394 GNUNET_PEER_change_rc(t->next_hop, 1);
3396 /* It's for somebody else! Retransmit. */
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3398 path2 = path_duplicate (path);
3399 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3400 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3401 send_create_path (t);
3409 * Core handler for path ACKs
3411 * @param cls closure
3412 * @param message message
3413 * @param peer peer identity this notification is about
3415 * @return GNUNET_OK to keep the connection open,
3416 * GNUNET_SYSERR to close it (signal serious error)
3419 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3420 const struct GNUNET_MessageHeader *message)
3422 struct GNUNET_MESH_PathACK *msg;
3423 struct MeshPeerInfo *peer_info;
3424 struct MeshPeerPath *p;
3425 struct MeshTunnel *t;
3427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3428 GNUNET_i2s (&my_full_id));
3429 msg = (struct GNUNET_MESH_PathACK *) message;
3430 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3433 /* TODO notify that we don't know the tunnel */
3434 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3436 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3440 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3442 peer_info = peer_get (&msg->peer_id);
3443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3444 GNUNET_i2s (&msg->peer_id));
3445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3448 /* Add path to peers? */
3452 path_add_to_peers (p, GNUNET_YES);
3458 t->state = MESH_TUNNEL_READY;
3459 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3460 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3462 /* Message for us? */
3463 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3466 if (NULL == t->owner)
3468 GNUNET_break_op (0);
3471 if (NULL != peer_info->dhtget)
3473 GNUNET_DHT_get_stop (peer_info->dhtget);
3474 peer_info->dhtget = NULL;
3476 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3477 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3482 " not for us, retransmitting...\n");
3483 peer_info = peer_get (&msg->oid);
3484 send_prebuilt_message (message, t->prev_hop, t);
3490 * Core handler for notifications of broken paths
3492 * @param cls closure
3493 * @param message message
3494 * @param peer peer identity this notification is about
3496 * @return GNUNET_OK to keep the connection open,
3497 * GNUNET_SYSERR to close it (signal serious error)
3500 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3501 const struct GNUNET_MessageHeader *message)
3503 struct GNUNET_MESH_PathBroken *msg;
3504 struct MeshTunnel *t;
3506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3507 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3508 msg = (struct GNUNET_MESH_PathBroken *) message;
3509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3510 GNUNET_i2s (&msg->peer1));
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3512 GNUNET_i2s (&msg->peer2));
3513 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3516 GNUNET_break_op (0);
3519 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3520 GNUNET_PEER_search (&msg->peer2));
3527 * Core handler for tunnel destruction
3529 * @param cls closure
3530 * @param message message
3531 * @param peer peer identity this notification is about
3533 * @return GNUNET_OK to keep the connection open,
3534 * GNUNET_SYSERR to close it (signal serious error)
3537 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3538 const struct GNUNET_MessageHeader *message)
3540 struct GNUNET_MESH_TunnelDestroy *msg;
3541 struct MeshTunnel *t;
3543 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3545 "Got a TUNNEL DESTROY packet from %s\n",
3547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3548 " for tunnel %s [%u]\n",
3549 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3550 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3553 /* Probably already got the message from another path,
3554 * destroyed the tunnel and retransmitted to children.
3557 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3561 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3564 t->local_tid, t->local_tid_dest);
3566 if (GNUNET_PEER_search (peer) == t->prev_hop)
3568 // TODO check owner's signature
3569 // TODO add owner's signatue to tunnel for retransmission
3570 peer_cancel_queues (t->prev_hop, t);
3571 GNUNET_PEER_change_rc (t->prev_hop, -1);
3574 else if (GNUNET_PEER_search (peer) == t->next_hop)
3576 // TODO check dest's signature
3577 // TODO add dest's signatue to tunnel for retransmission
3578 peer_cancel_queues (t->next_hop, t);
3579 GNUNET_PEER_change_rc (t->next_hop, -1);
3584 GNUNET_break_op (0);
3585 // TODO check both owner AND destination's signature to see which matches
3586 // TODO restransmit in appropriate direction
3589 tunnel_destroy_empty (t);
3591 // TODO: add timeout to destroy the tunnel anyway
3597 * Core handler for mesh network traffic going from the origin to a peer
3599 * @param cls closure
3600 * @param peer peer identity this notification is about
3601 * @param message message
3602 * @return GNUNET_OK to keep the connection open,
3603 * GNUNET_SYSERR to close it (signal serious error)
3606 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3607 const struct GNUNET_MessageHeader *message)
3609 struct GNUNET_MESH_Data *msg;
3610 struct MeshTunnel *t;
3615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3618 size = ntohs (message->size);
3620 sizeof (struct GNUNET_MESH_Data) +
3621 sizeof (struct GNUNET_MessageHeader))
3626 msg = (struct GNUNET_MESH_Data *) message;
3627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3628 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3630 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3633 /* TODO notify back: we don't know this tunnel */
3634 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3635 GNUNET_break_op (0);
3638 pid = ntohl (msg->pid);
3639 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3641 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3642 GNUNET_break_op (0);
3643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3644 "Received PID %u, ACK %u\n",
3645 pid, t->prev_fc.last_ack_sent);
3646 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3650 tunnel_reset_timeout (t);
3651 if (t->dest == myid)
3653 /* TODO signature verification */
3654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3655 " it's for us! sending to clients...\n");
3656 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3657 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
3658 if (pid == t->prev_fc.last_pid_recv + 1)
3660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3661 " pid %u not seen yet, forwarding\n", pid);
3662 t->prev_fc.last_pid_recv = pid;
3663 tunnel_send_client_ucast (t, msg);
3664 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3668 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
3669 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3670 " Pid %u not expected, sending FWD ACK!\n", pid);
3671 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3675 t->prev_fc.last_pid_recv = pid;
3676 if (0 == t->next_hop)
3681 ttl = ntohl (msg->ttl);
3682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3685 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3686 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3687 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3691 " not for us, retransmitting...\n");
3693 send_prebuilt_message (message, t->next_hop, t);
3694 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3700 * Core handler for mesh network traffic toward the owner of a tunnel
3702 * @param cls closure
3703 * @param message message
3704 * @param peer peer identity this notification is about
3706 * @return GNUNET_OK to keep the connection open,
3707 * GNUNET_SYSERR to close it (signal serious error)
3710 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3711 const struct GNUNET_MessageHeader *message)
3713 struct GNUNET_MESH_Data *msg;
3714 struct MeshTunnel *t;
3719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3721 size = ntohs (message->size);
3722 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
3723 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3725 GNUNET_break_op (0);
3728 msg = (struct GNUNET_MESH_Data *) message;
3729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3730 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3731 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3732 pid = ntohl (msg->pid);
3735 /* TODO notify that we dont know this tunnel (whom)? */
3736 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3738 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3739 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3743 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3745 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3746 GNUNET_break_op (0);
3747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3748 "Received PID %u, ACK %u\n",
3749 pid, t->next_fc.last_ack_sent);
3750 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3754 if (myid == t->id.oid)
3756 /* TODO signature verification */
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3758 " it's for us! sending to clients...\n");
3759 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3760 if (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 copy = GNUNET_CONTAINER_multihashmap32_get (hm, ack);
3879 GNUNET_break (0); // FIXME needed?
3882 GNUNET_break (GNUNET_YES ==
3883 GNUNET_CONTAINER_multihashmap32_remove (hm, ack, copy));
3884 if (GNUNET_SCHEDULER_NO_TASK != copy->retry_task)
3886 GNUNET_SCHEDULER_cancel (copy->retry_task);
3895 * Core handler for mesh network traffic point-to-point acks.
3897 * @param cls closure
3898 * @param message message
3899 * @param peer peer identity this notification is about
3901 * @return GNUNET_OK to keep the connection open,
3902 * GNUNET_SYSERR to close it (signal serious error)
3905 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3906 const struct GNUNET_MessageHeader *message)
3908 struct GNUNET_MESH_ACK *msg;
3909 struct MeshTunnel *t;
3913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3915 msg = (struct GNUNET_MESH_ACK *) message;
3917 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3921 /* TODO notify that we dont know this tunnel (whom)? */
3922 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3925 ack = ntohl (msg->pid);
3926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3928 /* Is this a forward or backward ACK? */
3929 id = GNUNET_PEER_search (peer);
3930 if (t->next_hop == id)
3933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3934 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3935 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3937 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3938 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3939 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3941 t->next_fc.last_ack_recv = ack;
3942 peer_unlock_queue (t->next_hop);
3943 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3945 else if (t->prev_hop == id)
3948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3949 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3950 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3952 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3953 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3954 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3956 t->prev_fc.last_ack_recv = ack;
3957 peer_unlock_queue (t->prev_hop);
3958 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3961 GNUNET_break_op (0);
3967 * Core handler for mesh network traffic point-to-point ack polls.
3969 * @param cls closure
3970 * @param message message
3971 * @param peer peer identity this notification is about
3973 * @return GNUNET_OK to keep the connection open,
3974 * GNUNET_SYSERR to close it (signal serious error)
3977 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3978 const struct GNUNET_MessageHeader *message)
3980 struct GNUNET_MESH_Poll *msg;
3981 struct MeshTunnel *t;
3984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3987 msg = (struct GNUNET_MESH_Poll *) message;
3989 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3993 /* TODO notify that we dont know this tunnel (whom)? */
3994 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3995 GNUNET_break_op (0);
3999 /* Is this a forward or backward ACK? */
4000 id = GNUNET_PEER_search(peer);
4001 if (t->next_hop == id)
4003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4004 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4006 else if (t->prev_hop == id)
4008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4009 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4019 * Core handler for mesh keepalives.
4021 * @param cls closure
4022 * @param message message
4023 * @param peer peer identity this notification is about
4024 * @return GNUNET_OK to keep the connection open,
4025 * GNUNET_SYSERR to close it (signal serious error)
4027 * TODO: Check who we got this from, to validate route.
4030 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4031 const struct GNUNET_MessageHeader *message)
4033 struct GNUNET_MESH_TunnelKeepAlive *msg;
4034 struct MeshTunnel *t;
4036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4039 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4040 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4044 /* TODO notify that we dont know that tunnel */
4045 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4050 tunnel_reset_timeout (t);
4051 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4054 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4055 send_prebuilt_message (message, t->next_hop, t);
4062 * Functions to handle messages from core
4064 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4065 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4066 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4067 sizeof (struct GNUNET_MESH_PathBroken)},
4068 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4069 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4070 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4071 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4072 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK,
4073 sizeof (struct GNUNET_MESH_DataACK)},
4074 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4075 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4076 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4077 sizeof (struct GNUNET_MESH_ACK)},
4078 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4079 sizeof (struct GNUNET_MESH_Poll)},
4080 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4081 sizeof (struct GNUNET_MESH_PathACK)},
4087 /******************************************************************************/
4088 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4089 /******************************************************************************/
4094 * notify_client_connection_failure: notify a client that the connection to the
4095 * requested remote peer is not possible (for instance, no route found)
4096 * Function called when the socket is ready to queue more data. "buf" will be
4097 * NULL and "size" zero if the socket was closed for writing in the meantime.
4099 * @param cls closure
4100 * @param size number of bytes available in buf
4101 * @param buf where the callee should write the message
4102 * @return number of bytes written to buf
4105 notify_client_connection_failure (void *cls, size_t size, void *buf)
4108 struct MeshPeerInfo *peer_info;
4109 struct GNUNET_MESH_PeerControl *msg;
4110 struct GNUNET_PeerIdentity id;
4112 if (0 == size && NULL == buf)
4114 // TODO retry? cancel?
4118 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4119 peer_info = (struct MeshPeerInfo *) cls;
4120 msg = (struct GNUNET_MESH_PeerControl *) buf;
4121 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4122 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4123 // msg->tunnel_id = htonl(peer_info->t->tid);
4124 GNUNET_PEER_resolve (peer_info->id, &id);
4125 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4133 * Send keepalive packets for a tunnel.
4135 * @param cls Closure (tunnel for which to send the keepalive).
4136 * @param tc Notification context.
4138 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4141 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4143 struct MeshTunnel *t = cls;
4144 struct GNUNET_MESH_TunnelKeepAlive *msg;
4145 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4148 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4149 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4150 NULL == t->owner || 0 == t->local_tid)
4155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4156 "sending keepalive for tunnel %d\n", t->id.tid);
4158 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4159 msg->header.size = htons (size);
4160 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4161 msg->oid = my_full_id;
4162 msg->tid = htonl (t->id.tid);
4163 send_prebuilt_message (&msg->header, t->next_hop, t);
4165 t->maintenance_task =
4166 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4171 * Function to process paths received for a new peer addition. The recorded
4172 * paths form the initial tunnel, which can be optimized later.
4173 * Called on each result obtained for the DHT search.
4175 * @param cls closure
4176 * @param exp when will this value expire
4177 * @param key key of the result
4178 * @param get_path path of the get request
4179 * @param get_path_length lenght of get_path
4180 * @param put_path path of the put request
4181 * @param put_path_length length of the put_path
4182 * @param type type of the result
4183 * @param size number of bytes in data
4184 * @param data pointer to the result data
4187 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4188 const struct GNUNET_HashCode * key,
4189 const struct GNUNET_PeerIdentity *get_path,
4190 unsigned int get_path_length,
4191 const struct GNUNET_PeerIdentity *put_path,
4192 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4193 size_t size, const void *data)
4195 struct MeshPeerInfo *peer = cls;
4196 struct MeshPeerPath *p;
4197 struct GNUNET_PeerIdentity pi;
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4201 GNUNET_PEER_resolve (peer->id, &pi);
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4204 p = path_build_from_dht (get_path, get_path_length,
4205 put_path, put_path_length);
4206 path_add_to_peers (p, GNUNET_NO);
4208 for (i = 0; i < peer->ntunnels; i++)
4210 struct GNUNET_PeerIdentity id;
4212 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4214 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4215 peer->tunnels[i]->local_tid,
4216 peer->tunnels[i]->local_tid_dest);
4217 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4220 peer_connect (peer, peer->tunnels[i]);
4228 /******************************************************************************/
4229 /********************* MESH LOCAL HANDLES **************************/
4230 /******************************************************************************/
4234 * Handler for client disconnection
4236 * @param cls closure
4237 * @param client identification of the client; NULL
4238 * for the last call when the server is destroyed
4241 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4243 struct MeshClient *c;
4244 struct MeshClient *next;
4246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4256 if (c->handle != client)
4258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4259 " ... searching %p (%u)\n",
4264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4266 GNUNET_SERVER_client_drop (c->handle);
4267 c->shutting_down = GNUNET_YES;
4268 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4269 &tunnel_destroy_iterator, c);
4270 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4271 &tunnel_destroy_iterator, c);
4272 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4273 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4275 if (NULL != c->ports)
4276 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
4278 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4281 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4290 * Handler for new clients
4292 * @param cls closure
4293 * @param client identification of the client
4294 * @param message the actual message, which includes messages the client wants
4297 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4298 const struct GNUNET_MessageHeader *message)
4300 struct GNUNET_MESH_ClientConnect *cc_msg;
4301 struct MeshClient *c;
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4308 /* Check data sanity */
4309 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4310 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4311 if (0 != (size % sizeof (uint32_t)))
4314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4317 size /= sizeof (uint32_t);
4319 /* Create new client structure */
4320 c = GNUNET_malloc (sizeof (struct MeshClient));
4321 c->id = next_client_id++; /* overflow not important: just for debug */
4322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4325 GNUNET_SERVER_client_keep (client);
4329 struct GNUNET_HashCode hc;
4331 p = (uint32_t *) &cc_msg[1];
4332 c->ports = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
4333 for (i = 0; i < size; i++)
4336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4337 GMC_hash32 (u32, &hc);
4339 /* store in client's hashmap */
4340 GNUNET_CONTAINER_multihashmap_put (c->ports, &hc, c,
4341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4342 /* store in global hashmap */
4343 /* FIXME only allow one client to have the port open,
4344 * have a backup hashmap with waiting clients */
4345 GNUNET_CONTAINER_multihashmap_put (ports, &hc, c,
4346 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4350 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4351 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4352 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4353 GNUNET_SERVER_notification_context_add (nc, client);
4354 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4356 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4362 * Handler for requests of new tunnels
4364 * @param cls Closure.
4365 * @param client Identification of the client.
4366 * @param message The actual message.
4369 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4370 const struct GNUNET_MessageHeader *message)
4372 struct GNUNET_MESH_TunnelMessage *t_msg;
4373 struct MeshPeerInfo *peer_info;
4374 struct MeshTunnel *t;
4375 struct MeshClient *c;
4376 MESH_TunnelNumber tid;
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4380 /* Sanity check for client registration */
4381 if (NULL == (c = client_get (client)))
4384 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4389 /* Message size sanity check */
4390 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4397 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4399 GNUNET_i2s (&t_msg->peer));
4400 /* Sanity check for tunnel numbering */
4401 tid = ntohl (t_msg->tunnel_id);
4402 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4408 /* Sanity check for duplicate tunnel IDs */
4409 if (NULL != tunnel_get_by_local_id (c, tid))
4412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4417 while (NULL != tunnel_get_by_pi (myid, next_tid))
4418 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4419 t = tunnel_new (myid, next_tid, c, tid);
4420 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4424 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4427 t->port = ntohl (t_msg->port);
4428 tunnel_set_options (t, ntohl (t_msg->options));
4429 if (GNUNET_YES == t->reliable)
4430 t->sent_messages_fwd =
4431 GNUNET_CONTAINER_multihashmap32_create (t->queue_max);
4432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4433 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4435 peer_info = peer_get (&t_msg->peer);
4436 peer_info_add_tunnel (peer_info, t);
4437 peer_connect (peer_info, t);
4438 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4444 * Handler for requests of deleting tunnels
4446 * @param cls closure
4447 * @param client identification of the client
4448 * @param message the actual message
4451 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4452 const struct GNUNET_MessageHeader *message)
4454 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4455 struct MeshClient *c;
4456 struct MeshTunnel *t;
4457 MESH_TunnelNumber tid;
4459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4460 "Got a DESTROY TUNNEL from client!\n");
4462 /* Sanity check for client registration */
4463 if (NULL == (c = client_get (client)))
4466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4471 /* Message sanity check */
4472 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4479 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4481 /* Retrieve tunnel */
4482 tid = ntohl (tunnel_msg->tunnel_id);
4483 t = tunnel_get_by_local_id(c, tid);
4486 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4488 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4492 /* Cleanup after the tunnel */
4493 client_delete_tunnel (c, t);
4498 else if (c == t->owner)
4500 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4504 /* The tunnel will be destroyed when the last message is transmitted. */
4505 tunnel_destroy_empty (t);
4507 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4513 * Handler for client traffic directed to one peer
4515 * @param cls closure
4516 * @param client identification of the client
4517 * @param message the actual message
4520 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4521 const struct GNUNET_MessageHeader *message)
4523 struct MeshClient *c;
4524 struct MeshTunnel *t;
4525 struct GNUNET_MESH_Data *data_msg;
4526 MESH_TunnelNumber tid;
4529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4530 "Got a unicast request from a client!\n");
4532 /* Sanity check for client registration */
4533 if (NULL == (c = client_get (client)))
4536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4541 data_msg = (struct GNUNET_MESH_Data *) message;
4543 /* Sanity check for message size */
4544 size = ntohs (message->size);
4545 if (sizeof (struct GNUNET_MESH_Data) +
4546 sizeof (struct GNUNET_MessageHeader) > size)
4549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4553 /* Tunnel exists? */
4554 tid = ntohl (data_msg->tid);
4555 t = tunnel_get_by_local_id (c, tid);
4559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4563 /* Is it a local tunnel? Then, does client own the tunnel? */
4564 if (t->owner->handle != client)
4567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4571 /* PID should be as expected: client<->service communication */
4572 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4576 "Unicast PID, expected %u, got %u\n",
4577 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4582 /* Ok, everything is correct, send the message
4583 * (pretend we got it from a mesh peer)
4586 struct MeshSentMessage *copy;
4587 struct GNUNET_MESH_Data *payload;
4589 copy = GNUNET_malloc (sizeof (struct MeshSentMessage) + size);
4591 copy->id = ntohl (data_msg->pid);
4592 copy->is_forward = GNUNET_YES;
4593 copy->retry_timer = GNUNET_TIME_UNIT_MINUTES;
4594 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
4595 &tunnel_retransmit_message,
4597 if (GNUNET_YES == t->reliable &&
4599 GNUNET_CONTAINER_multihashmap32_put (t->sent_messages_fwd,
4602 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4605 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4608 payload = (struct GNUNET_MESH_Data *) ©[1];
4609 memcpy (payload, data_msg, size);
4610 payload->oid = my_full_id;
4611 payload->tid = htonl (t->id.tid);
4612 payload->ttl = htonl (default_ttl);
4613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4614 " calling generic handler...\n");
4615 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4618 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4625 * Handler for client traffic directed to the origin
4627 * @param cls closure
4628 * @param client identification of the client
4629 * @param message the actual message
4632 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4633 const struct GNUNET_MessageHeader *message)
4635 struct GNUNET_MESH_Data *data_msg;
4636 struct MeshFlowControl *fc;
4637 struct MeshClient *c;
4638 struct MeshTunnel *t;
4639 MESH_TunnelNumber tid;
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4643 "Got a ToOrigin request from a client!\n");
4644 /* Sanity check for client registration */
4645 if (NULL == (c = client_get (client)))
4648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4653 data_msg = (struct GNUNET_MESH_Data *) message;
4655 /* Sanity check for message size */
4656 size = ntohs (message->size);
4657 if (sizeof (struct GNUNET_MESH_Data) +
4658 sizeof (struct GNUNET_MessageHeader) > size)
4661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4665 /* Tunnel exists? */
4666 tid = ntohl (data_msg->tid);
4667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4668 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4674 t = tunnel_get_by_local_id (c, tid);
4677 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4680 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4684 /* It should be sent by someone who has this as incoming tunnel. */
4688 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4692 /* PID should be as expected */
4694 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4698 "To Origin PID, expected %u, got %u\n",
4699 fc->last_pid_recv + 1,
4700 ntohl (data_msg->pid));
4701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4705 /* Ok, everything is correct, send the message
4706 * (pretend we got it from a mesh peer)
4709 struct MeshSentMessage *copy;
4710 struct GNUNET_MESH_Data *payload;
4712 copy = GNUNET_malloc (sizeof (struct MeshSentMessage) + size);
4714 copy->id = ntohl (data_msg->pid);
4715 copy->is_forward = GNUNET_NO;
4716 copy->retry_timer = GNUNET_TIME_UNIT_MINUTES;
4717 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
4718 &tunnel_retransmit_message,
4720 if (GNUNET_YES == t->reliable &&
4722 GNUNET_CONTAINER_multihashmap32_put (t->sent_messages_bck,
4725 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4731 payload = (struct GNUNET_MESH_Data *) ©[1];
4732 memcpy (payload, data_msg, size);
4733 GNUNET_PEER_resolve (t->id.oid, &payload->oid);
4734 payload->tid = htonl (t->id.tid);
4735 payload->ttl = htonl (default_ttl);
4737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4738 " calling generic handler...\n");
4739 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
4741 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4748 * Handler for client's ACKs for payload traffic.
4750 * @param cls Closure (unused).
4751 * @param client Identification of the client.
4752 * @param message The actual message.
4755 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4756 const struct GNUNET_MessageHeader *message)
4758 struct GNUNET_MESH_LocalAck *msg;
4759 struct MeshTunnel *t;
4760 struct MeshClient *c;
4761 MESH_TunnelNumber tid;
4764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4765 /* Sanity check for client registration */
4766 if (NULL == (c = client_get (client)))
4769 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4774 msg = (struct GNUNET_MESH_LocalAck *) message;
4776 /* Tunnel exists? */
4777 tid = ntohl (msg->tunnel_id);
4778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4779 t = tunnel_get_by_local_id (c, tid);
4783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4784 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4789 ack = ntohl (msg->ack);
4790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4792 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4795 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4796 t->prev_fc.last_ack_recv = ack;
4797 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4801 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4802 t->next_fc.last_ack_recv = ack;
4803 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4806 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4814 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4816 * @param cls Closure (client handle).
4817 * @param key Key (hashed tunnel ID, unused).
4818 * @param value Tunnel info.
4820 * @return GNUNET_YES, to keep iterating.
4823 monitor_all_tunnels_iterator (void *cls,
4824 const struct GNUNET_HashCode * key,
4827 struct GNUNET_SERVER_Client *client = cls;
4828 struct MeshTunnel *t = value;
4829 struct GNUNET_MESH_LocalMonitor *msg;
4831 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4832 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4833 msg->tunnel_id = htonl (t->id.tid);
4834 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4835 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4836 GNUNET_PEER_resolve (t->dest, &msg->destination);
4838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4839 "* sending info about tunnel %s [%u]\n",
4840 GNUNET_i2s (&msg->owner), t->id.tid);
4842 GNUNET_SERVER_notification_context_unicast (nc, client,
4843 &msg->header, GNUNET_NO);
4849 * Handler for client's MONITOR request.
4851 * @param cls Closure (unused).
4852 * @param client Identification of the client.
4853 * @param message The actual message.
4856 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4857 const struct GNUNET_MessageHeader *message)
4859 struct MeshClient *c;
4861 /* Sanity check for client registration */
4862 if (NULL == (c = client_get (client)))
4865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4869 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4870 "Received get tunnels request from client %u\n",
4872 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4873 monitor_all_tunnels_iterator,
4875 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4876 "Get tunnels request from client %u completed\n",
4878 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4883 * Handler for client's MONITOR_TUNNEL request.
4885 * @param cls Closure (unused).
4886 * @param client Identification of the client.
4887 * @param message The actual message.
4890 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4891 const struct GNUNET_MessageHeader *message)
4893 const struct GNUNET_MESH_LocalMonitor *msg;
4894 struct GNUNET_MESH_LocalMonitor *resp;
4895 struct MeshClient *c;
4896 struct MeshTunnel *t;
4898 /* Sanity check for client registration */
4899 if (NULL == (c = client_get (client)))
4902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4906 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4907 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4908 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4911 ntohl (msg->tunnel_id));
4912 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4915 /* We don't know the tunnel FIXME */
4916 struct GNUNET_MESH_LocalMonitor warn;
4919 GNUNET_SERVER_notification_context_unicast (nc, client,
4922 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4926 /* Initialize context */
4927 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4929 GNUNET_PEER_resolve (t->dest, &resp->destination);
4930 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4931 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4932 &resp->header, GNUNET_NO);
4935 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4936 "Monitor tunnel request from client %u completed\n",
4938 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4943 * Functions to handle messages from clients
4945 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4946 {&handle_local_new_client, NULL,
4947 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4948 {&handle_local_tunnel_create, NULL,
4949 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4950 sizeof (struct GNUNET_MESH_TunnelMessage)},
4951 {&handle_local_tunnel_destroy, NULL,
4952 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4953 sizeof (struct GNUNET_MESH_TunnelMessage)},
4954 {&handle_local_unicast, NULL,
4955 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4956 {&handle_local_to_origin, NULL,
4957 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4958 {&handle_local_ack, NULL,
4959 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4960 sizeof (struct GNUNET_MESH_LocalAck)},
4961 {&handle_local_get_tunnels, NULL,
4962 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4963 sizeof (struct GNUNET_MessageHeader)},
4964 {&handle_local_show_tunnel, NULL,
4965 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4966 sizeof (struct GNUNET_MESH_LocalMonitor)},
4972 * Method called whenever a given peer connects.
4974 * @param cls closure
4975 * @param peer peer identity this notification is about
4978 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4980 struct MeshPeerInfo *peer_info;
4981 struct MeshPeerPath *path;
4983 DEBUG_CONN ("Peer connected\n");
4984 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4985 peer_info = peer_get (peer);
4986 if (myid == peer_info->id)
4988 DEBUG_CONN (" (self)\n");
4989 path = path_new (1);
4993 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4994 path = path_new (2);
4995 path->peers[1] = peer_info->id;
4996 GNUNET_PEER_change_rc (peer_info->id, 1);
4997 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4999 path->peers[0] = myid;
5000 GNUNET_PEER_change_rc (myid, 1);
5001 peer_info_add_path (peer_info, path, GNUNET_YES);
5007 * Method called whenever a peer disconnects.
5009 * @param cls closure
5010 * @param peer peer identity this notification is about
5013 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5015 struct MeshPeerInfo *pi;
5016 struct MeshPeerQueue *q;
5017 struct MeshPeerQueue *n;
5019 DEBUG_CONN ("Peer disconnected\n");
5020 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5030 /* TODO try to reroute this traffic instead */
5031 queue_destroy(q, GNUNET_YES);
5034 if (NULL != pi->core_transmit)
5036 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5037 pi->core_transmit = NULL;
5039 peer_remove_path (pi, pi->id, myid);
5042 DEBUG_CONN (" (self)\n");
5044 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5050 * Install server (service) handlers and start listening to clients.
5055 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5056 GNUNET_SERVER_disconnect_notify (server_handle,
5057 &handle_local_client_disconnect, NULL);
5058 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5060 clients_head = NULL;
5061 clients_tail = NULL;
5063 GNUNET_SERVER_resume (server_handle);
5068 * To be called on core init/fail.
5070 * @param cls Closure (config)
5071 * @param server handle to the server for this service
5072 * @param identity the public identity of this peer
5075 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5076 const struct GNUNET_PeerIdentity *identity)
5078 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5082 GNUNET_break (core_handle == server);
5083 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5086 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5087 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5089 GNUNET_i2s (identity));
5090 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5092 GNUNET_i2s (&my_full_id));
5093 GNUNET_CORE_disconnect (core_handle);
5094 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5095 NULL, /* Closure passed to MESH functions */
5096 &core_init, /* Call core_init once connected */
5097 &core_connect, /* Handle connects */
5098 &core_disconnect, /* remove peers on disconnects */
5099 NULL, /* Don't notify about all incoming messages */
5100 GNUNET_NO, /* For header only in notification */
5101 NULL, /* Don't notify about all outbound messages */
5102 GNUNET_NO, /* For header-only out notification */
5103 core_handlers); /* Register these handlers */
5112 /******************************************************************************/
5113 /************************ MAIN FUNCTIONS ****************************/
5114 /******************************************************************************/
5117 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5119 * @param cls closure
5120 * @param key current key code
5121 * @param value value in the hash map
5122 * @return GNUNET_YES if we should continue to iterate,
5126 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5128 struct MeshTunnel *t = value;
5135 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5137 * @param cls closure
5138 * @param key current key code
5139 * @param value value in the hash map
5140 * @return GNUNET_YES if we should continue to iterate,
5144 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5146 struct MeshPeerInfo *p = value;
5147 struct MeshPeerQueue *q;
5148 struct MeshPeerQueue *n;
5156 queue_destroy(q, GNUNET_YES);
5160 peer_info_destroy (p);
5166 * Task run during shutdown.
5172 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5176 if (core_handle != NULL)
5178 GNUNET_CORE_disconnect (core_handle);
5181 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5182 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5183 if (dht_handle != NULL)
5185 GNUNET_DHT_disconnect (dht_handle);
5190 GNUNET_SERVER_notification_context_destroy (nc);
5193 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5195 GNUNET_SCHEDULER_cancel (announce_id_task);
5196 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5203 * Process mesh requests.
5205 * @param cls closure
5206 * @param server the initialized server
5207 * @param c configuration to use
5210 run (void *cls, struct GNUNET_SERVER_Handle *server,
5211 const struct GNUNET_CONFIGURATION_Handle *c)
5214 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5217 server_handle = server;
5218 GNUNET_SERVER_suspend (server_handle);
5221 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5226 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5227 "mesh", "peer/privatekey");
5228 GNUNET_SCHEDULER_shutdown ();
5233 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5234 &refresh_path_time))
5236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5238 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5239 "mesh", "refresh path time");
5240 GNUNET_SCHEDULER_shutdown ();
5245 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5250 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5251 "mesh", "id announce time");
5252 GNUNET_SCHEDULER_shutdown ();
5257 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5262 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5263 "mesh", "connect timeout");
5264 GNUNET_SCHEDULER_shutdown ();
5269 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5274 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5275 "mesh", "max msgs queue");
5276 GNUNET_SCHEDULER_shutdown ();
5281 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5286 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5287 "mesh", "max tunnels");
5288 GNUNET_SCHEDULER_shutdown ();
5293 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5298 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5299 "mesh", "default ttl", 64);
5304 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5307 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5308 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5309 "mesh", "max peers", 1000);
5314 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5322 "Mesh is running with drop mode enabled. "
5323 "This is NOT a good idea! "
5324 "Remove the DROP_PERCENT option from your configuration.\n");
5328 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5329 &dht_replication_level))
5331 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5333 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5334 "mesh", "dht replication level", 3);
5335 dht_replication_level = 3;
5338 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5339 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5340 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5341 ports = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5343 dht_handle = GNUNET_DHT_connect (c, 64);
5344 if (NULL == dht_handle)
5348 stats = GNUNET_STATISTICS_create ("mesh", c);
5350 /* Scheduled the task to clean up when shutdown is called */
5351 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5353 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5354 GNUNET_free (keyfile);
5355 GNUNET_assert (NULL != pk);
5356 my_private_key = pk;
5357 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5358 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5359 &my_full_id.hashPubKey);
5360 myid = GNUNET_PEER_intern (&my_full_id);
5361 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5362 "Mesh for peer [%s] starting\n",
5363 GNUNET_i2s(&my_full_id));
5365 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5366 NULL, /* Closure passed to MESH functions */
5367 &core_init, /* Call core_init once connected */
5368 &core_connect, /* Handle connects */
5369 &core_disconnect, /* remove peers on disconnects */
5370 NULL, /* Don't notify about all incoming messages */
5371 GNUNET_NO, /* For header only in notification */
5372 NULL, /* Don't notify about all outbound messages */
5373 GNUNET_NO, /* For header-only out notification */
5374 core_handlers); /* Register these handlers */
5375 if (NULL == core_handle)
5378 GNUNET_SCHEDULER_shutdown ();
5382 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5383 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5389 * The main function for the mesh service.
5391 * @param argc number of arguments from the command line
5392 * @param argv command line arguments
5393 * @return 0 ok, 1 on error
5396 main (int argc, char *const *argv)
5401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5402 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5404 ret = (GNUNET_OK == r) ? 0 : 1;
5405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5409 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5410 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5411 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);