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 * - when sending in-order buffered data, wait for client ACKs
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)
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.
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 fwd_maintenance_task;
476 * Task to keep the used paths alive at the destination,
477 * time tunnel out on all the other peers.
479 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
482 * Path being used for the tunnel.
484 struct MeshPeerPath *path;
487 * Flag to signal the destruction of the tunnel.
488 * If this is set GNUNET_YES the tunnel will be destroyed
489 * when the queue is empty.
494 * Total messages pending for this tunnels, payload or not.
496 unsigned int pending_messages;
500 * Only present (non-NULL) at the owner of a tunnel.
502 struct MeshTunnelReliability *fwd_rel;
506 * Only present (non-NULL) at the destination of a tunnel.
508 struct MeshTunnelReliability *bck_rel;
513 * Struct containing information about a client of the service
515 * TODO: add a list of 'waiting' ports
522 struct MeshClient *next;
527 struct MeshClient *prev;
530 * Tunnels that belong to this client, indexed by local id
532 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
535 * Tunnels this client has accepted, indexed by incoming local id
537 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
540 * Handle to communicate with the client
542 struct GNUNET_SERVER_Client *handle;
545 * Ports that this client has declared interest in.
546 * Indexed by port, contains *Client.
548 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
551 * Whether the client is active or shutting down (don't send confirmations
552 * to a client that is shutting down.
557 * ID of the client, mainly for debug messages
564 /******************************************************************************/
565 /************************ DEBUG FUNCTIONS ****************************/
566 /******************************************************************************/
570 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
571 * @param cls string to print
572 * @param success GNUNET_OK if the PUT was transmitted,
573 * GNUNET_NO on timeout,
574 * GNUNET_SYSERR on disconnect from service
575 * after the PUT message was transmitted
576 * (so we don't know if it was received or not)
581 mesh_debug (void *cls, int success)
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
591 /******************************************************************************/
592 /*********************** GLOBAL VARIABLES ****************************/
593 /******************************************************************************/
595 /************************** Configuration parameters **************************/
598 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
600 static struct GNUNET_TIME_Relative refresh_path_time;
603 * How often to PUT own ID in the DHT.
605 static struct GNUNET_TIME_Relative id_announce_time;
608 * Maximum time allowed to connect to a peer found by string.
610 static struct GNUNET_TIME_Relative connect_timeout;
613 * Default TTL for payload packets.
615 static unsigned long long default_ttl;
618 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
620 static unsigned long long dht_replication_level;
623 * How many tunnels are we willing to maintain.
624 * Local tunnels are always allowed, even if there are more tunnels than max.
626 static unsigned long long max_tunnels;
629 * How many messages *in total* are we willing to queue, divided by number of
630 * tunnels to get tunnel queue size.
632 static unsigned long long max_msgs_queue;
635 * How many peers do we want to remember?
637 static unsigned long long max_peers;
640 * Percentage of messages that will be dropped (for test purposes only).
642 static unsigned long long drop_percent;
644 /*************************** Static global variables **************************/
647 * DLL with all the clients, head.
649 static struct MeshClient *clients_head;
652 * DLL with all the clients, tail.
654 static struct MeshClient *clients_tail;
657 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
659 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
662 * Number of tunnels known.
664 static unsigned long long n_tunnels;
667 * Tunnels incoming, indexed by MESH_TunnelNumber
668 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
670 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
673 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
675 static struct GNUNET_CONTAINER_MultiHashMap *peers;
678 * Handle to communicate with transport
680 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
683 * Handle to communicate with core.
685 static struct GNUNET_CORE_Handle *core_handle;
690 static struct GNUNET_DHT_Handle *dht_handle;
695 static struct GNUNET_SERVER_Handle *server_handle;
698 * Handle to the statistics service.
700 static struct GNUNET_STATISTICS_Handle *stats;
703 * Notification context, to send messages to local clients.
705 static struct GNUNET_SERVER_NotificationContext *nc;
708 * Local peer own ID (memory efficient handle).
710 static GNUNET_PEER_Id myid;
713 * Local peer own ID (full value).
715 static struct GNUNET_PeerIdentity my_full_id;
720 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
725 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
728 * Tunnel ID for the next created tunnel (global tunnel number).
730 static MESH_TunnelNumber next_tid;
733 * Tunnel ID for the next incoming tunnel (local tunnel number).
735 static MESH_TunnelNumber next_local_tid;
738 * All ports clients of this peer have opened.
740 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
743 * Task to periodically announce itself in the network.
745 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
748 * Next ID to assign to a client.
750 unsigned int next_client_id;
753 /******************************************************************************/
754 /*********************** DECLARATIONS **************************/
755 /******************************************************************************/
758 * Function to process paths received for a new peer addition. The recorded
759 * paths form the initial tunnel, which can be optimized later.
760 * Called on each result obtained for the DHT search.
763 * @param exp when will this value expire
764 * @param key key of the result
765 * @param type type of the result
766 * @param size number of bytes in data
767 * @param data pointer to the result data
770 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
771 const struct GNUNET_HashCode * key,
772 const struct GNUNET_PeerIdentity *get_path,
773 unsigned int get_path_length,
774 const struct GNUNET_PeerIdentity *put_path,
775 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
776 size_t size, const void *data);
780 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
781 * and insert it in the appropriate structures if the peer is not known yet.
783 * @param peer Full identity of the peer.
785 * @return Existing or newly created peer info.
787 static struct MeshPeerInfo *
788 peer_get (const struct GNUNET_PeerIdentity *peer);
792 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
793 * and insert it in the appropriate structures if the peer is not known yet.
795 * @param peer Short identity of the peer.
797 * @return Existing or newly created peer info.
799 static struct MeshPeerInfo *
800 peer_get_short (const GNUNET_PEER_Id peer);
804 * Build a PeerPath from the paths returned from the DHT, reversing the paths
805 * to obtain a local peer -> destination path and interning the peer ids.
807 * @return Newly allocated and created path
809 static struct MeshPeerPath *
810 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
811 unsigned int get_path_length,
812 const struct GNUNET_PeerIdentity *put_path,
813 unsigned int put_path_length);
817 * Adds a path to the peer_infos of all the peers in the path
819 * @param p Path to process.
820 * @param confirmed Whether we know if the path works or not.
823 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
828 * Search for a tunnel by global ID using full PeerIdentities.
830 * @param oid owner of the tunnel.
831 * @param tid global tunnel number.
833 * @return tunnel handler, NULL if doesn't exist.
835 static struct MeshTunnel *
836 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
840 * Change the tunnel state.
842 * @param t Tunnel whose ttate to change.
843 * @param state New state.
846 tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state);
850 * Notify a tunnel that a connection has broken that affects at least
853 * @param t Tunnel affected.
854 * @param p1 Peer that got disconnected from p2.
855 * @param p2 Peer that got disconnected from p1.
857 * @return Short ID of the peer disconnected (either p1 or p2).
858 * 0 if the tunnel remained unaffected.
860 static GNUNET_PEER_Id
861 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
866 * Send FWD keepalive packets for a tunnel.
868 * @param cls Closure (tunnel for which to send the keepalive).
869 * @param tc Notification context.
872 tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
875 * Send BCK keepalive packets for a tunnel.
877 * @param cls Closure (tunnel for which to send the keepalive).
878 * @param tc Notification context.
881 tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
884 * @brief Use the given path for the tunnel.
885 * Update the next and prev hops (and RCs).
886 * (Re)start the path refresh in case the tunnel is locally owned.
888 * @param t Tunnel to update.
889 * @param p Path to use.
892 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
895 * Tunnel is empty: destroy it.
897 * Notifies all participants (peers, cleints) about the destruction.
899 * @param t Tunnel to destroy.
902 tunnel_destroy_empty (struct MeshTunnel *t);
905 * Destroy the tunnel.
907 * This function does not generate any warning traffic to clients or peers.
910 * Remove the tunnel from peer_info's and clients' hashmaps.
911 * Cancel messages belonging to this tunnel queued to neighbors.
912 * Free any allocated resources linked to the tunnel.
914 * @param t the tunnel to destroy
916 * @return GNUNET_OK on success
919 tunnel_destroy (struct MeshTunnel *t);
922 * @brief Queue and pass message to core when possible.
924 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
925 * and accounts for it. In case the queue is full, the message is dropped and
928 * Otherwise, message is treated as internal and allowed to go regardless of
931 * @param cls Closure (@c type dependant). It will be used by queue_send to
932 * build the message to be sent if not already prebuilt.
933 * @param type Type of the message, 0 for a raw message.
934 * @param size Size of the message.
935 * @param dst Neighbor to send message to.
936 * @param t Tunnel this message belongs to.
939 queue_add (void *cls, uint16_t type, size_t size,
940 struct MeshPeerInfo *dst, struct MeshTunnel *t);
944 * Free a transmission that was already queued with all resources
945 * associated to the request.
947 * @param queue Queue handler to cancel.
948 * @param clear_cls Is it necessary to free associated cls?
951 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
955 * @brief Get the next transmittable message from the queue.
957 * This will be the head, except in the case of being a data packet
958 * not allowed by the destination peer.
960 * @param peer Destination peer.
962 * @return The next viable MeshPeerQueue element to send to that peer.
963 * NULL when there are no transmittable messages.
965 struct MeshPeerQueue *
966 queue_get_next (const struct MeshPeerInfo *peer);
970 * Core callback to write a queued packet to core buffer
972 * @param cls Closure (peer info).
973 * @param size Number of bytes available in buf.
974 * @param buf Where the to write the message.
976 * @return number of bytes written to buf
979 queue_send (void *cls, size_t size, void *buf);
983 * Dummy function to separate declarations from definitions in function list.
986 __mesh_divider______________________________________________________________();
990 GNUNET_MESH_DEBUG_S2S (enum MeshTunnelState s)
992 static char buf[128];
997 * Uninitialized status, should never appear in operation.
999 case MESH_TUNNEL_NEW: return "MESH_TUNNEL_NEW";
1002 * Path to the peer not known yet
1004 case MESH_TUNNEL_SEARCHING: return "MESH_TUNNEL_SEARCHING";
1007 * Request sent, not yet answered.
1009 case MESH_TUNNEL_WAITING: return "MESH_TUNNEL_WAITING";
1012 * Peer connected and ready to accept data
1014 case MESH_TUNNEL_READY: return "MESH_TUNNEL_READY";
1017 * Peer connected previosly but not responding
1019 case MESH_TUNNEL_RECONNECTING: return "MESH_TUNNEL_RECONNECTING";
1022 sprintf (buf, "%u (UNKNOWN STATE)", s);
1028 /******************************************************************************/
1029 /************************ PERIODIC FUNCTIONS ****************************/
1030 /******************************************************************************/
1033 * Periodically announce self id in the DHT
1035 * @param cls closure
1036 * @param tc task context
1039 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1041 struct PBlock block;
1043 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1045 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1049 * - Set data expiration in function of X
1050 * - Adapt X to churn
1052 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1054 block.id = my_full_id;
1055 GNUNET_DHT_put (dht_handle, /* DHT handle */
1056 &my_full_id.hashPubKey, /* Key to use */
1057 dht_replication_level, /* Replication level */
1058 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1059 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1060 sizeof (block), /* Size of the data */
1061 (const char *) &block, /* Data itself */
1062 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1063 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1064 NULL, /* Continuation */
1065 NULL); /* Continuation closure */
1067 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1071 /******************************************************************************/
1072 /****************** GENERAL HELPER FUNCTIONS ************************/
1073 /******************************************************************************/
1077 * Check if client has registered with the service and has not disconnected
1079 * @param client the client to check
1081 * @return non-NULL if client exists in the global DLL
1083 static struct MeshClient *
1084 client_get (struct GNUNET_SERVER_Client *client)
1086 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1091 * Deletes a tunnel from a client (either owner or destination). To be used on
1094 * @param c Client whose tunnel to delete.
1095 * @param t Tunnel which should be deleted.
1098 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1102 GNUNET_assert (GNUNET_YES ==
1103 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1109 GNUNET_assert (GNUNET_YES ==
1110 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1118 * Notify the appropriate client that a new incoming tunnel was created.
1120 * @param t Tunnel that was created.
1123 send_local_tunnel_create (struct MeshTunnel *t)
1125 struct GNUNET_MESH_TunnelMessage msg;
1127 if (NULL == t->client)
1129 msg.header.size = htons (sizeof (msg));
1130 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1131 msg.tunnel_id = htonl (t->local_tid_dest);
1132 msg.port = htonl (t->port);
1134 msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1135 msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1136 msg.opt = htonl (msg.opt);
1137 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1138 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1139 &msg.header, GNUNET_NO);
1144 * Notify a client that the incoming tunnel is no longer valid.
1146 * @param t Tunnel that is destroyed.
1147 * @param fwd Forward notification (owner->dest)?
1150 send_local_tunnel_destroy (struct MeshTunnel *t, int fwd)
1152 struct GNUNET_MESH_TunnelMessage msg;
1153 struct MeshClient *c;
1155 c = fwd ? t->client : t->owner;
1161 msg.header.size = htons (sizeof (msg));
1162 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1163 msg.tunnel_id = htonl (fwd ? t->local_tid_dest : t->local_tid);
1164 msg.port = htonl (0);
1165 memset (&msg.peer, 0, sizeof (msg.peer));
1166 msg.opt = htonl (0);
1167 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1168 &msg.header, GNUNET_NO);
1173 * Build a local ACK message and send it to a local client.
1175 * @param t Tunnel on which to send the ACK.
1176 * @param c Client to whom send the ACK.
1177 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1180 send_local_ack (struct MeshTunnel *t,
1181 struct MeshClient *c,
1184 struct GNUNET_MESH_LocalAck msg;
1186 msg.header.size = htons (sizeof (msg));
1187 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1188 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
1189 GNUNET_SERVER_notification_context_unicast (nc,
1198 * Sends an already built message to a peer, properly registrating
1199 * all used resources.
1201 * @param message Message to send. Function makes a copy of it.
1202 * @param peer Short ID of the neighbor whom to send the message.
1203 * @param t Tunnel on which this message is transmitted.
1206 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1207 GNUNET_PEER_Id peer,
1208 struct MeshTunnel *t)
1210 struct GNUNET_PeerIdentity id;
1211 struct MeshPeerInfo *neighbor;
1212 struct MeshPeerPath *p;
1217 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1222 size = ntohs (message->size);
1223 data = GNUNET_malloc (size);
1224 memcpy (data, message, size);
1225 type = ntohs(message->type);
1226 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1227 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1229 struct GNUNET_MESH_Data *u;
1231 u = (struct GNUNET_MESH_Data *) data;
1232 u->ttl = htonl (ntohl (u->ttl) - 1);
1234 GNUNET_PEER_resolve (peer, &id);
1235 neighbor = peer_get (&id);
1236 for (p = neighbor->path_head; NULL != p; p = p->next)
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 " %s IS NOT DIRECTLY CONNECTED\n",
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252 for (p = neighbor->path_head; NULL != p; p = p->next)
1254 struct GNUNET_PeerIdentity debug_id;
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 " path with %u hops through:\n",
1260 for (i = 0; i < p->length; i++)
1262 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 i, GNUNET_i2s(&debug_id));
1270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1271 " no direct connection to %s\n",
1276 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1287 * Sends a CREATE PATH message for a path to a peer, properly registrating
1288 * all used resources.
1290 * @param t Tunnel for which the path is created.
1293 send_path_create (struct MeshTunnel *t)
1295 struct MeshPeerInfo *neighbor;
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1298 neighbor = peer_get_short (t->next_hop);
1300 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1301 sizeof (struct GNUNET_MESH_CreateTunnel) +
1302 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1305 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1310 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1312 * @param t Tunnel which to confirm.
1315 send_path_ack (struct MeshTunnel *t)
1317 struct MeshPeerInfo *neighbor;
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send path ack\n");
1320 neighbor = peer_get_short (t->prev_hop);
1322 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1323 sizeof (struct GNUNET_MESH_PathACK),
1326 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1331 * Build an ACK message and queue it to send to the given peer.
1333 * @param t Tunnel on which to send the ACK.
1334 * @param peer Peer to whom send the ACK.
1335 * @param ack Value of the ACK.
1338 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1340 struct GNUNET_MESH_ACK msg;
1342 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1343 msg.header.size = htons (sizeof (msg));
1344 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1345 msg.pid = htonl (ack);
1346 msg.tid = htonl (t->id.tid);
1348 send_prebuilt_message (&msg.header, peer, t);
1353 * Core callback to write a pre-constructed data packet to core buffer
1355 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1356 * @param size Number of bytes available in buf.
1357 * @param buf Where the to write the message.
1359 * @return number of bytes written to buf
1362 send_core_data_raw (void *cls, size_t size, void *buf)
1364 struct GNUNET_MessageHeader *msg = cls;
1367 GNUNET_assert (NULL != msg);
1368 total_size = ntohs (msg->size);
1370 if (total_size > size)
1375 memcpy (buf, msg, total_size);
1382 * Function to send a create path packet to a peer.
1384 * @param cls closure
1385 * @param size number of bytes available in buf
1386 * @param buf where the callee should write the message
1387 * @return number of bytes written to buf
1390 send_core_path_create (void *cls, size_t size, void *buf)
1392 struct MeshTunnel *t = cls;
1393 struct GNUNET_MESH_CreateTunnel *msg;
1394 struct GNUNET_PeerIdentity *peer_ptr;
1395 struct MeshPeerPath *p = t->path;
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
1402 sizeof (struct GNUNET_MESH_CreateTunnel) +
1403 p->length * sizeof (struct GNUNET_PeerIdentity);
1405 if (size < size_needed || NULL == buf)
1410 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
1411 msg->header.size = htons (size_needed);
1412 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1413 msg->tid = ntohl (t->id.tid);
1416 if (GNUNET_YES == t->nobuffer)
1417 opt |= GNUNET_MESH_OPTION_NOBUFFER;
1418 if (GNUNET_YES == t->reliable)
1419 opt |= GNUNET_MESH_OPTION_RELIABLE;
1420 msg->opt = htonl (opt);
1421 msg->port = htonl (t->port);
1423 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1424 for (i = 0; i < p->length; i++)
1426 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1430 "CREATE PATH (%u bytes long) sent!\n", size_needed);
1436 * Creates a path ack message in buf and frees all unused resources.
1438 * @param cls closure (MeshTransmissionDescriptor)
1439 * @param size number of bytes available in buf
1440 * @param buf where the callee should write the message
1441 * @return number of bytes written to buf
1444 send_core_path_ack (void *cls, size_t size, void *buf)
1446 struct MeshTunnel *t = cls;
1447 struct GNUNET_MESH_PathACK *msg = buf;
1449 GNUNET_assert (NULL != t);
1450 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1455 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
1456 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1457 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1458 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
1459 msg->tid = htonl (t->id.tid);
1460 msg->peer_id = my_full_id;
1461 msg->ack = htonl (t->prev_fc.last_ack_sent);
1463 /* TODO add signature */
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
1466 return sizeof (struct GNUNET_MESH_PathACK);
1471 * Iterator over all the peers to remove the oldest not-used entry.
1473 * @param cls Closure (unsued).
1474 * @param key ID of the peer.
1475 * @param value Peer_Info of the peer.
1480 peer_info_timeout (void *cls,
1481 const struct GNUNET_HashCode *key,
1488 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1489 * and insert it in the appropriate structures if the peer is not known yet.
1491 * @param peer Full identity of the peer.
1493 * @return Existing or newly created peer info.
1495 static struct MeshPeerInfo *
1496 peer_get (const struct GNUNET_PeerIdentity *peer)
1498 struct MeshPeerInfo *peer_info;
1500 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1501 if (NULL == peer_info)
1504 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1505 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1507 GNUNET_CONTAINER_multihashmap_iterate (peers,
1511 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1512 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1513 peer_info->id = GNUNET_PEER_intern (peer);
1515 peer_info->last_contact = GNUNET_TIME_absolute_get();
1522 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1523 * and insert it in the appropriate structures if the peer is not known yet.
1525 * @param peer Short identity of the peer.
1527 * @return Existing or newly created peer info.
1529 static struct MeshPeerInfo *
1530 peer_get_short (const GNUNET_PEER_Id peer)
1532 struct GNUNET_PeerIdentity id;
1534 GNUNET_PEER_resolve (peer, &id);
1535 return peer_get (&id);
1540 * Select which PID to POLL for, to compensate for lost messages.
1542 * @param pi Peer we want to poll.
1543 * @param t Tunnel about which we want to poll.
1545 * @return PID to use, either last sent or first_in_queue - 1
1548 peer_get_first_payload_pid (struct MeshPeerInfo *pi, struct MeshTunnel *t)
1550 struct MeshPeerQueue *q;
1553 type = pi->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST :
1554 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN;
1556 for (q = pi->queue_head; NULL != q; q = q->next)
1558 if (q->type == type && q->tunnel == t)
1560 struct GNUNET_MESH_Data *msg = q->cls;
1562 /* Pretend that the last one sent was the previous to this */
1563 return ntohl (msg->pid) - 1;
1567 /* No data in queue, use last sent */
1569 struct MeshFlowControl *fc;
1571 fc = pi->id == t->next_hop ? &t->next_fc : &t->prev_fc;
1572 return fc->last_pid_sent;
1578 * Choose the best path towards a peer considering the tunnel properties.
1580 * @param peer The destination peer.
1581 * @param t The tunnel the path is for.
1583 * @return Best current known path towards the peer, if any.
1585 static struct MeshPeerPath *
1586 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1588 struct MeshPeerPath *best_p;
1589 struct MeshPeerPath *p;
1590 unsigned int best_cost;
1593 best_p = p = peer->path_head;
1594 best_cost = cost = p->length;
1597 if ((cost = p->length) < best_cost)
1609 * Try to establish a new connection to this peer in the given tunnel.
1610 * If the peer doesn't have any path to it yet, try to get one.
1611 * If the peer already has some path, send a CREATE PATH towards it.
1613 * @param peer PeerInfo of the peer.
1614 * @param t Tunnel for which to create the path, if possible.
1617 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1619 struct MeshPeerPath *p;
1621 if (NULL != peer->path_head)
1623 p = peer_get_best_path (peer, t);
1624 tunnel_use_path (t, p);
1625 send_path_create (t);
1627 else if (NULL == peer->dhtget)
1629 struct GNUNET_PeerIdentity id;
1631 GNUNET_PEER_resolve (peer->id, &id);
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1633 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1634 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1635 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1636 &id.hashPubKey, /* key to search */
1637 dht_replication_level, /* replication level */
1638 GNUNET_DHT_RO_RECORD_ROUTE |
1639 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1641 0, /* xquery bits */
1642 &dht_get_id_handler, peer);
1643 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1648 "There is no path but the DHT GET is already started.\n");
1654 * @brief Re-initiate traffic to this peer if necessary.
1656 * Check if there is traffic queued towards this peer
1657 * and the core transmit handle is NULL (traffic was stalled).
1658 * If so, call core tmt rdy.
1660 * @param peer_id Short ID of peer to which initiate traffic.
1663 peer_unlock_queue (GNUNET_PEER_Id peer_id)
1665 struct MeshPeerInfo *peer;
1666 struct MeshPeerQueue *q;
1669 peer = peer_get_short (peer_id);
1670 if (NULL != peer->core_transmit)
1673 q = queue_get_next (peer);
1678 peer->core_transmit =
1679 GNUNET_CORE_notify_transmit_ready (core_handle,
1682 GNUNET_TIME_UNIT_FOREVER_REL,
1683 GNUNET_PEER_resolve2 (peer->id),
1691 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
1693 * @param t Tunnel which to cancel.
1694 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
1697 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
1699 struct MeshPeerInfo *peer_info;
1700 struct MeshPeerQueue *pq;
1701 struct MeshPeerQueue *next;
1704 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
1705 peer_info = peer_get_short (neighbor);
1706 for (pq = peer_info->queue_head; NULL != pq; pq = next)
1709 if (pq->tunnel == t)
1711 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
1712 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
1714 /* Should have been removed on destroy children */
1717 queue_destroy (pq, GNUNET_YES);
1720 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
1722 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
1723 peer_info->core_transmit = NULL;
1729 * Destroy the peer_info and free any allocated resources linked to it
1731 * @param pi The peer_info to destroy.
1733 * @return GNUNET_OK on success
1736 peer_info_destroy (struct MeshPeerInfo *pi)
1738 struct GNUNET_PeerIdentity id;
1739 struct MeshPeerPath *p;
1740 struct MeshPeerPath *nextp;
1743 GNUNET_PEER_resolve (pi->id, &id);
1744 GNUNET_PEER_change_rc (pi->id, -1);
1747 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1750 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1751 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1753 if (NULL != pi->dhtget)
1755 GNUNET_DHT_get_stop (pi->dhtget);
1761 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1765 for (i = 0; i < pi->ntunnels; i++)
1766 tunnel_destroy_empty (pi->tunnels[i]);
1767 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1774 * Remove all paths that rely on a direct connection between p1 and p2
1775 * from the peer itself and notify all tunnels about it.
1777 * @param peer PeerInfo of affected peer.
1778 * @param p1 GNUNET_PEER_Id of one peer.
1779 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1782 * TODO: optimize (see below)
1785 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1788 struct MeshPeerPath *p;
1789 struct MeshPeerPath *next;
1790 struct MeshPeerInfo *peer_d;
1792 unsigned int destroyed;
1795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1797 for (p = peer->path_head; NULL != p; p = next)
1800 for (i = 0; i < (p->length - 1); i++)
1802 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1803 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1805 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1815 for (i = 0; i < peer->ntunnels; i++)
1817 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1821 peer_d = peer_get_short (d);
1822 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1823 tunnel_use_path (peer->tunnels[i], next);
1824 peer_connect (peer_d, peer->tunnels[i]);
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1831 * Add the path to the peer and update the path used to reach it in case this
1834 * @param peer_info Destination peer to add the path to.
1835 * @param path New path to add. Last peer must be the peer in arg 1.
1836 * Path will be either used of freed if already known.
1837 * @param trusted Do we trust that this path is real?
1840 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1843 struct MeshPeerPath *aux;
1847 if ((NULL == peer_info) || (NULL == path))
1850 path_destroy (path);
1853 if (path->peers[path->length - 1] != peer_info->id)
1856 path_destroy (path);
1859 if (2 >= path->length && GNUNET_NO == trusted)
1861 /* Only allow CORE to tell us about direct paths */
1862 path_destroy (path);
1865 for (l = 1; l < path->length; l++)
1867 if (path->peers[l] == myid)
1869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1870 for (l2 = 0; l2 < path->length - l; l2++)
1872 path->peers[l2] = path->peers[l + l2];
1877 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1882 struct GNUNET_PeerIdentity id;
1884 GNUNET_PEER_resolve (peer_info->id, &id);
1885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1886 path->length, GNUNET_i2s (&id));
1889 l = path_get_length (path);
1892 path_destroy (path);
1896 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1897 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1899 l2 = path_get_length (aux);
1902 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1903 peer_info->path_tail, aux, path);
1908 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1910 path_destroy (path);
1915 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1922 * Add the path to the origin peer and update the path used to reach it in case
1923 * this is the shortest.
1924 * The path is given in peer_info -> destination, therefore we turn the path
1925 * upside down first.
1927 * @param peer_info Peer to add the path to, being the origin of the path.
1928 * @param path New path to add after being inversed.
1929 * Path will be either used or freed.
1930 * @param trusted Do we trust that this path is real?
1933 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1934 struct MeshPeerPath *path, int trusted)
1937 peer_info_add_path (peer_info, path, trusted);
1942 * Add a tunnel to the list of tunnels a peer participates in.
1943 * Update the tunnel's destination.
1945 * @param p Peer to add to.
1946 * @param t Tunnel to add.
1949 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1953 GNUNET_break (t->dest == p->id);
1957 GNUNET_PEER_change_rc (t->dest, 1);
1958 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1963 * Remove a tunnel from the list of tunnels a peer participates in.
1964 * Free the tunnel's destination.
1966 * @param p Peer to clean.
1967 * @param t Tunnel to remove.
1970 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1974 if (t->dest == p->id)
1976 GNUNET_PEER_change_rc (t->dest, -1);
1979 for (i = 0; i < p->ntunnels; i++)
1981 if (p->tunnels[i] == t)
1983 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1984 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1992 * Function called if the connection to the peer has been stalled for a while,
1993 * possibly due to a missed ACK. Poll the peer about its ACK status.
1995 * @param cls Closure (poll ctx).
1996 * @param tc TaskContext.
1999 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2001 struct MeshFlowControl *fc = cls;
2002 struct GNUNET_MESH_Poll msg;
2003 struct MeshTunnel *t = fc->t;
2004 GNUNET_PEER_Id peer;
2006 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2007 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2014 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2016 if (fc == &t->prev_fc)
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
2021 else if (fc == &t->next_fc)
2023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** peer: %s!\n",
2032 GNUNET_i2s(GNUNET_PEER_resolve2 (peer)));
2035 if (GNUNET_YES == t->destroy)
2042 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2043 msg.header.size = htons (sizeof (msg));
2044 msg.tid = htonl (t->id.tid);
2045 msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t));
2046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", ntohl (msg.pid));
2047 send_prebuilt_message (&msg.header, peer, t);
2048 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2049 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2055 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2056 * to obtain a local peer -> destination path and interning the peer ids.
2058 * @return Newly allocated and created path
2060 static struct MeshPeerPath *
2061 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2062 unsigned int get_path_length,
2063 const struct GNUNET_PeerIdentity *put_path,
2064 unsigned int put_path_length)
2066 struct MeshPeerPath *p;
2072 GNUNET_PEER_change_rc (myid, 1);
2073 i = get_path_length;
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2075 for (i--; i >= 0; i--)
2077 id = GNUNET_PEER_intern (&get_path[i]);
2078 if (p->length > 0 && id == p->peers[p->length - 1])
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2081 GNUNET_PEER_change_rc (id, -1);
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2086 GNUNET_i2s (&get_path[i]));
2088 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2089 p->peers[p->length - 1] = id;
2092 i = put_path_length;
2093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2094 for (i--; i >= 0; i--)
2096 id = GNUNET_PEER_intern (&put_path[i]);
2099 /* PUT path went through us, so discard the path up until now and start
2100 * from here to get a much shorter (and loop-free) path.
2105 if (p->length > 0 && id == p->peers[p->length - 1])
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2108 GNUNET_PEER_change_rc (id, -1);
2112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2113 GNUNET_i2s (&put_path[i]));
2115 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2116 p->peers[p->length - 1] = id;
2120 if (get_path_length > 0)
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2122 GNUNET_i2s (&get_path[0]));
2123 if (put_path_length > 0)
2124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2125 GNUNET_i2s (&put_path[0]));
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2128 for (i = 0; i < p->length; i++)
2130 struct GNUNET_PeerIdentity peer_id;
2132 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2134 GNUNET_i2s (&peer_id));
2142 * Adds a path to the peer_infos of all the peers in the path
2144 * @param p Path to process.
2145 * @param confirmed Whether we know if the path works or not.
2148 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2152 /* TODO: invert and add */
2153 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2154 for (i++; i < p->length; i++)
2156 struct MeshPeerInfo *aux;
2157 struct MeshPeerPath *copy;
2159 aux = peer_get_short (p->peers[i]);
2160 copy = path_duplicate (p);
2161 copy->length = i + 1;
2162 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2168 * Search for a tunnel among the incoming tunnels
2170 * @param tid the local id of the tunnel
2172 * @return tunnel handler, NULL if doesn't exist
2174 static struct MeshTunnel *
2175 tunnel_get_incoming (MESH_TunnelNumber tid)
2177 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2178 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
2183 * Search for a tunnel among the tunnels for a client
2185 * @param c the client whose tunnels to search in
2186 * @param tid the local id of the tunnel
2188 * @return tunnel handler, NULL if doesn't exist
2190 static struct MeshTunnel *
2191 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2193 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2195 GNUNET_break_op (0);
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TID %X not a local tid\n", tid);
2199 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2201 return tunnel_get_incoming (tid);
2205 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
2211 * Search for a tunnel by global ID using PEER_ID
2213 * @param pi owner of the tunnel
2214 * @param tid global tunnel number
2216 * @return tunnel handler, NULL if doesn't exist
2218 static struct MeshTunnel *
2219 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2221 struct MESH_TunnelID id;
2222 struct GNUNET_HashCode hash;
2227 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2228 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2233 * Search for a tunnel by global ID using full PeerIdentities
2235 * @param oid owner of the tunnel
2236 * @param tid global tunnel number
2238 * @return tunnel handler, NULL if doesn't exist
2240 static struct MeshTunnel *
2241 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2243 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2248 * Change the tunnel state.
2250 * @param t Tunnel whose ttate to change.
2251 * @param state New state.
2254 tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state)
2256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2257 "Tunnel %s[%X] state was %s\n",
2258 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid,
2259 GNUNET_MESH_DEBUG_S2S (t->state));
2260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2261 "Tunnel %s[%X] state is now %s\n",
2262 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid,
2263 GNUNET_MESH_DEBUG_S2S (state));
2270 * Add a client to a tunnel, initializing all needed data structures.
2272 * @param t Tunnel to which add the client.
2273 * @param c Client which to add to the tunnel.
2276 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2278 if (NULL != t->client)
2284 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
2285 t->local_tid_dest, t,
2286 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2292 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
2293 t->local_tid_dest, t,
2294 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2304 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
2306 unsigned int own_pos;
2308 for (own_pos = 0; own_pos < p->length; own_pos++)
2310 if (p->peers[own_pos] == myid)
2313 if (own_pos > p->length - 1)
2319 if (own_pos < p->length - 1)
2320 t->next_hop = p->peers[own_pos + 1];
2322 t->next_hop = p->peers[own_pos];
2323 GNUNET_PEER_change_rc (t->next_hop, 1);
2325 t->prev_hop = p->peers[own_pos - 1];
2327 t->prev_hop = p->peers[0];
2328 GNUNET_PEER_change_rc (t->prev_hop, 1);
2330 if (NULL != t->path)
2331 path_destroy (t->path);
2332 t->path = path_duplicate (p);
2335 if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
2336 GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
2337 t->fwd_maintenance_task =
2338 GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2339 &tunnel_fwd_keepalive, t);
2345 * Notifies a tunnel that a connection has broken that affects at least
2346 * some of its peers. Sends a notification towards the root of the tree.
2347 * In case the peer is the owner of the tree, notifies the client that owns
2348 * the tunnel and tries to reconnect.
2350 * @param t Tunnel affected.
2351 * @param p1 Peer that got disconnected from p2.
2352 * @param p2 Peer that got disconnected from p1.
2354 * @return Short ID of the peer disconnected (either p1 or p2).
2355 * 0 if the tunnel remained unaffected.
2357 static GNUNET_PEER_Id
2358 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2361 // if (myid != p1 && myid != p2) FIXME
2366 // if (tree_get_predecessor (t->tree) != 0)
2368 // /* We are the peer still connected, notify owner of the disconnection. */
2369 // struct GNUNET_MESH_PathBroken msg;
2370 // struct GNUNET_PeerIdentity neighbor;
2372 // msg.header.size = htons (sizeof (msg));
2373 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2374 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2375 // msg.tid = htonl (t->id.tid);
2376 // msg.peer1 = my_full_id;
2377 // GNUNET_PEER_resolve (pid, &msg.peer2);
2378 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2379 // send_prebuilt_message (&msg.header, &neighbor, t);
2386 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2388 * @param t Tunnel this is about.
2389 * @param fwd Is for FWD traffic? (ACK dest->owner)
2392 tunnel_send_data_ack (struct MeshTunnel *t, int fwd)
2394 struct GNUNET_MESH_DataACK msg;
2395 struct MeshTunnelReliability *rel;
2396 struct MeshReliableMessage *copy;
2401 rel = fwd ? t->bck_rel : t->fwd_rel;
2402 hop = fwd ? t->prev_hop : t->next_hop;
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 "send_data_ack for %u\n",
2407 if (GNUNET_NO == t->reliable)
2409 GNUNET_break_op (0);
2412 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2413 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2414 msg.header.size = htons (sizeof (msg));
2415 msg.tid = htonl (t->id.tid);
2416 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2417 msg.mid = htonl (rel->mid_recv - 1);
2419 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2421 delta = copy->mid - rel->mid_recv;
2424 mask = 0x1LL << delta;
2425 msg.futures |= mask;
2426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2427 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2428 copy->mid, delta, mask, msg.futures);
2430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2432 send_prebuilt_message (&msg.header, hop, t);
2433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2438 * Send an ACK informing the predecessor about the available buffer space.
2439 * In case there is no predecessor, inform the owning client.
2440 * If buffering is off, send only on behalf of children or self if endpoint.
2441 * If buffering is on, send when sent to children and buffer space is free.
2442 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2443 * the ACK itself goes "back" (towards root).
2445 * @param t Tunnel on which to send the ACK.
2446 * @param type Type of message that triggered the ACK transmission.
2447 * @param fwd Is this FWD ACK? (Going dest->owner)
2450 tunnel_send_ack (struct MeshTunnel *t, uint16_t type, int fwd)
2452 struct MeshTunnelReliability *rel;
2453 struct MeshFlowControl *next_fc;
2454 struct MeshFlowControl *prev_fc;
2455 struct MeshClient *c;
2456 struct MeshClient *o;
2462 rel = fwd ? t->fwd_rel : t->bck_rel;
2463 c = fwd ? t->client : t->owner;
2464 o = fwd ? t->owner : t->client;
2465 next_fc = fwd ? &t->next_fc : &t->prev_fc;
2466 prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2467 hop = fwd ? t->prev_hop : t->next_hop;
2471 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2472 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2475 GNUNET_MESH_DEBUG_M2S (type));
2476 if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c))
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2481 if (GNUNET_YES == t->reliable && NULL != c)
2482 tunnel_send_data_ack (t, fwd);
2484 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2485 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2486 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2487 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2489 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2490 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2491 t->force_ack = GNUNET_YES;
2497 /* Check if we need to transmit the ACK */
2499 prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 &&
2500 GNUNET_NO == t->force_ack)
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2504 " last pid recv: %u, last ack sent: %u\n",
2505 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2509 /* Ok, ACK might be necessary, what PID to ACK? */
2510 delta = t->queue_max - next_fc->queue_n;
2511 if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2512 delta_mid = rel->mid_sent - rel->head_sent->mid;
2515 if (0 > delta || (GNUNET_YES == t->reliable &&
2517 (10 < rel->n_sent || 64 <= delta_mid)))
2519 if (NULL != o && delta > 1)
2521 ack = prev_fc->last_pid_recv + delta;
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2524 " last pid %u, last ack %u, qmax %u, q %u\n",
2525 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2526 t->queue_max, next_fc->queue_n);
2527 if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2533 prev_fc->last_ack_sent = ack;
2535 send_local_ack (t, o, fwd);
2537 send_ack (t, hop, ack);
2539 GNUNET_break (GNUNET_YES == t->destroy);
2540 t->force_ack = GNUNET_NO;
2545 * Modify the mesh message TID from global to local and send to client.
2547 * @param t Tunnel on which to send the message.
2548 * @param msg Message to modify and send.
2549 * @param c Client to send to.
2550 * @param tid Tunnel ID to use (c can be both owner and client).
2553 tunnel_send_client_to_tid (struct MeshTunnel *t,
2554 const struct GNUNET_MESH_Data *msg,
2555 struct MeshClient *c, MESH_TunnelNumber tid)
2557 struct GNUNET_MESH_LocalData *copy;
2558 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2559 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2561 if (size < sizeof (struct GNUNET_MessageHeader))
2563 GNUNET_break_op (0);
2571 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2572 memcpy (©[1], &msg[1], size);
2573 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2574 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2575 copy->tid = htonl (tid);
2576 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2577 ©->header, GNUNET_NO);
2581 * Modify the unicast message TID from global to local and send to client.
2583 * @param t Tunnel on which to send the message.
2584 * @param msg Message to modify and send.
2585 * @param fwd Forward?
2588 tunnel_send_client_data (struct MeshTunnel *t,
2589 const struct GNUNET_MESH_Data *msg,
2593 tunnel_send_client_to_tid (t, msg, t->client, t->local_tid_dest);
2595 tunnel_send_client_to_tid (t, msg, t->owner, t->local_tid);
2600 * Send up to 64 buffered messages to the client for in order delivery.
2602 * @param t Tunnel on which to empty the message buffer.
2603 * @param c Client to send to.
2604 * @param rel Reliability structure to corresponding peer.
2605 * If rel == t->bck_rel, this is FWD data.
2608 tunnel_send_client_buffered_data (struct MeshTunnel *t, struct MeshClient *c,
2609 struct MeshTunnelReliability *rel)
2612 struct MeshReliableMessage *copy;
2613 struct MeshReliableMessage *next;
2615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2616 for (copy = rel->head_recv; NULL != copy; copy = next)
2619 if (copy->mid == rel->mid_recv)
2621 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2624 " have %u! now expecting %u\n",
2625 copy->mid, rel->mid_recv + 1);
2626 tunnel_send_client_data (t, msg, (rel == t->bck_rel));
2628 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2634 " don't have %u, next is %u\n",
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2645 * We have received a message out of order, buffer it until we receive
2646 * the missing one and we can feed the rest to the client.
2648 * @param t Tunnel to add to.
2649 * @param msg Message to buffer.
2650 * @param rel Reliability data to the corresponding direction.
2653 tunnel_add_buffered_data (struct MeshTunnel *t,
2654 const struct GNUNET_MESH_Data *msg,
2655 struct MeshTunnelReliability *rel)
2657 struct MeshReliableMessage *copy;
2658 struct MeshReliableMessage *prev;
2662 size = ntohs (msg->header.size);
2663 mid = ntohl (msg->mid);
2664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
2666 copy = GNUNET_malloc (sizeof (*copy) + size);
2669 memcpy (©[1], msg, size);
2671 // FIXME do something better than O(n), although n < 64...
2672 // FIXME start from the end (most messages are the latest ones)
2673 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
2676 if (GMC_is_pid_bigger (prev->mid, mid))
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2679 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2685 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
2691 * Destroy a reliable message after it has been acknowledged, either by
2692 * direct mid ACK or bitfield. Updates the appropriate data structures and
2693 * timers and frees all memory.
2695 * @param copy Message that is no longer needed: remote peer got it.
2698 tunnel_free_reliable_message (struct MeshReliableMessage *copy)
2700 struct MeshTunnelReliability *rel;
2701 struct GNUNET_TIME_Relative time;
2704 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2705 rel->expected_delay.rel_value *= 7;
2706 rel->expected_delay.rel_value += time.rel_value;
2707 rel->expected_delay.rel_value /= 8;
2709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
2710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
2712 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
2714 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
2716 rel->retry_timer = rel->expected_delay;
2717 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2723 * Destroy all reliable messages queued for a tunnel,
2724 * during a tunnel destruction.
2725 * Frees the reliability structure itself.
2727 * @param rel Reliability data for a tunnel.
2730 tunnel_free_reliable_all (struct MeshTunnelReliability *rel)
2732 struct MeshReliableMessage *copy;
2733 struct MeshReliableMessage *next;
2738 for (copy = rel->head_recv; NULL != copy; copy = next)
2741 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2744 for (copy = rel->head_sent; NULL != copy; copy = next)
2747 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2750 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
2751 GNUNET_SCHEDULER_cancel (rel->retry_task);
2757 * Mark future messages as ACK'd.
2759 * @param t Tunnel whose sent buffer to clean.
2760 * @param msg DataACK message with a bitfield of future ACK'd messages.
2761 * @param rel Reliability data.
2764 tunnel_free_sent_reliable (struct MeshTunnel *t,
2765 const struct GNUNET_MESH_DataACK *msg,
2766 struct MeshTunnelReliability *rel)
2768 struct MeshReliableMessage *copy;
2769 struct MeshReliableMessage *next;
2776 bitfield = msg->futures;
2777 mid = ntohl (msg->mid);
2778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2779 "free_sent_reliable %u %llX\n",
2781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2782 " rel %p, head %p\n",
2783 rel, rel->head_sent);
2784 for (i = 0, copy = rel->head_sent;
2785 i < 64 && NULL != copy && 0 != bitfield;
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2789 " trying bit %u (mid %u)\n",
2792 if (0 == (bitfield & mask))
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2796 /* Bit was set, clear the bit from the bitfield */
2799 /* The i-th bit was set. Do we have that copy? */
2800 /* Skip copies with mid < target */
2801 target = mid + i + 1;
2802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
2803 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
2806 /* Did we run out of copies? (previously freed, it's ok) */
2809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2813 /* Did we overshoot the target? (previously freed, it's ok) */
2814 if (GMC_is_pid_bigger (copy->mid, target))
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
2820 /* Now copy->mid == target, free it */
2822 tunnel_free_reliable_message (copy);
2825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
2830 * We haven't received an ACK after a certain time: restransmit the message.
2832 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2833 * @param tc TaskContext.
2836 tunnel_retransmit_message (void *cls,
2837 const struct GNUNET_SCHEDULER_TaskContext *tc)
2839 struct MeshTunnelReliability *rel = cls;
2840 struct MeshReliableMessage *copy;
2841 struct MeshFlowControl *fc;
2842 struct MeshPeerQueue *q;
2843 struct MeshPeerInfo *pi;
2844 struct MeshTunnel *t;
2845 struct GNUNET_MESH_Data *payload;
2848 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2849 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2853 copy = rel->head_sent;
2860 /* Search the message to be retransmitted in the outgoing queue */
2861 payload = (struct GNUNET_MESH_Data *) ©[1];
2862 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2863 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2864 pi = peer_get_short (hop);
2865 for (q = pi->queue_head; NULL != q; q = q->next)
2867 if (ntohs (payload->header.type) == q->type)
2869 struct GNUNET_MESH_Data *queued_data = q->cls;
2871 if (queued_data->mid == payload->mid)
2876 /* Message not found in the queue */
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
2881 fc->last_ack_sent++;
2882 fc->last_pid_recv++;
2883 payload->pid = htonl (fc->last_pid_recv);
2884 send_prebuilt_message (&payload->header, hop, t);
2885 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %u\n", copy->mid);
2892 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2893 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2894 &tunnel_retransmit_message,
2900 * Send keepalive packets for a tunnel.
2902 * @param t Tunnel to keep alive..
2903 * @param fwd Is this a FWD keepalive? (owner -> dest).
2906 tunnel_keepalive (struct MeshTunnel *t, int fwd)
2908 struct GNUNET_MESH_TunnelKeepAlive *msg;
2909 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
2914 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
2915 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
2916 hop = fwd ? t->next_hop : t->prev_hop;
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2919 "sending %s keepalive for tunnel %d\n",
2920 fwd ? "FWD" : "BCK", t->id.tid);
2922 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
2923 msg->header.size = htons (size);
2924 msg->header.type = htons (type);
2925 msg->oid = *(GNUNET_PEER_resolve2 (t->id.oid));
2926 msg->tid = htonl (t->id.tid);
2927 send_prebuilt_message (&msg->header, hop, t);
2932 * Send keepalive packets for a tunnel.
2934 * @param cls Closure (tunnel for which to send the keepalive).
2935 * @param tc Notification context.
2938 tunnel_recreate (struct MeshTunnel *t, int fwd)
2940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2941 "sending path recreate for tunnel %s[%X]\n",
2942 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
2944 send_path_create (t);
2951 * Generic tunnel timer management.
2952 * Depending on the role of the peer in the tunnel will send the
2953 * appropriate message (build or keepalive)
2955 * @param t Tunnel to maintain.
2959 tunnel_maintain (struct MeshTunnel *t, int fwd)
2963 case MESH_TUNNEL_NEW:
2965 case MESH_TUNNEL_SEARCHING:
2966 /* TODO DHT GET with RO_BART */
2968 case MESH_TUNNEL_WAITING:
2969 tunnel_recreate (t, fwd);
2971 case MESH_TUNNEL_READY:
2972 tunnel_keepalive (t, fwd);
2981 tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2983 struct MeshTunnel *t = cls;
2985 t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2986 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
2990 tunnel_maintain (t, GNUNET_YES);
2991 t->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2992 &tunnel_fwd_keepalive,
2998 tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3000 struct MeshTunnel *t = cls;
3002 t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3003 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3007 tunnel_keepalive (t, GNUNET_NO);
3008 t->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
3009 &tunnel_bck_keepalive,
3015 * Send a message to all peers and clients in this tunnel that the tunnel
3016 * is no longer valid. If some peer or client should not receive the message,
3017 * should be zero'ed out before calling this function.
3019 * @param t The tunnel whose peers and clients to notify.
3022 tunnel_send_destroy (struct MeshTunnel *t)
3024 struct GNUNET_MESH_TunnelDestroy msg;
3025 struct GNUNET_PeerIdentity id;
3027 msg.header.size = htons (sizeof (msg));
3028 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3029 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3030 msg.tid = htonl (t->id.tid);
3031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3032 " sending tunnel destroy for tunnel: %s [%X]\n",
3033 GNUNET_i2s (&msg.oid), t->id.tid);
3035 if (NULL == t->client && 0 != t->next_hop)
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
3038 GNUNET_PEER_resolve (t->next_hop, &id);
3039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3040 " sending forward to %s\n",
3042 send_prebuilt_message (&msg.header, t->next_hop, t);
3044 if (NULL == t->owner && 0 != t->prev_hop)
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
3047 GNUNET_PEER_resolve (t->prev_hop, &id);
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3049 " sending back to %s\n",
3051 send_prebuilt_message (&msg.header, t->prev_hop, t);
3053 if (NULL != t->owner)
3055 send_local_tunnel_destroy (t, GNUNET_NO);
3057 if (NULL != t->client)
3059 send_local_tunnel_destroy (t, GNUNET_YES);
3064 tunnel_destroy (struct MeshTunnel *t)
3066 struct MeshClient *c;
3067 struct GNUNET_HashCode hash;
3076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3077 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3081 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3082 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3090 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
3102 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
3103 t->local_tid_dest, t))
3109 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
3110 t->local_tid_dest, t))
3117 if(GNUNET_YES == t->reliable)
3119 tunnel_free_reliable_all (t->fwd_rel);
3120 tunnel_free_reliable_all (t->bck_rel);
3122 if (0 != t->prev_hop)
3124 peer_cancel_queues (t->prev_hop, t);
3125 GNUNET_PEER_change_rc (t->prev_hop, -1);
3127 if (0 != t->next_hop)
3129 peer_cancel_queues (t->next_hop, t);
3130 GNUNET_PEER_change_rc (t->next_hop, -1);
3132 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task)
3134 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3135 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3137 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task)
3139 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3140 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3143 peer_info_remove_tunnel (peer_get_short (t->dest), t);
3146 if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
3147 GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
3148 if (GNUNET_SCHEDULER_NO_TASK != t->bck_maintenance_task)
3149 GNUNET_SCHEDULER_cancel (t->bck_maintenance_task);
3152 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3153 path_destroy (t->path);
3159 * Tunnel is empty: destroy it.
3161 * Notifies all participants (peers, cleints) about the destruction.
3163 * @param t Tunnel to destroy.
3166 tunnel_destroy_empty (struct MeshTunnel *t)
3170 struct GNUNET_PeerIdentity id;
3172 GNUNET_PEER_resolve (t->id.oid, &id);
3173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3174 "executing destruction of empty tunnel %s [%X]\n",
3175 GNUNET_i2s (&id), t->id.tid);
3179 if (GNUNET_NO == t->destroy)
3180 tunnel_send_destroy (t);
3181 if (0 == t->pending_messages)
3184 t->destroy = GNUNET_YES;
3188 * Initialize a Flow Control structure to the initial state.
3190 * @param fc Flow Control structure to initialize.
3193 fc_init (struct MeshFlowControl *fc)
3195 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3196 fc->last_pid_recv = (uint32_t) -1;
3197 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
3198 fc->last_ack_recv = (uint32_t) -1;
3199 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3200 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3205 * Create a new tunnel
3207 * @param owner Who is the owner of the tunnel (short ID).
3208 * @param tid Tunnel Number of the tunnel.
3209 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3210 * @param local Tunnel Number for the tunnel, for the client point of view.
3212 * @return A new initialized tunnel. NULL on error.
3214 static struct MeshTunnel *
3215 tunnel_new (GNUNET_PEER_Id owner,
3216 MESH_TunnelNumber tid,
3217 struct MeshClient *client,
3218 MESH_TunnelNumber local)
3220 struct MeshTunnel *t;
3221 struct GNUNET_HashCode hash;
3223 if (n_tunnels >= max_tunnels && NULL == client)
3226 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3229 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
3231 fc_init (&t->next_fc);
3232 fc_init (&t->prev_fc);
3235 t->local_tid = local;
3237 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3239 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3241 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3242 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3249 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3257 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
3259 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3263 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3273 * Set options in a tunnel, extracted from a bit flag field
3275 * @param t Tunnel to set options to.
3276 * @param options Bit array in host byte order.
3279 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
3281 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
3282 GNUNET_YES : GNUNET_NO;
3283 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
3284 GNUNET_YES : GNUNET_NO;
3289 * Iterator for deleting each tunnel whose client endpoint disconnected.
3291 * @param cls Closure (client that has disconnected).
3292 * @param key The local tunnel id (used to access the hashmap).
3293 * @param value The value stored at the key (tunnel to destroy).
3295 * @return GNUNET_OK, keep iterating.
3298 tunnel_destroy_iterator (void *cls,
3302 struct MeshTunnel *t = value;
3303 struct MeshClient *c = cls;
3305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3306 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
3307 t->local_tid, t->local_tid_dest, c->id);
3308 client_delete_tunnel (c, t);
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
3313 if (0 != t->next_hop) { /* destroy could come before a path is used */
3314 GNUNET_PEER_change_rc (t->next_hop, -1);
3320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
3322 if (0 != t->prev_hop) { /* destroy could come before a path is used */
3323 GNUNET_PEER_change_rc (t->prev_hop, -1);
3328 tunnel_destroy_empty (t);
3335 * remove client's ports from the global hashmap on diconnect.
3337 * @param cls Closure (unused).
3339 * @param value ThClient structure.
3341 * @return GNUNET_OK, keep iterating.
3344 client_release_ports (void *cls,
3350 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
3351 if (GNUNET_YES != res)
3354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3355 "Port %u by client %p was not registered.\n",
3362 * Timeout function due to lack of keepalive/traffic from the owner.
3363 * Destroys tunnel if called.
3365 * @param cls Closure (tunnel to destroy).
3366 * @param tc TaskContext
3369 tunnel_fwd_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3371 struct MeshTunnel *t = cls;
3373 t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3374 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3377 "Tunnel %s [%X] FWD timed out. Destroying.\n",
3378 GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3379 if (NULL != t->client)
3380 send_local_tunnel_destroy (t, GNUNET_YES);
3381 tunnel_destroy (t); /* Do not notify other */
3386 * Timeout function due to lack of keepalive/traffic from the destination.
3387 * Destroys tunnel if called.
3389 * @param cls Closure (tunnel to destroy).
3390 * @param tc TaskContext
3393 tunnel_bck_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3395 struct MeshTunnel *t = cls;
3397 t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3398 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3401 "Tunnel %s [%X] BCK timed out. Destroying.\n",
3402 GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3403 if (NULL != t->owner)
3404 send_local_tunnel_destroy (t, GNUNET_NO);
3405 tunnel_destroy (t); /* Do not notify other */
3410 * Resets the tunnel timeout task, some other message has done the task's job.
3411 * - For the first peer on the direction this means to send
3412 * a keepalive or a path confirmation message (either create or ACK).
3413 * - For all other peers, this means to destroy the tunnel,
3414 * due to lack of activity.
3415 * Starts the tiemout if no timeout was running (tunnel just created).
3417 * @param t Tunnel whose timeout to reset.
3418 * @param fwd Is this forward?
3420 * TODO use heap to improve efficiency of scheduler.
3423 tunnel_reset_timeout (struct MeshTunnel *t, int fwd)
3425 GNUNET_SCHEDULER_TaskIdentifier *ti;
3426 GNUNET_SCHEDULER_Task f;
3427 struct MeshClient *c;
3429 ti = fwd ? &t->fwd_maintenance_task : &t->bck_maintenance_task;
3430 c = fwd ? t->owner : t->client;
3432 if (GNUNET_SCHEDULER_NO_TASK != *ti)
3433 GNUNET_SCHEDULER_cancel (*ti);
3437 f = fwd ? &tunnel_fwd_keepalive : &tunnel_bck_keepalive;
3438 *ti = GNUNET_SCHEDULER_add_delayed (refresh_path_time, f, t);
3442 f = fwd ? &tunnel_fwd_timeout : &tunnel_bck_timeout;
3443 *ti = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3444 (refresh_path_time, 4),
3450 /******************************************************************************/
3451 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3452 /******************************************************************************/
3455 * Free a transmission that was already queued with all resources
3456 * associated to the request.
3458 * @param queue Queue handler to cancel.
3459 * @param clear_cls Is it necessary to free associated cls?
3462 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3464 struct MeshFlowControl *fc;
3466 if (GNUNET_YES == clear_cls)
3468 switch (queue->type)
3470 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3471 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3472 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3474 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3475 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3476 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3477 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3478 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3479 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3480 case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
3481 case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3483 " prebuilt message\n");
3484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3486 GNUNET_MESH_DEBUG_M2S (queue->type));
3488 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3494 " type %s unknown!\n",
3495 GNUNET_MESH_DEBUG_M2S (queue->type));
3497 GNUNET_free_non_null (queue->cls);
3499 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3500 queue->peer->queue_tail,
3503 /* Delete from appropriate fc in the tunnel */
3504 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3505 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3507 if (queue->peer->id == queue->tunnel->prev_hop)
3508 fc = &queue->tunnel->prev_fc;
3509 else if (queue->peer->id == queue->tunnel->next_hop)
3510 fc = &queue->tunnel->next_fc;
3514 GNUNET_free (queue);
3519 GNUNET_free (queue);
3524 * @brief Get the next transmittable message from the queue.
3526 * This will be the head, except in the case of being a data packet
3527 * not allowed by the destination peer.
3529 * @param peer Destination peer.
3531 * @return The next viable MeshPeerQueue element to send to that peer.
3532 * NULL when there are no transmittable messages.
3534 struct MeshPeerQueue *
3535 queue_get_next (const struct MeshPeerInfo *peer)
3537 struct MeshPeerQueue *q;
3539 struct GNUNET_MESH_Data *dmsg;
3540 struct MeshTunnel* t;
3544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3545 for (q = peer->queue_head; NULL != q; q = q->next)
3548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3550 GNUNET_MESH_DEBUG_M2S (q->type));
3551 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3552 pid = ntohl (dmsg->pid);
3555 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3556 ack = t->next_fc.last_ack_recv;
3558 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3559 ack = t->prev_fc.last_ack_recv;
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 "* ACK: %u, PID: %u, MID: %u\n",
3568 ack, pid, ntohl (dmsg->mid));
3569 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3582 "* nothing found\n");
3588 queue_send (void *cls, size_t size, void *buf)
3590 struct MeshPeerInfo *peer = cls;
3591 struct GNUNET_MessageHeader *msg;
3592 struct MeshPeerQueue *queue;
3593 struct MeshTunnel *t;
3594 struct GNUNET_PeerIdentity dst_id;
3595 struct MeshFlowControl *fc;
3600 peer->core_transmit = NULL;
3602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3603 queue = queue_get_next (peer);
3605 /* Queue has no internal mesh traffic nor sendable payload */
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3609 if (NULL == peer->queue_head)
3610 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3615 GNUNET_PEER_resolve (peer->id, &dst_id);
3616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3618 GNUNET_i2s (&dst_id));
3619 /* Check if buffer size is enough for the message */
3620 if (queue->size > size)
3622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3623 "* not enough room, reissue\n");
3624 peer->core_transmit =
3625 GNUNET_CORE_notify_transmit_ready (core_handle,
3628 GNUNET_TIME_UNIT_FOREVER_REL,
3635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3638 GNUNET_assert (0 < t->pending_messages);
3639 t->pending_messages--;
3643 switch (queue->type)
3646 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3647 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3648 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3649 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3650 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3651 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3652 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3653 case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
3654 case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
3655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3657 GNUNET_MESH_DEBUG_M2S (queue->type));
3659 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3660 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3661 data_size = send_core_data_raw (queue->cls, size, buf);
3662 msg = (struct GNUNET_MessageHeader *) buf;
3663 type = ntohs (msg->type);
3665 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3667 data_size = send_core_path_create (queue->cls, size, buf);
3669 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3671 data_size = send_core_path_ack (queue->cls, size, buf);
3675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3676 "* type unknown: %u\n",
3681 if (0 < drop_percent &&
3682 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3684 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3685 "Dropping message of type %s\n",
3686 GNUNET_MESH_DEBUG_M2S(queue->type));
3689 /* Free queue, but cls was freed by send_core_* */
3690 queue_destroy (queue, GNUNET_NO);
3692 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3693 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3697 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3698 t->next_fc.last_pid_sent = pid;
3699 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3702 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3704 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3705 t->prev_fc.last_pid_sent = pid;
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3708 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3709 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
3715 /* If more data in queue, send next */
3716 queue = queue_get_next (peer);
3719 struct GNUNET_PeerIdentity id;
3721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3722 GNUNET_PEER_resolve (peer->id, &id);
3723 peer->core_transmit =
3724 GNUNET_CORE_notify_transmit_ready(core_handle,
3727 GNUNET_TIME_UNIT_FOREVER_REL,
3733 if (peer->id == t->next_hop)
3735 else if (peer->id == t->prev_hop)
3740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n",
3741 peer->id, t->next_hop, t->prev_hop);
3744 if (NULL != peer->queue_head)
3746 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
3748 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3749 "* %s starting poll timeout\n",
3750 GNUNET_i2s (&my_full_id));
3751 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3757 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3759 GNUNET_SCHEDULER_cancel (fc->poll_task);
3760 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3763 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3774 * @brief Queue and pass message to core when possible.
3776 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3777 * accounts for it. In case the queue is full, the message is dropped and
3780 * Otherwise, message is treated as internal and allowed to go regardless of
3783 * @param cls Closure (@c type dependant). It will be used by queue_send to
3784 * build the message to be sent if not already prebuilt.
3785 * @param type Type of the message, 0 for a raw message.
3786 * @param size Size of the message.
3787 * @param dst Neighbor to send message to.
3788 * @param t Tunnel this message belongs to.
3791 queue_add (void *cls, uint16_t type, size_t size,
3792 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3794 struct MeshPeerQueue *queue;
3795 struct GNUNET_PeerIdentity id;
3796 struct MeshFlowControl *fc;
3800 priority = GNUNET_NO;
3801 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3805 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3811 if (fc->queue_n >= t->queue_max)
3813 /* If this isn't a retransmission, drop the message */
3814 if (GNUNET_NO == t->reliable ||
3815 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3816 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3818 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3822 "queue full: %u/%u\n",
3823 fc->queue_n, t->queue_max);
3824 return; /* Drop this message */
3826 priority = GNUNET_YES;
3829 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
3830 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3831 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3835 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3841 if (GNUNET_YES == priority)
3843 struct GNUNET_MESH_Data *d;
3847 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3848 d = (struct GNUNET_MESH_Data *) queue->cls;
3850 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3852 if (queue->type != type)
3854 d = (struct GNUNET_MESH_Data *) queue->cls;
3861 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3863 if (NULL == dst->core_transmit)
3865 GNUNET_PEER_resolve (dst->id, &id);
3866 dst->core_transmit =
3867 GNUNET_CORE_notify_transmit_ready (core_handle,
3870 GNUNET_TIME_UNIT_FOREVER_REL,
3876 t->pending_messages++;
3880 /******************************************************************************/
3881 /******************** MESH NETWORK HANDLERS **************************/
3882 /******************************************************************************/
3886 * Core handler for path creation
3888 * @param cls closure
3889 * @param message message
3890 * @param peer peer identity this notification is about
3892 * @return GNUNET_OK to keep the connection open,
3893 * GNUNET_SYSERR to close it (signal serious error)
3896 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3897 const struct GNUNET_MessageHeader *message)
3899 unsigned int own_pos;
3902 MESH_TunnelNumber tid;
3903 struct GNUNET_MESH_CreateTunnel *msg;
3904 struct GNUNET_PeerIdentity *pi;
3905 struct MeshPeerPath *path;
3906 struct MeshPeerInfo *dest_peer_info;
3907 struct MeshPeerInfo *orig_peer_info;
3908 struct MeshTunnel *t;
3910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3911 "Received a path create msg [%s]\n",
3912 GNUNET_i2s (&my_full_id));
3913 size = ntohs (message->size);
3914 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3916 GNUNET_break_op (0);
3920 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3921 if (size % sizeof (struct GNUNET_PeerIdentity))
3923 GNUNET_break_op (0);
3926 size /= sizeof (struct GNUNET_PeerIdentity);
3929 GNUNET_break_op (0);
3932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3933 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3935 tid = ntohl (msg->tid);
3936 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3938 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3939 t = tunnel_get (pi, tid);
3940 if (NULL == t) /* might be a local tunnel */
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3945 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3951 t->port = ntohl (msg->port);
3952 opt = ntohl (msg->opt);
3953 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3955 t->nobuffer = GNUNET_YES;
3958 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3960 t->reliable = GNUNET_YES;
3962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3964 tunnel_reset_timeout (t, GNUNET_YES);
3965 tunnel_change_state (t, MESH_TUNNEL_WAITING);
3967 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3968 if (NULL == dest_peer_info)
3970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3971 " Creating PeerInfo for destination.\n");
3972 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3973 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3974 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3976 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3978 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3979 if (NULL == orig_peer_info)
3981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3982 " Creating PeerInfo for origin.\n");
3983 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3984 orig_peer_info->id = GNUNET_PEER_intern (pi);
3985 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3986 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3989 path = path_new (size);
3991 for (i = 0; i < size; i++)
3993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3994 GNUNET_i2s (&pi[i]));
3995 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3996 if (path->peers[i] == myid)
3999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4000 if (own_pos == 0 && path->peers[own_pos] != myid)
4002 /* create path: self not found in path through self */
4003 GNUNET_break_op (0);
4004 path_destroy (path);
4008 path_add_to_peers (path, GNUNET_NO);
4009 tunnel_use_path (t, path);
4011 peer_info_add_tunnel (dest_peer_info, t);
4013 if (own_pos == size - 1)
4015 struct MeshClient *c;
4017 /* Find target client */
4018 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
4021 /* TODO send reject */
4025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4026 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
4028 /* Assign local tid */
4029 while (NULL != tunnel_get_incoming (next_local_tid))
4030 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4031 t->local_tid_dest = next_local_tid++;
4032 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4034 if (GNUNET_YES == t->reliable)
4036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4037 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4039 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
4042 tunnel_add_client (t, c);
4043 send_local_tunnel_create (t);
4045 /* Eliminate tunnel when origin dies */
4046 tunnel_reset_timeout (t, GNUNET_YES);
4047 /* Keep tunnel alive in direction dest->owner*/
4048 tunnel_reset_timeout (t, GNUNET_NO);
4052 struct MeshPeerPath *path2;
4054 t->next_hop = path->peers[own_pos + 1];
4055 GNUNET_PEER_change_rc(t->next_hop, 1);
4057 /* It's for somebody else! Retransmit. */
4058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4059 path2 = path_duplicate (path);
4060 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4061 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4062 send_path_create (t);
4070 * Core handler for path ACKs
4072 * @param cls closure
4073 * @param message message
4074 * @param peer peer identity this notification is about
4076 * @return GNUNET_OK to keep the connection open,
4077 * GNUNET_SYSERR to close it (signal serious error)
4080 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4081 const struct GNUNET_MessageHeader *message)
4083 struct GNUNET_MESH_PathACK *msg;
4084 struct MeshPeerInfo *peer_info;
4085 struct MeshPeerPath *p;
4086 struct MeshTunnel *t;
4088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4089 GNUNET_i2s (&my_full_id));
4090 msg = (struct GNUNET_MESH_PathACK *) message;
4091 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4094 /* TODO notify that we don't know the tunnel */
4095 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4097 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4101 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4103 peer_info = peer_get (&msg->peer_id);
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4105 GNUNET_i2s (&msg->peer_id));
4106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4109 /* Add path to peers? */
4113 path_add_to_peers (p, GNUNET_YES);
4119 tunnel_change_state (t, MESH_TUNNEL_READY);
4120 tunnel_reset_timeout (t, GNUNET_NO);
4121 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
4122 t->prev_fc.last_ack_sent = ntohl (msg->ack);
4124 /* Message for us? */
4125 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4128 if (NULL == t->owner)
4130 GNUNET_break_op (0);
4133 if (NULL != peer_info->dhtget)
4135 GNUNET_DHT_get_stop (peer_info->dhtget);
4136 peer_info->dhtget = NULL;
4138 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES);
4139 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO);
4143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4144 " not for us, retransmitting...\n");
4145 send_prebuilt_message (message, t->prev_hop, t);
4151 * Core handler for notifications of broken paths
4153 * @param cls closure
4154 * @param message message
4155 * @param peer peer identity this notification is about
4157 * @return GNUNET_OK to keep the connection open,
4158 * GNUNET_SYSERR to close it (signal serious error)
4161 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4162 const struct GNUNET_MessageHeader *message)
4164 struct GNUNET_MESH_PathBroken *msg;
4165 struct MeshTunnel *t;
4167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4168 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4169 msg = (struct GNUNET_MESH_PathBroken *) message;
4170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4171 GNUNET_i2s (&msg->peer1));
4172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4173 GNUNET_i2s (&msg->peer2));
4174 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4177 GNUNET_break_op (0);
4180 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4181 GNUNET_PEER_search (&msg->peer2));
4188 * Core handler for tunnel destruction
4190 * @param cls closure
4191 * @param message message
4192 * @param peer peer identity this notification is about
4194 * @return GNUNET_OK to keep the connection open,
4195 * GNUNET_SYSERR to close it (signal serious error)
4198 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4199 const struct GNUNET_MessageHeader *message)
4201 struct GNUNET_MESH_TunnelDestroy *msg;
4202 struct MeshTunnel *t;
4204 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4206 "Got a TUNNEL DESTROY packet from %s\n",
4208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4209 " for tunnel %s [%u]\n",
4210 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4211 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4214 /* Probably already got the message from another path,
4215 * destroyed the tunnel and retransmitted to children.
4218 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4222 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4225 t->local_tid, t->local_tid_dest);
4227 if (GNUNET_PEER_search (peer) == t->prev_hop)
4229 // TODO check owner's signature
4230 // TODO add owner's signatue to tunnel for retransmission
4231 peer_cancel_queues (t->prev_hop, t);
4232 GNUNET_PEER_change_rc (t->prev_hop, -1);
4235 else if (GNUNET_PEER_search (peer) == t->next_hop)
4237 // TODO check dest's signature
4238 // TODO add dest's signatue to tunnel for retransmission
4239 peer_cancel_queues (t->next_hop, t);
4240 GNUNET_PEER_change_rc (t->next_hop, -1);
4245 GNUNET_break_op (0);
4246 // TODO check both owner AND destination's signature to see which matches
4247 // TODO restransmit in appropriate direction
4250 tunnel_destroy_empty (t);
4252 // TODO: add timeout to destroy the tunnel anyway
4258 * Generic handler for mesh network payload traffic.
4260 * @param peer Peer identity this notification is about.
4261 * @param message Data message.
4262 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4264 * @return GNUNET_OK to keep the connection open,
4265 * GNUNET_SYSERR to close it (signal serious error)
4268 handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
4269 const struct GNUNET_MessageHeader *message,
4272 struct GNUNET_MESH_Data *msg;
4273 struct MeshFlowControl *fc;
4274 struct MeshTunnelReliability *rel;
4275 struct MeshTunnel *t;
4276 struct MeshClient *c;
4284 size = ntohs (message->size);
4286 sizeof (struct GNUNET_MESH_Data) +
4287 sizeof (struct GNUNET_MessageHeader))
4292 type =ntohs (message->type);
4293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
4294 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4295 msg = (struct GNUNET_MESH_Data *) message;
4296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4297 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4299 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4302 /* TODO notify back: we don't know this tunnel */
4303 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4304 GNUNET_break_op (0);
4308 /* Initialize FWD/BCK data */
4309 pid = ntohl (msg->pid);
4310 fc = fwd ? &t->prev_fc : &t->next_fc;
4311 c = fwd ? t->client : t->owner;
4312 rel = fwd ? t->bck_rel : t->fwd_rel;
4313 hop = fwd ? t->next_hop : t->prev_hop;
4314 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
4316 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
4317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4318 "WARNING Received PID %u, (prev %u), ACK %u\n",
4319 pid, fc->last_pid_recv, fc->last_ack_sent);
4323 tunnel_change_state (t, MESH_TUNNEL_READY);
4324 tunnel_reset_timeout (t, fwd);
4327 /* TODO signature verification */
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
4329 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4330 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
4334 mid = ntohl (msg->mid);
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4336 " pid %u (mid %u) not seen yet\n", pid, mid);
4337 fc->last_pid_recv = pid;
4339 if (GNUNET_NO == t->reliable ||
4340 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4341 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4344 "!!! RECV %u\n", ntohl (msg->mid));
4345 if (GNUNET_YES == t->reliable)
4347 /* Is this the exact next expected messasge? */
4348 if (mid == rel->mid_recv)
4350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4352 tunnel_send_client_data (t, msg, fwd);
4353 tunnel_send_client_buffered_data (t, c, rel);
4357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4358 tunnel_add_buffered_data (t, msg, rel);
4361 else /* Tunnel unreliable, send to clients directly */
4363 tunnel_send_client_data (t, msg, fwd);
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4369 " MID %u not expected (%u - %u), dropping!\n",
4370 ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
4375 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4377 " Pid %u not expected (%u+), dropping!\n",
4378 pid, fc->last_pid_recv + 1);
4380 tunnel_send_ack (t, type, fwd);
4383 fc->last_pid_recv = pid;
4386 GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO);
4387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n",
4388 GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid));
4389 return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */
4391 ttl = ntohl (msg->ttl);
4392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4395 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4396 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4397 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4404 send_prebuilt_message (message, hop, t);
4405 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4412 * Core handler for mesh network traffic going from the origin to a peer
4414 * @param cls Closure (unused).
4415 * @param message Message received.
4416 * @param peer Peer who sent the message.
4418 * @return GNUNET_OK to keep the connection open,
4419 * GNUNET_SYSERR to close it (signal serious error)
4422 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4423 const struct GNUNET_MessageHeader *message)
4425 return handle_mesh_data (peer, message, GNUNET_YES);
4429 * Core handler for mesh network traffic towards the owner of a tunnel.
4431 * @param cls Closure (unused).
4432 * @param message Message received.
4433 * @param peer Peer who sent the message.
4435 * @return GNUNET_OK to keep the connection open,
4436 * GNUNET_SYSERR to close it (signal serious error)
4439 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4440 const struct GNUNET_MessageHeader *message)
4442 return handle_mesh_data (peer, message, GNUNET_NO);
4447 * Core handler for mesh network traffic end-to-end ACKs.
4449 * @param cls Closure.
4450 * @param message Message.
4451 * @param peer Peer identity this notification is about.
4453 * @return GNUNET_OK to keep the connection open,
4454 * GNUNET_SYSERR to close it (signal serious error)
4457 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4458 const struct GNUNET_MessageHeader *message)
4460 struct GNUNET_MESH_DataACK *msg;
4461 struct MeshTunnelReliability *rel;
4462 struct MeshReliableMessage *copy;
4463 struct MeshReliableMessage *next;
4464 struct MeshTunnel *t;
4470 type = ntohs (message->type);
4471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4472 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4473 msg = (struct GNUNET_MESH_DataACK *) message;
4475 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4478 /* TODO notify that we dont know this tunnel (whom)? */
4479 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4482 ack = ntohl (msg->mid);
4483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4485 /* Is this a forward or backward ACK? */
4486 id = GNUNET_PEER_search (peer);
4487 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4490 if (NULL == t->owner)
4492 send_prebuilt_message (message, t->prev_hop, t);
4497 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4500 if (NULL == t->client)
4502 send_prebuilt_message (message, t->next_hop, t);
4509 GNUNET_break_op (0);
4513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4514 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4516 if (GMC_is_pid_bigger (copy->mid, ack))
4518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4519 tunnel_free_sent_reliable (t, msg, rel);
4523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4525 tunnel_free_reliable_message (copy);
4527 /* Once buffers have been free'd, send ACK */
4528 tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4530 /* If some message was free'd, update the retransmission delay*/
4531 if (GNUNET_YES == work)
4533 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4535 GNUNET_SCHEDULER_cancel (rel->retry_task);
4536 if (NULL == rel->head_sent)
4538 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4542 struct GNUNET_TIME_Absolute new_target;
4543 struct GNUNET_TIME_Relative delay;
4545 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4546 MESH_RETRANSMIT_MARGIN);
4547 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4549 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4551 GNUNET_SCHEDULER_add_delayed (delay,
4552 &tunnel_retransmit_message,
4563 * Core handler for mesh network traffic point-to-point acks.
4565 * @param cls closure
4566 * @param message message
4567 * @param peer peer identity this notification is about
4569 * @return GNUNET_OK to keep the connection open,
4570 * GNUNET_SYSERR to close it (signal serious error)
4573 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4574 const struct GNUNET_MessageHeader *message)
4576 struct GNUNET_MESH_ACK *msg;
4577 struct MeshTunnel *t;
4578 struct MeshFlowControl *fc;
4582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4584 msg = (struct GNUNET_MESH_ACK *) message;
4586 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4590 /* TODO notify that we dont know this tunnel (whom)? */
4591 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4594 ack = ntohl (msg->pid);
4595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4597 /* Is this a forward or backward ACK? */
4598 id = GNUNET_PEER_search (peer);
4599 if (t->next_hop == id)
4601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4604 else if (t->prev_hop == id)
4606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4611 GNUNET_break_op (0);
4615 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4616 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4618 GNUNET_SCHEDULER_cancel (fc->poll_task);
4619 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4620 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4623 fc->last_ack_recv = ack;
4624 peer_unlock_queue (id);
4625 tunnel_change_state (t, MESH_TUNNEL_READY);
4627 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
4634 * Core handler for mesh network traffic point-to-point ack polls.
4636 * @param cls closure
4637 * @param message message
4638 * @param peer peer identity this notification is about
4640 * @return GNUNET_OK to keep the connection open,
4641 * GNUNET_SYSERR to close it (signal serious error)
4644 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4645 const struct GNUNET_MessageHeader *message)
4647 struct GNUNET_MESH_Poll *msg;
4648 struct MeshTunnel *t;
4649 struct MeshFlowControl *fc;
4654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4657 msg = (struct GNUNET_MESH_Poll *) message;
4659 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4663 /* TODO notify that we dont know this tunnel (whom)? */
4664 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4665 GNUNET_break_op (0);
4669 /* Is this a forward or backward ACK? */
4670 id = GNUNET_PEER_search (peer);
4671 pid = ntohl (msg->pid);
4672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4673 if (t->next_hop == id)
4675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4677 old = fc->last_pid_recv;
4679 else if (t->prev_hop == id)
4681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4683 old = fc->last_pid_recv;
4691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", fc->last_pid_recv);
4692 fc->last_pid_recv = pid;
4693 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id);
4695 if (GNUNET_YES == t->reliable)
4696 fc->last_pid_recv = old;
4703 * Core handler for mesh keepalives.
4705 * @param cls closure
4706 * @param message message
4707 * @param peer peer identity this notification is about
4708 * @return GNUNET_OK to keep the connection open,
4709 * GNUNET_SYSERR to close it (signal serious error)
4711 * TODO: Check who we got this from, to validate route.
4714 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4715 const struct GNUNET_MessageHeader *message)
4717 struct GNUNET_MESH_TunnelKeepAlive *msg;
4718 struct MeshTunnel *t;
4719 struct MeshClient *c;
4723 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4727 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4730 /* TODO notify that we dont know that tunnel */
4731 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4736 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
4737 GNUNET_YES : GNUNET_NO;
4738 c = fwd ? t->client : t->owner;
4739 hop = fwd ? t->next_hop : t->prev_hop;
4742 tunnel_change_state (t, MESH_TUNNEL_READY);
4743 tunnel_reset_timeout (t, fwd);
4744 if (NULL != c || 0 == hop || myid == hop)
4747 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4748 send_prebuilt_message (message, hop, t);
4755 * Functions to handle messages from core
4757 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4758 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4759 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4760 sizeof (struct GNUNET_MESH_PathBroken)},
4761 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4762 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4763 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4764 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4765 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4766 sizeof (struct GNUNET_MESH_DataACK)},
4767 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4768 sizeof (struct GNUNET_MESH_DataACK)},
4769 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
4770 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4771 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
4772 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4773 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4774 sizeof (struct GNUNET_MESH_ACK)},
4775 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4776 sizeof (struct GNUNET_MESH_Poll)},
4777 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4778 sizeof (struct GNUNET_MESH_PathACK)},
4784 * Function to process paths received for a new peer addition. The recorded
4785 * paths form the initial tunnel, which can be optimized later.
4786 * Called on each result obtained for the DHT search.
4788 * @param cls closure
4789 * @param exp when will this value expire
4790 * @param key key of the result
4791 * @param get_path path of the get request
4792 * @param get_path_length lenght of get_path
4793 * @param put_path path of the put request
4794 * @param put_path_length length of the put_path
4795 * @param type type of the result
4796 * @param size number of bytes in data
4797 * @param data pointer to the result data
4800 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4801 const struct GNUNET_HashCode * key,
4802 const struct GNUNET_PeerIdentity *get_path,
4803 unsigned int get_path_length,
4804 const struct GNUNET_PeerIdentity *put_path,
4805 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4806 size_t size, const void *data)
4808 struct MeshPeerInfo *peer = cls;
4809 struct MeshPeerPath *p;
4810 struct GNUNET_PeerIdentity pi;
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4814 GNUNET_PEER_resolve (peer->id, &pi);
4815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4817 p = path_build_from_dht (get_path, get_path_length,
4818 put_path, put_path_length);
4819 path_add_to_peers (p, GNUNET_NO);
4821 for (i = 0; i < peer->ntunnels; i++)
4823 struct GNUNET_PeerIdentity id;
4825 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4827 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4828 peer->tunnels[i]->local_tid,
4829 peer->tunnels[i]->local_tid_dest);
4830 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4833 peer_connect (peer, peer->tunnels[i]);
4841 /******************************************************************************/
4842 /********************* MESH LOCAL HANDLES **************************/
4843 /******************************************************************************/
4847 * Handler for client connection.
4849 * @param cls Closure (unused).
4850 * @param client Client handler.
4853 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4855 struct MeshClient *c;
4859 c = GNUNET_malloc (sizeof (struct MeshClient));
4861 GNUNET_SERVER_client_keep (client);
4862 GNUNET_SERVER_client_set_user_context (client, c);
4863 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4868 * Handler for client disconnection
4870 * @param cls closure
4871 * @param client identification of the client; NULL
4872 * for the last call when the server is destroyed
4875 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4877 struct MeshClient *c;
4878 struct MeshClient *next;
4880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4887 c = client_get (client);
4890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
4892 GNUNET_SERVER_client_drop (c->handle);
4893 c->shutting_down = GNUNET_YES;
4894 if (NULL != c->own_tunnels)
4896 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4897 &tunnel_destroy_iterator, c);
4898 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4901 if (NULL != c->incoming_tunnels)
4903 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4904 &tunnel_destroy_iterator, c);
4905 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4908 if (NULL != c->ports)
4910 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
4911 &client_release_ports, c);
4912 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4915 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
4918 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4923 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4931 * Handler for new clients
4933 * @param cls closure
4934 * @param client identification of the client
4935 * @param message the actual message, which includes messages the client wants
4938 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4939 const struct GNUNET_MessageHeader *message)
4941 struct GNUNET_MESH_ClientConnect *cc_msg;
4942 struct MeshClient *c;
4947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4949 /* Check data sanity */
4950 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4951 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4952 if (0 != (size % sizeof (uint32_t)))
4955 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4958 size /= sizeof (uint32_t);
4960 /* Initialize new client structure */
4961 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4962 c->id = next_client_id++; /* overflow not important: just for debug */
4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4969 p = (uint32_t *) &cc_msg[1];
4970 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4971 for (i = 0; i < size; i++)
4974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4976 /* store in client's hashmap */
4977 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4978 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4979 /* store in global hashmap */
4980 /* FIXME only allow one client to have the port open,
4981 * have a backup hashmap with waiting clients */
4982 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4983 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4987 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4988 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4989 GNUNET_SERVER_notification_context_add (nc, client);
4990 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4992 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4998 * Handler for requests of new tunnels
5000 * @param cls Closure.
5001 * @param client Identification of the client.
5002 * @param message The actual message.
5005 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5006 const struct GNUNET_MessageHeader *message)
5008 struct GNUNET_MESH_TunnelMessage *t_msg;
5009 struct MeshPeerInfo *peer_info;
5010 struct MeshTunnel *t;
5011 struct MeshClient *c;
5012 MESH_TunnelNumber tid;
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5016 /* Sanity check for client registration */
5017 if (NULL == (c = client_get (client)))
5020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5025 /* Message size sanity check */
5026 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5033 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
5035 GNUNET_i2s (&t_msg->peer));
5036 tid = ntohl (t_msg->tunnel_id);
5037 /* Sanity check for duplicate tunnel IDs */
5038 if (NULL != tunnel_get_by_local_id (c, tid))
5041 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5046 while (NULL != tunnel_get_by_pi (myid, next_tid))
5047 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5048 t = tunnel_new (myid, next_tid, c, tid);
5049 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5056 t->port = ntohl (t_msg->port);
5057 tunnel_set_options (t, ntohl (t_msg->opt));
5058 if (GNUNET_YES == t->reliable)
5060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5061 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
5063 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
5066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
5067 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
5069 peer_info = peer_get (&t_msg->peer);
5070 peer_info_add_tunnel (peer_info, t);
5071 peer_connect (peer_info, t);
5072 tunnel_reset_timeout (t, GNUNET_YES);
5073 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5079 * Handler for requests of deleting tunnels
5081 * @param cls closure
5082 * @param client identification of the client
5083 * @param message the actual message
5086 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5087 const struct GNUNET_MessageHeader *message)
5089 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5090 struct MeshClient *c;
5091 struct MeshTunnel *t;
5092 MESH_TunnelNumber tid;
5094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5095 "Got a DESTROY TUNNEL from client!\n");
5097 /* Sanity check for client registration */
5098 if (NULL == (c = client_get (client)))
5101 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5106 /* Message sanity check */
5107 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5114 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5116 /* Retrieve tunnel */
5117 tid = ntohl (tunnel_msg->tunnel_id);
5118 t = tunnel_get_by_local_id (c, tid);
5121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5123 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5127 /* Cleanup after the tunnel */
5128 client_delete_tunnel (c, t);
5129 if (c == t->client && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV <= tid)
5133 else if (c == t->owner && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV > tid)
5135 peer_info_remove_tunnel (peer_get_short (t->dest), t);
5140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5141 " tunnel %X client %p (%p, %p)\n",
5142 tid, c, t->owner, t->client);
5146 /* The tunnel will be destroyed when the last message is transmitted. */
5147 tunnel_destroy_empty (t);
5149 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5155 * Handler for client traffic
5157 * @param cls closure
5158 * @param client identification of the client
5159 * @param message the actual message
5162 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
5163 const struct GNUNET_MessageHeader *message)
5165 struct GNUNET_MESH_LocalData *data_msg;
5166 struct MeshClient *c;
5167 struct MeshTunnel *t;
5168 struct MeshFlowControl *fc;
5169 MESH_TunnelNumber tid;
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5173 "Got data from a client!\n");
5175 /* Sanity check for client registration */
5176 if (NULL == (c = client_get (client)))
5179 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5184 data_msg = (struct GNUNET_MESH_LocalData *) message;
5186 /* Sanity check for message size */
5187 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
5188 if (size < sizeof (struct GNUNET_MessageHeader))
5191 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5195 /* Tunnel exists? */
5196 tid = ntohl (data_msg->tid);
5197 t = tunnel_get_by_local_id (c, tid);
5201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5205 /* Is the client in the tunnel? */
5206 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5208 t->owner->handle == client)
5210 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5212 t->client->handle == client) ) )
5215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5219 /* Ok, everything is correct, send the message
5220 * (pretend we got it from a mesh peer)
5223 struct GNUNET_MESH_Data *payload;
5224 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5226 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5227 if (GNUNET_YES == t->reliable)
5229 struct MeshTunnelReliability *rel;
5230 struct MeshReliableMessage *copy;
5232 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5233 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5234 + sizeof(struct GNUNET_MESH_Data)
5236 copy->mid = rel->mid_sent++;
5237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
5238 copy->timestamp = GNUNET_TIME_absolute_get ();
5241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
5242 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5243 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5246 GNUNET_TIME_relative_multiply (rel->expected_delay,
5247 MESH_RETRANSMIT_MARGIN);
5249 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5250 &tunnel_retransmit_message,
5253 payload = (struct GNUNET_MESH_Data *) ©[1];
5254 payload->mid = htonl (copy->mid);
5258 payload = (struct GNUNET_MESH_Data *) cbuf;
5259 payload->mid = htonl (fc->last_pid_recv + 1);
5261 memcpy (&payload[1], &data_msg[1], size);
5262 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5263 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5264 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5265 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5266 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5267 payload->tid = htonl (t->id.tid);
5268 payload->ttl = htonl (default_ttl);
5269 payload->pid = htonl (fc->last_pid_recv + 1);
5270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5271 " calling generic handler...\n");
5272 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5273 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5275 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5278 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5285 * Handler for client's ACKs for payload traffic.
5287 * @param cls Closure (unused).
5288 * @param client Identification of the client.
5289 * @param message The actual message.
5292 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5293 const struct GNUNET_MessageHeader *message)
5295 struct GNUNET_MESH_LocalAck *msg;
5296 struct MeshTunnel *t;
5297 struct MeshClient *c;
5298 MESH_TunnelNumber tid;
5300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5301 /* Sanity check for client registration */
5302 if (NULL == (c = client_get (client)))
5305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5310 msg = (struct GNUNET_MESH_LocalAck *) message;
5312 /* Tunnel exists? */
5313 tid = ntohl (msg->tunnel_id);
5314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5315 t = tunnel_get_by_local_id (c, tid);
5319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5325 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5326 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5328 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5329 t->prev_fc.last_ack_recv++;
5330 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5334 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5335 t->next_fc.last_ack_recv++;
5336 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5339 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5347 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5349 * @param cls Closure (client handle).
5350 * @param key Key (hashed tunnel ID, unused).
5351 * @param value Tunnel info.
5353 * @return GNUNET_YES, to keep iterating.
5356 monitor_all_tunnels_iterator (void *cls,
5357 const struct GNUNET_HashCode * key,
5360 struct GNUNET_SERVER_Client *client = cls;
5361 struct MeshTunnel *t = value;
5362 struct GNUNET_MESH_LocalMonitor *msg;
5364 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5365 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5366 msg->tunnel_id = htonl (t->id.tid);
5367 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5368 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5369 GNUNET_PEER_resolve (t->dest, &msg->destination);
5371 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5372 "* sending info about tunnel %s [%u]\n",
5373 GNUNET_i2s (&msg->owner), t->id.tid);
5375 GNUNET_SERVER_notification_context_unicast (nc, client,
5376 &msg->header, GNUNET_NO);
5382 * Handler for client's MONITOR request.
5384 * @param cls Closure (unused).
5385 * @param client Identification of the client.
5386 * @param message The actual message.
5389 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5390 const struct GNUNET_MessageHeader *message)
5392 struct MeshClient *c;
5394 /* Sanity check for client registration */
5395 if (NULL == (c = client_get (client)))
5398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5402 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5403 "Received get tunnels request from client %u\n",
5405 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5406 monitor_all_tunnels_iterator,
5408 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5409 "Get tunnels request from client %u completed\n",
5411 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5416 * Handler for client's MONITOR_TUNNEL request.
5418 * @param cls Closure (unused).
5419 * @param client Identification of the client.
5420 * @param message The actual message.
5423 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5424 const struct GNUNET_MessageHeader *message)
5426 const struct GNUNET_MESH_LocalMonitor *msg;
5427 struct GNUNET_MESH_LocalMonitor *resp;
5428 struct MeshClient *c;
5429 struct MeshTunnel *t;
5431 /* Sanity check for client registration */
5432 if (NULL == (c = client_get (client)))
5435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5439 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5440 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5441 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5444 ntohl (msg->tunnel_id));
5445 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5448 /* We don't know the tunnel FIXME */
5449 struct GNUNET_MESH_LocalMonitor warn;
5452 GNUNET_SERVER_notification_context_unicast (nc, client,
5455 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5459 /* Initialize context */
5460 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5462 GNUNET_PEER_resolve (t->dest, &resp->destination);
5463 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5464 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5465 &resp->header, GNUNET_NO);
5468 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5469 "Monitor tunnel request from client %u completed\n",
5471 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5476 * Functions to handle messages from clients
5478 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5479 {&handle_local_new_client, NULL,
5480 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5481 {&handle_local_tunnel_create, NULL,
5482 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5483 sizeof (struct GNUNET_MESH_TunnelMessage)},
5484 {&handle_local_tunnel_destroy, NULL,
5485 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5486 sizeof (struct GNUNET_MESH_TunnelMessage)},
5487 {&handle_local_data, NULL,
5488 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5489 {&handle_local_ack, NULL,
5490 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5491 sizeof (struct GNUNET_MESH_LocalAck)},
5492 {&handle_local_get_tunnels, NULL,
5493 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5494 sizeof (struct GNUNET_MessageHeader)},
5495 {&handle_local_show_tunnel, NULL,
5496 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5497 sizeof (struct GNUNET_MESH_LocalMonitor)},
5503 * Method called whenever a given peer connects.
5505 * @param cls closure
5506 * @param peer peer identity this notification is about
5509 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5511 struct MeshPeerInfo *peer_info;
5512 struct MeshPeerPath *path;
5514 DEBUG_CONN ("Peer connected\n");
5515 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5516 peer_info = peer_get (peer);
5517 if (myid == peer_info->id)
5519 DEBUG_CONN (" (self)\n");
5520 path = path_new (1);
5524 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5525 path = path_new (2);
5526 path->peers[1] = peer_info->id;
5527 GNUNET_PEER_change_rc (peer_info->id, 1);
5528 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5530 path->peers[0] = myid;
5531 GNUNET_PEER_change_rc (myid, 1);
5532 peer_info_add_path (peer_info, path, GNUNET_YES);
5538 * Method called whenever a peer disconnects.
5540 * @param cls closure
5541 * @param peer peer identity this notification is about
5544 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5546 struct MeshPeerInfo *pi;
5547 struct MeshPeerQueue *q;
5548 struct MeshPeerQueue *n;
5550 DEBUG_CONN ("Peer disconnected\n");
5551 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5561 /* TODO try to reroute this traffic instead */
5562 queue_destroy(q, GNUNET_YES);
5565 if (NULL != pi->core_transmit)
5567 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5568 pi->core_transmit = NULL;
5570 peer_info_remove_path (pi, pi->id, myid);
5573 DEBUG_CONN (" (self)\n");
5575 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5581 * Install server (service) handlers and start listening to clients.
5586 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5587 GNUNET_SERVER_connect_notify (server_handle,
5588 &handle_local_client_connect, NULL);
5589 GNUNET_SERVER_disconnect_notify (server_handle,
5590 &handle_local_client_disconnect, NULL);
5591 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5593 clients_head = NULL;
5594 clients_tail = NULL;
5596 GNUNET_SERVER_resume (server_handle);
5601 * To be called on core init/fail.
5603 * @param cls Closure (config)
5604 * @param server handle to the server for this service
5605 * @param identity the public identity of this peer
5608 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5609 const struct GNUNET_PeerIdentity *identity)
5611 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5615 GNUNET_break (core_handle == server);
5616 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5620 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5622 GNUNET_i2s (identity));
5623 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5625 GNUNET_i2s (&my_full_id));
5626 GNUNET_CORE_disconnect (core_handle);
5627 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5628 NULL, /* Closure passed to MESH functions */
5629 &core_init, /* Call core_init once connected */
5630 &core_connect, /* Handle connects */
5631 &core_disconnect, /* remove peers on disconnects */
5632 NULL, /* Don't notify about all incoming messages */
5633 GNUNET_NO, /* For header only in notification */
5634 NULL, /* Don't notify about all outbound messages */
5635 GNUNET_NO, /* For header-only out notification */
5636 core_handlers); /* Register these handlers */
5645 /******************************************************************************/
5646 /************************ MAIN FUNCTIONS ****************************/
5647 /******************************************************************************/
5650 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5652 * @param cls closure
5653 * @param key current key code
5654 * @param value value in the hash map
5655 * @return GNUNET_YES if we should continue to iterate,
5659 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5661 struct MeshTunnel *t = value;
5668 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5670 * @param cls closure
5671 * @param key current key code
5672 * @param value value in the hash map
5673 * @return GNUNET_YES if we should continue to iterate,
5677 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5679 struct MeshPeerInfo *p = value;
5680 struct MeshPeerQueue *q;
5681 struct MeshPeerQueue *n;
5689 queue_destroy(q, GNUNET_YES);
5693 peer_info_destroy (p);
5699 * Task run during shutdown.
5705 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5709 if (core_handle != NULL)
5711 GNUNET_CORE_disconnect (core_handle);
5714 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5715 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5716 if (dht_handle != NULL)
5718 GNUNET_DHT_disconnect (dht_handle);
5723 GNUNET_SERVER_notification_context_destroy (nc);
5726 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5728 GNUNET_SCHEDULER_cancel (announce_id_task);
5729 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5736 * Process mesh requests.
5738 * @param cls closure
5739 * @param server the initialized server
5740 * @param c configuration to use
5743 run (void *cls, struct GNUNET_SERVER_Handle *server,
5744 const struct GNUNET_CONFIGURATION_Handle *c)
5747 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5750 server_handle = server;
5751 GNUNET_SERVER_suspend (server_handle);
5754 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5757 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5759 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5760 "mesh", "peer/privatekey");
5761 GNUNET_SCHEDULER_shutdown ();
5766 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5767 &refresh_path_time))
5769 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5771 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5772 "mesh", "refresh path time");
5773 GNUNET_SCHEDULER_shutdown ();
5778 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5781 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5783 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5784 "mesh", "id announce time");
5785 GNUNET_SCHEDULER_shutdown ();
5790 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5795 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5796 "mesh", "connect timeout");
5797 GNUNET_SCHEDULER_shutdown ();
5802 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5807 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5808 "mesh", "max msgs queue");
5809 GNUNET_SCHEDULER_shutdown ();
5814 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5817 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5819 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5820 "mesh", "max tunnels");
5821 GNUNET_SCHEDULER_shutdown ();
5826 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5829 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5831 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5832 "mesh", "default ttl", 64);
5837 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5840 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5841 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5842 "mesh", "max peers", 1000);
5847 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5855 "Mesh is running with drop mode enabled. "
5856 "This is NOT a good idea! "
5857 "Remove the DROP_PERCENT option from your configuration.\n");
5861 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5862 &dht_replication_level))
5864 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5866 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5867 "mesh", "dht replication level", 3);
5868 dht_replication_level = 3;
5871 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5872 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5873 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5874 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5876 dht_handle = GNUNET_DHT_connect (c, 64);
5877 if (NULL == dht_handle)
5881 stats = GNUNET_STATISTICS_create ("mesh", c);
5883 /* Scheduled the task to clean up when shutdown is called */
5884 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5886 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5887 GNUNET_free (keyfile);
5888 GNUNET_assert (NULL != pk);
5889 my_private_key = pk;
5890 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5891 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5892 &my_full_id.hashPubKey);
5893 myid = GNUNET_PEER_intern (&my_full_id);
5894 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5895 "Mesh for peer [%s] starting\n",
5896 GNUNET_i2s(&my_full_id));
5898 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5899 NULL, /* Closure passed to MESH functions */
5900 &core_init, /* Call core_init once connected */
5901 &core_connect, /* Handle connects */
5902 &core_disconnect, /* remove peers on disconnects */
5903 NULL, /* Don't notify about all incoming messages */
5904 GNUNET_NO, /* For header only in notification */
5905 NULL, /* Don't notify about all outbound messages */
5906 GNUNET_NO, /* For header-only out notification */
5907 core_handlers); /* Register these handlers */
5908 if (NULL == core_handle)
5911 GNUNET_SCHEDULER_shutdown ();
5915 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5916 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5922 * The main function for the mesh service.
5924 * @param argc number of arguments from the command line
5925 * @param argv command line arguments
5926 * @return 0 ok, 1 on error
5929 main (int argc, char *const *argv)
5934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5935 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5937 ret = (GNUNET_OK == r) ? 0 : 1;
5938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");