2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
27 * - keep queues until receiving ACK
30 * - relay corking down to core
31 * - set ttl relative to path length
39 #include "mesh_protocol.h"
40 #include "mesh_path.h"
41 #include "block_mesh.h"
42 #include "gnunet_dht_service.h"
43 #include "gnunet_statistics_service.h"
45 #define MESH_BLOOM_SIZE 128
47 #define MESH_DEBUG_DHT GNUNET_NO
48 #define MESH_DEBUG_CONNECTION GNUNET_NO
49 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
51 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
52 GNUNET_TIME_UNIT_MINUTES,\
54 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
55 #define MESH_RETRANSMIT_MARGIN 4
57 #if MESH_DEBUG_CONNECTION
58 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
60 #define DEBUG_CONN(...)
64 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
66 #define DEBUG_DHT(...)
73 struct timespec __mesh_start;
74 struct timespec __mesh_end;
75 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
76 #define INTERVAL_END \
78 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
79 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
80 if (__diff < 0) __diff += 1000000000;\
84 #define INTERVAL_SHOW \
86 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
88 #define INTERVAL_START
93 /******************************************************************************/
94 /************************ DATA STRUCTURES ****************************/
95 /******************************************************************************/
97 /** FWD declaration */
103 * Struct containing info about a queued transmission to this peer
110 struct MeshPeerQueue *next;
115 struct MeshPeerQueue *prev;
118 * Peer this transmission is directed to.
120 struct MeshPeerInfo *peer;
123 * Tunnel this message belongs to.
125 struct MeshTunnel *tunnel;
128 * Pointer to info stucture used as cls.
138 * Size of the message
145 * Struct containing all information regarding a given peer
155 * Last time we heard from this peer
157 struct GNUNET_TIME_Absolute last_contact;
160 * Number of attempts to reconnect so far
162 int n_reconnect_attempts;
165 * Paths to reach the peer, ordered by ascending hop count
167 struct MeshPeerPath *path_head;
170 * Paths to reach the peer, ordered by ascending hop count
172 struct MeshPeerPath *path_tail;
175 * Handle to stop the DHT search for a path to this peer
177 struct GNUNET_DHT_GetHandle *dhtget;
180 * Array of tunnels this peer is the target of.
181 * Most probably a small amount, therefore not a hashmap.
182 * When the path to the peer changes, notify these tunnels to let them
183 * re-adjust their path trees.
185 struct MeshTunnel **tunnels;
188 * Number of tunnels this peers participates in
190 unsigned int ntunnels;
193 * Transmission queue to core DLL head
195 struct MeshPeerQueue *queue_head;
198 * Transmission queue to core DLL tail
200 struct MeshPeerQueue *queue_tail;
203 * How many messages are in the queue to this peer.
205 unsigned int queue_n;
208 * Handle for queued transmissions
210 struct GNUNET_CORE_TransmitHandle *core_transmit;
215 * Struct to encapsulate all the Flow Control information to a peer in the
216 * context of a tunnel: Same peer in different tunnels will have independent
217 * flow control structures, allowing to choke/free tunnels according to its
220 struct MeshFlowControl
223 * ID of the last packet sent towards the peer.
225 uint32_t last_pid_sent;
228 * ID of the last packet received from the peer.
230 uint32_t last_pid_recv;
233 * Last ACK sent to the peer (peer can't send more than this PID).
235 uint32_t last_ack_sent;
238 * Last ACK sent towards the origin (for traffic towards leaf node).
240 uint32_t last_ack_recv;
243 * How many payload messages are in the queue towards this peer.
248 * Task to poll the peer in case of a lost ACK causes stall.
250 GNUNET_SCHEDULER_TaskIdentifier poll_task;
253 * How frequently to poll for ACKs.
255 struct GNUNET_TIME_Relative poll_time;
258 * On which tunnel to poll.
259 * Using an explicit poll_ctx would not help memory wise,
260 * since the allocated context would have to be stored in the
261 * fc struct in order to free it upon cancelling poll_task.
263 struct MeshTunnel *t;
268 * Globally unique tunnel identification (owner + number)
269 * DO NOT USE OVER THE NETWORK
274 * Node that owns the tunnel
279 * Tunnel number to differentiate all the tunnels owned by the node oid
280 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
282 MESH_TunnelNumber tid;
287 * Data needed for reliable tunnel endpoint retransmission management.
289 struct MeshTunnelReliability;
293 * Info needed to retry a message in case it gets lost.
295 struct MeshReliableMessage
298 * Double linked list, FIFO style
300 struct MeshReliableMessage *next;
301 struct MeshReliableMessage *prev;
304 * Tunnel Reliability queue this message is in.
306 struct MeshTunnelReliability *rel;
309 * ID of the message (ACK needed to free)
314 * When was this message issued (to calculate ACK delay) FIXME update with traffic
316 struct GNUNET_TIME_Absolute timestamp;
318 /* struct GNUNET_MESH_Data with payload */
322 struct MeshTunnelReliability
325 * Tunnel this is about.
327 struct MeshTunnel *t;
330 * DLL of messages sent and not yet ACK'd.
332 struct MeshReliableMessage *head_sent;
333 struct MeshReliableMessage *tail_sent;
346 * DLL of messages received out of order.
348 struct MeshReliableMessage *head_recv;
349 struct MeshReliableMessage *tail_recv;
357 * Task to resend/poll in case no ACK is received.
359 GNUNET_SCHEDULER_TaskIdentifier retry_task;
362 * Counter for exponential backoff.
364 struct GNUNET_TIME_Relative retry_timer;
367 * How long does it usually take to get an ACK. FIXME update with traffic
369 struct GNUNET_TIME_Relative expected_delay;
374 * Struct containing all information regarding a tunnel
375 * For an intermediate node the improtant info used will be:
376 * - id Tunnel unique identification
377 * - paths[0] To know where to send it next
378 * - metainfo: ready, speeds, accounting
385 struct MESH_TunnelID id;
388 * Port of the tunnel.
393 * State of the tunnel.
395 enum MeshTunnelState state;
398 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
400 MESH_TunnelNumber local_tid;
403 * Local tunnel number for local destination clients (incoming number)
404 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
407 MESH_TunnelNumber local_tid_dest;
410 * Is the tunnel bufferless (minimum latency)?
415 * Is the tunnel reliable?
420 * Force sending ACK? Flag to allow duplicate ACK on POLL.
425 * How many messages do we accept in the forward queue.
427 unsigned int queue_max;
430 * Last time the tunnel was used
432 struct GNUNET_TIME_Absolute timestamp;
435 * Destination of the tunnel.
440 * Next hop in the tunnel. If 0, @c client must be set.
442 GNUNET_PEER_Id next_hop;
445 * Previous hop in the tunnel. If 0, @c owner must be set.
447 GNUNET_PEER_Id prev_hop;
450 * Flow control information about @c next_hop or @c client.
452 struct MeshFlowControl next_fc;
455 * Flow control information about @c prev_hop or @c owner.
457 struct MeshFlowControl prev_fc;
460 * Client owner of the tunnel, if any
462 struct MeshClient *owner;
465 * Client destination of the tunnel, if any.
467 struct MeshClient *client;
470 * Task to keep the used paths alive at the owner,
471 * time tunnel out on all the other peers.
473 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
476 * Path being used for the tunnel.
478 struct MeshPeerPath *path;
481 * Flag to signal the destruction of the tunnel.
482 * If this is set GNUNET_YES the tunnel will be destroyed
483 * when the queue is empty.
488 * Total messages pending for this tunnels, payload or not.
490 unsigned int pending_messages;
494 * Only present (non-NULL) at the owner of a tunnel.
496 struct MeshTunnelReliability *fwd_rel;
500 * Only present (non-NULL) at the destination of a tunnel.
502 struct MeshTunnelReliability *bck_rel;
507 * Struct containing information about a client of the service
509 * TODO: add a list of 'waiting' ports
516 struct MeshClient *next;
521 struct MeshClient *prev;
524 * Tunnels that belong to this client, indexed by local id
526 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
529 * Tunnels this client has accepted, indexed by incoming local id
531 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
534 * Handle to communicate with the client
536 struct GNUNET_SERVER_Client *handle;
539 * Ports that this client has declared interest in.
540 * Indexed by port, contains *Client.
542 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
545 * Whether the client is active or shutting down (don't send confirmations
546 * to a client that is shutting down.
551 * ID of the client, mainly for debug messages
558 /******************************************************************************/
559 /************************ DEBUG FUNCTIONS ****************************/
560 /******************************************************************************/
564 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
565 * @param cls string to print
566 * @param success GNUNET_OK if the PUT was transmitted,
567 * GNUNET_NO on timeout,
568 * GNUNET_SYSERR on disconnect from service
569 * after the PUT message was transmitted
570 * (so we don't know if it was received or not)
575 mesh_debug (void *cls, int success)
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
584 unsigned int debug_fwd_ack;
585 unsigned int debug_bck_ack;
589 /******************************************************************************/
590 /*********************** GLOBAL VARIABLES ****************************/
591 /******************************************************************************/
593 /************************** Configuration parameters **************************/
596 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
598 static struct GNUNET_TIME_Relative refresh_path_time;
601 * How often to PUT own ID in the DHT.
603 static struct GNUNET_TIME_Relative id_announce_time;
606 * Maximum time allowed to connect to a peer found by string.
608 static struct GNUNET_TIME_Relative connect_timeout;
611 * Default TTL for payload packets.
613 static unsigned long long default_ttl;
616 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
618 static unsigned long long dht_replication_level;
621 * How many tunnels are we willing to maintain.
622 * Local tunnels are always allowed, even if there are more tunnels than max.
624 static unsigned long long max_tunnels;
627 * How many messages *in total* are we willing to queue, divided by number of
628 * tunnels to get tunnel queue size.
630 static unsigned long long max_msgs_queue;
633 * How many peers do we want to remember?
635 static unsigned long long max_peers;
638 * Percentage of messages that will be dropped (for test purposes only).
640 static unsigned long long drop_percent;
642 /*************************** Static global variables **************************/
645 * DLL with all the clients, head.
647 static struct MeshClient *clients_head;
650 * DLL with all the clients, tail.
652 static struct MeshClient *clients_tail;
655 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
657 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
660 * Number of tunnels known.
662 static unsigned long long n_tunnels;
665 * Tunnels incoming, indexed by MESH_TunnelNumber
666 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
668 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
671 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
673 static struct GNUNET_CONTAINER_MultiHashMap *peers;
676 * Handle to communicate with transport
678 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
681 * Handle to communicate with core.
683 static struct GNUNET_CORE_Handle *core_handle;
688 static struct GNUNET_DHT_Handle *dht_handle;
693 static struct GNUNET_SERVER_Handle *server_handle;
696 * Handle to the statistics service.
698 static struct GNUNET_STATISTICS_Handle *stats;
701 * Notification context, to send messages to local clients.
703 static struct GNUNET_SERVER_NotificationContext *nc;
706 * Local peer own ID (memory efficient handle).
708 static GNUNET_PEER_Id myid;
711 * Local peer own ID (full value).
713 static struct GNUNET_PeerIdentity my_full_id;
718 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
723 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
726 * Tunnel ID for the next created tunnel (global tunnel number).
728 static MESH_TunnelNumber next_tid;
731 * Tunnel ID for the next incoming tunnel (local tunnel number).
733 static MESH_TunnelNumber next_local_tid;
736 * All ports clients of this peer have opened.
738 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
741 * Task to periodically announce itself in the network.
743 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
746 * Next ID to assign to a client.
748 unsigned int next_client_id;
751 /******************************************************************************/
752 /*********************** DECLARATIONS **************************/
753 /******************************************************************************/
756 * Function to process paths received for a new peer addition. The recorded
757 * paths form the initial tunnel, which can be optimized later.
758 * Called on each result obtained for the DHT search.
761 * @param exp when will this value expire
762 * @param key key of the result
763 * @param type type of the result
764 * @param size number of bytes in data
765 * @param data pointer to the result data
768 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
769 const struct GNUNET_HashCode * key,
770 const struct GNUNET_PeerIdentity *get_path,
771 unsigned int get_path_length,
772 const struct GNUNET_PeerIdentity *put_path,
773 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
774 size_t size, const void *data);
778 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
779 * and insert it in the appropriate structures if the peer is not known yet.
781 * @param peer Full identity of the peer.
783 * @return Existing or newly created peer info.
785 static struct MeshPeerInfo *
786 peer_get (const struct GNUNET_PeerIdentity *peer);
790 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
791 * and insert it in the appropriate structures if the peer is not known yet.
793 * @param peer Short identity of the peer.
795 * @return Existing or newly created peer info.
797 static struct MeshPeerInfo *
798 peer_get_short (const GNUNET_PEER_Id peer);
802 * Build a PeerPath from the paths returned from the DHT, reversing the paths
803 * to obtain a local peer -> destination path and interning the peer ids.
805 * @return Newly allocated and created path
807 static struct MeshPeerPath *
808 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
809 unsigned int get_path_length,
810 const struct GNUNET_PeerIdentity *put_path,
811 unsigned int put_path_length);
815 * Adds a path to the peer_infos of all the peers in the path
817 * @param p Path to process.
818 * @param confirmed Whether we know if the path works or not.
821 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
826 * Search for a tunnel by global ID using full PeerIdentities.
828 * @param oid owner of the tunnel.
829 * @param tid global tunnel number.
831 * @return tunnel handler, NULL if doesn't exist.
833 static struct MeshTunnel *
834 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
838 * Notify a tunnel that a connection has broken that affects at least
841 * @param t Tunnel affected.
842 * @param p1 Peer that got disconnected from p2.
843 * @param p2 Peer that got disconnected from p1.
845 * @return Short ID of the peer disconnected (either p1 or p2).
846 * 0 if the tunnel remained unaffected.
848 static GNUNET_PEER_Id
849 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
854 * @brief Use the given path for the tunnel.
855 * Update the next and prev hops (and RCs).
856 * (Re)start the path refresh in case the tunnel is locally owned.
858 * @param t Tunnel to update.
859 * @param p Path to use.
862 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
865 * Tunnel is empty: destroy it.
867 * Notifies all participants (peers, cleints) about the destruction.
869 * @param t Tunnel to destroy.
872 tunnel_destroy_empty (struct MeshTunnel *t);
875 * @brief Queue and pass message to core when possible.
877 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
878 * and accounts for it. In case the queue is full, the message is dropped and
881 * Otherwise, message is treated as internal and allowed to go regardless of
884 * @param cls Closure (@c type dependant). It will be used by queue_send to
885 * build the message to be sent if not already prebuilt.
886 * @param type Type of the message, 0 for a raw message.
887 * @param size Size of the message.
888 * @param dst Neighbor to send message to.
889 * @param t Tunnel this message belongs to.
892 queue_add (void *cls, uint16_t type, size_t size,
893 struct MeshPeerInfo *dst, struct MeshTunnel *t);
897 * Free a transmission that was already queued with all resources
898 * associated to the request.
900 * @param queue Queue handler to cancel.
901 * @param clear_cls Is it necessary to free associated cls?
904 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
908 * @brief Get the next transmittable message from the queue.
910 * This will be the head, except in the case of being a data packet
911 * not allowed by the destination peer.
913 * @param peer Destination peer.
915 * @return The next viable MeshPeerQueue element to send to that peer.
916 * NULL when there are no transmittable messages.
918 struct MeshPeerQueue *
919 queue_get_next (const struct MeshPeerInfo *peer);
923 * Core callback to write a queued packet to core buffer
925 * @param cls Closure (peer info).
926 * @param size Number of bytes available in buf.
927 * @param buf Where the to write the message.
929 * @return number of bytes written to buf
932 queue_send (void *cls, size_t size, void *buf);
935 /******************************************************************************/
936 /************************ PERIODIC FUNCTIONS ****************************/
937 /******************************************************************************/
940 * Periodically announce self id in the DHT
943 * @param tc task context
946 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
950 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
952 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
956 * - Set data expiration in function of X
959 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
961 block.id = my_full_id;
962 GNUNET_DHT_put (dht_handle, /* DHT handle */
963 &my_full_id.hashPubKey, /* Key to use */
964 dht_replication_level, /* Replication level */
965 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
966 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
967 sizeof (block), /* Size of the data */
968 (const char *) &block, /* Data itself */
969 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
970 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
971 NULL, /* Continuation */
972 NULL); /* Continuation closure */
974 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
978 /******************************************************************************/
979 /****************** GENERAL HELPER FUNCTIONS ************************/
980 /******************************************************************************/
984 * Check if client has registered with the service and has not disconnected
986 * @param client the client to check
988 * @return non-NULL if client exists in the global DLL
990 static struct MeshClient *
991 client_get (struct GNUNET_SERVER_Client *client)
993 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
998 * Deletes a tunnel from a client (either owner or destination). To be used on
1001 * @param c Client whose tunnel to delete.
1002 * @param t Tunnel which should be deleted.
1005 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1009 GNUNET_assert (GNUNET_YES ==
1010 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1016 GNUNET_assert (GNUNET_YES ==
1017 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1024 * Notify the appropriate client that a new incoming tunnel was created.
1026 * @param t Tunnel that was created.
1029 send_client_tunnel_create (struct MeshTunnel *t)
1031 struct GNUNET_MESH_TunnelMessage msg;
1033 if (NULL == t->client)
1035 msg.header.size = htons (sizeof (msg));
1036 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1037 msg.tunnel_id = htonl (t->local_tid_dest);
1038 msg.port = htonl (t->port);
1040 msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1041 msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1042 msg.opt = htonl (msg.opt);
1043 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1044 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1045 &msg.header, GNUNET_NO);
1050 * Notify dest client that the incoming tunnel is no longer valid.
1052 * @param c Client to notify..
1053 * @param t Tunnel that is destroyed.
1056 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
1058 struct GNUNET_MESH_TunnelMessage msg;
1065 if (c != t->client && c != t->owner)
1070 msg.header.size = htons (sizeof (msg));
1071 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1072 msg.tunnel_id = htonl (t->local_tid_dest);
1073 msg.port = htonl (0);
1074 memset(&msg.peer, 0, sizeof (msg.peer));
1075 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1076 &msg.header, GNUNET_NO);
1081 * Iterator over all the peers to remove the oldest not-used entry.
1083 * @param cls Closure (unsued).
1084 * @param key ID of the peer.
1085 * @param value Peer_Info of the peer.
1090 peer_info_timeout (void *cls,
1091 const struct GNUNET_HashCode *key,
1098 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1099 * and insert it in the appropriate structures if the peer is not known yet.
1101 * @param peer Full identity of the peer.
1103 * @return Existing or newly created peer info.
1105 static struct MeshPeerInfo *
1106 peer_get (const struct GNUNET_PeerIdentity *peer)
1108 struct MeshPeerInfo *peer_info;
1110 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1111 if (NULL == peer_info)
1114 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1115 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1117 GNUNET_CONTAINER_multihashmap_iterate (peers,
1121 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1122 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1123 peer_info->id = GNUNET_PEER_intern (peer);
1125 peer_info->last_contact = GNUNET_TIME_absolute_get();
1132 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1133 * and insert it in the appropriate structures if the peer is not known yet.
1135 * @param peer Short identity of the peer.
1137 * @return Existing or newly created peer info.
1139 static struct MeshPeerInfo *
1140 peer_get_short (const GNUNET_PEER_Id peer)
1142 struct GNUNET_PeerIdentity id;
1144 GNUNET_PEER_resolve (peer, &id);
1145 return peer_get (&id);
1150 * Choose the best path towards a peer considering the tunnel properties.
1152 * @param peer The destination peer.
1153 * @param t The tunnel the path is for.
1155 * @return Best current known path towards the peer, if any.
1157 static struct MeshPeerPath *
1158 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1160 struct MeshPeerPath *best_p;
1161 struct MeshPeerPath *p;
1162 unsigned int best_cost;
1165 best_p = p = peer->path_head;
1166 best_cost = cost = p->length;
1169 if ((cost = p->length) < best_cost)
1180 * Core callback to write a pre-constructed data packet to core buffer
1182 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1183 * @param size Number of bytes available in buf.
1184 * @param buf Where the to write the message.
1186 * @return number of bytes written to buf
1189 send_core_data_raw (void *cls, size_t size, void *buf)
1191 struct GNUNET_MessageHeader *msg = cls;
1194 GNUNET_assert (NULL != msg);
1195 total_size = ntohs (msg->size);
1197 if (total_size > size)
1202 memcpy (buf, msg, total_size);
1209 * Sends an already built message to a peer, properly registrating
1210 * all used resources.
1212 * @param message Message to send. Function makes a copy of it.
1213 * @param peer Short ID of the neighbor whom to send the message.
1214 * @param t Tunnel on which this message is transmitted.
1217 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1218 GNUNET_PEER_Id peer,
1219 struct MeshTunnel *t)
1221 struct GNUNET_PeerIdentity id;
1222 struct MeshPeerInfo *neighbor;
1223 struct MeshPeerPath *p;
1228 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1233 size = ntohs (message->size);
1234 data = GNUNET_malloc (size);
1235 memcpy (data, message, size);
1236 type = ntohs(message->type);
1237 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1238 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1240 struct GNUNET_MESH_Data *u;
1242 u = (struct GNUNET_MESH_Data *) data;
1243 u->ttl = htonl (ntohl (u->ttl) - 1);
1245 GNUNET_PEER_resolve (peer, &id);
1246 neighbor = peer_get (&id);
1247 for (p = neighbor->path_head; NULL != p; p = p->next)
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 " %s IS NOT DIRECTLY CONNECTED\n",
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1263 for (p = neighbor->path_head; NULL != p; p = p->next)
1265 struct GNUNET_PeerIdentity debug_id;
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 " path with %u hops through:\n",
1271 for (i = 0; i < p->length; i++)
1273 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 i, GNUNET_i2s(&debug_id));
1280 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1281 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1282 " no direct connection to %s\n",
1287 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1298 * Sends a CREATE PATH message for a path to a peer, properly registrating
1299 * all used resources.
1301 * @param t Tunnel for which the path is created.
1304 send_create_path (struct MeshTunnel *t)
1306 struct MeshPeerInfo *neighbor;
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1309 neighbor = peer_get_short (t->next_hop);
1311 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1312 sizeof (struct GNUNET_MESH_CreateTunnel) +
1313 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1316 t->state = MESH_TUNNEL_WAITING;
1321 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1323 * @param t Tunnel which to confirm.
1326 send_path_ack (struct MeshTunnel *t)
1328 struct MeshPeerInfo *peer;
1330 peer = peer_get_short (t->prev_hop);
1333 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1334 sizeof (struct GNUNET_MESH_PathACK),
1341 * Try to establish a new connection to this peer in the given tunnel.
1342 * If the peer doesn't have any path to it yet, try to get one.
1343 * If the peer already has some path, send a CREATE PATH towards it.
1345 * @param peer PeerInfo of the peer.
1346 * @param t Tunnel for which to create the path, if possible.
1349 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1351 struct MeshPeerPath *p;
1353 if (NULL != peer->path_head)
1355 p = peer_get_best_path (peer, t);
1356 tunnel_use_path (t, p);
1357 send_create_path (t);
1359 else if (NULL == peer->dhtget)
1361 struct GNUNET_PeerIdentity id;
1363 GNUNET_PEER_resolve (peer->id, &id);
1364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1365 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1366 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1367 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1368 &id.hashPubKey, /* key to search */
1369 dht_replication_level, /* replication level */
1370 GNUNET_DHT_RO_RECORD_ROUTE |
1371 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1373 0, /* xquery bits */
1374 &dht_get_id_handler, peer);
1375 t->state = MESH_TUNNEL_SEARCHING;
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 "There is no path but the DHT GET is already started.\n");
1386 * Destroy the peer_info and free any allocated resources linked to it
1388 * @param pi The peer_info to destroy.
1390 * @return GNUNET_OK on success
1393 peer_info_destroy (struct MeshPeerInfo *pi)
1395 struct GNUNET_PeerIdentity id;
1396 struct MeshPeerPath *p;
1397 struct MeshPeerPath *nextp;
1400 GNUNET_PEER_resolve (pi->id, &id);
1401 GNUNET_PEER_change_rc (pi->id, -1);
1404 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1408 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1410 if (NULL != pi->dhtget)
1412 GNUNET_DHT_get_stop (pi->dhtget);
1418 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1422 for (i = 0; i < pi->ntunnels; i++)
1423 tunnel_destroy_empty (pi->tunnels[i]);
1424 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1431 * Remove all paths that rely on a direct connection between p1 and p2
1432 * from the peer itself and notify all tunnels about it.
1434 * @param peer PeerInfo of affected peer.
1435 * @param p1 GNUNET_PEER_Id of one peer.
1436 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1439 * TODO: optimize (see below)
1442 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1445 struct MeshPeerPath *p;
1446 struct MeshPeerPath *next;
1447 struct MeshPeerInfo *peer_d;
1449 unsigned int destroyed;
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1454 for (p = peer->path_head; NULL != p; p = next)
1457 for (i = 0; i < (p->length - 1); i++)
1459 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1460 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1462 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1472 for (i = 0; i < peer->ntunnels; i++)
1474 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1478 peer_d = peer_get_short (d);
1479 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1480 tunnel_use_path (peer->tunnels[i], next);
1481 peer_connect (peer_d, peer->tunnels[i]);
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1488 * Add the path to the peer and update the path used to reach it in case this
1491 * @param peer_info Destination peer to add the path to.
1492 * @param path New path to add. Last peer must be the peer in arg 1.
1493 * Path will be either used of freed if already known.
1494 * @param trusted Do we trust that this path is real?
1497 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1500 struct MeshPeerPath *aux;
1504 if ((NULL == peer_info) || (NULL == path))
1507 path_destroy (path);
1510 if (path->peers[path->length - 1] != peer_info->id)
1513 path_destroy (path);
1516 if (2 >= path->length && GNUNET_NO == trusted)
1518 /* Only allow CORE to tell us about direct paths */
1519 path_destroy (path);
1522 for (l = 1; l < path->length; l++)
1524 if (path->peers[l] == myid)
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1527 for (l2 = 0; l2 < path->length - l; l2++)
1529 path->peers[l2] = path->peers[l + l2];
1534 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1539 struct GNUNET_PeerIdentity id;
1541 GNUNET_PEER_resolve (peer_info->id, &id);
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1543 path->length, GNUNET_i2s (&id));
1546 l = path_get_length (path);
1549 path_destroy (path);
1553 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1554 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1556 l2 = path_get_length (aux);
1559 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1560 peer_info->path_tail, aux, path);
1565 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1567 path_destroy (path);
1572 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1579 * Add the path to the origin peer and update the path used to reach it in case
1580 * this is the shortest.
1581 * The path is given in peer_info -> destination, therefore we turn the path
1582 * upside down first.
1584 * @param peer_info Peer to add the path to, being the origin of the path.
1585 * @param path New path to add after being inversed.
1586 * Path will be either used or freed.
1587 * @param trusted Do we trust that this path is real?
1590 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1591 struct MeshPeerPath *path, int trusted)
1594 peer_info_add_path (peer_info, path, trusted);
1599 * Add a tunnel to the list of tunnels a peer participates in.
1600 * Update the tunnel's destination.
1602 * @param p Peer to add to.
1603 * @param t Tunnel to add.
1606 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1610 GNUNET_break (t->dest == p->id);
1614 GNUNET_PEER_change_rc (t->dest, 1);
1615 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1620 * Remove a tunnel from the list of tunnels a peer participates in.
1621 * Free the tunnel's destination.
1623 * @param p Peer to clean.
1624 * @param t Tunnel to remove.
1627 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1631 if (t->dest == p->id)
1633 GNUNET_PEER_change_rc (t->dest, -1);
1636 for (i = 0; i < p->ntunnels; i++)
1638 if (p->tunnels[i] == t)
1640 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1641 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1649 * Function called if the connection to the peer has been stalled for a while,
1650 * possibly due to a missed ACK. Poll the peer about its ACK status.
1652 * @param cls Closure (poll ctx).
1653 * @param tc TaskContext.
1656 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1658 struct MeshFlowControl *fc = cls;
1659 struct GNUNET_MESH_Poll msg;
1660 struct MeshTunnel *t = fc->t;
1661 GNUNET_PEER_Id peer;
1663 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1664 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1671 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1672 msg.header.size = htons (sizeof (msg));
1673 msg.tid = htonl (t->id.tid);
1674 msg.pid = htonl (fc->last_pid_sent);
1675 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1677 if (fc == &t->prev_fc)
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1682 else if (fc == &t->next_fc)
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1692 send_prebuilt_message (&msg.header, peer, t);
1693 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1694 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1700 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1701 * to obtain a local peer -> destination path and interning the peer ids.
1703 * @return Newly allocated and created path
1705 static struct MeshPeerPath *
1706 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1707 unsigned int get_path_length,
1708 const struct GNUNET_PeerIdentity *put_path,
1709 unsigned int put_path_length)
1711 struct MeshPeerPath *p;
1717 GNUNET_PEER_change_rc (myid, 1);
1718 i = get_path_length;
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1720 for (i--; i >= 0; i--)
1722 id = GNUNET_PEER_intern (&get_path[i]);
1723 if (p->length > 0 && id == p->peers[p->length - 1])
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1726 GNUNET_PEER_change_rc (id, -1);
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1731 GNUNET_i2s (&get_path[i]));
1733 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1734 p->peers[p->length - 1] = id;
1737 i = put_path_length;
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1739 for (i--; i >= 0; i--)
1741 id = GNUNET_PEER_intern (&put_path[i]);
1744 /* PUT path went through us, so discard the path up until now and start
1745 * from here to get a much shorter (and loop-free) path.
1750 if (p->length > 0 && id == p->peers[p->length - 1])
1752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1753 GNUNET_PEER_change_rc (id, -1);
1757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1758 GNUNET_i2s (&put_path[i]));
1760 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1761 p->peers[p->length - 1] = id;
1765 if (get_path_length > 0)
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1767 GNUNET_i2s (&get_path[0]));
1768 if (put_path_length > 0)
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1770 GNUNET_i2s (&put_path[0]));
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1773 for (i = 0; i < p->length; i++)
1775 struct GNUNET_PeerIdentity peer_id;
1777 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1779 GNUNET_i2s (&peer_id));
1787 * Adds a path to the peer_infos of all the peers in the path
1789 * @param p Path to process.
1790 * @param confirmed Whether we know if the path works or not.
1793 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1797 /* TODO: invert and add */
1798 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1799 for (i++; i < p->length; i++)
1801 struct MeshPeerInfo *aux;
1802 struct MeshPeerPath *copy;
1804 aux = peer_get_short (p->peers[i]);
1805 copy = path_duplicate (p);
1806 copy->length = i + 1;
1807 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1813 * Send keepalive packets for a peer
1815 * @param cls Closure (tunnel for which to send the keepalive).
1816 * @param tc Notification context.
1819 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1823 * Search for a tunnel among the incoming tunnels
1825 * @param tid the local id of the tunnel
1827 * @return tunnel handler, NULL if doesn't exist
1829 static struct MeshTunnel *
1830 tunnel_get_incoming (MESH_TunnelNumber tid)
1832 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1833 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1838 * Search for a tunnel among the tunnels for a client
1840 * @param c the client whose tunnels to search in
1841 * @param tid the local id of the tunnel
1843 * @return tunnel handler, NULL if doesn't exist
1845 static struct MeshTunnel *
1846 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1848 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1850 return tunnel_get_incoming (tid);
1854 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1855 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1861 * Search for a tunnel by global ID using PEER_ID
1863 * @param pi owner of the tunnel
1864 * @param tid global tunnel number
1866 * @return tunnel handler, NULL if doesn't exist
1868 static struct MeshTunnel *
1869 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1871 struct MESH_TunnelID id;
1872 struct GNUNET_HashCode hash;
1877 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1878 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1883 * Search for a tunnel by global ID using full PeerIdentities
1885 * @param oid owner of the tunnel
1886 * @param tid global tunnel number
1888 * @return tunnel handler, NULL if doesn't exist
1890 static struct MeshTunnel *
1891 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1893 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1898 * Add a client to a tunnel, initializing all needed data structures.
1900 * @param t Tunnel to which add the client.
1901 * @param c Client which to add to the tunnel.
1904 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1906 if (NULL != t->client)
1912 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1913 t->local_tid_dest, t,
1914 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1920 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1921 t->local_tid_dest, t,
1922 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1932 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1934 unsigned int own_pos;
1936 for (own_pos = 0; own_pos < p->length; own_pos++)
1938 if (p->peers[own_pos] == myid)
1941 if (own_pos > p->length - 1)
1947 if (own_pos < p->length - 1)
1948 t->next_hop = p->peers[own_pos + 1];
1950 t->next_hop = p->peers[own_pos];
1951 GNUNET_PEER_change_rc (t->next_hop, 1);
1953 t->prev_hop = p->peers[own_pos - 1];
1955 t->prev_hop = p->peers[0];
1956 GNUNET_PEER_change_rc (t->prev_hop, 1);
1958 if (NULL != t->path)
1959 path_destroy (t->path);
1960 t->path = path_duplicate (p);
1963 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1964 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1965 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1972 * Notifies a tunnel that a connection has broken that affects at least
1973 * some of its peers. Sends a notification towards the root of the tree.
1974 * In case the peer is the owner of the tree, notifies the client that owns
1975 * the tunnel and tries to reconnect.
1977 * @param t Tunnel affected.
1978 * @param p1 Peer that got disconnected from p2.
1979 * @param p2 Peer that got disconnected from p1.
1981 * @return Short ID of the peer disconnected (either p1 or p2).
1982 * 0 if the tunnel remained unaffected.
1984 static GNUNET_PEER_Id
1985 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1988 // if (myid != p1 && myid != p2) FIXME
1993 // if (tree_get_predecessor (t->tree) != 0)
1995 // /* We are the peer still connected, notify owner of the disconnection. */
1996 // struct GNUNET_MESH_PathBroken msg;
1997 // struct GNUNET_PeerIdentity neighbor;
1999 // msg.header.size = htons (sizeof (msg));
2000 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2001 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2002 // msg.tid = htonl (t->id.tid);
2003 // msg.peer1 = my_full_id;
2004 // GNUNET_PEER_resolve (pid, &msg.peer2);
2005 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2006 // send_prebuilt_message (&msg.header, &neighbor, t);
2013 * Build a local ACK message and send it to a local client.
2015 * @param t Tunnel on which to send the ACK.
2016 * @param c Client to whom send the ACK.
2017 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
2020 send_local_ack (struct MeshTunnel *t,
2021 struct MeshClient *c,
2024 struct GNUNET_MESH_LocalAck msg;
2026 msg.header.size = htons (sizeof (msg));
2027 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2028 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
2029 GNUNET_SERVER_notification_context_unicast (nc,
2036 * Build an ACK message and queue it to send to the given peer.
2038 * @param t Tunnel on which to send the ACK.
2039 * @param peer Peer to whom send the ACK.
2040 * @param ack Value of the ACK.
2043 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2045 struct GNUNET_MESH_ACK msg;
2047 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2048 msg.header.size = htons (sizeof (msg));
2049 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2050 msg.pid = htonl (ack);
2051 msg.tid = htonl (t->id.tid);
2053 send_prebuilt_message (&msg.header, peer, t);
2058 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2060 * @param t Tunnel this is about.
2063 tunnel_send_fwd_data_ack (struct MeshTunnel *t)
2065 struct GNUNET_MESH_DataACK msg;
2066 struct MeshTunnelReliability *rel;
2067 struct MeshReliableMessage *copy;
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2073 "send_fwd_data_ack for %llu\n",
2074 t->bck_rel->mid_recv - 1);
2076 if (GNUNET_NO == t->reliable)
2078 GNUNET_break_op (0);
2081 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
2082 msg.header.size = htons (sizeof (msg));
2083 msg.tid = htonl (t->id.tid);
2084 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2085 msg.mid = GNUNET_htonll (t->bck_rel->mid_recv - 1);
2088 for (i = 0, copy = rel->head_recv;
2089 i < 64 && NULL != copy;
2090 i++, copy = copy->next)
2092 delta = copy->mid - t->bck_rel->mid_recv;
2093 mask = 0x1 << delta;
2094 msg.futures |= mask;
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2096 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2097 copy->mid, delta, mask, msg.futures);
2099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2101 send_prebuilt_message (&msg.header, t->prev_hop, t);
2102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_fwd_data_ack END\n");
2107 * Send an end-to-end BCK ACK message for the most recent in-sequence payload.
2109 * @param t Tunnel this is about.
2112 tunnel_send_bck_data_ack (struct MeshTunnel *t)
2114 struct GNUNET_MESH_DataACK msg;
2116 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2117 msg.header.size = htons (sizeof (msg));
2118 msg.tid = htonl (t->id.tid);
2119 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2120 msg.mid = GNUNET_htonll (t->fwd_rel->mid_recv - 1);
2121 msg.futures = 0; // FIXME set bits of other newer messages received
2123 send_prebuilt_message (&msg.header, t->next_hop, t);
2128 * Send an ACK informing the predecessor about the available buffer space.
2129 * In case there is no predecessor, inform the owning client.
2130 * If buffering is off, send only on behalf of children or self if endpoint.
2131 * If buffering is on, send when sent to children and buffer space is free.
2132 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2133 * the ACK itself goes "back" (towards root).
2135 * @param t Tunnel on which to send the ACK.
2136 * @param type Type of message that triggered the ACK transmission.
2139 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2144 /* Is it after unicast retransmission? */
2147 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 "ACK due to FWD DATA retransmission\n");
2150 if (GNUNET_YES == t->nobuffer)
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2155 if (GNUNET_YES == t->reliable && NULL != t->client)
2156 tunnel_send_fwd_data_ack (t);
2158 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2159 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2161 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2162 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2163 t->force_ack = GNUNET_YES;
2169 /* Check if we need to transmit the ACK */
2170 if (0 && NULL == t->owner &&
2171 t->queue_max > t->next_fc.queue_n * 4 &&
2172 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2173 GNUNET_NO == t->force_ack)
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, buffer free\n");
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 " t->qmax: %u, t->qn: %u\n",
2178 t->queue_max, t->next_fc.queue_n);
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2180 " t->pid: %u, t->ack: %u\n",
2181 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2185 /* Ok, ACK might be necessary, what PID to ACK? */
2186 delta = t->queue_max - t->next_fc.queue_n;
2187 if (0 > delta || (GNUNET_YES == t->reliable &&
2189 t->fwd_rel->n_sent > 10))
2191 if (NULL != t->owner && delta > 1)
2193 ack = t->prev_fc.last_pid_recv + delta;
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK %u\n", ack);
2195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2196 " last %u, qmax %u, q %u\n",
2197 t->prev_fc.last_pid_recv, t->queue_max, t->next_fc.queue_n);
2198 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2204 t->prev_fc.last_ack_sent = ack;
2205 if (NULL != t->owner)
2206 send_local_ack (t, t->owner, GNUNET_YES);
2207 else if (0 != t->prev_hop)
2208 send_ack (t, t->prev_hop, ack);
2212 t->force_ack = GNUNET_NO;
2217 * Send an ACK informing the children node/client about the available
2219 * If buffering is off, send only on behalf of root (can be self).
2220 * If buffering is on, send when sent to predecessor and buffer space is free.
2221 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2222 * the ACK itself goes "forward" (towards children/clients).
2224 * @param t Tunnel on which to send the ACK.
2225 * @param type Type of message that triggered the ACK transmission.
2228 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2232 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2233 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2234 /* Is it after data to_origin retransmission? */
2237 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2238 if (GNUNET_YES == t->nobuffer)
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2241 " Not sending ACK, nobuffer + traffic\n");
2244 if (GNUNET_YES == t->reliable && NULL != t->owner)
2245 tunnel_send_bck_data_ack (t);
2247 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2248 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2250 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2251 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2252 t->force_ack = GNUNET_YES;
2258 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2260 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2262 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2265 " Not sending ACK, not needed, last ack sent was %u\n",
2266 t->next_fc.last_ack_sent);
2269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2270 " Sending BCK ACK %u (last sent: %u)\n",
2271 ack, t->next_fc.last_ack_sent);
2272 t->next_fc.last_ack_sent = ack;
2274 if (NULL != t->client)
2275 send_local_ack (t, t->client, GNUNET_NO);
2276 else if (0 != t->next_hop)
2277 send_ack (t, t->next_hop, ack);
2280 t->force_ack = GNUNET_NO;
2285 * Modify the mesh message TID from global to local and send to client.
2287 * @param t Tunnel on which to send the message.
2288 * @param msg Message to modify and send.
2289 * @param c Client to send to.
2290 * @param tid Tunnel ID to use (c can be both owner and client).
2293 tunnel_send_client_data (struct MeshTunnel *t,
2294 const struct GNUNET_MESH_Data *msg,
2295 struct MeshClient *c, MESH_TunnelNumber tid)
2297 struct GNUNET_MESH_LocalData *copy;
2298 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2299 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2301 if (size < sizeof (struct GNUNET_MessageHeader))
2303 GNUNET_break_op (0);
2311 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2312 memcpy (©[1], &msg[1], size);
2313 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2314 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2315 copy->tid = htonl (tid);
2316 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2317 ©->header, GNUNET_NO);
2321 * Modify the unicast message TID from global to local and send to client.
2323 * @param t Tunnel on which to send the message.
2324 * @param msg Message to modify and send.
2327 tunnel_send_client_ucast (struct MeshTunnel *t,
2328 const struct GNUNET_MESH_Data *msg)
2330 tunnel_send_client_data (t, msg, t->client, t->local_tid_dest);
2335 * Send up to 64 buffered messages to the client for in order delivery.
2337 * @param t Tunnel on which to empty the message buffer.
2340 tunnel_send_client_buffered_ucast (struct MeshTunnel *t)
2342 struct MeshTunnelReliability *rel;
2343 struct MeshReliableMessage *copy;
2344 struct MeshReliableMessage *next;
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_unicast\n");
2348 for (copy = rel->head_recv; NULL != copy; copy = next)
2351 if (copy->mid == rel->mid_recv)
2353 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2356 " have %llu! now expecting %llu\n",
2357 copy->mid, rel->mid_recv + 1LL);
2358 tunnel_send_client_ucast (t, msg);
2360 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2366 " don't have %llu, (%llu)\n",
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_unicast END\n");
2377 * We have received a message out of order, buffer it until we receive
2378 * the missing one and we can feed the rest to the client.
2381 tunnel_add_buffer_ucast (struct MeshTunnel *t,
2382 const struct GNUNET_MESH_Data *msg)
2384 struct MeshTunnelReliability *rel;
2385 struct MeshReliableMessage *copy;
2386 struct MeshReliableMessage *prev;
2391 size = ntohs (msg->header.size);
2392 mid = GNUNET_ntohll (msg->mid);
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffer_ucast %llu\n", mid);
2395 copy = GNUNET_malloc (sizeof (*copy) + size);
2398 memcpy (©[1], msg, size);
2400 // FIXME do something better than O(n), although n < 64...
2401 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %llu\n", prev->mid);
2404 if (mid < prev->mid)
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2407 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2413 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffer_ucast END\n");
2419 * Mark future messages as ACK'd.
2421 * @param t Tunnel whose sent buffer to clean.
2422 * @param msg DataACK message with a bitfield of future ACK'd messages.
2425 tunnel_free_buffer_ucast (struct MeshTunnel *t,
2426 const struct GNUNET_MESH_DataACK *msg)
2428 struct MeshTunnelReliability *rel;
2429 struct MeshReliableMessage *copy;
2430 struct MeshReliableMessage *next;
2437 bitfield = msg->futures;
2438 mid = GNUNET_ntohll (msg->mid);
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441 "free_sent_buffer %llu %llX\n",
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 " rel %p, head %p\n",
2445 rel, rel->head_sent);
2446 for (i = 0, copy = rel->head_sent;
2447 i < 64 && NULL != copy && 0 != bitfield;
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2451 " trying bit %u (mid %llu)\n",
2454 if (0 == (bitfield & mask))
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2458 /* Bit was set, clear the bit from the bitfield */
2461 /* The i-th bit was set. Do we have that copy? */
2462 /* Skip copies with mid < target */
2463 target = mid + i + 1;
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %llu\n", target);
2465 while (NULL != copy && copy->mid < target)
2468 /* Did we run out of copies? (previously freed, it's ok) */
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2475 /* Did we overshoot the target? (previously freed, it's ok) */
2476 if (copy->mid > target)
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %llu\n", copy->mid);
2483 /* Now copy->mid == target, free it */
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %llu\n", target);
2486 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_buffer END\n");
2495 * Modify the to_origin message TID from global to local and send to client.
2497 * @param t Tunnel on which to send the message.
2498 * @param msg Message to modify and send.
2501 tunnel_send_client_to_orig (struct MeshTunnel *t,
2502 const struct GNUNET_MESH_Data *msg)
2504 tunnel_send_client_data (t, msg, t->owner, t->local_tid);
2509 * We haven't received an ACK after a certain time: restransmit the message.
2511 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2512 * @param tc TaskContext.
2515 tunnel_retransmit_message (void *cls,
2516 const struct GNUNET_SCHEDULER_TaskContext *tc)
2518 struct MeshTunnelReliability *rel = cls;
2519 struct MeshReliableMessage *copy;
2520 struct MeshFlowControl *fc;
2521 struct MeshPeerQueue *q;
2522 struct MeshPeerInfo *pi;
2523 struct MeshTunnel *t;
2524 struct GNUNET_MESH_Data *payload;
2527 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2528 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2532 copy = rel->head_sent;
2539 /* Search the message to be retransmitted in the outgoing queue */
2540 payload = (struct GNUNET_MESH_Data *) ©[1];
2541 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2542 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2543 pi = peer_get_short (hop);
2544 for (q = pi->queue_head; NULL != q; q = q->next)
2546 if (ntohs (payload->header.type) == q->type)
2548 struct GNUNET_MESH_Data *queued_data = q->cls;
2550 if (queued_data->mid == payload->mid)
2555 /* Message not found in the queue */
2558 struct GNUNET_TIME_Relative diff;
2560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %llu\n", copy->mid);
2561 diff = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2562 diff = GNUNET_TIME_relative_divide (diff, 10);
2563 copy->timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get(),
2566 fc->last_ack_sent++;
2567 payload->pid = htonl (fc->last_pid_recv + 1);
2568 fc->last_pid_recv++;
2569 send_prebuilt_message (&payload->header, hop, t);
2570 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %llu\n", copy->mid);
2577 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2578 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2579 &tunnel_retransmit_message,
2585 * @brief Re-initiate traffic to this peer if necessary.
2587 * Check if there is traffic queued towards this peer
2588 * and the core transmit handle is NULL (traffic was stalled).
2589 * If so, call core tmt rdy.
2591 * @param peer_id Short ID of peer to which initiate traffic.
2594 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2596 struct MeshPeerInfo *peer;
2597 struct GNUNET_PeerIdentity id;
2598 struct MeshPeerQueue *q;
2601 peer = peer_get_short (peer_id);
2602 if (NULL != peer->core_transmit)
2605 q = queue_get_next (peer);
2608 /* Might br multicast traffic already sent to this particular peer but
2609 * not to other children in this tunnel.
2610 * This way t->queue_n would be > 0 but the queue of this particular peer
2616 GNUNET_PEER_resolve (peer->id, &id);
2617 peer->core_transmit =
2618 GNUNET_CORE_notify_transmit_ready(core_handle,
2621 GNUNET_TIME_UNIT_FOREVER_REL,
2631 * Send a message to all peers and clients in this tunnel that the tunnel
2632 * is no longer valid. If some peer or client should not receive the message,
2633 * should be zero'ed out before calling this function.
2635 * @param t The tunnel whose peers and clients to notify.
2638 tunnel_send_destroy (struct MeshTunnel *t)
2640 struct GNUNET_MESH_TunnelDestroy msg;
2641 struct GNUNET_PeerIdentity id;
2643 msg.header.size = htons (sizeof (msg));
2644 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2645 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2646 msg.tid = htonl (t->id.tid);
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2648 " sending tunnel destroy for tunnel: %s [%X]\n",
2649 GNUNET_i2s (&msg.oid), t->id.tid);
2651 if (NULL == t->client && 0 != t->next_hop)
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2654 GNUNET_PEER_resolve (t->next_hop, &id);
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2656 " sending forward to %s\n",
2658 send_prebuilt_message (&msg.header, t->next_hop, t);
2660 if (NULL == t->owner && 0 != t->prev_hop)
2662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2663 GNUNET_PEER_resolve (t->prev_hop, &id);
2664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2665 " sending back to %s\n",
2667 send_prebuilt_message (&msg.header, t->prev_hop, t);
2669 if (NULL != t->owner)
2671 send_client_tunnel_destroy (t->owner, t);
2673 if (NULL != t->client)
2675 send_client_tunnel_destroy (t->client, t);
2681 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2683 * @param t Tunnel which to cancel.
2684 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2687 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2689 struct MeshPeerInfo *peer_info;
2690 struct MeshPeerQueue *pq;
2691 struct MeshPeerQueue *next;
2694 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2695 peer_info = peer_get_short (neighbor);
2696 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2699 if (pq->tunnel == t)
2701 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2702 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2704 /* Should have been removed on destroy children */
2707 queue_destroy (pq, GNUNET_YES);
2710 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2712 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2713 peer_info->core_transmit = NULL;
2719 * Destroy the tunnel.
2721 * This function does not generate any warning traffic to clients or peers.
2724 * Remove the tunnel from peer_info's and clients' hashmaps.
2725 * Cancel messages belonging to this tunnel queued to neighbors.
2726 * Free any allocated resources linked to the tunnel.
2728 * @param t the tunnel to destroy
2730 * @return GNUNET_OK on success
2733 tunnel_destroy (struct MeshTunnel *t)
2735 struct MeshClient *c;
2736 struct GNUNET_HashCode hash;
2746 struct GNUNET_PeerIdentity id;
2748 GNUNET_PEER_resolve (t->id.oid, &id);
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2750 GNUNET_i2s (&id), t->id.tid);
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2756 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2757 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2766 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2773 if (NULL != t->client)
2777 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2778 t->local_tid_dest, t))
2784 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2785 t->local_tid_dest, t))
2792 if (0 != t->prev_hop)
2794 peer_cancel_queues (t->prev_hop, t);
2795 GNUNET_PEER_change_rc (t->prev_hop, -1);
2797 if (0 != t->next_hop)
2799 peer_cancel_queues (t->next_hop, t);
2800 GNUNET_PEER_change_rc (t->next_hop, -1);
2803 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2806 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2807 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2810 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2811 path_destroy (t->path);
2817 * Tunnel is empty: destroy it.
2819 * Notifies all participants (peers, cleints) about the destruction.
2821 * @param t Tunnel to destroy.
2824 tunnel_destroy_empty (struct MeshTunnel *t)
2828 struct GNUNET_PeerIdentity id;
2830 GNUNET_PEER_resolve (t->id.oid, &id);
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2832 "executing destruction of empty tunnel %s [%X]\n",
2833 GNUNET_i2s (&id), t->id.tid);
2837 if (GNUNET_NO == t->destroy)
2838 tunnel_send_destroy (t);
2839 if (0 == t->pending_messages)
2842 t->destroy = GNUNET_YES;
2846 * Initialize a Flow Control structure to the initial state.
2848 * @param fc Flow Control structure to initialize.
2851 fc_init (struct MeshFlowControl *fc)
2853 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2854 fc->last_pid_recv = (uint32_t) -1;
2855 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2856 fc->last_ack_recv = (uint32_t) -1;
2857 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2858 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2863 * Create a new tunnel
2865 * @param owner Who is the owner of the tunnel (short ID).
2866 * @param tid Tunnel Number of the tunnel.
2867 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2868 * @param local Tunnel Number for the tunnel, for the client point of view.
2870 * @return A new initialized tunnel. NULL on error.
2872 static struct MeshTunnel *
2873 tunnel_new (GNUNET_PEER_Id owner,
2874 MESH_TunnelNumber tid,
2875 struct MeshClient *client,
2876 MESH_TunnelNumber local)
2878 struct MeshTunnel *t;
2879 struct GNUNET_HashCode hash;
2881 if (n_tunnels >= max_tunnels && NULL == client)
2884 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2887 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2889 fc_init (&t->next_fc);
2890 fc_init (&t->prev_fc);
2891 t->local_tid = local;
2893 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2895 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2897 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2898 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2905 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2913 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2915 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2919 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2929 * Set options in a tunnel, extracted from a bit flag field
2931 * @param t Tunnel to set options to.
2932 * @param options Bit array in host byte order.
2935 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2937 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
2938 GNUNET_YES : GNUNET_NO;
2939 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
2940 GNUNET_YES : GNUNET_NO;
2945 * Iterator for deleting each tunnel whose client endpoint disconnected.
2947 * @param cls Closure (client that has disconnected).
2948 * @param key The local tunnel id (used to access the hashmap).
2949 * @param value The value stored at the key (tunnel to destroy).
2951 * @return GNUNET_OK, keep iterating.
2954 tunnel_destroy_iterator (void *cls,
2958 struct MeshTunnel *t = value;
2959 struct MeshClient *c = cls;
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2962 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2963 t->local_tid, t->local_tid_dest, c->id);
2964 client_delete_tunnel (c, t);
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2969 if (0 != t->next_hop) { /* destroy could come before a path is used */
2970 GNUNET_PEER_change_rc (t->next_hop, -1);
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2978 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2979 GNUNET_PEER_change_rc (t->prev_hop, -1);
2984 tunnel_destroy_empty (t);
2991 * Timeout function, destroys tunnel if called
2993 * @param cls Closure (tunnel to destroy).
2994 * @param tc TaskContext
2997 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2999 struct MeshTunnel *t = cls;
3000 struct GNUNET_PeerIdentity id;
3002 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3003 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3005 GNUNET_PEER_resolve(t->id.oid, &id);
3006 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3007 "Tunnel %s [%X] timed out. Destroying.\n",
3008 GNUNET_i2s(&id), t->id.tid);
3009 if (NULL != t->client)
3010 send_client_tunnel_destroy (t->client, t);
3011 tunnel_destroy (t); /* Do not notify other */
3016 * Resets the tunnel timeout. Starts it if no timeout was running.
3018 * @param t Tunnel whose timeout to reset.
3020 * TODO use heap to improve efficiency of scheduler.
3023 tunnel_reset_timeout (struct MeshTunnel *t)
3025 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
3027 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
3028 GNUNET_SCHEDULER_cancel (t->maintenance_task);
3029 t->maintenance_task =
3030 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3031 (refresh_path_time, 4), &tunnel_timeout, t);
3035 /******************************************************************************/
3036 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3037 /******************************************************************************/
3040 * Function to send a create path packet to a peer.
3042 * @param cls closure
3043 * @param size number of bytes available in buf
3044 * @param buf where the callee should write the message
3045 * @return number of bytes written to buf
3048 send_core_path_create (void *cls, size_t size, void *buf)
3050 struct MeshTunnel *t = cls;
3051 struct GNUNET_MESH_CreateTunnel *msg;
3052 struct GNUNET_PeerIdentity *peer_ptr;
3053 struct MeshPeerPath *p = t->path;
3058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3060 sizeof (struct GNUNET_MESH_CreateTunnel) +
3061 p->length * sizeof (struct GNUNET_PeerIdentity);
3063 if (size < size_needed || NULL == buf)
3068 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
3069 msg->header.size = htons (size_needed);
3070 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3071 msg->tid = ntohl (t->id.tid);
3074 if (GNUNET_YES == t->nobuffer)
3075 opt |= GNUNET_MESH_OPTION_NOBUFFER;
3076 if (GNUNET_YES == t->reliable)
3077 opt |= GNUNET_MESH_OPTION_RELIABLE;
3078 msg->opt = htonl (opt);
3079 msg->port = htonl (t->port);
3081 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3082 for (i = 0; i < p->length; i++)
3084 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3088 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3094 * Creates a path ack message in buf and frees all unused resources.
3096 * @param cls closure (MeshTransmissionDescriptor)
3097 * @param size number of bytes available in buf
3098 * @param buf where the callee should write the message
3099 * @return number of bytes written to buf
3102 send_core_path_ack (void *cls, size_t size, void *buf)
3104 struct MeshTunnel *t = cls;
3105 struct GNUNET_MESH_PathACK *msg = buf;
3107 GNUNET_assert (NULL != t);
3108 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3113 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
3114 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3115 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3116 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
3117 msg->tid = htonl (t->id.tid);
3118 msg->peer_id = my_full_id;
3119 msg->ack = htonl (t->prev_fc.last_ack_sent);
3121 /* TODO add signature */
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3124 return sizeof (struct GNUNET_MESH_PathACK);
3129 * Free a transmission that was already queued with all resources
3130 * associated to the request.
3132 * @param queue Queue handler to cancel.
3133 * @param clear_cls Is it necessary to free associated cls?
3136 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3138 struct MeshFlowControl *fc;
3140 if (GNUNET_YES == clear_cls)
3142 switch (queue->type)
3144 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3145 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3146 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3148 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3149 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3150 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3151 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3152 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3154 " prebuilt message\n");
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3157 GNUNET_MESH_DEBUG_M2S (queue->type));
3159 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3165 " type %s unknown!\n",
3166 GNUNET_MESH_DEBUG_M2S (queue->type));
3168 GNUNET_free_non_null (queue->cls);
3170 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3171 queue->peer->queue_tail,
3174 /* Delete from appropriate fc in the tunnel */
3175 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3176 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3178 if (queue->peer->id == queue->tunnel->prev_hop)
3179 fc = &queue->tunnel->prev_fc;
3180 else if (queue->peer->id == queue->tunnel->next_hop)
3181 fc = &queue->tunnel->next_fc;
3185 GNUNET_free (queue);
3190 GNUNET_free (queue);
3195 * @brief Get the next transmittable message from the queue.
3197 * This will be the head, except in the case of being a data packet
3198 * not allowed by the destination peer.
3200 * @param peer Destination peer.
3202 * @return The next viable MeshPeerQueue element to send to that peer.
3203 * NULL when there are no transmittable messages.
3205 struct MeshPeerQueue *
3206 queue_get_next (const struct MeshPeerInfo *peer)
3208 struct MeshPeerQueue *q;
3210 struct GNUNET_MESH_Data *dmsg;
3211 struct MeshTunnel* t;
3215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3216 for (q = peer->queue_head; NULL != q; q = q->next)
3219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3221 GNUNET_MESH_DEBUG_M2S (q->type));
3222 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3223 pid = ntohl (dmsg->pid);
3226 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3227 ack = t->next_fc.last_ack_recv;
3229 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3230 ack = t->prev_fc.last_ack_recv;
3233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3238 "* ACK: %u, PID: %u, MID: %llu\n",
3239 ack, pid, GNUNET_ntohll (dmsg->mid));
3240 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3253 "* nothing found\n");
3259 queue_send (void *cls, size_t size, void *buf)
3261 struct MeshPeerInfo *peer = cls;
3262 struct GNUNET_MessageHeader *msg;
3263 struct MeshPeerQueue *queue;
3264 struct MeshTunnel *t;
3265 struct GNUNET_PeerIdentity dst_id;
3266 struct MeshFlowControl *fc;
3271 peer->core_transmit = NULL;
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3274 queue = queue_get_next (peer);
3276 /* Queue has no internal mesh traffic nor sendable payload */
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3280 if (NULL == peer->queue_head)
3281 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3286 GNUNET_PEER_resolve (peer->id, &dst_id);
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3289 GNUNET_i2s (&dst_id));
3290 /* Check if buffer size is enough for the message */
3291 if (queue->size > size)
3293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3294 "* not enough room, reissue\n");
3295 peer->core_transmit =
3296 GNUNET_CORE_notify_transmit_ready (core_handle,
3299 GNUNET_TIME_UNIT_FOREVER_REL,
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3309 GNUNET_assert (0 < t->pending_messages);
3310 t->pending_messages--;
3314 switch (queue->type)
3317 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3318 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3319 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3320 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3321 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3322 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3323 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3324 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3327 GNUNET_MESH_DEBUG_M2S (queue->type));
3329 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3330 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3331 data_size = send_core_data_raw (queue->cls, size, buf);
3332 msg = (struct GNUNET_MessageHeader *) buf;
3333 type = ntohs (msg->type);
3335 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3337 data_size = send_core_path_create (queue->cls, size, buf);
3339 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3341 data_size = send_core_path_ack (queue->cls, size, buf);
3345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3346 "* type unknown: %u\n",
3351 if (0 < drop_percent &&
3352 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3355 "Dropping message of type %s\n",
3356 GNUNET_MESH_DEBUG_M2S(queue->type));
3359 /* Free queue, but cls was freed by send_core_* */
3360 queue_destroy (queue, GNUNET_NO);
3362 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3363 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3367 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3368 t->next_fc.last_pid_sent = pid;
3369 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3371 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3372 t->prev_fc.last_pid_sent = pid;
3373 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3379 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3385 /* If more data in queue, send next */
3386 queue = queue_get_next (peer);
3389 struct GNUNET_PeerIdentity id;
3391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3392 GNUNET_PEER_resolve (peer->id, &id);
3393 peer->core_transmit =
3394 GNUNET_CORE_notify_transmit_ready(core_handle,
3397 GNUNET_TIME_UNIT_FOREVER_REL,
3403 else if (NULL != peer->queue_head)
3405 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3407 GNUNET_i2s (&my_full_id));
3408 if (peer->id == t->next_hop)
3410 else if (peer->id == t->prev_hop)
3417 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3420 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3430 * @brief Queue and pass message to core when possible.
3432 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3433 * accounts for it. In case the queue is full, the message is dropped and
3436 * Otherwise, message is treated as internal and allowed to go regardless of
3439 * @param cls Closure (@c type dependant). It will be used by queue_send to
3440 * build the message to be sent if not already prebuilt.
3441 * @param type Type of the message, 0 for a raw message.
3442 * @param size Size of the message.
3443 * @param dst Neighbor to send message to.
3444 * @param t Tunnel this message belongs to.
3447 queue_add (void *cls, uint16_t type, size_t size,
3448 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3450 struct MeshPeerQueue *queue;
3451 struct GNUNET_PeerIdentity id;
3452 struct MeshFlowControl *fc;
3456 priority = GNUNET_NO;
3457 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3461 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3467 if (fc->queue_n >= t->queue_max)
3469 /* If this isn't a retransmission, drop the message */
3470 if (GNUNET_NO == t->reliable ||
3471 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3472 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3474 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3478 "queue full: %u/%u\n",
3479 fc->queue_n, t->queue_max);
3480 return; /* Drop this message */
3482 priority = GNUNET_YES;
3486 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3492 if (GNUNET_YES == priority)
3494 struct GNUNET_MESH_Data *d;
3498 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3499 d = (struct GNUNET_MESH_Data *) queue->cls;
3501 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3503 if (queue->type != type)
3505 d = (struct GNUNET_MESH_Data *) queue->cls;
3512 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3513 if (NULL == dst->core_transmit)
3515 GNUNET_PEER_resolve (dst->id, &id);
3516 dst->core_transmit =
3517 GNUNET_CORE_notify_transmit_ready (core_handle,
3520 GNUNET_TIME_UNIT_FOREVER_REL,
3526 t->pending_messages++;
3530 /******************************************************************************/
3531 /******************** MESH NETWORK HANDLERS **************************/
3532 /******************************************************************************/
3536 * Core handler for path creation
3538 * @param cls closure
3539 * @param message message
3540 * @param peer peer identity this notification is about
3542 * @return GNUNET_OK to keep the connection open,
3543 * GNUNET_SYSERR to close it (signal serious error)
3546 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3547 const struct GNUNET_MessageHeader *message)
3549 unsigned int own_pos;
3552 MESH_TunnelNumber tid;
3553 struct GNUNET_MESH_CreateTunnel *msg;
3554 struct GNUNET_PeerIdentity *pi;
3555 struct MeshPeerPath *path;
3556 struct MeshPeerInfo *dest_peer_info;
3557 struct MeshPeerInfo *orig_peer_info;
3558 struct MeshTunnel *t;
3560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3561 "Received a path create msg [%s]\n",
3562 GNUNET_i2s (&my_full_id));
3563 size = ntohs (message->size);
3564 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3566 GNUNET_break_op (0);
3570 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3571 if (size % sizeof (struct GNUNET_PeerIdentity))
3573 GNUNET_break_op (0);
3576 size /= sizeof (struct GNUNET_PeerIdentity);
3579 GNUNET_break_op (0);
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3583 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3585 tid = ntohl (msg->tid);
3586 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3588 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3589 t = tunnel_get (pi, tid);
3590 if (NULL == t) /* might be a local tunnel */
3594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3595 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3601 t->port = ntohl (msg->port);
3602 opt = ntohl (msg->opt);
3603 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3605 t->nobuffer = GNUNET_YES;
3608 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3610 t->reliable = GNUNET_YES;
3612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3614 tunnel_reset_timeout (t);
3616 t->state = MESH_TUNNEL_WAITING;
3618 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3619 if (NULL == dest_peer_info)
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3622 " Creating PeerInfo for destination.\n");
3623 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3624 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3625 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3627 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3629 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3630 if (NULL == orig_peer_info)
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3633 " Creating PeerInfo for origin.\n");
3634 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3635 orig_peer_info->id = GNUNET_PEER_intern (pi);
3636 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3637 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3640 path = path_new (size);
3642 for (i = 0; i < size; i++)
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3645 GNUNET_i2s (&pi[i]));
3646 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3647 if (path->peers[i] == myid)
3650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3651 if (own_pos == 0 && path->peers[own_pos] != myid)
3653 /* create path: self not found in path through self */
3654 GNUNET_break_op (0);
3655 path_destroy (path);
3659 path_add_to_peers (path, GNUNET_NO);
3660 tunnel_use_path (t, path);
3662 peer_info_add_tunnel (dest_peer_info, t);
3664 if (own_pos == size - 1)
3666 struct MeshClient *c;
3668 /* Find target client */
3669 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3672 /* TODO send reject */
3676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3677 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3679 /* Assign local tid */
3680 while (NULL != tunnel_get_incoming (next_local_tid))
3681 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3682 t->local_tid_dest = next_local_tid++;
3683 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3685 if (GNUNET_YES == t->reliable)
3687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3688 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
3690 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3693 tunnel_add_client (t, c);
3694 send_client_tunnel_create (t);
3699 struct MeshPeerPath *path2;
3701 t->next_hop = path->peers[own_pos + 1];
3702 GNUNET_PEER_change_rc(t->next_hop, 1);
3704 /* It's for somebody else! Retransmit. */
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3706 path2 = path_duplicate (path);
3707 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3708 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3709 send_create_path (t);
3717 * Core handler for path ACKs
3719 * @param cls closure
3720 * @param message message
3721 * @param peer peer identity this notification is about
3723 * @return GNUNET_OK to keep the connection open,
3724 * GNUNET_SYSERR to close it (signal serious error)
3727 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3728 const struct GNUNET_MessageHeader *message)
3730 struct GNUNET_MESH_PathACK *msg;
3731 struct MeshPeerInfo *peer_info;
3732 struct MeshPeerPath *p;
3733 struct MeshTunnel *t;
3735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3736 GNUNET_i2s (&my_full_id));
3737 msg = (struct GNUNET_MESH_PathACK *) message;
3738 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3741 /* TODO notify that we don't know the tunnel */
3742 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3744 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3748 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3750 peer_info = peer_get (&msg->peer_id);
3751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3752 GNUNET_i2s (&msg->peer_id));
3753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3756 /* Add path to peers? */
3760 path_add_to_peers (p, GNUNET_YES);
3766 t->state = MESH_TUNNEL_READY;
3767 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3768 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3770 /* Message for us? */
3771 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3774 if (NULL == t->owner)
3776 GNUNET_break_op (0);
3779 if (NULL != peer_info->dhtget)
3781 GNUNET_DHT_get_stop (peer_info->dhtget);
3782 peer_info->dhtget = NULL;
3784 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3785 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3790 " not for us, retransmitting...\n");
3791 peer_info = peer_get (&msg->oid);
3792 send_prebuilt_message (message, t->prev_hop, t);
3798 * Core handler for notifications of broken paths
3800 * @param cls closure
3801 * @param message message
3802 * @param peer peer identity this notification is about
3804 * @return GNUNET_OK to keep the connection open,
3805 * GNUNET_SYSERR to close it (signal serious error)
3808 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3809 const struct GNUNET_MessageHeader *message)
3811 struct GNUNET_MESH_PathBroken *msg;
3812 struct MeshTunnel *t;
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3815 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3816 msg = (struct GNUNET_MESH_PathBroken *) message;
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3818 GNUNET_i2s (&msg->peer1));
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3820 GNUNET_i2s (&msg->peer2));
3821 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3824 GNUNET_break_op (0);
3827 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3828 GNUNET_PEER_search (&msg->peer2));
3835 * Core handler for tunnel destruction
3837 * @param cls closure
3838 * @param message message
3839 * @param peer peer identity this notification is about
3841 * @return GNUNET_OK to keep the connection open,
3842 * GNUNET_SYSERR to close it (signal serious error)
3845 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3846 const struct GNUNET_MessageHeader *message)
3848 struct GNUNET_MESH_TunnelDestroy *msg;
3849 struct MeshTunnel *t;
3851 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3853 "Got a TUNNEL DESTROY packet from %s\n",
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3856 " for tunnel %s [%u]\n",
3857 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3858 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3861 /* Probably already got the message from another path,
3862 * destroyed the tunnel and retransmitted to children.
3865 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3869 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3872 t->local_tid, t->local_tid_dest);
3874 if (GNUNET_PEER_search (peer) == t->prev_hop)
3876 // TODO check owner's signature
3877 // TODO add owner's signatue to tunnel for retransmission
3878 peer_cancel_queues (t->prev_hop, t);
3879 GNUNET_PEER_change_rc (t->prev_hop, -1);
3882 else if (GNUNET_PEER_search (peer) == t->next_hop)
3884 // TODO check dest's signature
3885 // TODO add dest's signatue to tunnel for retransmission
3886 peer_cancel_queues (t->next_hop, t);
3887 GNUNET_PEER_change_rc (t->next_hop, -1);
3892 GNUNET_break_op (0);
3893 // TODO check both owner AND destination's signature to see which matches
3894 // TODO restransmit in appropriate direction
3897 tunnel_destroy_empty (t);
3899 // TODO: add timeout to destroy the tunnel anyway
3905 * Core handler for mesh network traffic going from the origin to a peer
3907 * @param cls closure
3908 * @param peer peer identity this notification is about
3909 * @param message message
3910 * @return GNUNET_OK to keep the connection open,
3911 * GNUNET_SYSERR to close it (signal serious error)
3914 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3915 const struct GNUNET_MessageHeader *message)
3917 struct GNUNET_MESH_Data *msg;
3918 struct MeshTunnel *t;
3923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3926 size = ntohs (message->size);
3928 sizeof (struct GNUNET_MESH_Data) +
3929 sizeof (struct GNUNET_MessageHeader))
3934 msg = (struct GNUNET_MESH_Data *) message;
3935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3936 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3938 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3941 /* TODO notify back: we don't know this tunnel */
3942 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3943 GNUNET_break_op (0);
3946 pid = ntohl (msg->pid);
3947 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3949 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3950 GNUNET_break_op (0);
3951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3952 "Received PID %u, (prev %u), ACK %u\n",
3953 pid, t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
3954 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3958 tunnel_reset_timeout (t);
3959 if (t->dest == myid)
3961 /* TODO signature verification */
3962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3963 " it's for us! sending to clients...\n");
3964 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3965 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
3966 if (GMC_is_pid_bigger (pid, t->prev_fc.last_pid_recv))
3970 mid = GNUNET_ntohll (msg->mid);
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3972 " pid %u (%llu) not seen yet\n", pid, mid);
3973 t->prev_fc.last_pid_recv = pid;
3975 if (GNUNET_NO == t->reliable ||
3976 (mid >= t->bck_rel->mid_recv && mid <= t->bck_rel->mid_recv + 64))
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3979 "!!! RECV %llu\n", GNUNET_ntohll(msg->mid));
3980 if (GNUNET_YES == t->reliable)
3982 /* Is this the exact next expected messasge? */
3983 if (mid == t->bck_rel->mid_recv)
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
3986 t->bck_rel->mid_recv++;
3987 tunnel_send_client_ucast (t, msg);
3988 tunnel_send_client_buffered_ucast (t);
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
3993 tunnel_add_buffer_ucast (t, msg);
3999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4000 " MID %llu not expected (%llu - %llu), dropping!\n",
4001 GNUNET_ntohll (msg->mid),
4002 t->bck_rel->mid_recv, t->bck_rel->mid_recv + 64LL);
4007 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4009 " Pid %u not expected (%u), dropping!\n",
4010 pid, t->prev_fc.last_pid_recv + 1);
4012 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4015 t->prev_fc.last_pid_recv = pid;
4016 if (0 == t->next_hop)
4021 ttl = ntohl (msg->ttl);
4022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4025 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4026 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4027 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4031 " not for us, retransmitting...\n");
4033 send_prebuilt_message (message, t->next_hop, t);
4034 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4040 * Core handler for mesh network traffic toward the owner of a tunnel
4042 * @param cls closure
4043 * @param message message
4044 * @param peer peer identity this notification is about
4046 * @return GNUNET_OK to keep the connection open,
4047 * GNUNET_SYSERR to close it (signal serious error)
4050 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4051 const struct GNUNET_MessageHeader *message)
4053 struct GNUNET_MESH_Data *msg;
4054 struct MeshTunnel *t;
4059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4061 size = ntohs (message->size);
4062 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
4063 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4065 GNUNET_break_op (0);
4068 msg = (struct GNUNET_MESH_Data *) message;
4069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4070 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4071 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4072 pid = ntohl (msg->pid);
4075 /* TODO notify that we dont know this tunnel (whom)? */
4076 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4078 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4079 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4083 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
4085 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
4086 GNUNET_break_op (0);
4087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4088 "Received PID %u, ACK %u\n",
4089 pid, t->next_fc.last_ack_sent);
4090 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4094 if (myid == t->id.oid)
4096 /* TODO signature verification */
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4098 " it's for us! sending to clients...\n");
4099 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4100 if ( (GNUNET_NO == t->reliable &&
4101 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
4103 (GNUNET_YES == t->reliable &&
4104 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
4106 t->next_fc.last_pid_recv = pid;
4107 tunnel_send_client_to_orig (t, msg);
4111 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4112 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4113 " Pid %u not expected, sending FWD ACK!\n", pid);
4115 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4119 " not for us, retransmitting...\n");
4120 t->next_fc.last_pid_recv = pid;
4121 if (0 == t->prev_hop) /* No owner AND no prev hop */
4123 if (GNUNET_YES == t->destroy)
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4126 "to orig received on a dying tunnel %s [%X]\n",
4127 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4131 "unknown to origin at %s\n",
4132 GNUNET_i2s (&my_full_id));
4133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4137 "on tunnel %s [%X]\n",
4138 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4141 ttl = ntohl (msg->ttl);
4142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4145 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4147 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4150 send_prebuilt_message (message, t->prev_hop, t);
4151 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4158 * Core handler for mesh network traffic end-to-end ACKs.
4160 * @param cls Closure.
4161 * @param message Message.
4162 * @param peer Peer identity this notification is about.
4164 * @return GNUNET_OK to keep the connection open,
4165 * GNUNET_SYSERR to close it (signal serious error)
4168 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4169 const struct GNUNET_MessageHeader *message)
4171 struct GNUNET_MESH_DataACK *msg;
4172 struct MeshTunnelReliability *rel;
4173 struct MeshReliableMessage *copy;
4174 struct MeshReliableMessage *next;
4175 struct MeshTunnel *t;
4181 type = ntohs (message->type);
4182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4183 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4184 msg = (struct GNUNET_MESH_DataACK *) message;
4186 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4189 /* TODO notify that we dont know this tunnel (whom)? */
4190 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4193 ack = GNUNET_ntohll (msg->mid);
4194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %llu\n", ack);
4196 /* Is this a forward or backward ACK? */
4197 id = GNUNET_PEER_search (peer);
4198 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4201 if (NULL == t->owner)
4203 send_prebuilt_message (message, t->prev_hop, t);
4207 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4209 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4212 if (NULL == t->client)
4214 send_prebuilt_message (message, t->next_hop, t);
4218 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4222 GNUNET_break_op (0);
4226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %llu\n", ack);
4227 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4229 struct GNUNET_TIME_Relative time;
4231 if (copy->mid > ack)
4233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %llu, out!\n", copy->mid);
4234 tunnel_free_buffer_ucast (t, msg);
4238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %llu\n", copy->mid);
4240 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
4241 rel->expected_delay.rel_value += time.rel_value;
4242 rel->expected_delay.rel_value /= 2;
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
4245 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
4247 rel->retry_timer = rel->expected_delay;
4248 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
4252 if (GNUNET_YES == work)
4254 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4256 GNUNET_SCHEDULER_cancel (rel->retry_task);
4257 if (NULL == rel->head_sent)
4259 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4263 struct GNUNET_TIME_Absolute new_target;
4264 struct GNUNET_TIME_Relative delay;
4266 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4267 MESH_RETRANSMIT_MARGIN);
4268 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4270 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4272 GNUNET_SCHEDULER_add_delayed (delay,
4273 &tunnel_retransmit_message,
4284 * Core handler for mesh network traffic point-to-point acks.
4286 * @param cls closure
4287 * @param message message
4288 * @param peer peer identity this notification is about
4290 * @return GNUNET_OK to keep the connection open,
4291 * GNUNET_SYSERR to close it (signal serious error)
4294 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4295 const struct GNUNET_MessageHeader *message)
4297 struct GNUNET_MESH_ACK *msg;
4298 struct MeshTunnel *t;
4299 struct MeshFlowControl *fc;
4303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4305 msg = (struct GNUNET_MESH_ACK *) message;
4307 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4311 /* TODO notify that we dont know this tunnel (whom)? */
4312 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4315 ack = ntohl (msg->pid);
4316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4318 /* Is this a forward or backward ACK? */
4319 id = GNUNET_PEER_search (peer);
4320 if (t->next_hop == id)
4323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4326 else if (t->prev_hop == id)
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4334 GNUNET_break_op (0);
4338 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4339 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4341 GNUNET_SCHEDULER_cancel (fc->poll_task);
4342 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4343 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4345 fc->last_ack_recv = ack;
4346 peer_unlock_queue (id);
4348 if (t->next_hop == id)
4349 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4351 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4358 * Core handler for mesh network traffic point-to-point ack polls.
4360 * @param cls closure
4361 * @param message message
4362 * @param peer peer identity this notification is about
4364 * @return GNUNET_OK to keep the connection open,
4365 * GNUNET_SYSERR to close it (signal serious error)
4368 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4369 const struct GNUNET_MessageHeader *message)
4371 struct GNUNET_MESH_Poll *msg;
4372 struct MeshTunnel *t;
4373 struct MeshFlowControl *fc;
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4381 msg = (struct GNUNET_MESH_Poll *) message;
4383 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4387 /* TODO notify that we dont know this tunnel (whom)? */
4388 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4389 GNUNET_break_op (0);
4393 /* Is this a forward or backward ACK? */
4394 id = GNUNET_PEER_search(peer);
4395 pid = ntohl (msg->pid);
4396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4397 if (t->next_hop == id)
4399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->next_fc.last_pid_recv);
4402 old = fc->last_pid_recv;
4403 fc->last_pid_recv = pid;
4404 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4406 else if (t->prev_hop == id)
4408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->prev_fc.last_pid_recv);
4411 old = fc->last_pid_recv;
4412 fc->last_pid_recv = pid;
4413 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4418 if (GNUNET_YES == t->reliable)
4419 fc->last_pid_recv = old;
4426 * Core handler for mesh keepalives.
4428 * @param cls closure
4429 * @param message message
4430 * @param peer peer identity this notification is about
4431 * @return GNUNET_OK to keep the connection open,
4432 * GNUNET_SYSERR to close it (signal serious error)
4434 * TODO: Check who we got this from, to validate route.
4437 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4438 const struct GNUNET_MessageHeader *message)
4440 struct GNUNET_MESH_TunnelKeepAlive *msg;
4441 struct MeshTunnel *t;
4443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4446 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4447 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4451 /* TODO notify that we dont know that tunnel */
4452 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4457 tunnel_reset_timeout (t);
4458 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4461 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4462 send_prebuilt_message (message, t->next_hop, t);
4469 * Functions to handle messages from core
4471 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4472 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4473 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4474 sizeof (struct GNUNET_MESH_PathBroken)},
4475 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4476 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4477 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4478 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4479 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4480 sizeof (struct GNUNET_MESH_DataACK)},
4481 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4482 sizeof (struct GNUNET_MESH_DataACK)},
4483 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4484 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4485 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4486 sizeof (struct GNUNET_MESH_ACK)},
4487 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4488 sizeof (struct GNUNET_MESH_Poll)},
4489 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4490 sizeof (struct GNUNET_MESH_PathACK)},
4496 /******************************************************************************/
4497 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4498 /******************************************************************************/
4503 * notify_client_connection_failure: notify a client that the connection to the
4504 * requested remote peer is not possible (for instance, no route found)
4505 * Function called when the socket is ready to queue more data. "buf" will be
4506 * NULL and "size" zero if the socket was closed for writing in the meantime.
4508 * @param cls closure
4509 * @param size number of bytes available in buf
4510 * @param buf where the callee should write the message
4511 * @return number of bytes written to buf
4514 notify_client_connection_failure (void *cls, size_t size, void *buf)
4517 struct MeshPeerInfo *peer_info;
4518 struct GNUNET_MESH_PeerControl *msg;
4519 struct GNUNET_PeerIdentity id;
4521 if (0 == size && NULL == buf)
4523 // TODO retry? cancel?
4527 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4528 peer_info = (struct MeshPeerInfo *) cls;
4529 msg = (struct GNUNET_MESH_PeerControl *) buf;
4530 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4531 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4532 // msg->tunnel_id = htonl(peer_info->t->tid);
4533 GNUNET_PEER_resolve (peer_info->id, &id);
4534 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4542 * Send keepalive packets for a tunnel.
4544 * @param cls Closure (tunnel for which to send the keepalive).
4545 * @param tc Notification context.
4547 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4550 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4552 struct MeshTunnel *t = cls;
4553 struct GNUNET_MESH_TunnelKeepAlive *msg;
4554 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4557 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4558 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4559 NULL == t->owner || 0 == t->local_tid)
4564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4565 "sending keepalive for tunnel %d\n", t->id.tid);
4567 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4568 msg->header.size = htons (size);
4569 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4570 msg->oid = my_full_id;
4571 msg->tid = htonl (t->id.tid);
4572 send_prebuilt_message (&msg->header, t->next_hop, t);
4574 t->maintenance_task =
4575 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4580 * Function to process paths received for a new peer addition. The recorded
4581 * paths form the initial tunnel, which can be optimized later.
4582 * Called on each result obtained for the DHT search.
4584 * @param cls closure
4585 * @param exp when will this value expire
4586 * @param key key of the result
4587 * @param get_path path of the get request
4588 * @param get_path_length lenght of get_path
4589 * @param put_path path of the put request
4590 * @param put_path_length length of the put_path
4591 * @param type type of the result
4592 * @param size number of bytes in data
4593 * @param data pointer to the result data
4596 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4597 const struct GNUNET_HashCode * key,
4598 const struct GNUNET_PeerIdentity *get_path,
4599 unsigned int get_path_length,
4600 const struct GNUNET_PeerIdentity *put_path,
4601 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4602 size_t size, const void *data)
4604 struct MeshPeerInfo *peer = cls;
4605 struct MeshPeerPath *p;
4606 struct GNUNET_PeerIdentity pi;
4609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4610 GNUNET_PEER_resolve (peer->id, &pi);
4611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4613 p = path_build_from_dht (get_path, get_path_length,
4614 put_path, put_path_length);
4615 path_add_to_peers (p, GNUNET_NO);
4617 for (i = 0; i < peer->ntunnels; i++)
4619 struct GNUNET_PeerIdentity id;
4621 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4623 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4624 peer->tunnels[i]->local_tid,
4625 peer->tunnels[i]->local_tid_dest);
4626 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4629 peer_connect (peer, peer->tunnels[i]);
4637 /******************************************************************************/
4638 /********************* MESH LOCAL HANDLES **************************/
4639 /******************************************************************************/
4643 * Handler for client connection.
4645 * @param cls Closure (unused).
4646 * @param client Client handler.
4649 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4651 struct MeshClient *c;
4655 c = GNUNET_malloc (sizeof (struct MeshClient));
4657 GNUNET_SERVER_client_keep (client);
4658 GNUNET_SERVER_client_set_user_context (client, c);
4659 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4664 * Handler for client disconnection
4666 * @param cls closure
4667 * @param client identification of the client; NULL
4668 * for the last call when the server is destroyed
4671 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4673 struct MeshClient *c;
4674 struct MeshClient *next;
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4683 c = client_get (client);
4686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4688 GNUNET_SERVER_client_drop (c->handle);
4689 c->shutting_down = GNUNET_YES;
4690 if (NULL != c->own_tunnels)
4692 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4693 &tunnel_destroy_iterator, c);
4694 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4697 if (NULL != c->incoming_tunnels)
4699 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4700 &tunnel_destroy_iterator, c);
4701 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4704 if (NULL != c->ports)
4705 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4707 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4710 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4715 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4723 * Handler for new clients
4725 * @param cls closure
4726 * @param client identification of the client
4727 * @param message the actual message, which includes messages the client wants
4730 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4731 const struct GNUNET_MessageHeader *message)
4733 struct GNUNET_MESH_ClientConnect *cc_msg;
4734 struct MeshClient *c;
4739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4741 /* Check data sanity */
4742 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4743 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4744 if (0 != (size % sizeof (uint32_t)))
4747 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4750 size /= sizeof (uint32_t);
4752 /* Initialize new client structure */
4753 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4754 c->id = next_client_id++; /* overflow not important: just for debug */
4755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4761 p = (uint32_t *) &cc_msg[1];
4762 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4763 for (i = 0; i < size; i++)
4766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4768 /* store in client's hashmap */
4769 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4770 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4771 /* store in global hashmap */
4772 /* FIXME only allow one client to have the port open,
4773 * have a backup hashmap with waiting clients */
4774 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4775 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4779 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4780 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4781 GNUNET_SERVER_notification_context_add (nc, client);
4782 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4784 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4790 * Handler for requests of new tunnels
4792 * @param cls Closure.
4793 * @param client Identification of the client.
4794 * @param message The actual message.
4797 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4798 const struct GNUNET_MessageHeader *message)
4800 struct GNUNET_MESH_TunnelMessage *t_msg;
4801 struct MeshPeerInfo *peer_info;
4802 struct MeshTunnel *t;
4803 struct MeshClient *c;
4804 MESH_TunnelNumber tid;
4806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4808 /* Sanity check for client registration */
4809 if (NULL == (c = client_get (client)))
4812 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4817 /* Message size sanity check */
4818 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4821 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4825 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4827 GNUNET_i2s (&t_msg->peer));
4828 /* Sanity check for tunnel numbering */
4829 tid = ntohl (t_msg->tunnel_id);
4830 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4833 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4836 /* Sanity check for duplicate tunnel IDs */
4837 if (NULL != tunnel_get_by_local_id (c, tid))
4840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4845 while (NULL != tunnel_get_by_pi (myid, next_tid))
4846 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4847 t = tunnel_new (myid, next_tid, c, tid);
4848 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4855 t->port = ntohl (t_msg->port);
4856 tunnel_set_options (t, ntohl (t_msg->opt));
4857 if (GNUNET_YES == t->reliable)
4859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4860 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4862 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4866 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4868 peer_info = peer_get (&t_msg->peer);
4869 peer_info_add_tunnel (peer_info, t);
4870 peer_connect (peer_info, t);
4871 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4877 * Handler for requests of deleting tunnels
4879 * @param cls closure
4880 * @param client identification of the client
4881 * @param message the actual message
4884 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4885 const struct GNUNET_MessageHeader *message)
4887 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4888 struct MeshClient *c;
4889 struct MeshTunnel *t;
4890 MESH_TunnelNumber tid;
4892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4893 "Got a DESTROY TUNNEL from client!\n");
4895 /* Sanity check for client registration */
4896 if (NULL == (c = client_get (client)))
4899 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4904 /* Message sanity check */
4905 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4908 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4912 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4914 /* Retrieve tunnel */
4915 tid = ntohl (tunnel_msg->tunnel_id);
4916 t = tunnel_get_by_local_id(c, tid);
4919 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4921 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4925 /* Cleanup after the tunnel */
4926 client_delete_tunnel (c, t);
4933 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4937 /* The tunnel will be destroyed when the last message is transmitted. */
4938 tunnel_destroy_empty (t);
4940 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4946 * Handler for client traffic
4948 * @param cls closure
4949 * @param client identification of the client
4950 * @param message the actual message
4953 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4954 const struct GNUNET_MessageHeader *message)
4956 struct GNUNET_MESH_LocalData *data_msg;
4957 struct MeshClient *c;
4958 struct MeshTunnel *t;
4959 struct MeshFlowControl *fc;
4960 MESH_TunnelNumber tid;
4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4964 "Got data from a client!\n");
4966 /* Sanity check for client registration */
4967 if (NULL == (c = client_get (client)))
4970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4975 data_msg = (struct GNUNET_MESH_LocalData *) message;
4977 /* Sanity check for message size */
4978 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4979 if (size < sizeof (struct GNUNET_MessageHeader))
4982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4986 /* Tunnel exists? */
4987 tid = ntohl (data_msg->tid);
4988 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4991 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4994 t = tunnel_get_by_local_id (c, tid);
4998 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5002 /* Is the client in the tunnel? */
5003 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5005 t->owner->handle == client)
5007 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5009 t->client->handle == client) ) )
5012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5016 /* Ok, everything is correct, send the message
5017 * (pretend we got it from a mesh peer)
5020 struct GNUNET_MESH_Data *payload;
5021 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5023 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5024 if (GNUNET_YES == t->reliable)
5026 struct MeshTunnelReliability *rel;
5027 struct MeshReliableMessage *copy;
5029 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5030 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5031 + sizeof(struct GNUNET_MESH_Data)
5033 copy->mid = rel->mid_sent++;
5034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %llu\n", copy->mid);
5035 copy->timestamp = GNUNET_TIME_absolute_get ();
5038 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5039 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5042 GNUNET_TIME_relative_multiply (rel->expected_delay,
5043 MESH_RETRANSMIT_MARGIN);
5045 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5046 &tunnel_retransmit_message,
5049 payload = (struct GNUNET_MESH_Data *) ©[1];
5050 payload->mid = GNUNET_htonll (copy->mid);
5054 payload = (struct GNUNET_MESH_Data *) cbuf;
5056 // FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
5057 // use different struct for unreliable traffic, save 8 bytes
5059 memcpy (&payload[1], &data_msg[1], size);
5060 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5061 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5062 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5063 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5064 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5065 payload->tid = htonl (t->id.tid);
5066 payload->ttl = htonl (default_ttl);
5067 payload->pid = htonl (fc->last_pid_recv + 1);
5068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5069 " calling generic handler...\n");
5070 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5071 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5073 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5076 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5083 * Handler for client's ACKs for payload traffic.
5085 * @param cls Closure (unused).
5086 * @param client Identification of the client.
5087 * @param message The actual message.
5090 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5091 const struct GNUNET_MessageHeader *message)
5093 struct GNUNET_MESH_LocalAck *msg;
5094 struct MeshTunnel *t;
5095 struct MeshClient *c;
5096 MESH_TunnelNumber tid;
5098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5099 /* Sanity check for client registration */
5100 if (NULL == (c = client_get (client)))
5103 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5108 msg = (struct GNUNET_MESH_LocalAck *) message;
5110 /* Tunnel exists? */
5111 tid = ntohl (msg->tunnel_id);
5112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5113 t = tunnel_get_by_local_id (c, tid);
5117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5118 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5119 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5123 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5126 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5127 t->prev_fc.last_ack_recv++;
5128 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5132 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5133 t->next_fc.last_ack_recv++;
5134 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5137 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5145 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5147 * @param cls Closure (client handle).
5148 * @param key Key (hashed tunnel ID, unused).
5149 * @param value Tunnel info.
5151 * @return GNUNET_YES, to keep iterating.
5154 monitor_all_tunnels_iterator (void *cls,
5155 const struct GNUNET_HashCode * key,
5158 struct GNUNET_SERVER_Client *client = cls;
5159 struct MeshTunnel *t = value;
5160 struct GNUNET_MESH_LocalMonitor *msg;
5162 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5163 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5164 msg->tunnel_id = htonl (t->id.tid);
5165 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5166 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5167 GNUNET_PEER_resolve (t->dest, &msg->destination);
5169 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5170 "* sending info about tunnel %s [%u]\n",
5171 GNUNET_i2s (&msg->owner), t->id.tid);
5173 GNUNET_SERVER_notification_context_unicast (nc, client,
5174 &msg->header, GNUNET_NO);
5180 * Handler for client's MONITOR request.
5182 * @param cls Closure (unused).
5183 * @param client Identification of the client.
5184 * @param message The actual message.
5187 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5188 const struct GNUNET_MessageHeader *message)
5190 struct MeshClient *c;
5192 /* Sanity check for client registration */
5193 if (NULL == (c = client_get (client)))
5196 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5200 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5201 "Received get tunnels request from client %u\n",
5203 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5204 monitor_all_tunnels_iterator,
5206 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5207 "Get tunnels request from client %u completed\n",
5209 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5214 * Handler for client's MONITOR_TUNNEL request.
5216 * @param cls Closure (unused).
5217 * @param client Identification of the client.
5218 * @param message The actual message.
5221 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5222 const struct GNUNET_MessageHeader *message)
5224 const struct GNUNET_MESH_LocalMonitor *msg;
5225 struct GNUNET_MESH_LocalMonitor *resp;
5226 struct MeshClient *c;
5227 struct MeshTunnel *t;
5229 /* Sanity check for client registration */
5230 if (NULL == (c = client_get (client)))
5233 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5237 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5238 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5239 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5242 ntohl (msg->tunnel_id));
5243 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5246 /* We don't know the tunnel FIXME */
5247 struct GNUNET_MESH_LocalMonitor warn;
5250 GNUNET_SERVER_notification_context_unicast (nc, client,
5253 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5257 /* Initialize context */
5258 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5260 GNUNET_PEER_resolve (t->dest, &resp->destination);
5261 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5262 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5263 &resp->header, GNUNET_NO);
5266 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5267 "Monitor tunnel request from client %u completed\n",
5269 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5274 * Functions to handle messages from clients
5276 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5277 {&handle_local_new_client, NULL,
5278 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5279 {&handle_local_tunnel_create, NULL,
5280 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5281 sizeof (struct GNUNET_MESH_TunnelMessage)},
5282 {&handle_local_tunnel_destroy, NULL,
5283 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5284 sizeof (struct GNUNET_MESH_TunnelMessage)},
5285 {&handle_local_data, NULL,
5286 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5287 {&handle_local_ack, NULL,
5288 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5289 sizeof (struct GNUNET_MESH_LocalAck)},
5290 {&handle_local_get_tunnels, NULL,
5291 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5292 sizeof (struct GNUNET_MessageHeader)},
5293 {&handle_local_show_tunnel, NULL,
5294 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5295 sizeof (struct GNUNET_MESH_LocalMonitor)},
5301 * Method called whenever a given peer connects.
5303 * @param cls closure
5304 * @param peer peer identity this notification is about
5307 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5309 struct MeshPeerInfo *peer_info;
5310 struct MeshPeerPath *path;
5312 DEBUG_CONN ("Peer connected\n");
5313 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5314 peer_info = peer_get (peer);
5315 if (myid == peer_info->id)
5317 DEBUG_CONN (" (self)\n");
5318 path = path_new (1);
5322 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5323 path = path_new (2);
5324 path->peers[1] = peer_info->id;
5325 GNUNET_PEER_change_rc (peer_info->id, 1);
5326 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5328 path->peers[0] = myid;
5329 GNUNET_PEER_change_rc (myid, 1);
5330 peer_info_add_path (peer_info, path, GNUNET_YES);
5336 * Method called whenever a peer disconnects.
5338 * @param cls closure
5339 * @param peer peer identity this notification is about
5342 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5344 struct MeshPeerInfo *pi;
5345 struct MeshPeerQueue *q;
5346 struct MeshPeerQueue *n;
5348 DEBUG_CONN ("Peer disconnected\n");
5349 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5359 /* TODO try to reroute this traffic instead */
5360 queue_destroy(q, GNUNET_YES);
5363 if (NULL != pi->core_transmit)
5365 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5366 pi->core_transmit = NULL;
5368 peer_remove_path (pi, pi->id, myid);
5371 DEBUG_CONN (" (self)\n");
5373 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5379 * Install server (service) handlers and start listening to clients.
5384 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5385 GNUNET_SERVER_connect_notify (server_handle,
5386 &handle_local_client_connect, NULL);
5387 GNUNET_SERVER_disconnect_notify (server_handle,
5388 &handle_local_client_disconnect, NULL);
5389 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5391 clients_head = NULL;
5392 clients_tail = NULL;
5394 GNUNET_SERVER_resume (server_handle);
5399 * To be called on core init/fail.
5401 * @param cls Closure (config)
5402 * @param server handle to the server for this service
5403 * @param identity the public identity of this peer
5406 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5407 const struct GNUNET_PeerIdentity *identity)
5409 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5413 GNUNET_break (core_handle == server);
5414 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5420 GNUNET_i2s (identity));
5421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5423 GNUNET_i2s (&my_full_id));
5424 GNUNET_CORE_disconnect (core_handle);
5425 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5426 NULL, /* Closure passed to MESH functions */
5427 &core_init, /* Call core_init once connected */
5428 &core_connect, /* Handle connects */
5429 &core_disconnect, /* remove peers on disconnects */
5430 NULL, /* Don't notify about all incoming messages */
5431 GNUNET_NO, /* For header only in notification */
5432 NULL, /* Don't notify about all outbound messages */
5433 GNUNET_NO, /* For header-only out notification */
5434 core_handlers); /* Register these handlers */
5443 /******************************************************************************/
5444 /************************ MAIN FUNCTIONS ****************************/
5445 /******************************************************************************/
5448 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5450 * @param cls closure
5451 * @param key current key code
5452 * @param value value in the hash map
5453 * @return GNUNET_YES if we should continue to iterate,
5457 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5459 struct MeshTunnel *t = value;
5466 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5468 * @param cls closure
5469 * @param key current key code
5470 * @param value value in the hash map
5471 * @return GNUNET_YES if we should continue to iterate,
5475 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5477 struct MeshPeerInfo *p = value;
5478 struct MeshPeerQueue *q;
5479 struct MeshPeerQueue *n;
5487 queue_destroy(q, GNUNET_YES);
5491 peer_info_destroy (p);
5497 * Task run during shutdown.
5503 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5507 if (core_handle != NULL)
5509 GNUNET_CORE_disconnect (core_handle);
5512 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5513 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5514 if (dht_handle != NULL)
5516 GNUNET_DHT_disconnect (dht_handle);
5521 GNUNET_SERVER_notification_context_destroy (nc);
5524 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5526 GNUNET_SCHEDULER_cancel (announce_id_task);
5527 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5534 * Process mesh requests.
5536 * @param cls closure
5537 * @param server the initialized server
5538 * @param c configuration to use
5541 run (void *cls, struct GNUNET_SERVER_Handle *server,
5542 const struct GNUNET_CONFIGURATION_Handle *c)
5545 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5548 server_handle = server;
5549 GNUNET_SERVER_suspend (server_handle);
5552 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5557 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5558 "mesh", "peer/privatekey");
5559 GNUNET_SCHEDULER_shutdown ();
5564 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5565 &refresh_path_time))
5567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5569 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5570 "mesh", "refresh path time");
5571 GNUNET_SCHEDULER_shutdown ();
5576 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5581 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5582 "mesh", "id announce time");
5583 GNUNET_SCHEDULER_shutdown ();
5588 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5593 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5594 "mesh", "connect timeout");
5595 GNUNET_SCHEDULER_shutdown ();
5600 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5605 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5606 "mesh", "max msgs queue");
5607 GNUNET_SCHEDULER_shutdown ();
5612 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5617 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5618 "mesh", "max tunnels");
5619 GNUNET_SCHEDULER_shutdown ();
5624 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5627 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5629 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5630 "mesh", "default ttl", 64);
5635 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5639 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5640 "mesh", "max peers", 1000);
5645 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5652 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5653 "Mesh is running with drop mode enabled. "
5654 "This is NOT a good idea! "
5655 "Remove the DROP_PERCENT option from your configuration.\n");
5659 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5660 &dht_replication_level))
5662 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5664 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5665 "mesh", "dht replication level", 3);
5666 dht_replication_level = 3;
5669 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5670 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5671 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5672 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5674 dht_handle = GNUNET_DHT_connect (c, 64);
5675 if (NULL == dht_handle)
5679 stats = GNUNET_STATISTICS_create ("mesh", c);
5681 /* Scheduled the task to clean up when shutdown is called */
5682 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5684 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5685 GNUNET_free (keyfile);
5686 GNUNET_assert (NULL != pk);
5687 my_private_key = pk;
5688 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5689 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5690 &my_full_id.hashPubKey);
5691 myid = GNUNET_PEER_intern (&my_full_id);
5692 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5693 "Mesh for peer [%s] starting\n",
5694 GNUNET_i2s(&my_full_id));
5696 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5697 NULL, /* Closure passed to MESH functions */
5698 &core_init, /* Call core_init once connected */
5699 &core_connect, /* Handle connects */
5700 &core_disconnect, /* remove peers on disconnects */
5701 NULL, /* Don't notify about all incoming messages */
5702 GNUNET_NO, /* For header only in notification */
5703 NULL, /* Don't notify about all outbound messages */
5704 GNUNET_NO, /* For header-only out notification */
5705 core_handlers); /* Register these handlers */
5706 if (NULL == core_handle)
5709 GNUNET_SCHEDULER_shutdown ();
5713 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5714 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5720 * The main function for the mesh service.
5722 * @param argc number of arguments from the command line
5723 * @param argv command line arguments
5724 * @return 0 ok, 1 on error
5727 main (int argc, char *const *argv)
5732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5733 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5735 ret = (GNUNET_OK == r) ? 0 : 1;
5736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5740 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5741 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5742 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);