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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK %u\n", ack);
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2127 " last %u, qmax %u, q %u\n",
2128 t->prev_fc.last_pid_recv, t->queue_max, t->next_fc.queue_n);
2129 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2135 t->prev_fc.last_ack_sent = ack;
2136 if (NULL != t->owner)
2137 send_local_ack (t, t->owner, ack, GNUNET_YES);
2138 else if (0 != t->prev_hop)
2139 send_ack (t, t->prev_hop, ack);
2143 t->force_ack = GNUNET_NO;
2148 * Send an ACK informing the children node/client about the available
2150 * If buffering is off, send only on behalf of root (can be self).
2151 * If buffering is on, send when sent to predecessor and buffer space is free.
2152 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2153 * the ACK itself goes "forward" (towards children/clients).
2155 * @param t Tunnel on which to send the ACK.
2156 * @param type Type of message that triggered the ACK transmission.
2159 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2163 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2164 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2165 /* Is it after data to_origin retransmission? */
2168 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2169 if (GNUNET_YES == t->nobuffer)
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 " Not sending ACK, nobuffer + traffic\n");
2176 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2177 if (NULL != t->client && GNUNET_YES == t->reliable)
2179 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2181 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
2182 tunnel_send_bck_data_ack (t);
2184 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2185 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2186 t->force_ack = GNUNET_YES;
2192 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2194 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2196 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2199 " Not sending ACK, not needed, last ack sent was %u\n",
2200 t->next_fc.last_ack_sent);
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204 " Sending BCK ACK %u (last sent: %u)\n",
2205 ack, t->next_fc.last_ack_sent);
2206 t->next_fc.last_ack_sent = ack;
2208 if (NULL != t->client)
2209 send_local_ack (t, t->client, ack, GNUNET_NO);
2210 else if (0 != t->next_hop)
2211 send_ack (t, t->next_hop, ack);
2214 t->force_ack = GNUNET_NO;
2219 * Modify the unicast message TID from global to local and send to client.
2221 * @param t Tunnel on which to send the message.
2222 * @param msg Message to modify and send.
2225 tunnel_send_client_ucast (struct MeshTunnel *t,
2226 const struct GNUNET_MESH_Data *msg)
2228 struct GNUNET_MESH_Data *copy;
2229 uint16_t size = ntohs (msg->header.size);
2232 if (size < sizeof (struct GNUNET_MESH_Data) +
2233 sizeof (struct GNUNET_MessageHeader))
2235 GNUNET_break_op (0);
2238 if (NULL == t->client)
2243 copy = (struct GNUNET_MESH_Data *) cbuf;
2244 memcpy (copy, msg, size);
2245 copy->tid = htonl (t->local_tid_dest);
2246 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2247 ©->header, GNUNET_NO);
2252 * Modify the to_origin message TID from global to local and send to client.
2254 * @param t Tunnel on which to send the message.
2255 * @param msg Message to modify and send.
2258 tunnel_send_client_to_orig (struct MeshTunnel *t,
2259 const struct GNUNET_MESH_Data *msg)
2261 struct GNUNET_MESH_Data *copy;
2262 uint16_t size = ntohs (msg->header.size);
2265 if (size < sizeof (struct GNUNET_MESH_Data) +
2266 sizeof (struct GNUNET_MessageHeader))
2268 GNUNET_break_op (0);
2271 if (NULL == t->owner)
2276 copy = (struct GNUNET_MESH_Data *) cbuf;
2277 memcpy (cbuf, msg, size);
2278 copy->tid = htonl (t->local_tid);
2279 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2280 ©->header, GNUNET_NO);
2285 * We haven't received an ACK after a certain time: restransmit the message.
2287 * @param cls Closure (MeshSentMessage with the message to restransmit)
2288 * @param tc TaskContext.
2291 tunnel_retransmit_message (void *cls,
2292 const struct GNUNET_SCHEDULER_TaskContext *tc)
2294 struct MeshSentMessage *copy = cls;
2295 struct GNUNET_MESH_Data *payload;
2298 copy->retry_task = GNUNET_SCHEDULER_NO_TASK;
2299 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2302 payload = (struct GNUNET_MESH_Data *) ©[1];
2303 hop = copy->is_forward ? copy->t->next_hop : copy->t->prev_hop;
2304 send_prebuilt_message (&payload->header, hop, copy->t);
2305 GNUNET_STATISTICS_update (stats, "# unicast retransmitted", 1, GNUNET_NO);
2306 copy->retry_timer = GNUNET_TIME_STD_BACKOFF (copy->retry_timer);
2307 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
2308 &tunnel_retransmit_message,
2314 * @brief Re-initiate traffic to this peer if necessary.
2316 * Check if there is traffic queued towards this peer
2317 * and the core transmit handle is NULL (traffic was stalled).
2318 * If so, call core tmt rdy.
2320 * @param peer_id Short ID of peer to which initiate traffic.
2323 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2325 struct MeshPeerInfo *peer;
2326 struct GNUNET_PeerIdentity id;
2327 struct MeshPeerQueue *q;
2330 peer = peer_get_short (peer_id);
2331 if (NULL != peer->core_transmit)
2334 q = queue_get_next (peer);
2337 /* Might br multicast traffic already sent to this particular peer but
2338 * not to other children in this tunnel.
2339 * This way t->queue_n would be > 0 but the queue of this particular peer
2345 GNUNET_PEER_resolve (peer->id, &id);
2346 peer->core_transmit =
2347 GNUNET_CORE_notify_transmit_ready(core_handle,
2350 GNUNET_TIME_UNIT_FOREVER_REL,
2360 * Send a message to all peers and clients in this tunnel that the tunnel
2361 * is no longer valid. If some peer or client should not receive the message,
2362 * should be zero'ed out before calling this function.
2364 * @param t The tunnel whose peers and clients to notify.
2367 tunnel_send_destroy (struct MeshTunnel *t)
2369 struct GNUNET_MESH_TunnelDestroy msg;
2370 struct GNUNET_PeerIdentity id;
2372 msg.header.size = htons (sizeof (msg));
2373 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2374 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2375 msg.tid = htonl (t->id.tid);
2376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2377 " sending tunnel destroy for tunnel: %s [%X]\n",
2378 GNUNET_i2s (&msg.oid), t->id.tid);
2380 if (NULL == t->client && 0 != t->next_hop)
2382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2383 GNUNET_PEER_resolve (t->next_hop, &id);
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385 " sending forward to %s\n",
2387 send_prebuilt_message (&msg.header, t->next_hop, t);
2389 if (NULL == t->owner && 0 != t->prev_hop)
2391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2392 GNUNET_PEER_resolve (t->prev_hop, &id);
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2394 " sending back to %s\n",
2396 send_prebuilt_message (&msg.header, t->prev_hop, t);
2398 if (NULL != t->owner)
2400 send_client_tunnel_destroy (t->owner, t);
2402 if (NULL != t->client)
2404 send_client_tunnel_destroy (t->client, t);
2410 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2412 * @param t Tunnel which to cancel.
2413 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2416 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2418 struct MeshPeerInfo *peer_info;
2419 struct MeshPeerQueue *pq;
2420 struct MeshPeerQueue *next;
2423 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2424 peer_info = peer_get_short (neighbor);
2425 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2428 if (pq->tunnel == t)
2430 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2431 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2433 /* Should have been removed on destroy children */
2436 queue_destroy (pq, GNUNET_YES);
2439 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2441 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2442 peer_info->core_transmit = NULL;
2448 * Destroy the tunnel.
2450 * This function does not generate any warning traffic to clients or peers.
2453 * Remove the tunnel from peer_info's and clients' hashmaps.
2454 * Cancel messages belonging to this tunnel queued to neighbors.
2455 * Free any allocated resources linked to the tunnel.
2457 * @param t the tunnel to destroy
2459 * @return GNUNET_OK on success
2462 tunnel_destroy (struct MeshTunnel *t)
2464 struct MeshClient *c;
2465 struct GNUNET_HashCode hash;
2475 struct GNUNET_PeerIdentity id;
2477 GNUNET_PEER_resolve (t->id.oid, &id);
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2479 GNUNET_i2s (&id), t->id.tid);
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2485 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2486 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2494 GMC_hash32 (t->local_tid, &hash);
2496 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2503 if (NULL != t->client)
2506 GMC_hash32 (t->local_tid_dest, &hash);
2508 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2514 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2521 if (0 != t->prev_hop)
2523 peer_cancel_queues (t->prev_hop, t);
2524 GNUNET_PEER_change_rc (t->prev_hop, -1);
2526 if (0 != t->next_hop)
2528 peer_cancel_queues (t->next_hop, t);
2529 GNUNET_PEER_change_rc (t->next_hop, -1);
2532 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2535 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2536 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2539 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2540 path_destroy (t->path);
2546 * Tunnel is empty: destroy it.
2548 * Notifies all participants (peers, cleints) about the destruction.
2550 * @param t Tunnel to destroy.
2553 tunnel_destroy_empty (struct MeshTunnel *t)
2557 struct GNUNET_PeerIdentity id;
2559 GNUNET_PEER_resolve (t->id.oid, &id);
2560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2561 "executing destruction of empty tunnel %s [%X]\n",
2562 GNUNET_i2s (&id), t->id.tid);
2566 if (GNUNET_NO == t->destroy)
2567 tunnel_send_destroy (t);
2568 if (0 == t->pending_messages)
2571 t->destroy = GNUNET_YES;
2575 * Initialize a Flow Control structure to the initial state.
2577 * @param fc Flow Control structure to initialize.
2580 fc_init (struct MeshFlowControl *fc)
2582 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2583 fc->last_pid_recv = (uint32_t) -1;
2584 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2585 fc->last_ack_recv = (uint32_t) -1;
2586 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2587 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2592 * Create a new tunnel
2594 * @param owner Who is the owner of the tunnel (short ID).
2595 * @param tid Tunnel Number of the tunnel.
2596 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2597 * @param local Tunnel Number for the tunnel, for the client point of view.
2599 * @return A new initialized tunnel. NULL on error.
2601 static struct MeshTunnel *
2602 tunnel_new (GNUNET_PEER_Id owner,
2603 MESH_TunnelNumber tid,
2604 struct MeshClient *client,
2605 MESH_TunnelNumber local)
2607 struct MeshTunnel *t;
2608 struct GNUNET_HashCode hash;
2610 if (n_tunnels >= max_tunnels && NULL == client)
2613 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2616 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2618 fc_init (&t->next_fc);
2619 fc_init (&t->prev_fc);
2620 t->local_tid = local;
2622 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2624 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2626 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2627 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2634 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2641 GMC_hash32 (t->local_tid, &hash);
2643 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2644 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2648 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2658 * Set options in a tunnel, extracted from a bit flag field
2660 * @param t Tunnel to set options to.
2661 * @param options Bit array in host byte order.
2664 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2666 t->nobuffer = options & GNUNET_MESH_OPTION_NOBUFFER;
2667 t->reliable = options & GNUNET_MESH_OPTION_RELIABLE;
2672 * Iterator for deleting each tunnel whose client endpoint disconnected.
2674 * @param cls Closure (client that has disconnected).
2675 * @param key The hash of the local tunnel id (used to access the hashmap).
2676 * @param value The value stored at the key (tunnel to destroy).
2678 * @return GNUNET_OK, keep iterating.
2681 tunnel_destroy_iterator (void *cls,
2682 const struct GNUNET_HashCode * key,
2685 struct MeshTunnel *t = value;
2686 struct MeshClient *c = cls;
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2689 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2690 t->local_tid, t->local_tid_dest, c->id);
2691 client_delete_tunnel (c, t);
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2696 if (0 != t->next_hop) { /* destroy could come before a path is used */
2697 GNUNET_PEER_change_rc (t->next_hop, -1);
2701 else if (c == t->owner)
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2705 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2706 GNUNET_PEER_change_rc (t->prev_hop, -1);
2714 tunnel_destroy_empty (t);
2721 * Timeout function, destroys tunnel if called
2723 * @param cls Closure (tunnel to destroy).
2724 * @param tc TaskContext
2727 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2729 struct MeshTunnel *t = cls;
2730 struct GNUNET_PeerIdentity id;
2732 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2733 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2735 GNUNET_PEER_resolve(t->id.oid, &id);
2736 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2737 "Tunnel %s [%X] timed out. Destroying.\n",
2738 GNUNET_i2s(&id), t->id.tid);
2739 if (NULL != t->client)
2740 send_client_tunnel_destroy (t->client, t);
2741 tunnel_destroy (t); /* Do not notify other */
2746 * Resets the tunnel timeout. Starts it if no timeout was running.
2748 * @param t Tunnel whose timeout to reset.
2750 * TODO use heap to improve efficiency of scheduler.
2753 tunnel_reset_timeout (struct MeshTunnel *t)
2755 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2757 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2758 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2759 t->maintenance_task =
2760 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2761 (refresh_path_time, 4), &tunnel_timeout, t);
2765 /******************************************************************************/
2766 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2767 /******************************************************************************/
2770 * Function to send a create path packet to a peer.
2772 * @param cls closure
2773 * @param size number of bytes available in buf
2774 * @param buf where the callee should write the message
2775 * @return number of bytes written to buf
2778 send_core_path_create (void *cls, size_t size, void *buf)
2780 struct MeshTunnel *t = cls;
2781 struct GNUNET_MESH_CreateTunnel *msg;
2782 struct GNUNET_PeerIdentity *peer_ptr;
2783 struct MeshPeerPath *p = t->path;
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2790 sizeof (struct GNUNET_MESH_CreateTunnel) +
2791 p->length * sizeof (struct GNUNET_PeerIdentity);
2793 if (size < size_needed || NULL == buf)
2798 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2799 msg->header.size = htons (size_needed);
2800 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2801 msg->tid = ntohl (t->id.tid);
2804 if (GNUNET_YES == t->nobuffer)
2805 opt |= GNUNET_MESH_OPTION_NOBUFFER;
2806 if (GNUNET_YES == t->reliable)
2807 opt |= GNUNET_MESH_OPTION_RELIABLE;
2808 msg->opt = htonl (opt);
2809 msg->port = htonl (t->port);
2811 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2812 for (i = 0; i < p->length; i++)
2814 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2818 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2824 * Creates a path ack message in buf and frees all unused resources.
2826 * @param cls closure (MeshTransmissionDescriptor)
2827 * @param size number of bytes available in buf
2828 * @param buf where the callee should write the message
2829 * @return number of bytes written to buf
2832 send_core_path_ack (void *cls, size_t size, void *buf)
2834 struct MeshTunnel *t = cls;
2835 struct GNUNET_MESH_PathACK *msg = buf;
2837 GNUNET_assert (NULL != t);
2838 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2843 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2844 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2845 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2846 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2847 msg->tid = htonl (t->id.tid);
2848 msg->peer_id = my_full_id;
2849 msg->ack = htonl (t->prev_fc.last_ack_sent);
2851 /* TODO add signature */
2853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2854 return sizeof (struct GNUNET_MESH_PathACK);
2859 * Free a transmission that was already queued with all resources
2860 * associated to the request.
2862 * @param queue Queue handler to cancel.
2863 * @param clear_cls Is it necessary to free associated cls?
2866 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2868 struct MeshFlowControl *fc;
2870 if (GNUNET_YES == clear_cls)
2872 switch (queue->type)
2874 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2875 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2876 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2878 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2879 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2880 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2881 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2882 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2884 " prebuilt message\n");
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2887 GNUNET_MESH_DEBUG_M2S (queue->type));
2889 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2894 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2895 " type %s unknown!\n",
2896 GNUNET_MESH_DEBUG_M2S (queue->type));
2898 GNUNET_free_non_null (queue->cls);
2900 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2901 queue->peer->queue_tail,
2904 /* Delete from appropriate fc in the tunnel */
2905 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
2906 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
2908 if (queue->peer->id == queue->tunnel->prev_hop)
2909 fc = &queue->tunnel->prev_fc;
2910 else if (queue->peer->id == queue->tunnel->next_hop)
2911 fc = &queue->tunnel->next_fc;
2919 GNUNET_free (queue);
2924 * @brief Get the next transmittable message from the queue.
2926 * This will be the head, except in the case of being a data packet
2927 * not allowed by the destination peer.
2929 * @param peer Destination peer.
2931 * @return The next viable MeshPeerQueue element to send to that peer.
2932 * NULL when there are no transmittable messages.
2934 struct MeshPeerQueue *
2935 queue_get_next (const struct MeshPeerInfo *peer)
2937 struct MeshPeerQueue *q;
2939 struct GNUNET_MESH_Data *dmsg;
2940 struct MeshTunnel* t;
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2945 for (q = peer->queue_head; NULL != q; q = q->next)
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2950 GNUNET_MESH_DEBUG_M2S (q->type));
2951 dmsg = (struct GNUNET_MESH_Data *) q->cls;
2952 pid = ntohl (dmsg->pid);
2955 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2956 ack = t->next_fc.last_ack_recv;
2958 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2959 ack = t->prev_fc.last_ack_recv;
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2967 "* ACK: %u, PID: %u\n",
2969 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2982 "* nothing found\n");
2988 queue_send (void *cls, size_t size, void *buf)
2990 struct MeshPeerInfo *peer = cls;
2991 struct GNUNET_MessageHeader *msg;
2992 struct MeshPeerQueue *queue;
2993 struct MeshTunnel *t;
2994 struct GNUNET_PeerIdentity dst_id;
2995 struct MeshFlowControl *fc;
3000 peer->core_transmit = NULL;
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3003 queue = queue_get_next (peer);
3005 /* Queue has no internal mesh traffic nor sendable payload */
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3009 if (NULL == peer->queue_head)
3010 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3015 GNUNET_PEER_resolve (peer->id, &dst_id);
3016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3018 GNUNET_i2s (&dst_id));
3019 /* Check if buffer size is enough for the message */
3020 if (queue->size > size)
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3023 "* not enough room, reissue\n");
3024 peer->core_transmit =
3025 GNUNET_CORE_notify_transmit_ready (core_handle,
3028 GNUNET_TIME_UNIT_FOREVER_REL,
3035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3038 GNUNET_assert (0 < t->pending_messages);
3039 t->pending_messages--;
3043 switch (queue->type)
3046 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3047 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3048 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3049 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3050 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3051 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3054 GNUNET_MESH_DEBUG_M2S (queue->type));
3056 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3057 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3058 data_size = send_core_data_raw (queue->cls, size, buf);
3059 msg = (struct GNUNET_MessageHeader *) buf;
3060 type = ntohs (msg->type);
3062 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3064 data_size = send_core_path_create (queue->cls, size, buf);
3066 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3068 data_size = send_core_path_ack (queue->cls, size, buf);
3072 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3073 "* type unknown: %u\n",
3078 if (0 < drop_percent &&
3079 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3081 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3082 "Dropping message of type %s\n",
3083 GNUNET_MESH_DEBUG_M2S(queue->type));
3086 /* Free queue, but cls was freed by send_core_* */
3087 queue_destroy (queue, GNUNET_NO);
3089 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3090 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3093 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3094 t->next_fc.last_pid_sent = pid;
3095 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3097 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3098 t->prev_fc.last_pid_sent = pid;
3099 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3105 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3111 /* If more data in queue, send next */
3112 queue = queue_get_next (peer);
3115 struct GNUNET_PeerIdentity id;
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3118 GNUNET_PEER_resolve (peer->id, &id);
3119 peer->core_transmit =
3120 GNUNET_CORE_notify_transmit_ready(core_handle,
3123 GNUNET_TIME_UNIT_FOREVER_REL,
3129 else if (NULL != peer->queue_head)
3131 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3133 GNUNET_i2s (&my_full_id));
3134 if (peer->id == t->next_hop)
3136 else if (peer->id == t->prev_hop)
3143 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3146 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3156 * @brief Queue and pass message to core when possible.
3158 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3159 * accounts for it. In case the queue is full, the message is dropped and
3162 * Otherwise, message is treated as internal and allowed to go regardless of
3165 * @param cls Closure (@c type dependant). It will be used by queue_send to
3166 * build the message to be sent if not already prebuilt.
3167 * @param type Type of the message, 0 for a raw message.
3168 * @param size Size of the message.
3169 * @param dst Neighbor to send message to.
3170 * @param t Tunnel this message belongs to.
3173 queue_add (void *cls, uint16_t type, size_t size,
3174 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3176 struct MeshPeerQueue *queue;
3177 struct GNUNET_PeerIdentity id;
3181 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3183 n = &t->next_fc.queue_n;
3185 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3187 n = &t->prev_fc.queue_n;
3191 if (*n >= t->queue_max)
3194 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3195 "queue full: %u/%u\n",
3197 GNUNET_STATISTICS_update(stats,
3198 "# messages dropped (buffer full)",
3200 return; /* Drop message */
3204 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3210 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3211 if (NULL == dst->core_transmit)
3213 GNUNET_PEER_resolve (dst->id, &id);
3214 dst->core_transmit =
3215 GNUNET_CORE_notify_transmit_ready (core_handle,
3218 GNUNET_TIME_UNIT_FOREVER_REL,
3224 t->pending_messages++;
3228 /******************************************************************************/
3229 /******************** MESH NETWORK HANDLERS **************************/
3230 /******************************************************************************/
3234 * Core handler for path creation
3236 * @param cls closure
3237 * @param message message
3238 * @param peer peer identity this notification is about
3240 * @return GNUNET_OK to keep the connection open,
3241 * GNUNET_SYSERR to close it (signal serious error)
3244 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3245 const struct GNUNET_MessageHeader *message)
3247 unsigned int own_pos;
3250 MESH_TunnelNumber tid;
3251 struct GNUNET_MESH_CreateTunnel *msg;
3252 struct GNUNET_PeerIdentity *pi;
3253 struct MeshPeerPath *path;
3254 struct MeshPeerInfo *dest_peer_info;
3255 struct MeshPeerInfo *orig_peer_info;
3256 struct MeshTunnel *t;
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 "Received a path create msg [%s]\n",
3260 GNUNET_i2s (&my_full_id));
3261 size = ntohs (message->size);
3262 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3264 GNUNET_break_op (0);
3268 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3269 if (size % sizeof (struct GNUNET_PeerIdentity))
3271 GNUNET_break_op (0);
3274 size /= sizeof (struct GNUNET_PeerIdentity);
3277 GNUNET_break_op (0);
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3281 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3283 tid = ntohl (msg->tid);
3284 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3286 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3287 t = tunnel_get (pi, tid);
3288 if (NULL == t) /* might be a local tunnel */
3292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3293 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3299 t->port = ntohl (msg->port);
3300 opt = ntohl (msg->opt);
3301 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3303 t->nobuffer = GNUNET_YES;
3306 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3308 t->reliable = GNUNET_YES;
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3312 tunnel_reset_timeout (t);
3314 t->state = MESH_TUNNEL_WAITING;
3316 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3317 if (NULL == dest_peer_info)
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3320 " Creating PeerInfo for destination.\n");
3321 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3322 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3323 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3325 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3327 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3328 if (NULL == orig_peer_info)
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3331 " Creating PeerInfo for origin.\n");
3332 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3333 orig_peer_info->id = GNUNET_PEER_intern (pi);
3334 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3335 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3338 path = path_new (size);
3340 for (i = 0; i < size; i++)
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3343 GNUNET_i2s (&pi[i]));
3344 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3345 if (path->peers[i] == myid)
3348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3349 if (own_pos == 0 && path->peers[own_pos] != myid)
3351 /* create path: self not found in path through self */
3352 GNUNET_break_op (0);
3353 path_destroy (path);
3357 path_add_to_peers (path, GNUNET_NO);
3358 tunnel_use_path (t, path);
3360 peer_info_add_tunnel (dest_peer_info, t);
3362 if (own_pos == size - 1)
3364 struct MeshClient *c;
3365 struct GNUNET_HashCode hc;
3367 /* Find target client */
3368 GMC_hash32 (t->port, &hc);
3369 c = GNUNET_CONTAINER_multihashmap_get (ports, &hc);
3372 /* TODO send reject */
3376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3377 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3379 /* Assign local tid */
3380 while (NULL != tunnel_get_incoming (next_local_tid))
3381 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3382 t->local_tid_dest = next_local_tid++;
3383 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3385 if (GNUNET_YES == t->reliable)
3386 t->sent_messages_bck =
3387 GNUNET_CONTAINER_multihashmap32_create (t->queue_max);
3389 tunnel_add_client (t, c);
3390 send_client_tunnel_create (t);
3395 struct MeshPeerPath *path2;
3397 t->next_hop = path->peers[own_pos + 1];
3398 GNUNET_PEER_change_rc(t->next_hop, 1);
3400 /* It's for somebody else! Retransmit. */
3401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3402 path2 = path_duplicate (path);
3403 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3404 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3405 send_create_path (t);
3413 * Core handler for path ACKs
3415 * @param cls closure
3416 * @param message message
3417 * @param peer peer identity this notification is about
3419 * @return GNUNET_OK to keep the connection open,
3420 * GNUNET_SYSERR to close it (signal serious error)
3423 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3424 const struct GNUNET_MessageHeader *message)
3426 struct GNUNET_MESH_PathACK *msg;
3427 struct MeshPeerInfo *peer_info;
3428 struct MeshPeerPath *p;
3429 struct MeshTunnel *t;
3431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3432 GNUNET_i2s (&my_full_id));
3433 msg = (struct GNUNET_MESH_PathACK *) message;
3434 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3437 /* TODO notify that we don't know the tunnel */
3438 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3440 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3444 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3446 peer_info = peer_get (&msg->peer_id);
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3448 GNUNET_i2s (&msg->peer_id));
3449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3452 /* Add path to peers? */
3456 path_add_to_peers (p, GNUNET_YES);
3462 t->state = MESH_TUNNEL_READY;
3463 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3464 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3466 /* Message for us? */
3467 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3470 if (NULL == t->owner)
3472 GNUNET_break_op (0);
3475 if (NULL != peer_info->dhtget)
3477 GNUNET_DHT_get_stop (peer_info->dhtget);
3478 peer_info->dhtget = NULL;
3480 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3481 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3486 " not for us, retransmitting...\n");
3487 peer_info = peer_get (&msg->oid);
3488 send_prebuilt_message (message, t->prev_hop, t);
3494 * Core handler for notifications of broken paths
3496 * @param cls closure
3497 * @param message message
3498 * @param peer peer identity this notification is about
3500 * @return GNUNET_OK to keep the connection open,
3501 * GNUNET_SYSERR to close it (signal serious error)
3504 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3505 const struct GNUNET_MessageHeader *message)
3507 struct GNUNET_MESH_PathBroken *msg;
3508 struct MeshTunnel *t;
3510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3511 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3512 msg = (struct GNUNET_MESH_PathBroken *) message;
3513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3514 GNUNET_i2s (&msg->peer1));
3515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3516 GNUNET_i2s (&msg->peer2));
3517 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3520 GNUNET_break_op (0);
3523 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3524 GNUNET_PEER_search (&msg->peer2));
3531 * Core handler for tunnel destruction
3533 * @param cls closure
3534 * @param message message
3535 * @param peer peer identity this notification is about
3537 * @return GNUNET_OK to keep the connection open,
3538 * GNUNET_SYSERR to close it (signal serious error)
3541 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3542 const struct GNUNET_MessageHeader *message)
3544 struct GNUNET_MESH_TunnelDestroy *msg;
3545 struct MeshTunnel *t;
3547 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3549 "Got a TUNNEL DESTROY packet from %s\n",
3551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3552 " for tunnel %s [%u]\n",
3553 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3554 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3557 /* Probably already got the message from another path,
3558 * destroyed the tunnel and retransmitted to children.
3561 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3565 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3568 t->local_tid, t->local_tid_dest);
3570 if (GNUNET_PEER_search (peer) == t->prev_hop)
3572 // TODO check owner's signature
3573 // TODO add owner's signatue to tunnel for retransmission
3574 peer_cancel_queues (t->prev_hop, t);
3575 GNUNET_PEER_change_rc (t->prev_hop, -1);
3578 else if (GNUNET_PEER_search (peer) == t->next_hop)
3580 // TODO check dest's signature
3581 // TODO add dest's signatue to tunnel for retransmission
3582 peer_cancel_queues (t->next_hop, t);
3583 GNUNET_PEER_change_rc (t->next_hop, -1);
3588 GNUNET_break_op (0);
3589 // TODO check both owner AND destination's signature to see which matches
3590 // TODO restransmit in appropriate direction
3593 tunnel_destroy_empty (t);
3595 // TODO: add timeout to destroy the tunnel anyway
3601 * Core handler for mesh network traffic going from the origin to a peer
3603 * @param cls closure
3604 * @param peer peer identity this notification is about
3605 * @param message message
3606 * @return GNUNET_OK to keep the connection open,
3607 * GNUNET_SYSERR to close it (signal serious error)
3610 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3611 const struct GNUNET_MessageHeader *message)
3613 struct GNUNET_MESH_Data *msg;
3614 struct MeshTunnel *t;
3619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3622 size = ntohs (message->size);
3624 sizeof (struct GNUNET_MESH_Data) +
3625 sizeof (struct GNUNET_MessageHeader))
3630 msg = (struct GNUNET_MESH_Data *) message;
3631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3632 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3634 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3637 /* TODO notify back: we don't know this tunnel */
3638 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3639 GNUNET_break_op (0);
3642 pid = ntohl (msg->pid);
3643 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3645 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3646 GNUNET_break_op (0);
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 "Received PID %u, ACK %u\n",
3649 pid, t->prev_fc.last_ack_sent);
3650 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3654 tunnel_reset_timeout (t);
3655 if (t->dest == myid)
3657 /* TODO signature verification */
3658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3659 " it's for us! sending to clients...\n");
3660 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3661 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
3662 if ( (GNUNET_NO == t->reliable &&
3663 GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv))
3665 (GNUNET_YES == t->reliable &&
3666 pid == t->prev_fc.last_pid_recv + 1) )
3668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3669 " pid %u not seen yet, forwarding\n", pid);
3670 t->prev_fc.last_pid_recv = pid;
3671 tunnel_send_client_ucast (t, msg);
3672 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3676 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
3677 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3678 " Pid %u not expected, sending FWD ACK!\n", pid);
3679 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3683 t->prev_fc.last_pid_recv = pid;
3684 if (0 == t->next_hop)
3689 ttl = ntohl (msg->ttl);
3690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3693 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3694 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3695 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3699 " not for us, retransmitting...\n");
3701 send_prebuilt_message (message, t->next_hop, t);
3702 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3708 * Core handler for mesh network traffic toward the owner of a tunnel
3710 * @param cls closure
3711 * @param message message
3712 * @param peer peer identity this notification is about
3714 * @return GNUNET_OK to keep the connection open,
3715 * GNUNET_SYSERR to close it (signal serious error)
3718 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3719 const struct GNUNET_MessageHeader *message)
3721 struct GNUNET_MESH_Data *msg;
3722 struct MeshTunnel *t;
3727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3729 size = ntohs (message->size);
3730 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
3731 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3733 GNUNET_break_op (0);
3736 msg = (struct GNUNET_MESH_Data *) message;
3737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3738 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3739 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3740 pid = ntohl (msg->pid);
3743 /* TODO notify that we dont know this tunnel (whom)? */
3744 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3746 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3747 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3751 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3753 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3754 GNUNET_break_op (0);
3755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3756 "Received PID %u, ACK %u\n",
3757 pid, t->next_fc.last_ack_sent);
3758 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3762 if (myid == t->id.oid)
3764 /* TODO signature verification */
3765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3766 " it's for us! sending to clients...\n");
3767 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3768 if ( (GNUNET_NO == t->reliable &&
3769 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
3771 (GNUNET_YES == t->reliable &&
3772 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
3774 t->next_fc.last_pid_recv = pid;
3775 tunnel_send_client_to_orig (t, msg);
3776 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3780 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3781 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3782 " Pid %u not expected, sending FWD ACK!\n", pid);
3783 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3788 " not for us, retransmitting...\n");
3789 t->next_fc.last_pid_recv = pid;
3790 if (0 == t->prev_hop) /* No owner AND no prev hop */
3792 if (GNUNET_YES == t->destroy)
3794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3795 "to orig received on a dying tunnel %s [%X]\n",
3796 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3800 "unknown to origin at %s\n",
3801 GNUNET_i2s (&my_full_id));
3802 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3806 "on tunnel %s [%X]\n",
3807 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3810 ttl = ntohl (msg->ttl);
3811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3814 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3815 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3816 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3819 send_prebuilt_message (message, t->prev_hop, t);
3820 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3827 * Core handler for mesh network traffic end-to-end ACKs.
3829 * @param cls Closure.
3830 * @param message Message.
3831 * @param peer Peer identity this notification is about.
3833 * @return GNUNET_OK to keep the connection open,
3834 * GNUNET_SYSERR to close it (signal serious error)
3837 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3838 const struct GNUNET_MessageHeader *message)
3840 struct GNUNET_MESH_DataACK *msg;
3841 struct GNUNET_CONTAINER_MultiHashMap32 *hm;
3842 struct MeshSentMessage *copy;
3843 struct MeshTunnel *t;
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a DATA ACK message from %s!\n",
3849 msg = (struct GNUNET_MESH_DataACK *) message;
3851 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3854 /* TODO notify that we dont know this tunnel (whom)? */
3855 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3858 ack = ntohl (msg->pid);
3859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3861 /* Is this a forward or backward ACK? */
3862 id = GNUNET_PEER_search (peer);
3863 if (t->next_hop == id)
3865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3866 if (NULL == t->owner)
3868 send_prebuilt_message (message, t->prev_hop, t);
3871 hm = t->sent_messages_fwd;
3872 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3874 else if (t->prev_hop == id)
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3877 if (NULL == t->client)
3879 send_prebuilt_message (message, t->next_hop, t);
3882 hm = t->sent_messages_bck;
3883 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
3886 GNUNET_break_op (0);
3888 copy = GNUNET_CONTAINER_multihashmap32_get (hm, ack);
3891 GNUNET_break (0); // FIXME needed?
3894 GNUNET_break (GNUNET_YES ==
3895 GNUNET_CONTAINER_multihashmap32_remove (hm, ack, copy));
3896 if (GNUNET_SCHEDULER_NO_TASK != copy->retry_task)
3898 GNUNET_SCHEDULER_cancel (copy->retry_task);
3907 * Core handler for mesh network traffic point-to-point acks.
3909 * @param cls closure
3910 * @param message message
3911 * @param peer peer identity this notification is about
3913 * @return GNUNET_OK to keep the connection open,
3914 * GNUNET_SYSERR to close it (signal serious error)
3917 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3918 const struct GNUNET_MessageHeader *message)
3920 struct GNUNET_MESH_ACK *msg;
3921 struct MeshTunnel *t;
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3927 msg = (struct GNUNET_MESH_ACK *) message;
3929 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3933 /* TODO notify that we dont know this tunnel (whom)? */
3934 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3937 ack = ntohl (msg->pid);
3938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3940 /* Is this a forward or backward ACK? */
3941 id = GNUNET_PEER_search (peer);
3942 if (t->next_hop == id)
3945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3946 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3947 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3949 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3950 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3951 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3953 t->next_fc.last_ack_recv = ack;
3954 peer_unlock_queue (t->next_hop);
3955 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3957 else if (t->prev_hop == id)
3960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3961 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3962 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3964 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3965 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3966 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3968 t->prev_fc.last_ack_recv = ack;
3969 peer_unlock_queue (t->prev_hop);
3970 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3973 GNUNET_break_op (0);
3979 * Core handler for mesh network traffic point-to-point ack polls.
3981 * @param cls closure
3982 * @param message message
3983 * @param peer peer identity this notification is about
3985 * @return GNUNET_OK to keep the connection open,
3986 * GNUNET_SYSERR to close it (signal serious error)
3989 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3990 const struct GNUNET_MessageHeader *message)
3992 struct GNUNET_MESH_Poll *msg;
3993 struct MeshTunnel *t;
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3999 msg = (struct GNUNET_MESH_Poll *) message;
4001 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4005 /* TODO notify that we dont know this tunnel (whom)? */
4006 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4007 GNUNET_break_op (0);
4011 /* Is this a forward or backward ACK? */
4012 id = GNUNET_PEER_search(peer);
4013 if (t->next_hop == id)
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4016 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4018 else if (t->prev_hop == id)
4020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4021 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4031 * Core handler for mesh keepalives.
4033 * @param cls closure
4034 * @param message message
4035 * @param peer peer identity this notification is about
4036 * @return GNUNET_OK to keep the connection open,
4037 * GNUNET_SYSERR to close it (signal serious error)
4039 * TODO: Check who we got this from, to validate route.
4042 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4043 const struct GNUNET_MessageHeader *message)
4045 struct GNUNET_MESH_TunnelKeepAlive *msg;
4046 struct MeshTunnel *t;
4048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4051 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4052 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4056 /* TODO notify that we dont know that tunnel */
4057 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4062 tunnel_reset_timeout (t);
4063 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4066 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4067 send_prebuilt_message (message, t->next_hop, t);
4074 * Functions to handle messages from core
4076 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4077 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4078 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4079 sizeof (struct GNUNET_MESH_PathBroken)},
4080 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4081 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4082 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4083 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4084 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_DATA_ACK,
4085 sizeof (struct GNUNET_MESH_DataACK)},
4086 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4087 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4088 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4089 sizeof (struct GNUNET_MESH_ACK)},
4090 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4091 sizeof (struct GNUNET_MESH_Poll)},
4092 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4093 sizeof (struct GNUNET_MESH_PathACK)},
4099 /******************************************************************************/
4100 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4101 /******************************************************************************/
4106 * notify_client_connection_failure: notify a client that the connection to the
4107 * requested remote peer is not possible (for instance, no route found)
4108 * Function called when the socket is ready to queue more data. "buf" will be
4109 * NULL and "size" zero if the socket was closed for writing in the meantime.
4111 * @param cls closure
4112 * @param size number of bytes available in buf
4113 * @param buf where the callee should write the message
4114 * @return number of bytes written to buf
4117 notify_client_connection_failure (void *cls, size_t size, void *buf)
4120 struct MeshPeerInfo *peer_info;
4121 struct GNUNET_MESH_PeerControl *msg;
4122 struct GNUNET_PeerIdentity id;
4124 if (0 == size && NULL == buf)
4126 // TODO retry? cancel?
4130 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4131 peer_info = (struct MeshPeerInfo *) cls;
4132 msg = (struct GNUNET_MESH_PeerControl *) buf;
4133 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4134 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4135 // msg->tunnel_id = htonl(peer_info->t->tid);
4136 GNUNET_PEER_resolve (peer_info->id, &id);
4137 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4145 * Send keepalive packets for a tunnel.
4147 * @param cls Closure (tunnel for which to send the keepalive).
4148 * @param tc Notification context.
4150 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4153 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4155 struct MeshTunnel *t = cls;
4156 struct GNUNET_MESH_TunnelKeepAlive *msg;
4157 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4160 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4161 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4162 NULL == t->owner || 0 == t->local_tid)
4167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4168 "sending keepalive for tunnel %d\n", t->id.tid);
4170 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4171 msg->header.size = htons (size);
4172 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4173 msg->oid = my_full_id;
4174 msg->tid = htonl (t->id.tid);
4175 send_prebuilt_message (&msg->header, t->next_hop, t);
4177 t->maintenance_task =
4178 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4183 * Function to process paths received for a new peer addition. The recorded
4184 * paths form the initial tunnel, which can be optimized later.
4185 * Called on each result obtained for the DHT search.
4187 * @param cls closure
4188 * @param exp when will this value expire
4189 * @param key key of the result
4190 * @param get_path path of the get request
4191 * @param get_path_length lenght of get_path
4192 * @param put_path path of the put request
4193 * @param put_path_length length of the put_path
4194 * @param type type of the result
4195 * @param size number of bytes in data
4196 * @param data pointer to the result data
4199 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4200 const struct GNUNET_HashCode * key,
4201 const struct GNUNET_PeerIdentity *get_path,
4202 unsigned int get_path_length,
4203 const struct GNUNET_PeerIdentity *put_path,
4204 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4205 size_t size, const void *data)
4207 struct MeshPeerInfo *peer = cls;
4208 struct MeshPeerPath *p;
4209 struct GNUNET_PeerIdentity pi;
4212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4213 GNUNET_PEER_resolve (peer->id, &pi);
4214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4216 p = path_build_from_dht (get_path, get_path_length,
4217 put_path, put_path_length);
4218 path_add_to_peers (p, GNUNET_NO);
4220 for (i = 0; i < peer->ntunnels; i++)
4222 struct GNUNET_PeerIdentity id;
4224 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4226 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4227 peer->tunnels[i]->local_tid,
4228 peer->tunnels[i]->local_tid_dest);
4229 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4232 peer_connect (peer, peer->tunnels[i]);
4240 /******************************************************************************/
4241 /********************* MESH LOCAL HANDLES **************************/
4242 /******************************************************************************/
4246 * Handler for client disconnection
4248 * @param cls closure
4249 * @param client identification of the client; NULL
4250 * for the last call when the server is destroyed
4253 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4255 struct MeshClient *c;
4256 struct MeshClient *next;
4258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4268 if (c->handle != client)
4270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4271 " ... searching %p (%u)\n",
4276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4278 GNUNET_SERVER_client_drop (c->handle);
4279 c->shutting_down = GNUNET_YES;
4280 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4281 &tunnel_destroy_iterator, c);
4282 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4283 &tunnel_destroy_iterator, c);
4284 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4285 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4287 if (NULL != c->ports)
4288 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
4290 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4293 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4302 * Handler for new clients
4304 * @param cls closure
4305 * @param client identification of the client
4306 * @param message the actual message, which includes messages the client wants
4309 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4310 const struct GNUNET_MessageHeader *message)
4312 struct GNUNET_MESH_ClientConnect *cc_msg;
4313 struct MeshClient *c;
4318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4320 /* Check data sanity */
4321 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4322 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4323 if (0 != (size % sizeof (uint32_t)))
4326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4329 size /= sizeof (uint32_t);
4331 /* Create new client structure */
4332 c = GNUNET_malloc (sizeof (struct MeshClient));
4333 c->id = next_client_id++; /* overflow not important: just for debug */
4334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4337 GNUNET_SERVER_client_keep (client);
4341 struct GNUNET_HashCode hc;
4343 p = (uint32_t *) &cc_msg[1];
4344 c->ports = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
4345 for (i = 0; i < size; i++)
4348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4349 GMC_hash32 (u32, &hc);
4351 /* store in client's hashmap */
4352 GNUNET_CONTAINER_multihashmap_put (c->ports, &hc, c,
4353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4354 /* store in global hashmap */
4355 /* FIXME only allow one client to have the port open,
4356 * have a backup hashmap with waiting clients */
4357 GNUNET_CONTAINER_multihashmap_put (ports, &hc, c,
4358 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4362 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4363 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4364 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4365 GNUNET_SERVER_notification_context_add (nc, client);
4366 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4368 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4374 * Handler for requests of new tunnels
4376 * @param cls Closure.
4377 * @param client Identification of the client.
4378 * @param message The actual message.
4381 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4382 const struct GNUNET_MessageHeader *message)
4384 struct GNUNET_MESH_TunnelMessage *t_msg;
4385 struct MeshPeerInfo *peer_info;
4386 struct MeshTunnel *t;
4387 struct MeshClient *c;
4388 MESH_TunnelNumber tid;
4390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4392 /* Sanity check for client registration */
4393 if (NULL == (c = client_get (client)))
4396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4401 /* Message size sanity check */
4402 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4409 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4411 GNUNET_i2s (&t_msg->peer));
4412 /* Sanity check for tunnel numbering */
4413 tid = ntohl (t_msg->tunnel_id);
4414 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4417 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4420 /* Sanity check for duplicate tunnel IDs */
4421 if (NULL != tunnel_get_by_local_id (c, tid))
4424 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4429 while (NULL != tunnel_get_by_pi (myid, next_tid))
4430 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4431 t = tunnel_new (myid, next_tid, c, tid);
4432 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4436 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4439 t->port = ntohl (t_msg->port);
4440 tunnel_set_options (t, ntohl (t_msg->options));
4441 if (GNUNET_YES == t->reliable)
4442 t->sent_messages_fwd =
4443 GNUNET_CONTAINER_multihashmap32_create (t->queue_max);
4444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4445 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4447 peer_info = peer_get (&t_msg->peer);
4448 peer_info_add_tunnel (peer_info, t);
4449 peer_connect (peer_info, t);
4450 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4456 * Handler for requests of deleting tunnels
4458 * @param cls closure
4459 * @param client identification of the client
4460 * @param message the actual message
4463 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4464 const struct GNUNET_MessageHeader *message)
4466 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4467 struct MeshClient *c;
4468 struct MeshTunnel *t;
4469 MESH_TunnelNumber tid;
4471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4472 "Got a DESTROY TUNNEL from client!\n");
4474 /* Sanity check for client registration */
4475 if (NULL == (c = client_get (client)))
4478 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4483 /* Message sanity check */
4484 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4491 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4493 /* Retrieve tunnel */
4494 tid = ntohl (tunnel_msg->tunnel_id);
4495 t = tunnel_get_by_local_id(c, tid);
4498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4504 /* Cleanup after the tunnel */
4505 client_delete_tunnel (c, t);
4510 else if (c == t->owner)
4512 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4516 /* The tunnel will be destroyed when the last message is transmitted. */
4517 tunnel_destroy_empty (t);
4519 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4525 * Handler for client traffic directed to one peer
4527 * @param cls closure
4528 * @param client identification of the client
4529 * @param message the actual message
4532 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4533 const struct GNUNET_MessageHeader *message)
4535 struct MeshClient *c;
4536 struct MeshTunnel *t;
4537 struct GNUNET_MESH_Data *data_msg;
4538 MESH_TunnelNumber tid;
4541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4542 "Got a unicast request from a client!\n");
4544 /* Sanity check for client registration */
4545 if (NULL == (c = client_get (client)))
4548 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4553 data_msg = (struct GNUNET_MESH_Data *) message;
4555 /* Sanity check for message size */
4556 size = ntohs (message->size);
4557 if (sizeof (struct GNUNET_MESH_Data) +
4558 sizeof (struct GNUNET_MessageHeader) > size)
4561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4565 /* Tunnel exists? */
4566 tid = ntohl (data_msg->tid);
4567 t = tunnel_get_by_local_id (c, tid);
4571 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4575 /* Is it a local tunnel? Then, does client own the tunnel? */
4576 if (t->owner->handle != client)
4579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4583 /* PID should be as expected: client<->service communication */
4584 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4587 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4588 "Unicast PID, expected %u, got %u\n",
4589 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4590 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4594 /* Ok, everything is correct, send the message
4595 * (pretend we got it from a mesh peer)
4598 struct GNUNET_MESH_Data *payload;
4600 if (GNUNET_YES == t->reliable)
4602 struct MeshSentMessage *copy;
4604 copy = GNUNET_malloc (sizeof (struct MeshSentMessage) + size);
4606 copy->id = ntohl (data_msg->pid);
4607 copy->is_forward = GNUNET_YES;
4608 copy->retry_timer = GNUNET_TIME_UNIT_MINUTES;
4609 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
4610 &tunnel_retransmit_message,
4613 GNUNET_CONTAINER_multihashmap32_put (t->sent_messages_fwd,
4616 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4619 GNUNET_SCHEDULER_cancel (copy->retry_task);
4621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4624 payload = (struct GNUNET_MESH_Data *) ©[1];
4628 static struct GNUNET_MESH_Data data_message;
4629 payload = &data_message;
4631 memcpy (payload, data_msg, size);
4632 payload->oid = my_full_id;
4633 payload->tid = htonl (t->id.tid);
4634 payload->ttl = htonl (default_ttl);
4635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4636 " calling generic handler...\n");
4637 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
4639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4640 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4647 * Handler for client traffic directed to the origin
4649 * @param cls closure
4650 * @param client identification of the client
4651 * @param message the actual message
4654 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4655 const struct GNUNET_MessageHeader *message)
4657 struct GNUNET_MESH_Data *data_msg;
4658 struct MeshFlowControl *fc;
4659 struct MeshClient *c;
4660 struct MeshTunnel *t;
4661 MESH_TunnelNumber tid;
4664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4665 "Got a ToOrigin request from a client!\n");
4666 /* Sanity check for client registration */
4667 if (NULL == (c = client_get (client)))
4670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4675 data_msg = (struct GNUNET_MESH_Data *) message;
4677 /* Sanity check for message size */
4678 size = ntohs (message->size);
4679 if (sizeof (struct GNUNET_MESH_Data) +
4680 sizeof (struct GNUNET_MessageHeader) > size)
4683 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4687 /* Tunnel exists? */
4688 tid = ntohl (data_msg->tid);
4689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4690 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4696 t = tunnel_get_by_local_id (c, tid);
4699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4700 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4702 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4706 /* It should be sent by someone who has this as incoming tunnel. */
4710 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4714 /* PID should be as expected */
4716 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4719 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4720 "To Origin PID, expected %u, got %u\n",
4721 fc->last_pid_recv + 1,
4722 ntohl (data_msg->pid));
4723 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4727 /* Ok, everything is correct, send the message
4728 * (pretend we got it from a mesh peer)
4731 struct GNUNET_MESH_Data *payload;
4733 if (GNUNET_YES == t->reliable)
4735 struct MeshSentMessage *copy;
4737 copy = GNUNET_malloc (sizeof (struct MeshSentMessage) + size);
4739 copy->id = ntohl (data_msg->pid);
4740 copy->is_forward = GNUNET_NO;
4741 copy->retry_timer = GNUNET_TIME_UNIT_MINUTES;
4742 copy->retry_task = GNUNET_SCHEDULER_add_delayed (copy->retry_timer,
4743 &tunnel_retransmit_message,
4746 GNUNET_CONTAINER_multihashmap32_put (t->sent_messages_bck,
4749 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4752 GNUNET_SCHEDULER_cancel (copy->retry_task);
4754 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4757 payload = (struct GNUNET_MESH_Data *) ©[1];
4761 static struct GNUNET_MESH_Data data_message;
4762 payload = &data_message;
4765 memcpy (payload, data_msg, size);
4766 GNUNET_PEER_resolve (t->id.oid, &payload->oid);
4767 payload->tid = htonl (t->id.tid);
4768 payload->ttl = htonl (default_ttl);
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4771 " calling generic handler...\n");
4772 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
4774 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4781 * Handler for client's ACKs for payload traffic.
4783 * @param cls Closure (unused).
4784 * @param client Identification of the client.
4785 * @param message The actual message.
4788 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4789 const struct GNUNET_MessageHeader *message)
4791 struct GNUNET_MESH_LocalAck *msg;
4792 struct MeshTunnel *t;
4793 struct MeshClient *c;
4794 MESH_TunnelNumber tid;
4797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4798 /* Sanity check for client registration */
4799 if (NULL == (c = client_get (client)))
4802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4807 msg = (struct GNUNET_MESH_LocalAck *) message;
4809 /* Tunnel exists? */
4810 tid = ntohl (msg->tunnel_id);
4811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4812 t = tunnel_get_by_local_id (c, tid);
4816 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4818 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4822 ack = ntohl (msg->ack);
4823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4825 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4828 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4829 t->prev_fc.last_ack_recv = ack;
4830 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4834 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4835 t->next_fc.last_ack_recv = ack;
4836 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4839 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4847 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4849 * @param cls Closure (client handle).
4850 * @param key Key (hashed tunnel ID, unused).
4851 * @param value Tunnel info.
4853 * @return GNUNET_YES, to keep iterating.
4856 monitor_all_tunnels_iterator (void *cls,
4857 const struct GNUNET_HashCode * key,
4860 struct GNUNET_SERVER_Client *client = cls;
4861 struct MeshTunnel *t = value;
4862 struct GNUNET_MESH_LocalMonitor *msg;
4864 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4865 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4866 msg->tunnel_id = htonl (t->id.tid);
4867 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4868 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4869 GNUNET_PEER_resolve (t->dest, &msg->destination);
4871 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4872 "* sending info about tunnel %s [%u]\n",
4873 GNUNET_i2s (&msg->owner), t->id.tid);
4875 GNUNET_SERVER_notification_context_unicast (nc, client,
4876 &msg->header, GNUNET_NO);
4882 * Handler for client's MONITOR request.
4884 * @param cls Closure (unused).
4885 * @param client Identification of the client.
4886 * @param message The actual message.
4889 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4890 const struct GNUNET_MessageHeader *message)
4892 struct MeshClient *c;
4894 /* Sanity check for client registration */
4895 if (NULL == (c = client_get (client)))
4898 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4902 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4903 "Received get tunnels request from client %u\n",
4905 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4906 monitor_all_tunnels_iterator,
4908 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4909 "Get tunnels request from client %u completed\n",
4911 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4916 * Handler for client's MONITOR_TUNNEL request.
4918 * @param cls Closure (unused).
4919 * @param client Identification of the client.
4920 * @param message The actual message.
4923 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4924 const struct GNUNET_MessageHeader *message)
4926 const struct GNUNET_MESH_LocalMonitor *msg;
4927 struct GNUNET_MESH_LocalMonitor *resp;
4928 struct MeshClient *c;
4929 struct MeshTunnel *t;
4931 /* Sanity check for client registration */
4932 if (NULL == (c = client_get (client)))
4935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4939 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4940 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4941 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4944 ntohl (msg->tunnel_id));
4945 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4948 /* We don't know the tunnel FIXME */
4949 struct GNUNET_MESH_LocalMonitor warn;
4952 GNUNET_SERVER_notification_context_unicast (nc, client,
4955 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4959 /* Initialize context */
4960 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4962 GNUNET_PEER_resolve (t->dest, &resp->destination);
4963 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4964 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4965 &resp->header, GNUNET_NO);
4968 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4969 "Monitor tunnel request from client %u completed\n",
4971 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4976 * Functions to handle messages from clients
4978 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4979 {&handle_local_new_client, NULL,
4980 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4981 {&handle_local_tunnel_create, NULL,
4982 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4983 sizeof (struct GNUNET_MESH_TunnelMessage)},
4984 {&handle_local_tunnel_destroy, NULL,
4985 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4986 sizeof (struct GNUNET_MESH_TunnelMessage)},
4987 {&handle_local_unicast, NULL,
4988 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4989 {&handle_local_to_origin, NULL,
4990 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4991 {&handle_local_ack, NULL,
4992 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4993 sizeof (struct GNUNET_MESH_LocalAck)},
4994 {&handle_local_get_tunnels, NULL,
4995 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4996 sizeof (struct GNUNET_MessageHeader)},
4997 {&handle_local_show_tunnel, NULL,
4998 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4999 sizeof (struct GNUNET_MESH_LocalMonitor)},
5005 * Method called whenever a given peer connects.
5007 * @param cls closure
5008 * @param peer peer identity this notification is about
5011 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5013 struct MeshPeerInfo *peer_info;
5014 struct MeshPeerPath *path;
5016 DEBUG_CONN ("Peer connected\n");
5017 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5018 peer_info = peer_get (peer);
5019 if (myid == peer_info->id)
5021 DEBUG_CONN (" (self)\n");
5022 path = path_new (1);
5026 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5027 path = path_new (2);
5028 path->peers[1] = peer_info->id;
5029 GNUNET_PEER_change_rc (peer_info->id, 1);
5030 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5032 path->peers[0] = myid;
5033 GNUNET_PEER_change_rc (myid, 1);
5034 peer_info_add_path (peer_info, path, GNUNET_YES);
5040 * Method called whenever a peer disconnects.
5042 * @param cls closure
5043 * @param peer peer identity this notification is about
5046 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5048 struct MeshPeerInfo *pi;
5049 struct MeshPeerQueue *q;
5050 struct MeshPeerQueue *n;
5052 DEBUG_CONN ("Peer disconnected\n");
5053 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5063 /* TODO try to reroute this traffic instead */
5064 queue_destroy(q, GNUNET_YES);
5067 if (NULL != pi->core_transmit)
5069 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5070 pi->core_transmit = NULL;
5072 peer_remove_path (pi, pi->id, myid);
5075 DEBUG_CONN (" (self)\n");
5077 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5083 * Install server (service) handlers and start listening to clients.
5088 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5089 GNUNET_SERVER_disconnect_notify (server_handle,
5090 &handle_local_client_disconnect, NULL);
5091 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5093 clients_head = NULL;
5094 clients_tail = NULL;
5096 GNUNET_SERVER_resume (server_handle);
5101 * To be called on core init/fail.
5103 * @param cls Closure (config)
5104 * @param server handle to the server for this service
5105 * @param identity the public identity of this peer
5108 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5109 const struct GNUNET_PeerIdentity *identity)
5111 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5115 GNUNET_break (core_handle == server);
5116 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5122 GNUNET_i2s (identity));
5123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5125 GNUNET_i2s (&my_full_id));
5126 GNUNET_CORE_disconnect (core_handle);
5127 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5128 NULL, /* Closure passed to MESH functions */
5129 &core_init, /* Call core_init once connected */
5130 &core_connect, /* Handle connects */
5131 &core_disconnect, /* remove peers on disconnects */
5132 NULL, /* Don't notify about all incoming messages */
5133 GNUNET_NO, /* For header only in notification */
5134 NULL, /* Don't notify about all outbound messages */
5135 GNUNET_NO, /* For header-only out notification */
5136 core_handlers); /* Register these handlers */
5145 /******************************************************************************/
5146 /************************ MAIN FUNCTIONS ****************************/
5147 /******************************************************************************/
5150 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5152 * @param cls closure
5153 * @param key current key code
5154 * @param value value in the hash map
5155 * @return GNUNET_YES if we should continue to iterate,
5159 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5161 struct MeshTunnel *t = value;
5168 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5170 * @param cls closure
5171 * @param key current key code
5172 * @param value value in the hash map
5173 * @return GNUNET_YES if we should continue to iterate,
5177 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5179 struct MeshPeerInfo *p = value;
5180 struct MeshPeerQueue *q;
5181 struct MeshPeerQueue *n;
5189 queue_destroy(q, GNUNET_YES);
5193 peer_info_destroy (p);
5199 * Task run during shutdown.
5205 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5209 if (core_handle != NULL)
5211 GNUNET_CORE_disconnect (core_handle);
5214 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5215 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5216 if (dht_handle != NULL)
5218 GNUNET_DHT_disconnect (dht_handle);
5223 GNUNET_SERVER_notification_context_destroy (nc);
5226 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5228 GNUNET_SCHEDULER_cancel (announce_id_task);
5229 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5236 * Process mesh requests.
5238 * @param cls closure
5239 * @param server the initialized server
5240 * @param c configuration to use
5243 run (void *cls, struct GNUNET_SERVER_Handle *server,
5244 const struct GNUNET_CONFIGURATION_Handle *c)
5247 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5250 server_handle = server;
5251 GNUNET_SERVER_suspend (server_handle);
5254 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5259 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5260 "mesh", "peer/privatekey");
5261 GNUNET_SCHEDULER_shutdown ();
5266 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5267 &refresh_path_time))
5269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5271 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5272 "mesh", "refresh path time");
5273 GNUNET_SCHEDULER_shutdown ();
5278 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5283 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5284 "mesh", "id announce time");
5285 GNUNET_SCHEDULER_shutdown ();
5290 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5295 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5296 "mesh", "connect timeout");
5297 GNUNET_SCHEDULER_shutdown ();
5302 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5307 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5308 "mesh", "max msgs queue");
5309 GNUNET_SCHEDULER_shutdown ();
5314 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5319 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5320 "mesh", "max tunnels");
5321 GNUNET_SCHEDULER_shutdown ();
5326 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5331 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5332 "mesh", "default ttl", 64);
5337 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5341 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5342 "mesh", "max peers", 1000);
5347 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5355 "Mesh is running with drop mode enabled. "
5356 "This is NOT a good idea! "
5357 "Remove the DROP_PERCENT option from your configuration.\n");
5361 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5362 &dht_replication_level))
5364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5366 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5367 "mesh", "dht replication level", 3);
5368 dht_replication_level = 3;
5371 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5372 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5373 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5374 ports = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5376 dht_handle = GNUNET_DHT_connect (c, 64);
5377 if (NULL == dht_handle)
5381 stats = GNUNET_STATISTICS_create ("mesh", c);
5383 /* Scheduled the task to clean up when shutdown is called */
5384 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5386 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5387 GNUNET_free (keyfile);
5388 GNUNET_assert (NULL != pk);
5389 my_private_key = pk;
5390 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5391 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5392 &my_full_id.hashPubKey);
5393 myid = GNUNET_PEER_intern (&my_full_id);
5394 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5395 "Mesh for peer [%s] starting\n",
5396 GNUNET_i2s(&my_full_id));
5398 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5399 NULL, /* Closure passed to MESH functions */
5400 &core_init, /* Call core_init once connected */
5401 &core_connect, /* Handle connects */
5402 &core_disconnect, /* remove peers on disconnects */
5403 NULL, /* Don't notify about all incoming messages */
5404 GNUNET_NO, /* For header only in notification */
5405 NULL, /* Don't notify about all outbound messages */
5406 GNUNET_NO, /* For header-only out notification */
5407 core_handlers); /* Register these handlers */
5408 if (NULL == core_handle)
5411 GNUNET_SCHEDULER_shutdown ();
5415 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5416 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5422 * The main function for the mesh service.
5424 * @param argc number of arguments from the command line
5425 * @param argv command line arguments
5426 * @return 0 ok, 1 on error
5429 main (int argc, char *const *argv)
5434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5435 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5437 ret = (GNUNET_OK == r) ? 0 : 1;
5438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5442 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5443 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5444 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);