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 MeshPeer *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 (MeshPeer).
675 static struct GNUNET_CONTAINER_MultiPeerMap *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;
723 * Tunnel ID for the next created tunnel (global tunnel number).
725 static MESH_TunnelNumber next_tid;
728 * Tunnel ID for the next incoming tunnel (local tunnel number).
730 static MESH_TunnelNumber next_local_tid;
733 * All ports clients of this peer have opened.
735 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
738 * Task to periodically announce itself in the network.
740 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
743 * Next ID to assign to a client.
745 unsigned int next_client_id;
748 /******************************************************************************/
749 /*********************** DECLARATIONS **************************/
750 /******************************************************************************/
753 * Function to process paths received for a new peer addition. The recorded
754 * paths form the initial tunnel, which can be optimized later.
755 * Called on each result obtained for the DHT search.
758 * @param exp when will this value expire
759 * @param key key of the result
760 * @param type type of the result
761 * @param size number of bytes in data
762 * @param data pointer to the result data
765 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
766 const struct GNUNET_HashCode * key,
767 const struct GNUNET_PeerIdentity *get_path,
768 unsigned int get_path_length,
769 const struct GNUNET_PeerIdentity *put_path,
770 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
771 size_t size, const void *data);
775 * Retrieve the MeshPeer stucture associated with the peer, create one
776 * and insert it in the appropriate structures if the peer is not known yet.
778 * @param peer Full identity of the peer.
780 * @return Existing or newly created peer info.
782 static struct MeshPeer *
783 peer_get (const struct GNUNET_PeerIdentity *peer);
787 * Retrieve the MeshPeer stucture associated with the peer, create one
788 * and insert it in the appropriate structures if the peer is not known yet.
790 * @param peer Short identity of the peer.
792 * @return Existing or newly created peer info.
794 static struct MeshPeer *
795 peer_get_short (const GNUNET_PEER_Id peer);
799 * Build a PeerPath from the paths returned from the DHT, reversing the paths
800 * to obtain a local peer -> destination path and interning the peer ids.
802 * @return Newly allocated and created path
804 static struct MeshPeerPath *
805 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
806 unsigned int get_path_length,
807 const struct GNUNET_PeerIdentity *put_path,
808 unsigned int put_path_length);
812 * Adds a path to the peer_infos of all the peers in the path
814 * @param p Path to process.
815 * @param confirmed Whether we know if the path works or not.
818 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
823 * Search for a tunnel by global ID using full PeerIdentities.
825 * @param oid owner of the tunnel.
826 * @param tid global tunnel number.
828 * @return tunnel handler, NULL if doesn't exist.
830 static struct MeshTunnel *
831 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
835 * Change the tunnel state.
837 * @param t Tunnel whose ttate to change.
838 * @param state New state.
841 tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state);
845 * Notify a tunnel that a connection has broken that affects at least
848 * @param t Tunnel affected.
849 * @param p1 Peer that got disconnected from p2.
850 * @param p2 Peer that got disconnected from p1.
852 * @return Short ID of the peer disconnected (either p1 or p2).
853 * 0 if the tunnel remained unaffected.
855 static GNUNET_PEER_Id
856 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
861 * Send FWD keepalive packets for a tunnel.
863 * @param cls Closure (tunnel for which to send the keepalive).
864 * @param tc Notification context.
867 tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
870 * Send BCK keepalive packets for a tunnel.
872 * @param cls Closure (tunnel for which to send the keepalive).
873 * @param tc Notification context.
876 tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
879 * @brief Use the given path for the tunnel.
880 * Update the next and prev hops (and RCs).
881 * (Re)start the path refresh in case the tunnel is locally owned.
883 * @param t Tunnel to update.
884 * @param p Path to use.
887 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
890 * Tunnel is empty: destroy it.
892 * Notifies all participants (peers, cleints) about the destruction.
894 * @param t Tunnel to destroy.
897 tunnel_destroy_empty (struct MeshTunnel *t);
900 * Destroy the tunnel.
902 * This function does not generate any warning traffic to clients or peers.
905 * Remove the tunnel from peer_info's and clients' hashmaps.
906 * Cancel messages belonging to this tunnel queued to neighbors.
907 * Free any allocated resources linked to the tunnel.
909 * @param t the tunnel to destroy
911 * @return GNUNET_OK on success
914 tunnel_destroy (struct MeshTunnel *t);
917 * @brief Queue and pass message to core when possible.
919 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
920 * and accounts for it. In case the queue is full, the message is dropped and
923 * Otherwise, message is treated as internal and allowed to go regardless of
926 * @param cls Closure (@c type dependant). It will be used by queue_send to
927 * build the message to be sent if not already prebuilt.
928 * @param type Type of the message, 0 for a raw message.
929 * @param size Size of the message.
930 * @param dst Neighbor to send message to.
931 * @param t Tunnel this message belongs to.
934 queue_add (void *cls, uint16_t type, size_t size,
935 struct MeshPeer *dst, struct MeshTunnel *t);
939 * Free a transmission that was already queued with all resources
940 * associated to the request.
942 * @param queue Queue handler to cancel.
943 * @param clear_cls Is it necessary to free associated cls?
946 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
950 * @brief Get the next transmittable message from the queue.
952 * This will be the head, except in the case of being a data packet
953 * not allowed by the destination peer.
955 * @param peer Destination peer.
957 * @return The next viable MeshPeerQueue element to send to that peer.
958 * NULL when there are no transmittable messages.
960 struct MeshPeerQueue *
961 queue_get_next (const struct MeshPeer *peer);
965 * Core callback to write a queued packet to core buffer
967 * @param cls Closure (peer info).
968 * @param size Number of bytes available in buf.
969 * @param buf Where the to write the message.
971 * @return number of bytes written to buf
974 queue_send (void *cls, size_t size, void *buf);
978 * Dummy function to separate declarations from definitions in function list.
981 __mesh_divider______________________________________________________________();
985 GNUNET_MESH_DEBUG_S2S (enum MeshTunnelState s)
987 static char buf[128];
992 * Uninitialized status, should never appear in operation.
994 case MESH_TUNNEL_NEW: return "MESH_TUNNEL_NEW";
997 * Path to the peer not known yet
999 case MESH_TUNNEL_SEARCHING: return "MESH_TUNNEL_SEARCHING";
1002 * Request sent, not yet answered.
1004 case MESH_TUNNEL_WAITING: return "MESH_TUNNEL_WAITING";
1007 * Peer connected and ready to accept data
1009 case MESH_TUNNEL_READY: return "MESH_TUNNEL_READY";
1012 * Peer connected previosly but not responding
1014 case MESH_TUNNEL_RECONNECTING: return "MESH_TUNNEL_RECONNECTING";
1017 sprintf (buf, "%u (UNKNOWN STATE)", s);
1023 /******************************************************************************/
1024 /************************ PERIODIC FUNCTIONS ****************************/
1025 /******************************************************************************/
1028 * Periodically announce self id in the DHT
1030 * @param cls closure
1031 * @param tc task context
1034 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1036 struct PBlock block;
1037 struct GNUNET_HashCode phash;
1039 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1041 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1045 * - Set data expiration in function of X
1046 * - Adapt X to churn
1048 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1049 GNUNET_CRYPTO_hash (&my_full_id, sizeof (my_full_id), &phash);
1050 block.id = my_full_id;
1051 GNUNET_DHT_put (dht_handle, /* DHT handle */
1053 dht_replication_level, /* Replication level */
1054 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1055 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1056 sizeof (block), /* Size of the data */
1057 (const char *) &block, /* Data itself */
1058 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1059 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1060 NULL, /* Continuation */
1061 NULL); /* Continuation closure */
1063 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1067 /******************************************************************************/
1068 /****************** GENERAL HELPER FUNCTIONS ************************/
1069 /******************************************************************************/
1073 * Check if client has registered with the service and has not disconnected
1075 * @param client the client to check
1077 * @return non-NULL if client exists in the global DLL
1079 static struct MeshClient *
1080 client_get (struct GNUNET_SERVER_Client *client)
1082 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1087 * Deletes a tunnel from a client (either owner or destination). To be used on
1090 * @param c Client whose tunnel to delete.
1091 * @param t Tunnel which should be deleted.
1094 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1100 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1102 if (GNUNET_YES != res)
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel owner KO\n");
1107 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1108 t->local_tid_dest, t);
1109 if (GNUNET_YES != res)
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1116 * Notify the appropriate client that a new incoming tunnel was created.
1118 * @param t Tunnel that was created.
1121 send_local_tunnel_create (struct MeshTunnel *t)
1123 struct GNUNET_MESH_TunnelMessage msg;
1125 if (NULL == t->client)
1127 msg.header.size = htons (sizeof (msg));
1128 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1129 msg.tunnel_id = htonl (t->local_tid_dest);
1130 msg.port = htonl (t->port);
1132 msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1133 msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1134 msg.opt = htonl (msg.opt);
1135 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1136 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1137 &msg.header, GNUNET_NO);
1142 * Notify a client that the incoming tunnel is no longer valid.
1144 * @param t Tunnel that is destroyed.
1145 * @param fwd Forward notification (owner->dest)?
1148 send_local_tunnel_destroy (struct MeshTunnel *t, int fwd)
1150 struct GNUNET_MESH_TunnelMessage msg;
1151 struct MeshClient *c;
1153 c = fwd ? t->client : t->owner;
1159 msg.header.size = htons (sizeof (msg));
1160 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1161 msg.tunnel_id = htonl (fwd ? t->local_tid_dest : t->local_tid);
1162 msg.port = htonl (0);
1163 memset (&msg.peer, 0, sizeof (msg.peer));
1164 msg.opt = htonl (0);
1165 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1166 &msg.header, GNUNET_NO);
1171 * Build a local ACK message and send it to a local client.
1173 * @param t Tunnel on which to send the ACK.
1174 * @param c Client to whom send the ACK.
1175 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1178 send_local_ack (struct MeshTunnel *t,
1179 struct MeshClient *c,
1182 struct GNUNET_MESH_LocalAck msg;
1184 msg.header.size = htons (sizeof (msg));
1185 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1186 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
1187 GNUNET_SERVER_notification_context_unicast (nc,
1196 * Sends an already built message to a peer, properly registrating
1197 * all used resources.
1199 * @param message Message to send. Function makes a copy of it.
1200 * @param peer Short ID of the neighbor whom to send the message.
1201 * @param t Tunnel on which this message is transmitted.
1204 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1205 GNUNET_PEER_Id peer,
1206 struct MeshTunnel *t)
1208 struct GNUNET_PeerIdentity id;
1209 struct MeshPeer *neighbor;
1210 struct MeshPeerPath *p;
1215 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1220 size = ntohs (message->size);
1221 data = GNUNET_malloc (size);
1222 memcpy (data, message, size);
1223 type = ntohs(message->type);
1224 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1225 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1227 struct GNUNET_MESH_Data *u;
1229 u = (struct GNUNET_MESH_Data *) data;
1230 u->ttl = htonl (ntohl (u->ttl) - 1);
1232 GNUNET_PEER_resolve (peer, &id);
1233 neighbor = peer_get (&id);
1234 for (p = neighbor->path_head; NULL != p; p = p->next)
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 " %s IS NOT DIRECTLY CONNECTED\n",
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1250 for (p = neighbor->path_head; NULL != p; p = p->next)
1252 struct GNUNET_PeerIdentity debug_id;
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 " path with %u hops through:\n",
1258 for (i = 0; i < p->length; i++)
1260 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1263 i, GNUNET_i2s(&debug_id));
1268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1269 " no direct connection to %s\n",
1274 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1285 * Sends a CREATE PATH message for a path to a peer, properly registrating
1286 * all used resources.
1288 * @param t Tunnel for which the path is created.
1291 send_path_create (struct MeshTunnel *t)
1293 struct MeshPeer *neighbor;
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1296 neighbor = peer_get_short (t->next_hop);
1298 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1299 sizeof (struct GNUNET_MESH_CreateTunnel) +
1300 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1303 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1308 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1310 * @param t Tunnel which to confirm.
1313 send_path_ack (struct MeshTunnel *t)
1315 struct MeshPeer *neighbor;
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send path ack\n");
1318 neighbor = peer_get_short (t->prev_hop);
1320 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1321 sizeof (struct GNUNET_MESH_PathACK),
1324 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1329 * Build an ACK message and queue it to send to the given peer.
1331 * @param t Tunnel on which to send the ACK.
1332 * @param peer Peer to whom send the ACK.
1333 * @param ack Value of the ACK.
1336 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1338 struct GNUNET_MESH_ACK msg;
1340 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1341 msg.header.size = htons (sizeof (msg));
1342 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1343 msg.pid = htonl (ack);
1344 msg.tid = htonl (t->id.tid);
1346 send_prebuilt_message (&msg.header, peer, t);
1351 * Core callback to write a pre-constructed data packet to core buffer
1353 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1354 * @param size Number of bytes available in buf.
1355 * @param buf Where the to write the message.
1357 * @return number of bytes written to buf
1360 send_core_data_raw (void *cls, size_t size, void *buf)
1362 struct GNUNET_MessageHeader *msg = cls;
1365 GNUNET_assert (NULL != msg);
1366 total_size = ntohs (msg->size);
1368 if (total_size > size)
1373 memcpy (buf, msg, total_size);
1380 * Function to send a create path packet to a peer.
1382 * @param cls closure
1383 * @param size number of bytes available in buf
1384 * @param buf where the callee should write the message
1385 * @return number of bytes written to buf
1388 send_core_path_create (void *cls, size_t size, void *buf)
1390 struct MeshTunnel *t = cls;
1391 struct GNUNET_MESH_CreateTunnel *msg;
1392 struct GNUNET_PeerIdentity *peer_ptr;
1393 struct MeshPeerPath *p = t->path;
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
1400 sizeof (struct GNUNET_MESH_CreateTunnel) +
1401 p->length * sizeof (struct GNUNET_PeerIdentity);
1403 if (size < size_needed || NULL == buf)
1408 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
1409 msg->header.size = htons (size_needed);
1410 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1411 msg->tid = ntohl (t->id.tid);
1414 if (GNUNET_YES == t->nobuffer)
1415 opt |= GNUNET_MESH_OPTION_NOBUFFER;
1416 if (GNUNET_YES == t->reliable)
1417 opt |= GNUNET_MESH_OPTION_RELIABLE;
1418 msg->opt = htonl (opt);
1419 msg->port = htonl (t->port);
1421 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1422 for (i = 0; i < p->length; i++)
1424 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1428 "CREATE PATH (%u bytes long) sent!\n", size_needed);
1434 * Creates a path ack message in buf and frees all unused resources.
1436 * @param cls closure (MeshTransmissionDescriptor)
1437 * @param size number of bytes available in buf
1438 * @param buf where the callee should write the message
1439 * @return number of bytes written to buf
1442 send_core_path_ack (void *cls, size_t size, void *buf)
1444 struct MeshTunnel *t = cls;
1445 struct GNUNET_MESH_PathACK *msg = buf;
1447 GNUNET_assert (NULL != t);
1448 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1453 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
1454 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1455 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1456 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
1457 msg->tid = htonl (t->id.tid);
1458 msg->peer_id = my_full_id;
1459 msg->ack = htonl (t->prev_fc.last_ack_sent);
1461 /* TODO add signature */
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
1464 return sizeof (struct GNUNET_MESH_PathACK);
1469 * Iterator over all the peers to remove the oldest not-used entry.
1471 * @param cls Closure (unsued).
1472 * @param key ID of the peer.
1473 * @param value Peer_Info of the peer.
1478 peer_timeout (void *cls,
1479 const struct GNUNET_PeerIdentity *key,
1487 * Retrieve the MeshPeer stucture associated with the peer, create one
1488 * and insert it in the appropriate structures if the peer is not known yet.
1490 * @param peer Full identity of the peer.
1492 * @return Existing or newly created peer info.
1494 static struct MeshPeer *
1495 peer_get (const struct GNUNET_PeerIdentity *peer_id)
1497 struct MeshPeer *peer;
1499 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1502 peer = GNUNET_new (struct MeshPeer);
1503 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1505 GNUNET_CONTAINER_multipeermap_iterate (peers,
1509 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1510 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1511 peer->id = GNUNET_PEER_intern (peer_id);
1513 peer->last_contact = GNUNET_TIME_absolute_get();
1520 * Retrieve the MeshPeer stucture associated with the peer, create one
1521 * and insert it in the appropriate structures if the peer is not known yet.
1523 * @param peer Short identity of the peer.
1525 * @return Existing or newly created peer info.
1527 static struct MeshPeer *
1528 peer_get_short (const GNUNET_PEER_Id peer)
1530 return peer_get (GNUNET_PEER_resolve2 (peer));
1535 * Select which PID to POLL for, to compensate for lost messages.
1537 * @param pi Peer we want to poll.
1538 * @param t Tunnel about which we want to poll.
1540 * @return PID to use, either last sent or first_in_queue - 1
1543 peer_get_first_payload_pid (struct MeshPeer *p, struct MeshTunnel *t)
1545 struct MeshPeerQueue *q;
1548 type = p->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST :
1549 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN;
1551 for (q = p->queue_head; NULL != q; q = q->next)
1553 if (q->type == type && q->tunnel == t)
1555 struct GNUNET_MESH_Data *msg = q->cls;
1557 /* Pretend that the last one sent was the previous to this */
1558 return ntohl (msg->pid) - 1;
1562 /* No data in queue, use last sent */
1564 struct MeshFlowControl *fc;
1566 fc = p->id == t->next_hop ? &t->next_fc : &t->prev_fc;
1567 return fc->last_pid_sent;
1573 * Choose the best path towards a peer considering the tunnel properties.
1575 * @param peer The destination peer.
1576 * @param t The tunnel the path is for.
1578 * @return Best current known path towards the peer, if any.
1580 static struct MeshPeerPath *
1581 peer_get_best_path (const struct MeshPeer *peer, const struct MeshTunnel *t)
1583 struct MeshPeerPath *best_p;
1584 struct MeshPeerPath *p;
1585 unsigned int best_cost;
1588 best_p = p = peer->path_head;
1589 best_cost = cost = p->length;
1592 if ((cost = p->length) < best_cost)
1604 * Try to establish a new connection to this peer in the given tunnel.
1605 * If the peer doesn't have any path to it yet, try to get one.
1606 * If the peer already has some path, send a CREATE PATH towards it.
1608 * @param peer PeerInfo of the peer.
1609 * @param t Tunnel for which to create the path, if possible.
1612 peer_connect (struct MeshPeer *peer, struct MeshTunnel *t)
1614 struct MeshPeerPath *p;
1615 struct GNUNET_HashCode phash;
1617 if (NULL != peer->path_head)
1619 p = peer_get_best_path (peer, t);
1620 tunnel_use_path (t, p);
1621 send_path_create (t);
1623 else if (NULL == peer->dhtget)
1625 struct GNUNET_PeerIdentity id;
1627 GNUNET_PEER_resolve (peer->id, &id);
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1629 "Starting DHT GET for peer %s\n",
1631 GNUNET_CRYPTO_hash (&id, sizeof (my_full_id), &phash);
1632 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1633 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1634 &phash, /* key to search */
1635 dht_replication_level, /* replication level */
1636 GNUNET_DHT_RO_RECORD_ROUTE |
1637 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1639 0, /* xquery bits */
1640 &dht_get_id_handler, peer);
1641 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1646 "There is no path but the DHT GET is already started.\n");
1652 * @brief Re-initiate traffic to this peer if necessary.
1654 * Check if there is traffic queued towards this peer
1655 * and the core transmit handle is NULL (traffic was stalled).
1656 * If so, call core tmt rdy.
1658 * @param peer_id Short ID of peer to which initiate traffic.
1661 peer_unlock_queue (GNUNET_PEER_Id peer_id)
1663 struct MeshPeer *peer;
1664 struct MeshPeerQueue *q;
1667 peer = peer_get_short (peer_id);
1668 if (NULL != peer->core_transmit)
1671 q = queue_get_next (peer);
1676 peer->core_transmit =
1677 GNUNET_CORE_notify_transmit_ready (core_handle,
1680 GNUNET_TIME_UNIT_FOREVER_REL,
1681 GNUNET_PEER_resolve2 (peer->id),
1689 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
1691 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
1692 * @param t Tunnel which to cancel.
1695 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
1697 struct MeshPeer *peer;
1698 struct MeshPeerQueue *q;
1699 struct MeshPeerQueue *next;
1700 struct MeshFlowControl *fc;
1703 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
1704 peer = peer_get_short (neighbor);
1705 for (q = peer->queue_head; NULL != q; q = next)
1710 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == q->type ||
1711 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == q->type)
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714 "peer_cancel_queues %s\n",
1715 GNUNET_MESH_DEBUG_M2S (q->type));
1717 queue_destroy (q, GNUNET_YES);
1720 if (NULL == peer->queue_head && NULL != peer->core_transmit)
1722 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1723 peer->core_transmit = NULL;
1725 fc = neighbor == t->next_hop ? &t->next_fc : &t->prev_fc;
1726 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1728 GNUNET_SCHEDULER_cancel (fc->poll_task);
1729 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1735 * Destroy the peer_info and free any allocated resources linked to it
1737 * @param peer The peer_info to destroy.
1739 * @return GNUNET_OK on success
1742 peer_destroy (struct MeshPeer *peer)
1744 struct GNUNET_PeerIdentity id;
1745 struct MeshPeerPath *p;
1746 struct MeshPeerPath *nextp;
1749 GNUNET_PEER_resolve (peer->id, &id);
1750 GNUNET_PEER_change_rc (peer->id, -1);
1753 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
1756 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1757 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1759 if (NULL != peer->dhtget)
1761 GNUNET_DHT_get_stop (peer->dhtget);
1763 p = peer->path_head;
1767 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1771 for (i = 0; i < peer->ntunnels; i++)
1772 tunnel_destroy_empty (peer->tunnels[i]);
1773 GNUNET_array_grow (peer->tunnels, peer->ntunnels, 0);
1780 * Remove all paths that rely on a direct connection between p1 and p2
1781 * from the peer itself and notify all tunnels about it.
1783 * @param peer PeerInfo of affected peer.
1784 * @param p1 GNUNET_PEER_Id of one peer.
1785 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1788 * TODO: optimize (see below)
1791 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
1794 struct MeshPeerPath *p;
1795 struct MeshPeerPath *next;
1796 struct MeshPeer *peer_d;
1798 unsigned int destroyed;
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1803 for (p = peer->path_head; NULL != p; p = next)
1806 for (i = 0; i < (p->length - 1); i++)
1808 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1809 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1811 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1821 for (i = 0; i < peer->ntunnels; i++)
1823 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1827 peer_d = peer_get_short (d);
1828 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1829 tunnel_use_path (peer->tunnels[i], next);
1830 peer_connect (peer_d, peer->tunnels[i]);
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1837 * Add the path to the peer and update the path used to reach it in case this
1840 * @param peer_info Destination peer to add the path to.
1841 * @param path New path to add. Last peer must be the peer in arg 1.
1842 * Path will be either used of freed if already known.
1843 * @param trusted Do we trust that this path is real?
1846 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
1849 struct MeshPeerPath *aux;
1853 if ((NULL == peer_info) || (NULL == path))
1856 path_destroy (path);
1859 if (path->peers[path->length - 1] != peer_info->id)
1862 path_destroy (path);
1865 if (2 >= path->length && GNUNET_NO == trusted)
1867 /* Only allow CORE to tell us about direct paths */
1868 path_destroy (path);
1871 for (l = 1; l < path->length; l++)
1873 if (path->peers[l] == myid)
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1876 for (l2 = 0; l2 < path->length - l; l2++)
1878 path->peers[l2] = path->peers[l + l2];
1883 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1888 struct GNUNET_PeerIdentity id;
1890 GNUNET_PEER_resolve (peer_info->id, &id);
1891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1892 path->length, GNUNET_i2s (&id));
1895 l = path_get_length (path);
1898 path_destroy (path);
1902 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1903 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1905 l2 = path_get_length (aux);
1908 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1909 peer_info->path_tail, aux, path);
1914 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1916 path_destroy (path);
1921 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1928 * Add the path to the origin peer and update the path used to reach it in case
1929 * this is the shortest.
1930 * The path is given in peer_info -> destination, therefore we turn the path
1931 * upside down first.
1933 * @param peer_info Peer to add the path to, being the origin of the path.
1934 * @param path New path to add after being inversed.
1935 * Path will be either used or freed.
1936 * @param trusted Do we trust that this path is real?
1939 peer_add_path_to_origin (struct MeshPeer *peer_info,
1940 struct MeshPeerPath *path, int trusted)
1943 peer_add_path (peer_info, path, trusted);
1948 * Add a tunnel to the list of tunnels a peer participates in.
1949 * Update the tunnel's destination.
1951 * @param p Peer to add to.
1952 * @param t Tunnel to add.
1955 peer_add_tunnel (struct MeshPeer *p, struct MeshTunnel *t)
1959 GNUNET_break (t->dest == p->id);
1963 GNUNET_PEER_change_rc (t->dest, 1);
1964 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1969 * Remove a tunnel from the list of tunnels a peer participates in.
1970 * Free the tunnel's destination.
1972 * @param p Peer to clean.
1973 * @param t Tunnel to remove.
1976 peer_remove_tunnel (struct MeshPeer *p, struct MeshTunnel *t)
1980 if (t->dest == p->id)
1982 GNUNET_PEER_change_rc (t->dest, -1);
1985 for (i = 0; i < p->ntunnels; i++)
1987 if (p->tunnels[i] == t)
1989 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1990 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1998 * Function called if the connection to the peer has been stalled for a while,
1999 * possibly due to a missed ACK. Poll the peer about its ACK status.
2001 * @param cls Closure (poll ctx).
2002 * @param tc TaskContext.
2005 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2007 struct MeshFlowControl *fc = cls;
2008 struct GNUNET_MESH_Poll msg;
2009 struct MeshTunnel *t = fc->t;
2010 GNUNET_PEER_Id peer;
2012 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2013 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2020 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2022 if (fc == &t->prev_fc)
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
2027 else if (fc == &t->next_fc)
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** peer: %s!\n",
2038 GNUNET_i2s(GNUNET_PEER_resolve2 (peer)));
2041 if (GNUNET_YES == t->destroy)
2048 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2049 msg.header.size = htons (sizeof (msg));
2050 msg.tid = htonl (t->id.tid);
2051 msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t));
2052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", ntohl (msg.pid));
2053 send_prebuilt_message (&msg.header, peer, t);
2054 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2055 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2061 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2062 * to obtain a local peer -> destination path and interning the peer ids.
2064 * @return Newly allocated and created path
2066 static struct MeshPeerPath *
2067 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2068 unsigned int get_path_length,
2069 const struct GNUNET_PeerIdentity *put_path,
2070 unsigned int put_path_length)
2072 struct MeshPeerPath *p;
2078 GNUNET_PEER_change_rc (myid, 1);
2079 i = get_path_length;
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2081 for (i--; i >= 0; i--)
2083 id = GNUNET_PEER_intern (&get_path[i]);
2084 if (p->length > 0 && id == p->peers[p->length - 1])
2086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2087 GNUNET_PEER_change_rc (id, -1);
2091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2092 GNUNET_i2s (&get_path[i]));
2094 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2095 p->peers[p->length - 1] = id;
2098 i = put_path_length;
2099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2100 for (i--; i >= 0; i--)
2102 id = GNUNET_PEER_intern (&put_path[i]);
2105 /* PUT path went through us, so discard the path up until now and start
2106 * from here to get a much shorter (and loop-free) path.
2111 if (p->length > 0 && id == p->peers[p->length - 1])
2113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2114 GNUNET_PEER_change_rc (id, -1);
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2119 GNUNET_i2s (&put_path[i]));
2121 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2122 p->peers[p->length - 1] = id;
2126 if (get_path_length > 0)
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2128 GNUNET_i2s (&get_path[0]));
2129 if (put_path_length > 0)
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2131 GNUNET_i2s (&put_path[0]));
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2134 for (i = 0; i < p->length; i++)
2136 struct GNUNET_PeerIdentity peer_id;
2138 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2140 GNUNET_i2s (&peer_id));
2148 * Adds a path to the peer_infos of all the peers in the path
2150 * @param p Path to process.
2151 * @param confirmed Whether we know if the path works or not.
2154 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2158 /* TODO: invert and add */
2159 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2160 for (i++; i < p->length; i++)
2162 struct MeshPeer *aux;
2163 struct MeshPeerPath *copy;
2165 aux = peer_get_short (p->peers[i]);
2166 copy = path_duplicate (p);
2167 copy->length = i + 1;
2168 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2174 * Search for a tunnel among the incoming tunnels
2176 * @param tid the local id of the tunnel
2178 * @return tunnel handler, NULL if doesn't exist
2180 static struct MeshTunnel *
2181 tunnel_get_incoming (MESH_TunnelNumber tid)
2183 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2184 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
2189 * Search for a tunnel among the tunnels for a client
2191 * @param c the client whose tunnels to search in
2192 * @param tid the local id of the tunnel
2194 * @return tunnel handler, NULL if doesn't exist
2196 static struct MeshTunnel *
2197 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2199 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2201 GNUNET_break_op (0);
2202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TID %X not a local tid\n", tid);
2205 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2207 return tunnel_get_incoming (tid);
2211 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
2217 * Search for a tunnel by global ID using PEER_ID
2219 * @param pi owner of the tunnel
2220 * @param tid global tunnel number
2222 * @return tunnel handler, NULL if doesn't exist
2224 static struct MeshTunnel *
2225 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2227 struct MESH_TunnelID id;
2228 struct GNUNET_HashCode hash;
2233 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2234 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2239 * Search for a tunnel by global ID using full PeerIdentities
2241 * @param oid owner of the tunnel
2242 * @param tid global tunnel number
2244 * @return tunnel handler, NULL if doesn't exist
2246 static struct MeshTunnel *
2247 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2249 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2254 * Change the tunnel state.
2256 * @param t Tunnel whose ttate to change.
2257 * @param state New state.
2260 tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state)
2262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2263 "Tunnel %s[%X] state was %s\n",
2264 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid,
2265 GNUNET_MESH_DEBUG_S2S (t->state));
2266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2267 "Tunnel %s[%X] state is now %s\n",
2268 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid,
2269 GNUNET_MESH_DEBUG_S2S (state));
2276 * Add a client to a tunnel, initializing all needed data structures.
2278 * @param t Tunnel to which add the client.
2279 * @param c Client which to add to the tunnel.
2282 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2284 if (NULL != t->client)
2290 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
2291 t->local_tid_dest, t,
2292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2298 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
2299 t->local_tid_dest, t,
2300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2310 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
2312 unsigned int own_pos;
2314 for (own_pos = 0; own_pos < p->length; own_pos++)
2316 if (p->peers[own_pos] == myid)
2319 if (own_pos > p->length - 1)
2325 if (own_pos < p->length - 1)
2326 t->next_hop = p->peers[own_pos + 1];
2328 t->next_hop = p->peers[own_pos];
2329 GNUNET_PEER_change_rc (t->next_hop, 1);
2331 t->prev_hop = p->peers[own_pos - 1];
2333 t->prev_hop = p->peers[0];
2334 GNUNET_PEER_change_rc (t->prev_hop, 1);
2336 if (NULL != t->path)
2337 path_destroy (t->path);
2338 t->path = path_duplicate (p);
2341 if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
2342 GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
2343 t->fwd_maintenance_task =
2344 GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2345 &tunnel_fwd_keepalive, t);
2351 * Notifies a tunnel that a connection has broken that affects at least
2352 * some of its peers. Sends a notification towards the root of the tree.
2353 * In case the peer is the owner of the tree, notifies the client that owns
2354 * the tunnel and tries to reconnect.
2356 * @param t Tunnel affected.
2357 * @param p1 Peer that got disconnected from p2.
2358 * @param p2 Peer that got disconnected from p1.
2360 * @return Short ID of the peer disconnected (either p1 or p2).
2361 * 0 if the tunnel remained unaffected.
2363 static GNUNET_PEER_Id
2364 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2367 // if (myid != p1 && myid != p2) FIXME
2372 // if (tree_get_predecessor (t->tree) != 0)
2374 // /* We are the peer still connected, notify owner of the disconnection. */
2375 // struct GNUNET_MESH_PathBroken msg;
2376 // struct GNUNET_PeerIdentity neighbor;
2378 // msg.header.size = htons (sizeof (msg));
2379 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2380 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2381 // msg.tid = htonl (t->id.tid);
2382 // msg.peer1 = my_full_id;
2383 // GNUNET_PEER_resolve (pid, &msg.peer2);
2384 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2385 // send_prebuilt_message (&msg.header, &neighbor, t);
2392 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2394 * @param t Tunnel this is about.
2395 * @param fwd Is for FWD traffic? (ACK dest->owner)
2398 tunnel_send_data_ack (struct MeshTunnel *t, int fwd)
2400 struct GNUNET_MESH_DataACK msg;
2401 struct MeshTunnelReliability *rel;
2402 struct MeshReliableMessage *copy;
2407 rel = fwd ? t->bck_rel : t->fwd_rel;
2408 hop = fwd ? t->prev_hop : t->next_hop;
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "send_data_ack for %u\n",
2413 if (GNUNET_NO == t->reliable)
2415 GNUNET_break_op (0);
2418 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2419 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2420 msg.header.size = htons (sizeof (msg));
2421 msg.tid = htonl (t->id.tid);
2422 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2423 msg.mid = htonl (rel->mid_recv - 1);
2425 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2427 delta = copy->mid - rel->mid_recv;
2430 mask = 0x1LL << delta;
2431 msg.futures |= mask;
2432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2433 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2434 copy->mid, delta, mask, msg.futures);
2436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2438 send_prebuilt_message (&msg.header, hop, t);
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2444 * Send an ACK informing the predecessor about the available buffer space.
2445 * In case there is no predecessor, inform the owning client.
2446 * If buffering is off, send only on behalf of children or self if endpoint.
2447 * If buffering is on, send when sent to children and buffer space is free.
2448 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2449 * the ACK itself goes "back" (towards root).
2451 * @param t Tunnel on which to send the ACK.
2452 * @param type Type of message that triggered the ACK transmission.
2453 * @param fwd Is this FWD ACK? (Going dest->owner)
2456 tunnel_send_ack (struct MeshTunnel *t, uint16_t type, int fwd)
2458 struct MeshTunnelReliability *rel;
2459 struct MeshFlowControl *next_fc;
2460 struct MeshFlowControl *prev_fc;
2461 struct MeshClient *c;
2462 struct MeshClient *o;
2468 rel = fwd ? t->fwd_rel : t->bck_rel;
2469 c = fwd ? t->client : t->owner;
2470 o = fwd ? t->owner : t->client;
2471 next_fc = fwd ? &t->next_fc : &t->prev_fc;
2472 prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2473 hop = fwd ? t->prev_hop : t->next_hop;
2477 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2478 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2481 GNUNET_MESH_DEBUG_M2S (type));
2482 if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c))
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2487 if (GNUNET_YES == t->reliable && NULL != c)
2488 tunnel_send_data_ack (t, fwd);
2490 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2491 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2492 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2493 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2495 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2496 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2497 t->force_ack = GNUNET_YES;
2503 /* Check if we need to transmit the ACK */
2505 prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 &&
2506 GNUNET_NO == t->force_ack)
2508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2510 " last pid recv: %u, last ack sent: %u\n",
2511 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2515 /* Ok, ACK might be necessary, what PID to ACK? */
2516 delta = t->queue_max - next_fc->queue_n;
2517 if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2518 delta_mid = rel->mid_sent - rel->head_sent->mid;
2521 if (0 > delta || (GNUNET_YES == t->reliable &&
2523 (10 < rel->n_sent || 64 <= delta_mid)))
2525 if (NULL != o && delta > 1)
2527 ack = prev_fc->last_pid_recv + delta;
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 " last pid %u, last ack %u, qmax %u, q %u\n",
2531 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2532 t->queue_max, next_fc->queue_n);
2533 if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2539 prev_fc->last_ack_sent = ack;
2541 send_local_ack (t, o, fwd);
2543 send_ack (t, hop, ack);
2545 GNUNET_break (GNUNET_YES == t->destroy);
2546 t->force_ack = GNUNET_NO;
2551 * Modify the mesh message TID from global to local and send to client.
2553 * @param t Tunnel on which to send the message.
2554 * @param msg Message to modify and send.
2555 * @param c Client to send to.
2556 * @param tid Tunnel ID to use (c can be both owner and client).
2559 tunnel_send_client_to_tid (struct MeshTunnel *t,
2560 const struct GNUNET_MESH_Data *msg,
2561 struct MeshClient *c, MESH_TunnelNumber tid)
2563 struct GNUNET_MESH_LocalData *copy;
2564 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2565 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2567 if (size < sizeof (struct GNUNET_MessageHeader))
2569 GNUNET_break_op (0);
2577 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2578 memcpy (©[1], &msg[1], size);
2579 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2580 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2581 copy->tid = htonl (tid);
2582 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2583 ©->header, GNUNET_NO);
2587 * Modify the unicast message TID from global to local and send to client.
2589 * @param t Tunnel on which to send the message.
2590 * @param msg Message to modify and send.
2591 * @param fwd Forward?
2594 tunnel_send_client_data (struct MeshTunnel *t,
2595 const struct GNUNET_MESH_Data *msg,
2599 tunnel_send_client_to_tid (t, msg, t->client, t->local_tid_dest);
2601 tunnel_send_client_to_tid (t, msg, t->owner, t->local_tid);
2606 * Send up to 64 buffered messages to the client for in order delivery.
2608 * @param t Tunnel on which to empty the message buffer.
2609 * @param c Client to send to.
2610 * @param rel Reliability structure to corresponding peer.
2611 * If rel == t->bck_rel, this is FWD data.
2614 tunnel_send_client_buffered_data (struct MeshTunnel *t, struct MeshClient *c,
2615 struct MeshTunnelReliability *rel)
2618 struct MeshReliableMessage *copy;
2619 struct MeshReliableMessage *next;
2621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2622 for (copy = rel->head_recv; NULL != copy; copy = next)
2625 if (copy->mid == rel->mid_recv)
2627 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2630 " have %u! now expecting %u\n",
2631 copy->mid, rel->mid_recv + 1);
2632 tunnel_send_client_data (t, msg, (rel == t->bck_rel));
2634 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2640 " don't have %u, next is %u\n",
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2651 * We have received a message out of order, buffer it until we receive
2652 * the missing one and we can feed the rest to the client.
2654 * @param t Tunnel to add to.
2655 * @param msg Message to buffer.
2656 * @param rel Reliability data to the corresponding direction.
2659 tunnel_add_buffered_data (struct MeshTunnel *t,
2660 const struct GNUNET_MESH_Data *msg,
2661 struct MeshTunnelReliability *rel)
2663 struct MeshReliableMessage *copy;
2664 struct MeshReliableMessage *prev;
2668 size = ntohs (msg->header.size);
2669 mid = ntohl (msg->mid);
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
2672 copy = GNUNET_malloc (sizeof (*copy) + size);
2675 memcpy (©[1], msg, size);
2677 // FIXME do something better than O(n), although n < 64...
2678 // FIXME start from the end (most messages are the latest ones)
2679 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
2682 if (GMC_is_pid_bigger (prev->mid, mid))
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2685 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2691 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
2697 * Destroy a reliable message after it has been acknowledged, either by
2698 * direct mid ACK or bitfield. Updates the appropriate data structures and
2699 * timers and frees all memory.
2701 * @param copy Message that is no longer needed: remote peer got it.
2704 tunnel_free_reliable_message (struct MeshReliableMessage *copy)
2706 struct MeshTunnelReliability *rel;
2707 struct GNUNET_TIME_Relative time;
2710 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2711 rel->expected_delay.rel_value_us *= 7;
2712 rel->expected_delay.rel_value_us += time.rel_value_us;
2713 rel->expected_delay.rel_value_us /= 8;
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
2716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
2717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
2718 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
2720 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
2722 rel->retry_timer = rel->expected_delay;
2723 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2729 * Destroy all reliable messages queued for a tunnel,
2730 * during a tunnel destruction.
2731 * Frees the reliability structure itself.
2733 * @param rel Reliability data for a tunnel.
2736 tunnel_free_reliable_all (struct MeshTunnelReliability *rel)
2738 struct MeshReliableMessage *copy;
2739 struct MeshReliableMessage *next;
2744 for (copy = rel->head_recv; NULL != copy; copy = next)
2747 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2750 for (copy = rel->head_sent; NULL != copy; copy = next)
2753 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2756 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
2757 GNUNET_SCHEDULER_cancel (rel->retry_task);
2763 * Mark future messages as ACK'd.
2765 * @param t Tunnel whose sent buffer to clean.
2766 * @param msg DataACK message with a bitfield of future ACK'd messages.
2767 * @param rel Reliability data.
2770 tunnel_free_sent_reliable (struct MeshTunnel *t,
2771 const struct GNUNET_MESH_DataACK *msg,
2772 struct MeshTunnelReliability *rel)
2774 struct MeshReliableMessage *copy;
2775 struct MeshReliableMessage *next;
2782 bitfield = msg->futures;
2783 mid = ntohl (msg->mid);
2784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2785 "free_sent_reliable %u %llX\n",
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2788 " rel %p, head %p\n",
2789 rel, rel->head_sent);
2790 for (i = 0, copy = rel->head_sent;
2791 i < 64 && NULL != copy && 0 != bitfield;
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2795 " trying bit %u (mid %u)\n",
2798 if (0 == (bitfield & mask))
2801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2802 /* Bit was set, clear the bit from the bitfield */
2805 /* The i-th bit was set. Do we have that copy? */
2806 /* Skip copies with mid < target */
2807 target = mid + i + 1;
2808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
2809 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
2812 /* Did we run out of copies? (previously freed, it's ok) */
2815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2819 /* Did we overshoot the target? (previously freed, it's ok) */
2820 if (GMC_is_pid_bigger (copy->mid, target))
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
2826 /* Now copy->mid == target, free it */
2828 tunnel_free_reliable_message (copy);
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
2836 * We haven't received an ACK after a certain time: restransmit the message.
2838 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2839 * @param tc TaskContext.
2842 tunnel_retransmit_message (void *cls,
2843 const struct GNUNET_SCHEDULER_TaskContext *tc)
2845 struct MeshTunnelReliability *rel = cls;
2846 struct MeshReliableMessage *copy;
2847 struct MeshFlowControl *fc;
2848 struct MeshPeerQueue *q;
2849 struct MeshPeer *pi;
2850 struct MeshTunnel *t;
2851 struct GNUNET_MESH_Data *payload;
2854 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2855 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2859 copy = rel->head_sent;
2866 /* Search the message to be retransmitted in the outgoing queue */
2867 payload = (struct GNUNET_MESH_Data *) ©[1];
2868 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2869 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2870 pi = peer_get_short (hop);
2871 for (q = pi->queue_head; NULL != q; q = q->next)
2873 if (ntohs (payload->header.type) == q->type)
2875 struct GNUNET_MESH_Data *queued_data = q->cls;
2877 if (queued_data->mid == payload->mid)
2882 /* Message not found in the queue */
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
2887 fc->last_ack_sent++;
2888 fc->last_pid_recv++;
2889 payload->pid = htonl (fc->last_pid_recv);
2890 send_prebuilt_message (&payload->header, hop, t);
2891 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %u\n", copy->mid);
2898 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2899 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2900 &tunnel_retransmit_message,
2906 * Send keepalive packets for a tunnel.
2908 * @param t Tunnel to keep alive..
2909 * @param fwd Is this a FWD keepalive? (owner -> dest).
2912 tunnel_keepalive (struct MeshTunnel *t, int fwd)
2914 struct GNUNET_MESH_TunnelKeepAlive *msg;
2915 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
2920 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
2921 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
2922 hop = fwd ? t->next_hop : t->prev_hop;
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2925 "sending %s keepalive for tunnel %d\n",
2926 fwd ? "FWD" : "BCK", t->id.tid);
2928 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
2929 msg->header.size = htons (size);
2930 msg->header.type = htons (type);
2931 msg->oid = *(GNUNET_PEER_resolve2 (t->id.oid));
2932 msg->tid = htonl (t->id.tid);
2933 send_prebuilt_message (&msg->header, hop, t);
2938 * Send create (PATH_CREATE/PATH_ACK) packets for a tunnel.
2940 * @param t Tunnel for which to send the message.
2941 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
2944 tunnel_recreate (struct MeshTunnel *t, int fwd)
2946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2947 "sending path recreate for tunnel %s[%X]\n",
2948 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
2950 send_path_create (t);
2957 * Generic tunnel timer management.
2958 * Depending on the role of the peer in the tunnel will send the
2959 * appropriate message (build or keepalive)
2961 * @param t Tunnel to maintain.
2962 * @param fwd Is FWD?
2965 tunnel_maintain (struct MeshTunnel *t, int fwd)
2969 case MESH_TUNNEL_NEW:
2971 case MESH_TUNNEL_SEARCHING:
2972 /* TODO DHT GET with RO_BART */
2974 case MESH_TUNNEL_WAITING:
2975 tunnel_recreate (t, fwd);
2977 case MESH_TUNNEL_READY:
2978 tunnel_keepalive (t, fwd);
2987 tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2989 struct MeshTunnel *t = cls;
2991 t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2992 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
2996 tunnel_maintain (t, GNUNET_YES);
2997 t->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2998 &tunnel_fwd_keepalive,
3004 tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3006 struct MeshTunnel *t = cls;
3008 t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3009 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3013 tunnel_keepalive (t, GNUNET_NO);
3014 t->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
3015 &tunnel_bck_keepalive,
3021 * Send a message to all peers and clients in this tunnel that the tunnel
3022 * is no longer valid. If some peer or client should not receive the message,
3023 * should be zero'ed out before calling this function.
3025 * @param t The tunnel whose peers and clients to notify.
3028 tunnel_send_destroy (struct MeshTunnel *t)
3030 struct GNUNET_MESH_TunnelDestroy msg;
3031 struct GNUNET_PeerIdentity id;
3033 msg.header.size = htons (sizeof (msg));
3034 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3035 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3036 msg.tid = htonl (t->id.tid);
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3038 " sending tunnel destroy for tunnel: %s [%X]\n",
3039 GNUNET_i2s (&msg.oid), t->id.tid);
3041 if (NULL == t->client && 0 != t->next_hop)
3043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
3044 GNUNET_PEER_resolve (t->next_hop, &id);
3045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3046 " sending forward to %s\n",
3048 send_prebuilt_message (&msg.header, t->next_hop, t);
3050 if (NULL == t->owner && 0 != t->prev_hop)
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
3053 GNUNET_PEER_resolve (t->prev_hop, &id);
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3055 " sending back to %s\n",
3057 send_prebuilt_message (&msg.header, t->prev_hop, t);
3059 if (NULL != t->owner)
3061 send_local_tunnel_destroy (t, GNUNET_NO);
3063 if (NULL != t->client)
3065 send_local_tunnel_destroy (t, GNUNET_YES);
3070 tunnel_destroy (struct MeshTunnel *t)
3072 struct MeshClient *c;
3073 struct GNUNET_HashCode hash;
3082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3083 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3087 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3088 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3096 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
3108 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
3109 t->local_tid_dest, t))
3115 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
3116 t->local_tid_dest, t))
3123 if(GNUNET_YES == t->reliable)
3125 tunnel_free_reliable_all (t->fwd_rel);
3126 tunnel_free_reliable_all (t->bck_rel);
3128 if (0 != t->prev_hop)
3130 peer_cancel_queues (t->prev_hop, t);
3131 GNUNET_PEER_change_rc (t->prev_hop, -1);
3133 if (0 != t->next_hop)
3135 peer_cancel_queues (t->next_hop, t);
3136 GNUNET_PEER_change_rc (t->next_hop, -1);
3138 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task)
3140 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3141 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3143 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task)
3145 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3146 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3149 peer_remove_tunnel (peer_get_short (t->dest), t);
3152 if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
3153 GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
3154 if (GNUNET_SCHEDULER_NO_TASK != t->bck_maintenance_task)
3155 GNUNET_SCHEDULER_cancel (t->bck_maintenance_task);
3158 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3159 path_destroy (t->path);
3165 * Tunnel is empty: destroy it.
3167 * Notifies all participants (peers, cleints) about the destruction.
3169 * @param t Tunnel to destroy.
3172 tunnel_destroy_empty (struct MeshTunnel *t)
3176 struct GNUNET_PeerIdentity id;
3178 GNUNET_PEER_resolve (t->id.oid, &id);
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3180 "executing destruction of empty tunnel %s [%X]\n",
3181 GNUNET_i2s (&id), t->id.tid);
3185 if (GNUNET_NO == t->destroy)
3186 tunnel_send_destroy (t);
3187 if (0 == t->pending_messages)
3190 t->destroy = GNUNET_YES;
3194 * Initialize a Flow Control structure to the initial state.
3196 * @param fc Flow Control structure to initialize.
3199 fc_init (struct MeshFlowControl *fc)
3201 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3202 fc->last_pid_recv = (uint32_t) -1;
3203 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
3204 fc->last_ack_recv = (uint32_t) -1;
3205 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3206 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3211 * Create a new tunnel
3213 * @param owner Who is the owner of the tunnel (short ID).
3214 * @param tid Tunnel Number of the tunnel.
3215 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3216 * @param local Tunnel Number for the tunnel, for the client point of view.
3218 * @return A new initialized tunnel. NULL on error.
3220 static struct MeshTunnel *
3221 tunnel_new (GNUNET_PEER_Id owner,
3222 MESH_TunnelNumber tid,
3223 struct MeshClient *client,
3224 MESH_TunnelNumber local)
3226 struct MeshTunnel *t;
3227 struct GNUNET_HashCode hash;
3229 if (n_tunnels >= max_tunnels && NULL == client)
3232 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3235 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
3237 fc_init (&t->next_fc);
3238 fc_init (&t->prev_fc);
3241 t->local_tid = local;
3243 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3245 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3247 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3248 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3255 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3263 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
3265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3269 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3279 * Set options in a tunnel, extracted from a bit flag field
3281 * @param t Tunnel to set options to.
3282 * @param options Bit array in host byte order.
3285 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
3287 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
3288 GNUNET_YES : GNUNET_NO;
3289 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
3290 GNUNET_YES : GNUNET_NO;
3295 * Iterator for deleting each tunnel whose client endpoint disconnected.
3297 * @param cls Closure (client that has disconnected).
3298 * @param key The local tunnel id (used to access the hashmap).
3299 * @param value The value stored at the key (tunnel to destroy).
3301 * @return GNUNET_OK, keep iterating.
3304 tunnel_destroy_iterator (void *cls,
3308 struct MeshTunnel *t = value;
3309 struct MeshClient *c = cls;
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3312 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
3313 t->local_tid, t->local_tid_dest, c->id);
3314 client_delete_tunnel (c, t);
3317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
3319 if (0 != t->next_hop) { /* destroy could come before a path is used */
3320 GNUNET_PEER_change_rc (t->next_hop, -1);
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
3328 if (0 != t->prev_hop) { /* destroy could come before a path is used */
3329 GNUNET_PEER_change_rc (t->prev_hop, -1);
3334 tunnel_destroy_empty (t);
3341 * remove client's ports from the global hashmap on diconnect.
3343 * @param cls Closure (unused).
3345 * @param value ThClient structure.
3347 * @return GNUNET_OK, keep iterating.
3350 client_release_ports (void *cls,
3356 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
3357 if (GNUNET_YES != res)
3360 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3361 "Port %u by client %p was not registered.\n",
3368 * Timeout function due to lack of keepalive/traffic from the owner.
3369 * Destroys tunnel if called.
3371 * @param cls Closure (tunnel to destroy).
3372 * @param tc TaskContext
3375 tunnel_fwd_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3377 struct MeshTunnel *t = cls;
3379 t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3380 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3383 "Tunnel %s [%X] FWD timed out. Destroying.\n",
3384 GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3385 if (NULL != t->client)
3386 send_local_tunnel_destroy (t, GNUNET_YES);
3387 tunnel_destroy (t); /* Do not notify other */
3392 * Timeout function due to lack of keepalive/traffic from the destination.
3393 * Destroys tunnel if called.
3395 * @param cls Closure (tunnel to destroy).
3396 * @param tc TaskContext
3399 tunnel_bck_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3401 struct MeshTunnel *t = cls;
3403 t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3404 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3407 "Tunnel %s [%X] BCK timed out. Destroying.\n",
3408 GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3409 if (NULL != t->owner)
3410 send_local_tunnel_destroy (t, GNUNET_NO);
3411 tunnel_destroy (t); /* Do not notify other */
3416 * Resets the tunnel timeout task, some other message has done the task's job.
3417 * - For the first peer on the direction this means to send
3418 * a keepalive or a path confirmation message (either create or ACK).
3419 * - For all other peers, this means to destroy the tunnel,
3420 * due to lack of activity.
3421 * Starts the tiemout if no timeout was running (tunnel just created).
3423 * @param t Tunnel whose timeout to reset.
3424 * @param fwd Is this forward?
3426 * TODO use heap to improve efficiency of scheduler.
3429 tunnel_reset_timeout (struct MeshTunnel *t, int fwd)
3431 GNUNET_SCHEDULER_TaskIdentifier *ti;
3432 GNUNET_SCHEDULER_Task f;
3433 struct MeshClient *c;
3435 ti = fwd ? &t->fwd_maintenance_task : &t->bck_maintenance_task;
3436 c = fwd ? t->owner : t->client;
3438 if (GNUNET_SCHEDULER_NO_TASK != *ti)
3439 GNUNET_SCHEDULER_cancel (*ti);
3443 f = fwd ? &tunnel_fwd_keepalive : &tunnel_bck_keepalive;
3444 *ti = GNUNET_SCHEDULER_add_delayed (refresh_path_time, f, t);
3448 f = fwd ? &tunnel_fwd_timeout : &tunnel_bck_timeout;
3449 *ti = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3450 (refresh_path_time, 4),
3456 /******************************************************************************/
3457 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3458 /******************************************************************************/
3461 * Free a transmission that was already queued with all resources
3462 * associated to the request.
3464 * @param queue Queue handler to cancel.
3465 * @param clear_cls Is it necessary to free associated cls?
3468 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3470 struct MeshFlowControl *fc;
3472 if (GNUNET_YES == clear_cls)
3474 switch (queue->type)
3476 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3477 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3478 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3480 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3481 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3482 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3483 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3484 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3485 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3486 case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
3487 case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3489 " prebuilt message\n");
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3492 GNUNET_MESH_DEBUG_M2S (queue->type));
3494 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3500 " type %s unknown!\n",
3501 GNUNET_MESH_DEBUG_M2S (queue->type));
3503 GNUNET_free_non_null (queue->cls);
3505 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3506 queue->peer->queue_tail,
3509 /* Delete from appropriate fc in the tunnel */
3510 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3511 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3513 if (queue->peer->id == queue->tunnel->prev_hop)
3514 fc = &queue->tunnel->prev_fc;
3515 else if (queue->peer->id == queue->tunnel->next_hop)
3516 fc = &queue->tunnel->next_fc;
3520 GNUNET_free (queue);
3525 GNUNET_free (queue);
3530 * @brief Get the next transmittable message from the queue.
3532 * This will be the head, except in the case of being a data packet
3533 * not allowed by the destination peer.
3535 * @param peer Destination peer.
3537 * @return The next viable MeshPeerQueue element to send to that peer.
3538 * NULL when there are no transmittable messages.
3540 struct MeshPeerQueue *
3541 queue_get_next (const struct MeshPeer *peer)
3543 struct MeshPeerQueue *q;
3545 struct GNUNET_MESH_Data *dmsg;
3546 struct MeshTunnel* t;
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3551 for (q = peer->queue_head; NULL != q; q = q->next)
3554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3556 GNUNET_MESH_DEBUG_M2S (q->type));
3557 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3558 pid = ntohl (dmsg->pid);
3561 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3562 ack = t->next_fc.last_ack_recv;
3564 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3565 ack = t->prev_fc.last_ack_recv;
3568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3573 "* ACK: %u, PID: %u, MID: %u\n",
3574 ack, pid, ntohl (dmsg->mid));
3575 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3588 "* nothing found\n");
3594 queue_send (void *cls, size_t size, void *buf)
3596 struct MeshPeer *peer = cls;
3597 struct GNUNET_MessageHeader *msg;
3598 struct MeshPeerQueue *queue;
3599 struct MeshTunnel *t;
3600 struct GNUNET_PeerIdentity dst_id;
3601 struct MeshFlowControl *fc;
3606 peer->core_transmit = NULL;
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3610 if (NULL == buf || 0 == size)
3612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
3615 queue = queue_get_next (peer);
3617 /* Queue has no internal mesh traffic nor sendable payload */
3620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3621 if (NULL == peer->queue_head)
3622 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3628 GNUNET_PEER_resolve (peer->id, &dst_id);
3629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3631 GNUNET_i2s (&dst_id));
3632 /* Check if buffer size is enough for the message */
3633 if (queue->size > size)
3635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3636 "* not enough room, reissue\n");
3637 peer->core_transmit =
3638 GNUNET_CORE_notify_transmit_ready (core_handle,
3641 GNUNET_TIME_UNIT_FOREVER_REL,
3648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3651 GNUNET_assert (0 < t->pending_messages);
3652 t->pending_messages--;
3656 switch (queue->type)
3659 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3660 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3661 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3662 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3663 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3664 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3665 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3666 case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
3667 case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
3668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3670 GNUNET_MESH_DEBUG_M2S (queue->type));
3672 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3673 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3674 data_size = send_core_data_raw (queue->cls, size, buf);
3675 msg = (struct GNUNET_MessageHeader *) buf;
3676 type = ntohs (msg->type);
3678 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3680 data_size = send_core_path_create (queue->cls, size, buf);
3682 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3684 data_size = send_core_path_ack (queue->cls, size, buf);
3688 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3689 "* type unknown: %u\n",
3694 if (0 < drop_percent &&
3695 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3698 "Dropping message of type %s\n",
3699 GNUNET_MESH_DEBUG_M2S(queue->type));
3702 /* Free queue, but cls was freed by send_core_* */
3703 queue_destroy (queue, GNUNET_NO);
3705 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3706 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3710 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3711 t->next_fc.last_pid_sent = pid;
3712 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
3713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3715 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3717 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3718 t->prev_fc.last_pid_sent = pid;
3719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3721 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3722 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
3728 /* If more data in queue, send next */
3729 queue = queue_get_next (peer);
3732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3733 if (NULL == peer->core_transmit) {
3734 peer->core_transmit =
3735 GNUNET_CORE_notify_transmit_ready(core_handle,
3738 GNUNET_TIME_UNIT_FOREVER_REL,
3746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3747 "* tmt rdy called somewhere else\n");
3750 if (peer->id == t->next_hop)
3752 else if (peer->id == t->prev_hop)
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n",
3758 peer->id, t->next_hop, t->prev_hop);
3761 if (NULL != peer->queue_head)
3763 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
3765 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3766 "* %s starting poll timeout\n",
3767 GNUNET_i2s (&my_full_id));
3768 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3774 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3776 GNUNET_SCHEDULER_cancel (fc->poll_task);
3777 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3780 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3791 * @brief Queue and pass message to core when possible.
3793 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3794 * accounts for it. In case the queue is full, the message is dropped and
3797 * Otherwise, message is treated as internal and allowed to go regardless of
3800 * @param cls Closure (@c type dependant). It will be used by queue_send to
3801 * build the message to be sent if not already prebuilt.
3802 * @param type Type of the message, 0 for a raw message.
3803 * @param size Size of the message.
3804 * @param dst Neighbor to send message to.
3805 * @param t Tunnel this message belongs to.
3808 queue_add (void *cls, uint16_t type, size_t size,
3809 struct MeshPeer *dst, struct MeshTunnel *t)
3811 struct MeshPeerQueue *queue;
3812 struct MeshFlowControl *fc;
3816 priority = GNUNET_NO;
3817 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3821 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3827 if (fc->queue_n >= t->queue_max)
3829 /* If this isn't a retransmission, drop the message */
3830 if (GNUNET_NO == t->reliable ||
3831 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3832 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3834 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3838 "queue full: %u/%u\n",
3839 fc->queue_n, t->queue_max);
3840 return; /* Drop this message */
3842 priority = GNUNET_YES;
3845 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
3846 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3847 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3851 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3857 if (GNUNET_YES == priority)
3859 struct GNUNET_MESH_Data *d;
3863 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3864 d = (struct GNUNET_MESH_Data *) queue->cls;
3866 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3868 if (queue->type != type)
3870 d = (struct GNUNET_MESH_Data *) queue->cls;
3877 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3879 if (NULL == dst->core_transmit)
3881 dst->core_transmit =
3882 GNUNET_CORE_notify_transmit_ready (core_handle,
3885 GNUNET_TIME_UNIT_FOREVER_REL,
3886 GNUNET_PEER_resolve2 (dst->id),
3891 t->pending_messages++;
3895 /******************************************************************************/
3896 /******************** MESH NETWORK HANDLERS **************************/
3897 /******************************************************************************/
3901 * Core handler for path creation
3903 * @param cls closure
3904 * @param message message
3905 * @param peer peer identity this notification is about
3907 * @return GNUNET_OK to keep the connection open,
3908 * GNUNET_SYSERR to close it (signal serious error)
3911 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3912 const struct GNUNET_MessageHeader *message)
3914 unsigned int own_pos;
3917 MESH_TunnelNumber tid;
3918 struct GNUNET_MESH_CreateTunnel *msg;
3919 struct GNUNET_PeerIdentity *pi;
3920 struct MeshPeerPath *path;
3921 struct MeshPeer *dest_peer_info;
3922 struct MeshPeer *orig_peer_info;
3923 struct MeshTunnel *t;
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3926 "Received a path create msg [%s]\n",
3927 GNUNET_i2s (&my_full_id));
3928 size = ntohs (message->size);
3929 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3931 GNUNET_break_op (0);
3935 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3936 if (size % sizeof (struct GNUNET_PeerIdentity))
3938 GNUNET_break_op (0);
3941 size /= sizeof (struct GNUNET_PeerIdentity);
3944 GNUNET_break_op (0);
3947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3948 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3950 tid = ntohl (msg->tid);
3951 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3953 " path is for tunnel %s[%X]:%u.\n",
3954 GNUNET_i2s (pi), tid, ntohl (msg->port));
3955 t = tunnel_get (pi, tid);
3956 if (NULL == t) /* might be a local tunnel */
3960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3961 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3967 t->port = ntohl (msg->port);
3968 opt = ntohl (msg->opt);
3969 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3971 t->nobuffer = GNUNET_YES;
3974 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3976 t->reliable = GNUNET_YES;
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3980 tunnel_reset_timeout (t, GNUNET_YES);
3981 tunnel_change_state (t, MESH_TUNNEL_WAITING);
3983 GNUNET_CONTAINER_multipeermap_get (peers, &pi[size - 1]);
3984 if (NULL == dest_peer_info)
3986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3987 " Creating PeerInfo for destination.\n");
3988 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeer));
3989 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3990 GNUNET_CONTAINER_multipeermap_put (peers, &pi[size - 1],
3992 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3994 orig_peer_info = GNUNET_CONTAINER_multipeermap_get (peers, pi);
3995 if (NULL == orig_peer_info)
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3998 " Creating PeerInfo for origin.\n");
3999 orig_peer_info = GNUNET_new (struct MeshPeer);
4000 orig_peer_info->id = GNUNET_PEER_intern (pi);
4001 GNUNET_CONTAINER_multipeermap_put (peers, pi, orig_peer_info,
4002 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4005 path = path_new (size);
4007 for (i = 0; i < size; i++)
4009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4010 GNUNET_i2s (&pi[i]));
4011 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4012 if (path->peers[i] == myid)
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4016 if (own_pos == 0 && path->peers[own_pos] != myid)
4018 /* create path: self not found in path through self */
4019 GNUNET_break_op (0);
4020 path_destroy (path);
4024 path_add_to_peers (path, GNUNET_NO);
4025 tunnel_use_path (t, path);
4027 peer_add_tunnel (dest_peer_info, t);
4029 if (own_pos == size - 1)
4031 struct MeshClient *c;
4033 /* Find target client */
4034 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
4037 /* TODO send reject */
4041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4042 peer_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
4043 /* This can be a retransmission due to a lost PATH ACK.
4044 * Check if we already have a destination client for the tunnel. */
4047 /* Assign local tid */
4048 while (NULL != tunnel_get_incoming (next_local_tid))
4049 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4050 t->local_tid_dest = next_local_tid++;
4051 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4053 if (GNUNET_YES == t->reliable)
4055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4056 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4058 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
4061 tunnel_add_client (t, c);
4062 send_local_tunnel_create (t);
4065 /* Eliminate tunnel when origin dies */
4066 tunnel_reset_timeout (t, GNUNET_YES);
4067 /* Keep tunnel alive in direction dest->owner*/
4068 tunnel_reset_timeout (t, GNUNET_NO);
4072 struct MeshPeerPath *path2;
4074 t->next_hop = path->peers[own_pos + 1];
4075 GNUNET_PEER_change_rc(t->next_hop, 1);
4077 /* It's for somebody else! Retransmit. */
4078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4079 path2 = path_duplicate (path);
4080 peer_add_path (dest_peer_info, path2, GNUNET_NO);
4081 peer_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4082 send_path_create (t);
4090 * Core handler for path ACKs
4092 * @param cls closure
4093 * @param message message
4094 * @param peer peer identity this notification is about
4096 * @return GNUNET_OK to keep the connection open,
4097 * GNUNET_SYSERR to close it (signal serious error)
4100 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4101 const struct GNUNET_MessageHeader *message)
4103 struct GNUNET_MESH_PathACK *msg;
4104 struct MeshPeer *peer_info;
4105 struct MeshPeerPath *p;
4106 struct MeshTunnel *t;
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4109 GNUNET_i2s (&my_full_id));
4110 msg = (struct GNUNET_MESH_PathACK *) message;
4111 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4114 /* TODO notify that we don't know the tunnel */
4115 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4117 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4121 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4123 peer_info = peer_get (&msg->peer_id);
4124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4125 GNUNET_i2s (&msg->peer_id));
4126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4129 /* Add path to peers? */
4133 path_add_to_peers (p, GNUNET_YES);
4139 tunnel_change_state (t, MESH_TUNNEL_READY);
4140 tunnel_reset_timeout (t, GNUNET_NO);
4141 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
4142 t->prev_fc.last_ack_sent = ntohl (msg->ack);
4144 /* Message for us? */
4145 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4148 if (NULL == t->owner)
4150 GNUNET_break_op (0);
4153 if (NULL != peer_info->dhtget)
4155 GNUNET_DHT_get_stop (peer_info->dhtget);
4156 peer_info->dhtget = NULL;
4158 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES);
4159 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO);
4163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4164 " not for us, retransmitting...\n");
4165 send_prebuilt_message (message, t->prev_hop, t);
4171 * Core handler for notifications of broken paths
4173 * @param cls closure
4174 * @param message message
4175 * @param peer peer identity this notification is about
4177 * @return GNUNET_OK to keep the connection open,
4178 * GNUNET_SYSERR to close it (signal serious error)
4181 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4182 const struct GNUNET_MessageHeader *message)
4184 struct GNUNET_MESH_PathBroken *msg;
4185 struct MeshTunnel *t;
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4188 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4189 msg = (struct GNUNET_MESH_PathBroken *) message;
4190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4191 GNUNET_i2s (&msg->peer1));
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4193 GNUNET_i2s (&msg->peer2));
4194 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4197 GNUNET_break_op (0);
4200 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4201 GNUNET_PEER_search (&msg->peer2));
4208 * Core handler for tunnel destruction
4210 * @param cls closure
4211 * @param message message
4212 * @param peer peer identity this notification is about
4214 * @return GNUNET_OK to keep the connection open,
4215 * GNUNET_SYSERR to close it (signal serious error)
4218 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4219 const struct GNUNET_MessageHeader *message)
4221 struct GNUNET_MESH_TunnelDestroy *msg;
4222 struct MeshTunnel *t;
4224 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4226 "Got a TUNNEL DESTROY packet from %s\n",
4228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4229 " for tunnel %s [%u]\n",
4230 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4231 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4234 /* Probably already got the message from another path,
4235 * destroyed the tunnel and retransmitted to children.
4238 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4242 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4245 t->local_tid, t->local_tid_dest);
4247 if (GNUNET_PEER_search (peer) == t->prev_hop)
4249 // TODO check owner's signature
4250 // TODO add owner's signatue to tunnel for retransmission
4251 peer_cancel_queues (t->prev_hop, t);
4252 GNUNET_PEER_change_rc (t->prev_hop, -1);
4255 else if (GNUNET_PEER_search (peer) == t->next_hop)
4257 // TODO check dest's signature
4258 // TODO add dest's signatue to tunnel for retransmission
4259 peer_cancel_queues (t->next_hop, t);
4260 GNUNET_PEER_change_rc (t->next_hop, -1);
4265 GNUNET_break_op (0);
4266 // TODO check both owner AND destination's signature to see which matches
4267 // TODO restransmit in appropriate direction
4270 tunnel_destroy_empty (t);
4272 // TODO: add timeout to destroy the tunnel anyway
4278 * Generic handler for mesh network payload traffic.
4280 * @param peer Peer identity this notification is about.
4281 * @param message Data message.
4282 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4284 * @return GNUNET_OK to keep the connection open,
4285 * GNUNET_SYSERR to close it (signal serious error)
4288 handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
4289 const struct GNUNET_MessageHeader *message,
4292 struct GNUNET_MESH_Data *msg;
4293 struct MeshFlowControl *fc;
4294 struct MeshTunnelReliability *rel;
4295 struct MeshTunnel *t;
4296 struct MeshClient *c;
4304 size = ntohs (message->size);
4306 sizeof (struct GNUNET_MESH_Data) +
4307 sizeof (struct GNUNET_MessageHeader))
4312 type =ntohs (message->type);
4313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
4314 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4315 msg = (struct GNUNET_MESH_Data *) message;
4316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4317 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4319 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4322 /* TODO notify back: we don't know this tunnel */
4323 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING tunnel unknown\n");
4328 /* Initialize FWD/BCK data */
4329 pid = ntohl (msg->pid);
4330 fc = fwd ? &t->prev_fc : &t->next_fc;
4331 c = fwd ? t->client : t->owner;
4332 rel = fwd ? t->bck_rel : t->fwd_rel;
4333 hop = fwd ? t->next_hop : t->prev_hop;
4334 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
4336 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
4337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4338 "WARNING Received PID %u, (prev %u), ACK %u\n",
4339 pid, fc->last_pid_recv, fc->last_ack_sent);
4343 tunnel_change_state (t, MESH_TUNNEL_READY);
4344 tunnel_reset_timeout (t, fwd);
4347 /* TODO signature verification */
4348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
4349 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4350 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
4354 mid = ntohl (msg->mid);
4355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4356 " pid %u (mid %u) not seen yet\n", pid, mid);
4357 fc->last_pid_recv = pid;
4359 if (GNUNET_NO == t->reliable ||
4360 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4361 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4364 "!!! RECV %u\n", ntohl (msg->mid));
4365 if (GNUNET_YES == t->reliable)
4367 /* Is this the exact next expected messasge? */
4368 if (mid == rel->mid_recv)
4370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4372 tunnel_send_client_data (t, msg, fwd);
4373 tunnel_send_client_buffered_data (t, c, rel);
4377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4378 tunnel_add_buffered_data (t, msg, rel);
4381 else /* Tunnel unreliable, send to clients directly */
4383 tunnel_send_client_data (t, msg, fwd);
4388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4389 " MID %u not expected (%u - %u), dropping!\n",
4390 ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
4395 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4397 " Pid %u not expected (%u+), dropping!\n",
4398 pid, fc->last_pid_recv + 1);
4400 tunnel_send_ack (t, type, fwd);
4403 fc->last_pid_recv = pid;
4406 GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO);
4407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n",
4408 GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid));
4409 return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */
4411 ttl = ntohl (msg->ttl);
4412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4415 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4417 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4424 send_prebuilt_message (message, hop, t);
4425 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4432 * Core handler for mesh network traffic going from the origin to a peer
4434 * @param cls Closure (unused).
4435 * @param message Message received.
4436 * @param peer Peer who sent the message.
4438 * @return GNUNET_OK to keep the connection open,
4439 * GNUNET_SYSERR to close it (signal serious error)
4442 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4443 const struct GNUNET_MessageHeader *message)
4445 return handle_mesh_data (peer, message, GNUNET_YES);
4449 * Core handler for mesh network traffic towards the owner of a tunnel.
4451 * @param cls Closure (unused).
4452 * @param message Message received.
4453 * @param peer Peer who sent the message.
4455 * @return GNUNET_OK to keep the connection open,
4456 * GNUNET_SYSERR to close it (signal serious error)
4459 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4460 const struct GNUNET_MessageHeader *message)
4462 return handle_mesh_data (peer, message, GNUNET_NO);
4467 * Core handler for mesh network traffic end-to-end ACKs.
4469 * @param cls Closure.
4470 * @param message Message.
4471 * @param peer Peer identity this notification is about.
4473 * @return GNUNET_OK to keep the connection open,
4474 * GNUNET_SYSERR to close it (signal serious error)
4477 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4478 const struct GNUNET_MessageHeader *message)
4480 struct GNUNET_MESH_DataACK *msg;
4481 struct MeshTunnelReliability *rel;
4482 struct MeshReliableMessage *copy;
4483 struct MeshReliableMessage *next;
4484 struct MeshTunnel *t;
4490 type = ntohs (message->type);
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4492 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4493 msg = (struct GNUNET_MESH_DataACK *) message;
4495 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4498 /* TODO notify that we dont know this tunnel (whom)? */
4499 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4502 ack = ntohl (msg->mid);
4503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4505 /* Is this a forward or backward ACK? */
4506 id = GNUNET_PEER_search (peer);
4507 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4510 if (NULL == t->owner)
4512 send_prebuilt_message (message, t->prev_hop, t);
4517 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4520 if (NULL == t->client)
4522 send_prebuilt_message (message, t->next_hop, t);
4529 GNUNET_break_op (0);
4533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4534 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4536 if (GMC_is_pid_bigger (copy->mid, ack))
4538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4539 tunnel_free_sent_reliable (t, msg, rel);
4543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4545 tunnel_free_reliable_message (copy);
4547 /* Once buffers have been free'd, send ACK */
4548 tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4550 /* If some message was free'd, update the retransmission delay*/
4551 if (GNUNET_YES == work)
4553 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4555 GNUNET_SCHEDULER_cancel (rel->retry_task);
4556 if (NULL == rel->head_sent)
4558 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4562 struct GNUNET_TIME_Absolute new_target;
4563 struct GNUNET_TIME_Relative delay;
4565 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4566 MESH_RETRANSMIT_MARGIN);
4567 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4569 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4571 GNUNET_SCHEDULER_add_delayed (delay,
4572 &tunnel_retransmit_message,
4583 * Core handler for mesh network traffic point-to-point acks.
4585 * @param cls closure
4586 * @param message message
4587 * @param peer peer identity this notification is about
4589 * @return GNUNET_OK to keep the connection open,
4590 * GNUNET_SYSERR to close it (signal serious error)
4593 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4594 const struct GNUNET_MessageHeader *message)
4596 struct GNUNET_MESH_ACK *msg;
4597 struct MeshTunnel *t;
4598 struct MeshFlowControl *fc;
4602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4604 msg = (struct GNUNET_MESH_ACK *) message;
4606 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4610 /* TODO notify that we dont know this tunnel (whom)? */
4611 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4614 ack = ntohl (msg->pid);
4615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4617 /* Is this a forward or backward ACK? */
4618 id = GNUNET_PEER_search (peer);
4619 if (t->next_hop == id)
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4624 else if (t->prev_hop == id)
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4631 GNUNET_break_op (0);
4635 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4636 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4638 GNUNET_SCHEDULER_cancel (fc->poll_task);
4639 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4640 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4643 fc->last_ack_recv = ack;
4644 peer_unlock_queue (id);
4645 tunnel_change_state (t, MESH_TUNNEL_READY);
4647 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
4654 * Core handler for mesh network traffic point-to-point ack polls.
4656 * @param cls closure
4657 * @param message message
4658 * @param peer peer identity this notification is about
4660 * @return GNUNET_OK to keep the connection open,
4661 * GNUNET_SYSERR to close it (signal serious error)
4664 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4665 const struct GNUNET_MessageHeader *message)
4667 struct GNUNET_MESH_Poll *msg;
4668 struct MeshTunnel *t;
4669 struct MeshFlowControl *fc;
4674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
4677 msg = (struct GNUNET_MESH_Poll *) message;
4679 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4683 /* TODO notify that we dont know this tunnel (whom)? */
4684 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4685 GNUNET_break_op (0);
4689 /* Is this a forward or backward ACK? */
4690 id = GNUNET_PEER_search (peer);
4691 pid = ntohl (msg->pid);
4692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4693 if (t->next_hop == id)
4695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4697 old = fc->last_pid_recv;
4699 else if (t->prev_hop == id)
4701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4703 old = fc->last_pid_recv;
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", fc->last_pid_recv);
4712 fc->last_pid_recv = pid;
4713 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id);
4715 if (GNUNET_YES == t->reliable)
4716 fc->last_pid_recv = old;
4723 * Core handler for mesh keepalives.
4725 * @param cls closure
4726 * @param message message
4727 * @param peer peer identity this notification is about
4728 * @return GNUNET_OK to keep the connection open,
4729 * GNUNET_SYSERR to close it (signal serious error)
4731 * TODO: Check who we got this from, to validate route.
4734 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4735 const struct GNUNET_MessageHeader *message)
4737 struct GNUNET_MESH_TunnelKeepAlive *msg;
4738 struct MeshTunnel *t;
4739 struct MeshClient *c;
4743 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4747 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4750 /* TODO notify that we dont know that tunnel */
4751 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4756 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
4757 GNUNET_YES : GNUNET_NO;
4758 c = fwd ? t->client : t->owner;
4759 hop = fwd ? t->next_hop : t->prev_hop;
4762 tunnel_change_state (t, MESH_TUNNEL_READY);
4763 tunnel_reset_timeout (t, fwd);
4764 if (NULL != c || 0 == hop || myid == hop)
4767 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4768 send_prebuilt_message (message, hop, t);
4775 * Functions to handle messages from core
4777 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4778 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4779 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4780 sizeof (struct GNUNET_MESH_PathBroken)},
4781 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4782 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4783 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4784 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4785 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4786 sizeof (struct GNUNET_MESH_DataACK)},
4787 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4788 sizeof (struct GNUNET_MESH_DataACK)},
4789 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
4790 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4791 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
4792 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4793 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4794 sizeof (struct GNUNET_MESH_ACK)},
4795 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4796 sizeof (struct GNUNET_MESH_Poll)},
4797 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4798 sizeof (struct GNUNET_MESH_PathACK)},
4804 * Function to process paths received for a new peer addition. The recorded
4805 * paths form the initial tunnel, which can be optimized later.
4806 * Called on each result obtained for the DHT search.
4808 * @param cls closure
4809 * @param exp when will this value expire
4810 * @param key key of the result
4811 * @param get_path path of the get request
4812 * @param get_path_length lenght of get_path
4813 * @param put_path path of the put request
4814 * @param put_path_length length of the put_path
4815 * @param type type of the result
4816 * @param size number of bytes in data
4817 * @param data pointer to the result data
4820 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4821 const struct GNUNET_HashCode * key,
4822 const struct GNUNET_PeerIdentity *get_path,
4823 unsigned int get_path_length,
4824 const struct GNUNET_PeerIdentity *put_path,
4825 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4826 size_t size, const void *data)
4828 struct MeshPeer *peer = cls;
4829 struct MeshPeerPath *p;
4830 struct GNUNET_PeerIdentity pi;
4833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4834 GNUNET_PEER_resolve (peer->id, &pi);
4835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4837 p = path_build_from_dht (get_path, get_path_length,
4838 put_path, put_path_length);
4839 path_add_to_peers (p, GNUNET_NO);
4841 for (i = 0; i < peer->ntunnels; i++)
4843 struct GNUNET_PeerIdentity id;
4845 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4847 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4848 peer->tunnels[i]->local_tid,
4849 peer->tunnels[i]->local_tid_dest);
4850 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4853 peer_connect (peer, peer->tunnels[i]);
4861 /******************************************************************************/
4862 /********************* MESH LOCAL HANDLES **************************/
4863 /******************************************************************************/
4867 * Handler for client connection.
4869 * @param cls Closure (unused).
4870 * @param client Client handler.
4873 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4875 struct MeshClient *c;
4879 c = GNUNET_malloc (sizeof (struct MeshClient));
4881 GNUNET_SERVER_client_keep (client);
4882 GNUNET_SERVER_client_set_user_context (client, c);
4883 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4888 * Handler for client disconnection
4890 * @param cls closure
4891 * @param client identification of the client; NULL
4892 * for the last call when the server is destroyed
4895 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4897 struct MeshClient *c;
4899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4906 c = client_get (client);
4909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
4911 GNUNET_SERVER_client_drop (c->handle);
4912 c->shutting_down = GNUNET_YES;
4913 if (NULL != c->own_tunnels)
4915 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4916 &tunnel_destroy_iterator, c);
4917 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4920 if (NULL != c->incoming_tunnels)
4922 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4923 &tunnel_destroy_iterator, c);
4924 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4927 if (NULL != c->ports)
4929 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
4930 &client_release_ports, c);
4931 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4933 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
4936 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4940 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4948 * Handler for new clients
4950 * @param cls closure
4951 * @param client identification of the client
4952 * @param message the actual message, which includes messages the client wants
4955 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4956 const struct GNUNET_MessageHeader *message)
4958 struct GNUNET_MESH_ClientConnect *cc_msg;
4959 struct MeshClient *c;
4964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4966 /* Check data sanity */
4967 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4968 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4969 if (0 != (size % sizeof (uint32_t)))
4972 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4975 size /= sizeof (uint32_t);
4977 /* Initialize new client structure */
4978 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4979 c->id = next_client_id++; /* overflow not important: just for debug */
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4986 p = (uint32_t *) &cc_msg[1];
4987 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4988 for (i = 0; i < size; i++)
4991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4993 /* store in client's hashmap */
4994 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4995 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4996 /* store in global hashmap */
4997 /* FIXME only allow one client to have the port open,
4998 * have a backup hashmap with waiting clients */
4999 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
5000 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5004 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5005 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5006 GNUNET_SERVER_notification_context_add (nc, client);
5007 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5009 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5015 * Handler for requests of new tunnels
5017 * @param cls Closure.
5018 * @param client Identification of the client.
5019 * @param message The actual message.
5022 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5023 const struct GNUNET_MessageHeader *message)
5025 struct GNUNET_MESH_TunnelMessage *t_msg;
5026 struct MeshPeer *peer_info;
5027 struct MeshTunnel *t;
5028 struct MeshClient *c;
5029 MESH_TunnelNumber tid;
5031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5033 /* Sanity check for client registration */
5034 if (NULL == (c = client_get (client)))
5037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5042 /* Message size sanity check */
5043 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5046 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5050 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
5052 GNUNET_i2s (&t_msg->peer), ntohl (t_msg->port));
5053 tid = ntohl (t_msg->tunnel_id);
5054 /* Sanity check for duplicate tunnel IDs */
5055 if (NULL != tunnel_get_by_local_id (c, tid))
5058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5063 while (NULL != tunnel_get_by_pi (myid, next_tid))
5064 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5065 t = tunnel_new (myid, next_tid, c, tid);
5066 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5073 t->port = ntohl (t_msg->port);
5074 tunnel_set_options (t, ntohl (t_msg->opt));
5075 if (GNUNET_YES == t->reliable)
5077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5078 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
5080 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
5083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
5084 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
5086 peer_info = peer_get (&t_msg->peer);
5087 peer_add_tunnel (peer_info, t);
5088 peer_connect (peer_info, t);
5089 tunnel_reset_timeout (t, GNUNET_YES);
5090 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5096 * Handler for requests of deleting tunnels
5098 * @param cls closure
5099 * @param client identification of the client
5100 * @param message the actual message
5103 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5104 const struct GNUNET_MessageHeader *message)
5106 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5107 struct MeshClient *c;
5108 struct MeshTunnel *t;
5109 MESH_TunnelNumber tid;
5111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5112 "Got a DESTROY TUNNEL from client!\n");
5114 /* Sanity check for client registration */
5115 if (NULL == (c = client_get (client)))
5118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5123 /* Message sanity check */
5124 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5131 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5133 /* Retrieve tunnel */
5134 tid = ntohl (tunnel_msg->tunnel_id);
5135 t = tunnel_get_by_local_id (c, tid);
5138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5144 /* Cleanup after the tunnel */
5145 client_delete_tunnel (c, t);
5146 if (c == t->client && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV <= tid)
5150 else if (c == t->owner && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV > tid)
5152 peer_remove_tunnel (peer_get_short (t->dest), t);
5157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5158 " tunnel %X client %p (%p, %p)\n",
5159 tid, c, t->owner, t->client);
5163 /* The tunnel will be destroyed when the last message is transmitted. */
5164 tunnel_destroy_empty (t);
5166 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5172 * Handler for client traffic
5174 * @param cls closure
5175 * @param client identification of the client
5176 * @param message the actual message
5179 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
5180 const struct GNUNET_MessageHeader *message)
5182 struct GNUNET_MESH_LocalData *data_msg;
5183 struct MeshClient *c;
5184 struct MeshTunnel *t;
5185 struct MeshFlowControl *fc;
5186 MESH_TunnelNumber tid;
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5190 "Got data from a client!\n");
5192 /* Sanity check for client registration */
5193 if (NULL == (c = client_get (client)))
5196 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5201 data_msg = (struct GNUNET_MESH_LocalData *) message;
5203 /* Sanity check for message size */
5204 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
5205 if (size < sizeof (struct GNUNET_MessageHeader))
5208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5212 /* Tunnel exists? */
5213 tid = ntohl (data_msg->tid);
5214 t = tunnel_get_by_local_id (c, tid);
5218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5222 /* Is the client in the tunnel? */
5223 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5225 t->owner->handle == client)
5227 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5229 t->client->handle == client) ) )
5232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5236 /* Ok, everything is correct, send the message
5237 * (pretend we got it from a mesh peer)
5240 struct GNUNET_MESH_Data *payload;
5241 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5243 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5244 if (GNUNET_YES == t->reliable)
5246 struct MeshTunnelReliability *rel;
5247 struct MeshReliableMessage *copy;
5249 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5250 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5251 + sizeof(struct GNUNET_MESH_Data)
5253 copy->mid = rel->mid_sent++;
5254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
5255 copy->timestamp = GNUNET_TIME_absolute_get ();
5258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
5259 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5260 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5263 GNUNET_TIME_relative_multiply (rel->expected_delay,
5264 MESH_RETRANSMIT_MARGIN);
5266 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5267 &tunnel_retransmit_message,
5270 payload = (struct GNUNET_MESH_Data *) ©[1];
5271 payload->mid = htonl (copy->mid);
5275 payload = (struct GNUNET_MESH_Data *) cbuf;
5276 payload->mid = htonl (fc->last_pid_recv + 1);
5278 memcpy (&payload[1], &data_msg[1], size);
5279 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5280 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5281 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5282 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5283 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5284 payload->tid = htonl (t->id.tid);
5285 payload->ttl = htonl (default_ttl);
5286 payload->pid = htonl (fc->last_pid_recv + 1);
5287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5288 " calling generic handler...\n");
5289 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5290 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5292 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5295 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5302 * Handler for client's ACKs for payload traffic.
5304 * @param cls Closure (unused).
5305 * @param client Identification of the client.
5306 * @param message The actual message.
5309 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5310 const struct GNUNET_MessageHeader *message)
5312 struct GNUNET_MESH_LocalAck *msg;
5313 struct MeshTunnel *t;
5314 struct MeshClient *c;
5315 MESH_TunnelNumber tid;
5317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5318 /* Sanity check for client registration */
5319 if (NULL == (c = client_get (client)))
5322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5327 msg = (struct GNUNET_MESH_LocalAck *) message;
5329 /* Tunnel exists? */
5330 tid = ntohl (msg->tunnel_id);
5331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5332 t = tunnel_get_by_local_id (c, tid);
5336 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5337 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5342 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5343 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5345 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5346 t->prev_fc.last_ack_recv++;
5347 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5351 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5352 t->next_fc.last_ack_recv++;
5353 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5356 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5364 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5366 * @param cls Closure (client handle).
5367 * @param key Key (hashed tunnel ID, unused).
5368 * @param value Tunnel info.
5370 * @return GNUNET_YES, to keep iterating.
5373 monitor_all_tunnels_iterator (void *cls,
5374 const struct GNUNET_HashCode * key,
5377 struct GNUNET_SERVER_Client *client = cls;
5378 struct MeshTunnel *t = value;
5379 struct GNUNET_MESH_LocalMonitor *msg;
5381 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5382 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5383 msg->tunnel_id = htonl (t->id.tid);
5384 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5385 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5386 GNUNET_PEER_resolve (t->dest, &msg->destination);
5388 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5389 "* sending info about tunnel %s [%u]\n",
5390 GNUNET_i2s (&msg->owner), t->id.tid);
5392 GNUNET_SERVER_notification_context_unicast (nc, client,
5393 &msg->header, GNUNET_NO);
5399 * Handler for client's MONITOR request.
5401 * @param cls Closure (unused).
5402 * @param client Identification of the client.
5403 * @param message The actual message.
5406 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5407 const struct GNUNET_MessageHeader *message)
5409 struct MeshClient *c;
5411 /* Sanity check for client registration */
5412 if (NULL == (c = client_get (client)))
5415 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5419 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5420 "Received get tunnels request from client %u\n",
5422 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5423 monitor_all_tunnels_iterator,
5425 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5426 "Get tunnels request from client %u completed\n",
5428 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5433 * Handler for client's MONITOR_TUNNEL request.
5435 * @param cls Closure (unused).
5436 * @param client Identification of the client.
5437 * @param message The actual message.
5440 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5441 const struct GNUNET_MessageHeader *message)
5443 const struct GNUNET_MESH_LocalMonitor *msg;
5444 struct GNUNET_MESH_LocalMonitor *resp;
5445 struct MeshClient *c;
5446 struct MeshTunnel *t;
5448 /* Sanity check for client registration */
5449 if (NULL == (c = client_get (client)))
5452 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5456 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5457 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5458 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5461 ntohl (msg->tunnel_id));
5462 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5465 /* We don't know the tunnel FIXME */
5466 struct GNUNET_MESH_LocalMonitor warn;
5469 GNUNET_SERVER_notification_context_unicast (nc, client,
5472 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5476 /* Initialize context */
5477 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5479 GNUNET_PEER_resolve (t->dest, &resp->destination);
5480 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5481 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5482 &resp->header, GNUNET_NO);
5485 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5486 "Monitor tunnel request from client %u completed\n",
5488 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5493 * Functions to handle messages from clients
5495 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5496 {&handle_local_new_client, NULL,
5497 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5498 {&handle_local_tunnel_create, NULL,
5499 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5500 sizeof (struct GNUNET_MESH_TunnelMessage)},
5501 {&handle_local_tunnel_destroy, NULL,
5502 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5503 sizeof (struct GNUNET_MESH_TunnelMessage)},
5504 {&handle_local_data, NULL,
5505 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5506 {&handle_local_ack, NULL,
5507 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5508 sizeof (struct GNUNET_MESH_LocalAck)},
5509 {&handle_local_get_tunnels, NULL,
5510 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5511 sizeof (struct GNUNET_MessageHeader)},
5512 {&handle_local_show_tunnel, NULL,
5513 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5514 sizeof (struct GNUNET_MESH_LocalMonitor)},
5520 * Method called whenever a given peer connects.
5522 * @param cls closure
5523 * @param peer peer identity this notification is about
5526 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5528 struct MeshPeer *peer_info;
5529 struct MeshPeerPath *path;
5531 DEBUG_CONN ("Peer connected\n");
5532 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5533 peer_info = peer_get (peer);
5534 if (myid == peer_info->id)
5536 DEBUG_CONN (" (self)\n");
5537 path = path_new (1);
5541 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5542 path = path_new (2);
5543 path->peers[1] = peer_info->id;
5544 GNUNET_PEER_change_rc (peer_info->id, 1);
5545 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5547 path->peers[0] = myid;
5548 GNUNET_PEER_change_rc (myid, 1);
5549 peer_add_path (peer_info, path, GNUNET_YES);
5555 * Method called whenever a peer disconnects.
5557 * @param cls closure
5558 * @param peer peer identity this notification is about
5561 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5563 struct MeshPeer *pi;
5564 struct MeshPeerQueue *q;
5565 struct MeshPeerQueue *n;
5567 DEBUG_CONN ("Peer disconnected\n");
5568 pi = GNUNET_CONTAINER_multipeermap_get (peers, peer);
5578 /* TODO try to reroute this traffic instead */
5579 queue_destroy(q, GNUNET_YES);
5582 if (NULL != pi->core_transmit)
5584 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5585 pi->core_transmit = NULL;
5587 peer_remove_path (pi, pi->id, myid);
5590 DEBUG_CONN (" (self)\n");
5592 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5598 * Install server (service) handlers and start listening to clients.
5603 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5604 GNUNET_SERVER_connect_notify (server_handle,
5605 &handle_local_client_connect, NULL);
5606 GNUNET_SERVER_disconnect_notify (server_handle,
5607 &handle_local_client_disconnect, NULL);
5608 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5610 clients_head = NULL;
5611 clients_tail = NULL;
5613 GNUNET_SERVER_resume (server_handle);
5618 * To be called on core init/fail.
5620 * @param cls Closure (config)
5621 * @param identity the public identity of this peer
5624 core_init (void *cls,
5625 const struct GNUNET_PeerIdentity *identity)
5627 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5631 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
5633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5634 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5636 GNUNET_i2s (identity));
5637 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5639 GNUNET_i2s (&my_full_id));
5640 GNUNET_CORE_disconnect (core_handle);
5641 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5642 NULL, /* Closure passed to MESH functions */
5643 &core_init, /* Call core_init once connected */
5644 &core_connect, /* Handle connects */
5645 &core_disconnect, /* remove peers on disconnects */
5646 NULL, /* Don't notify about all incoming messages */
5647 GNUNET_NO, /* For header only in notification */
5648 NULL, /* Don't notify about all outbound messages */
5649 GNUNET_NO, /* For header-only out notification */
5650 core_handlers); /* Register these handlers */
5659 /******************************************************************************/
5660 /************************ MAIN FUNCTIONS ****************************/
5661 /******************************************************************************/
5664 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5666 * @param cls closure
5667 * @param key current key code
5668 * @param value value in the hash map
5669 * @return GNUNET_YES if we should continue to iterate,
5673 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5675 struct MeshTunnel *t = value;
5682 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5684 * @param cls closure
5685 * @param key current key code
5686 * @param value value in the hash map
5687 * @return GNUNET_YES if we should continue to iterate,
5691 shutdown_peer (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
5693 struct MeshPeer *p = value;
5694 struct MeshPeerQueue *q;
5695 struct MeshPeerQueue *n;
5703 queue_destroy(q, GNUNET_YES);
5713 * Task run during shutdown.
5719 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5723 if (core_handle != NULL)
5725 GNUNET_CORE_disconnect (core_handle);
5728 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5729 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_peer, NULL);
5730 if (dht_handle != NULL)
5732 GNUNET_DHT_disconnect (dht_handle);
5737 GNUNET_SERVER_notification_context_destroy (nc);
5740 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5742 GNUNET_SCHEDULER_cancel (announce_id_task);
5743 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5750 * Process mesh requests.
5752 * @param cls closure
5753 * @param server the initialized server
5754 * @param c configuration to use
5757 run (void *cls, struct GNUNET_SERVER_Handle *server,
5758 const struct GNUNET_CONFIGURATION_Handle *c)
5761 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5764 server_handle = server;
5765 GNUNET_SERVER_suspend (server_handle);
5768 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5771 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5773 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5774 "mesh", "peer/privatekey");
5775 GNUNET_SCHEDULER_shutdown ();
5780 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5781 &refresh_path_time))
5783 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5785 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5786 "mesh", "refresh path time");
5787 GNUNET_SCHEDULER_shutdown ();
5792 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5795 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5797 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5798 "mesh", "id announce time");
5799 GNUNET_SCHEDULER_shutdown ();
5804 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5807 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5809 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5810 "mesh", "connect timeout");
5811 GNUNET_SCHEDULER_shutdown ();
5816 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5819 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5821 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5822 "mesh", "max msgs queue");
5823 GNUNET_SCHEDULER_shutdown ();
5828 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5831 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5833 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5834 "mesh", "max tunnels");
5835 GNUNET_SCHEDULER_shutdown ();
5840 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5845 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5846 "mesh", "default ttl", 64);
5851 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5855 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5856 "mesh", "max peers", 1000);
5861 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5868 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5869 "Mesh is running with drop mode enabled. "
5870 "This is NOT a good idea! "
5871 "Remove the DROP_PERCENT option from your configuration.\n");
5875 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5876 &dht_replication_level))
5878 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5880 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5881 "mesh", "dht replication level", 3);
5882 dht_replication_level = 3;
5885 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5886 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5887 peers = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_NO);
5888 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5890 dht_handle = GNUNET_DHT_connect (c, 64);
5891 if (NULL == dht_handle)
5895 stats = GNUNET_STATISTICS_create ("mesh", c);
5897 /* Scheduled the task to clean up when shutdown is called */
5898 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5900 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5901 GNUNET_free (keyfile);
5902 GNUNET_assert (NULL != pk);
5903 my_private_key = pk;
5904 GNUNET_CRYPTO_ecc_key_get_public_for_signature (my_private_key,
5905 &my_full_id.public_key);
5906 myid = GNUNET_PEER_intern (&my_full_id);
5907 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5908 "Mesh for peer [%s] starting\n",
5909 GNUNET_i2s(&my_full_id));
5911 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5912 NULL, /* Closure passed to MESH functions */
5913 &core_init, /* Call core_init once connected */
5914 &core_connect, /* Handle connects */
5915 &core_disconnect, /* remove peers on disconnects */
5916 NULL, /* Don't notify about all incoming messages */
5917 GNUNET_NO, /* For header only in notification */
5918 NULL, /* Don't notify about all outbound messages */
5919 GNUNET_NO, /* For header-only out notification */
5920 core_handlers); /* Register these handlers */
5921 if (NULL == core_handle)
5924 GNUNET_SCHEDULER_shutdown ();
5928 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5929 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5935 * The main function for the mesh service.
5937 * @param argc number of arguments from the command line
5938 * @param argv command line arguments
5939 * @return 0 ok, 1 on error
5942 main (int argc, char *const *argv)
5947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5948 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5950 ret = (GNUNET_OK == r) ? 0 : 1;
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");