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_MultiHashMap *peers;
678 * Handle to communicate with transport
680 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
683 * Handle to communicate with core.
685 static struct GNUNET_CORE_Handle *core_handle;
690 static struct GNUNET_DHT_Handle *dht_handle;
695 static struct GNUNET_SERVER_Handle *server_handle;
698 * Handle to the statistics service.
700 static struct GNUNET_STATISTICS_Handle *stats;
703 * Notification context, to send messages to local clients.
705 static struct GNUNET_SERVER_NotificationContext *nc;
708 * Local peer own ID (memory efficient handle).
710 static GNUNET_PEER_Id myid;
713 * Local peer own ID (full value).
715 static struct GNUNET_PeerIdentity my_full_id;
720 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
725 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
728 * Tunnel ID for the next created tunnel (global tunnel number).
730 static MESH_TunnelNumber next_tid;
733 * Tunnel ID for the next incoming tunnel (local tunnel number).
735 static MESH_TunnelNumber next_local_tid;
738 * All ports clients of this peer have opened.
740 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
743 * Task to periodically announce itself in the network.
745 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
748 * Next ID to assign to a client.
750 unsigned int next_client_id;
753 /******************************************************************************/
754 /*********************** DECLARATIONS **************************/
755 /******************************************************************************/
758 * Function to process paths received for a new peer addition. The recorded
759 * paths form the initial tunnel, which can be optimized later.
760 * Called on each result obtained for the DHT search.
763 * @param exp when will this value expire
764 * @param key key of the result
765 * @param type type of the result
766 * @param size number of bytes in data
767 * @param data pointer to the result data
770 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
771 const struct GNUNET_HashCode * key,
772 const struct GNUNET_PeerIdentity *get_path,
773 unsigned int get_path_length,
774 const struct GNUNET_PeerIdentity *put_path,
775 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
776 size_t size, const void *data);
780 * Retrieve the MeshPeer stucture associated with the peer, create one
781 * and insert it in the appropriate structures if the peer is not known yet.
783 * @param peer Full identity of the peer.
785 * @return Existing or newly created peer info.
787 static struct MeshPeer *
788 peer_get (const struct GNUNET_PeerIdentity *peer);
792 * Retrieve the MeshPeer stucture associated with the peer, create one
793 * and insert it in the appropriate structures if the peer is not known yet.
795 * @param peer Short identity of the peer.
797 * @return Existing or newly created peer info.
799 static struct MeshPeer *
800 peer_get_short (const GNUNET_PEER_Id peer);
804 * Build a PeerPath from the paths returned from the DHT, reversing the paths
805 * to obtain a local peer -> destination path and interning the peer ids.
807 * @return Newly allocated and created path
809 static struct MeshPeerPath *
810 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
811 unsigned int get_path_length,
812 const struct GNUNET_PeerIdentity *put_path,
813 unsigned int put_path_length);
817 * Adds a path to the peer_infos of all the peers in the path
819 * @param p Path to process.
820 * @param confirmed Whether we know if the path works or not.
823 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
828 * Search for a tunnel by global ID using full PeerIdentities.
830 * @param oid owner of the tunnel.
831 * @param tid global tunnel number.
833 * @return tunnel handler, NULL if doesn't exist.
835 static struct MeshTunnel *
836 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
840 * Change the tunnel state.
842 * @param t Tunnel whose ttate to change.
843 * @param state New state.
846 tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state);
850 * Notify a tunnel that a connection has broken that affects at least
853 * @param t Tunnel affected.
854 * @param p1 Peer that got disconnected from p2.
855 * @param p2 Peer that got disconnected from p1.
857 * @return Short ID of the peer disconnected (either p1 or p2).
858 * 0 if the tunnel remained unaffected.
860 static GNUNET_PEER_Id
861 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
866 * Send FWD keepalive packets for a tunnel.
868 * @param cls Closure (tunnel for which to send the keepalive).
869 * @param tc Notification context.
872 tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
875 * Send BCK keepalive packets for a tunnel.
877 * @param cls Closure (tunnel for which to send the keepalive).
878 * @param tc Notification context.
881 tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
884 * @brief Use the given path for the tunnel.
885 * Update the next and prev hops (and RCs).
886 * (Re)start the path refresh in case the tunnel is locally owned.
888 * @param t Tunnel to update.
889 * @param p Path to use.
892 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
895 * Tunnel is empty: destroy it.
897 * Notifies all participants (peers, cleints) about the destruction.
899 * @param t Tunnel to destroy.
902 tunnel_destroy_empty (struct MeshTunnel *t);
905 * Destroy the tunnel.
907 * This function does not generate any warning traffic to clients or peers.
910 * Remove the tunnel from peer_info's and clients' hashmaps.
911 * Cancel messages belonging to this tunnel queued to neighbors.
912 * Free any allocated resources linked to the tunnel.
914 * @param t the tunnel to destroy
916 * @return GNUNET_OK on success
919 tunnel_destroy (struct MeshTunnel *t);
922 * @brief Queue and pass message to core when possible.
924 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
925 * and accounts for it. In case the queue is full, the message is dropped and
928 * Otherwise, message is treated as internal and allowed to go regardless of
931 * @param cls Closure (@c type dependant). It will be used by queue_send to
932 * build the message to be sent if not already prebuilt.
933 * @param type Type of the message, 0 for a raw message.
934 * @param size Size of the message.
935 * @param dst Neighbor to send message to.
936 * @param t Tunnel this message belongs to.
939 queue_add (void *cls, uint16_t type, size_t size,
940 struct MeshPeer *dst, struct MeshTunnel *t);
944 * Free a transmission that was already queued with all resources
945 * associated to the request.
947 * @param queue Queue handler to cancel.
948 * @param clear_cls Is it necessary to free associated cls?
951 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
955 * @brief Get the next transmittable message from the queue.
957 * This will be the head, except in the case of being a data packet
958 * not allowed by the destination peer.
960 * @param peer Destination peer.
962 * @return The next viable MeshPeerQueue element to send to that peer.
963 * NULL when there are no transmittable messages.
965 struct MeshPeerQueue *
966 queue_get_next (const struct MeshPeer *peer);
970 * Core callback to write a queued packet to core buffer
972 * @param cls Closure (peer info).
973 * @param size Number of bytes available in buf.
974 * @param buf Where the to write the message.
976 * @return number of bytes written to buf
979 queue_send (void *cls, size_t size, void *buf);
983 * Dummy function to separate declarations from definitions in function list.
986 __mesh_divider______________________________________________________________();
990 GNUNET_MESH_DEBUG_S2S (enum MeshTunnelState s)
992 static char buf[128];
997 * Uninitialized status, should never appear in operation.
999 case MESH_TUNNEL_NEW: return "MESH_TUNNEL_NEW";
1002 * Path to the peer not known yet
1004 case MESH_TUNNEL_SEARCHING: return "MESH_TUNNEL_SEARCHING";
1007 * Request sent, not yet answered.
1009 case MESH_TUNNEL_WAITING: return "MESH_TUNNEL_WAITING";
1012 * Peer connected and ready to accept data
1014 case MESH_TUNNEL_READY: return "MESH_TUNNEL_READY";
1017 * Peer connected previosly but not responding
1019 case MESH_TUNNEL_RECONNECTING: return "MESH_TUNNEL_RECONNECTING";
1022 sprintf (buf, "%u (UNKNOWN STATE)", s);
1028 /******************************************************************************/
1029 /************************ PERIODIC FUNCTIONS ****************************/
1030 /******************************************************************************/
1033 * Periodically announce self id in the DHT
1035 * @param cls closure
1036 * @param tc task context
1039 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1041 struct PBlock block;
1043 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1045 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1049 * - Set data expiration in function of X
1050 * - Adapt X to churn
1052 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1054 block.id = my_full_id;
1055 GNUNET_DHT_put (dht_handle, /* DHT handle */
1056 &my_full_id.hashPubKey, /* Key to use */
1057 dht_replication_level, /* Replication level */
1058 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1059 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1060 sizeof (block), /* Size of the data */
1061 (const char *) &block, /* Data itself */
1062 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1063 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1064 NULL, /* Continuation */
1065 NULL); /* Continuation closure */
1067 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1071 /******************************************************************************/
1072 /****************** GENERAL HELPER FUNCTIONS ************************/
1073 /******************************************************************************/
1077 * Check if client has registered with the service and has not disconnected
1079 * @param client the client to check
1081 * @return non-NULL if client exists in the global DLL
1083 static struct MeshClient *
1084 client_get (struct GNUNET_SERVER_Client *client)
1086 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1091 * Deletes a tunnel from a client (either owner or destination). To be used on
1094 * @param c Client whose tunnel to delete.
1095 * @param t Tunnel which should be deleted.
1098 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1104 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1106 if (GNUNET_YES != res)
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel owner KO\n");
1111 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1112 t->local_tid_dest, t);
1113 if (GNUNET_YES != res)
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1120 * Notify the appropriate client that a new incoming tunnel was created.
1122 * @param t Tunnel that was created.
1125 send_local_tunnel_create (struct MeshTunnel *t)
1127 struct GNUNET_MESH_TunnelMessage msg;
1129 if (NULL == t->client)
1131 msg.header.size = htons (sizeof (msg));
1132 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1133 msg.tunnel_id = htonl (t->local_tid_dest);
1134 msg.port = htonl (t->port);
1136 msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1137 msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1138 msg.opt = htonl (msg.opt);
1139 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1140 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1141 &msg.header, GNUNET_NO);
1146 * Notify a client that the incoming tunnel is no longer valid.
1148 * @param t Tunnel that is destroyed.
1149 * @param fwd Forward notification (owner->dest)?
1152 send_local_tunnel_destroy (struct MeshTunnel *t, int fwd)
1154 struct GNUNET_MESH_TunnelMessage msg;
1155 struct MeshClient *c;
1157 c = fwd ? t->client : t->owner;
1163 msg.header.size = htons (sizeof (msg));
1164 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1165 msg.tunnel_id = htonl (fwd ? t->local_tid_dest : t->local_tid);
1166 msg.port = htonl (0);
1167 memset (&msg.peer, 0, sizeof (msg.peer));
1168 msg.opt = htonl (0);
1169 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1170 &msg.header, GNUNET_NO);
1175 * Build a local ACK message and send it to a local client.
1177 * @param t Tunnel on which to send the ACK.
1178 * @param c Client to whom send the ACK.
1179 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1182 send_local_ack (struct MeshTunnel *t,
1183 struct MeshClient *c,
1186 struct GNUNET_MESH_LocalAck msg;
1188 msg.header.size = htons (sizeof (msg));
1189 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1190 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
1191 GNUNET_SERVER_notification_context_unicast (nc,
1200 * Sends an already built message to a peer, properly registrating
1201 * all used resources.
1203 * @param message Message to send. Function makes a copy of it.
1204 * @param peer Short ID of the neighbor whom to send the message.
1205 * @param t Tunnel on which this message is transmitted.
1208 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1209 GNUNET_PEER_Id peer,
1210 struct MeshTunnel *t)
1212 struct GNUNET_PeerIdentity id;
1213 struct MeshPeer *neighbor;
1214 struct MeshPeerPath *p;
1219 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1224 size = ntohs (message->size);
1225 data = GNUNET_malloc (size);
1226 memcpy (data, message, size);
1227 type = ntohs(message->type);
1228 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1229 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1231 struct GNUNET_MESH_Data *u;
1233 u = (struct GNUNET_MESH_Data *) data;
1234 u->ttl = htonl (ntohl (u->ttl) - 1);
1236 GNUNET_PEER_resolve (peer, &id);
1237 neighbor = peer_get (&id);
1238 for (p = neighbor->path_head; NULL != p; p = p->next)
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 " %s IS NOT DIRECTLY CONNECTED\n",
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 for (p = neighbor->path_head; NULL != p; p = p->next)
1256 struct GNUNET_PeerIdentity debug_id;
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 " path with %u hops through:\n",
1262 for (i = 0; i < p->length; i++)
1264 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 i, GNUNET_i2s(&debug_id));
1272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1273 " no direct connection to %s\n",
1278 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1289 * Sends a CREATE PATH message for a path to a peer, properly registrating
1290 * all used resources.
1292 * @param t Tunnel for which the path is created.
1295 send_path_create (struct MeshTunnel *t)
1297 struct MeshPeer *neighbor;
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1300 neighbor = peer_get_short (t->next_hop);
1302 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1303 sizeof (struct GNUNET_MESH_CreateTunnel) +
1304 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1307 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1312 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1314 * @param t Tunnel which to confirm.
1317 send_path_ack (struct MeshTunnel *t)
1319 struct MeshPeer *neighbor;
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send path ack\n");
1322 neighbor = peer_get_short (t->prev_hop);
1324 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1325 sizeof (struct GNUNET_MESH_PathACK),
1328 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1333 * Build an ACK message and queue it to send to the given peer.
1335 * @param t Tunnel on which to send the ACK.
1336 * @param peer Peer to whom send the ACK.
1337 * @param ack Value of the ACK.
1340 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1342 struct GNUNET_MESH_ACK msg;
1344 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1345 msg.header.size = htons (sizeof (msg));
1346 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1347 msg.pid = htonl (ack);
1348 msg.tid = htonl (t->id.tid);
1350 send_prebuilt_message (&msg.header, peer, t);
1355 * Core callback to write a pre-constructed data packet to core buffer
1357 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1358 * @param size Number of bytes available in buf.
1359 * @param buf Where the to write the message.
1361 * @return number of bytes written to buf
1364 send_core_data_raw (void *cls, size_t size, void *buf)
1366 struct GNUNET_MessageHeader *msg = cls;
1369 GNUNET_assert (NULL != msg);
1370 total_size = ntohs (msg->size);
1372 if (total_size > size)
1377 memcpy (buf, msg, total_size);
1384 * Function to send a create path packet to a peer.
1386 * @param cls closure
1387 * @param size number of bytes available in buf
1388 * @param buf where the callee should write the message
1389 * @return number of bytes written to buf
1392 send_core_path_create (void *cls, size_t size, void *buf)
1394 struct MeshTunnel *t = cls;
1395 struct GNUNET_MESH_CreateTunnel *msg;
1396 struct GNUNET_PeerIdentity *peer_ptr;
1397 struct MeshPeerPath *p = t->path;
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
1404 sizeof (struct GNUNET_MESH_CreateTunnel) +
1405 p->length * sizeof (struct GNUNET_PeerIdentity);
1407 if (size < size_needed || NULL == buf)
1412 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
1413 msg->header.size = htons (size_needed);
1414 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1415 msg->tid = ntohl (t->id.tid);
1418 if (GNUNET_YES == t->nobuffer)
1419 opt |= GNUNET_MESH_OPTION_NOBUFFER;
1420 if (GNUNET_YES == t->reliable)
1421 opt |= GNUNET_MESH_OPTION_RELIABLE;
1422 msg->opt = htonl (opt);
1423 msg->port = htonl (t->port);
1425 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1426 for (i = 0; i < p->length; i++)
1428 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1432 "CREATE PATH (%u bytes long) sent!\n", size_needed);
1438 * Creates a path ack message in buf and frees all unused resources.
1440 * @param cls closure (MeshTransmissionDescriptor)
1441 * @param size number of bytes available in buf
1442 * @param buf where the callee should write the message
1443 * @return number of bytes written to buf
1446 send_core_path_ack (void *cls, size_t size, void *buf)
1448 struct MeshTunnel *t = cls;
1449 struct GNUNET_MESH_PathACK *msg = buf;
1451 GNUNET_assert (NULL != t);
1452 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1457 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
1458 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1459 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1460 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
1461 msg->tid = htonl (t->id.tid);
1462 msg->peer_id = my_full_id;
1463 msg->ack = htonl (t->prev_fc.last_ack_sent);
1465 /* TODO add signature */
1467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
1468 return sizeof (struct GNUNET_MESH_PathACK);
1473 * Iterator over all the peers to remove the oldest not-used entry.
1475 * @param cls Closure (unsued).
1476 * @param key ID of the peer.
1477 * @param value Peer_Info of the peer.
1482 peer_timeout (void *cls,
1483 const struct GNUNET_HashCode *key,
1491 * Retrieve the MeshPeer stucture associated with the peer, create one
1492 * and insert it in the appropriate structures if the peer is not known yet.
1494 * @param peer Full identity of the peer.
1496 * @return Existing or newly created peer info.
1498 static struct MeshPeer *
1499 peer_get (const struct GNUNET_PeerIdentity *peer_id)
1501 struct MeshPeer *peer;
1503 peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer_id->hashPubKey);
1506 peer = (struct MeshPeer *) GNUNET_malloc (sizeof (struct MeshPeer));
1507 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1509 GNUNET_CONTAINER_multihashmap_iterate (peers,
1513 GNUNET_CONTAINER_multihashmap_put (peers, &peer_id->hashPubKey, peer,
1514 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1515 peer->id = GNUNET_PEER_intern (peer_id);
1517 peer->last_contact = GNUNET_TIME_absolute_get();
1524 * Retrieve the MeshPeer stucture associated with the peer, create one
1525 * and insert it in the appropriate structures if the peer is not known yet.
1527 * @param peer Short identity of the peer.
1529 * @return Existing or newly created peer info.
1531 static struct MeshPeer *
1532 peer_get_short (const GNUNET_PEER_Id peer)
1534 return peer_get (GNUNET_PEER_resolve2 (peer));
1539 * Select which PID to POLL for, to compensate for lost messages.
1541 * @param pi Peer we want to poll.
1542 * @param t Tunnel about which we want to poll.
1544 * @return PID to use, either last sent or first_in_queue - 1
1547 peer_get_first_payload_pid (struct MeshPeer *p, struct MeshTunnel *t)
1549 struct MeshPeerQueue *q;
1552 type = p->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST :
1553 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN;
1555 for (q = p->queue_head; NULL != q; q = q->next)
1557 if (q->type == type && q->tunnel == t)
1559 struct GNUNET_MESH_Data *msg = q->cls;
1561 /* Pretend that the last one sent was the previous to this */
1562 return ntohl (msg->pid) - 1;
1566 /* No data in queue, use last sent */
1568 struct MeshFlowControl *fc;
1570 fc = p->id == t->next_hop ? &t->next_fc : &t->prev_fc;
1571 return fc->last_pid_sent;
1577 * Choose the best path towards a peer considering the tunnel properties.
1579 * @param peer The destination peer.
1580 * @param t The tunnel the path is for.
1582 * @return Best current known path towards the peer, if any.
1584 static struct MeshPeerPath *
1585 peer_get_best_path (const struct MeshPeer *peer, const struct MeshTunnel *t)
1587 struct MeshPeerPath *best_p;
1588 struct MeshPeerPath *p;
1589 unsigned int best_cost;
1592 best_p = p = peer->path_head;
1593 best_cost = cost = p->length;
1596 if ((cost = p->length) < best_cost)
1608 * Try to establish a new connection to this peer in the given tunnel.
1609 * If the peer doesn't have any path to it yet, try to get one.
1610 * If the peer already has some path, send a CREATE PATH towards it.
1612 * @param peer PeerInfo of the peer.
1613 * @param t Tunnel for which to create the path, if possible.
1616 peer_connect (struct MeshPeer *peer, struct MeshTunnel *t)
1618 struct MeshPeerPath *p;
1620 if (NULL != peer->path_head)
1622 p = peer_get_best_path (peer, t);
1623 tunnel_use_path (t, p);
1624 send_path_create (t);
1626 else if (NULL == peer->dhtget)
1628 struct GNUNET_PeerIdentity id;
1630 GNUNET_PEER_resolve (peer->id, &id);
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1632 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1633 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1634 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1635 &id.hashPubKey, /* key to search */
1636 dht_replication_level, /* replication level */
1637 GNUNET_DHT_RO_RECORD_ROUTE |
1638 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1640 0, /* xquery bits */
1641 &dht_get_id_handler, peer);
1642 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
1646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1647 "There is no path but the DHT GET is already started.\n");
1653 * @brief Re-initiate traffic to this peer if necessary.
1655 * Check if there is traffic queued towards this peer
1656 * and the core transmit handle is NULL (traffic was stalled).
1657 * If so, call core tmt rdy.
1659 * @param peer_id Short ID of peer to which initiate traffic.
1662 peer_unlock_queue (GNUNET_PEER_Id peer_id)
1664 struct MeshPeer *peer;
1665 struct MeshPeerQueue *q;
1668 peer = peer_get_short (peer_id);
1669 if (NULL != peer->core_transmit)
1672 q = queue_get_next (peer);
1677 peer->core_transmit =
1678 GNUNET_CORE_notify_transmit_ready (core_handle,
1681 GNUNET_TIME_UNIT_FOREVER_REL,
1682 GNUNET_PEER_resolve2 (peer->id),
1690 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
1692 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
1693 * @param t Tunnel which to cancel.
1696 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
1698 struct MeshPeer *peer;
1699 struct MeshPeerQueue *q;
1700 struct MeshPeerQueue *next;
1701 struct MeshFlowControl *fc;
1704 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
1705 peer = peer_get_short (neighbor);
1706 for (q = peer->queue_head; NULL != q; q = next)
1711 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == q->type ||
1712 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == q->type)
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1715 "peer_cancel_queues %s\n",
1716 GNUNET_MESH_DEBUG_M2S (q->type));
1718 queue_destroy (q, GNUNET_YES);
1721 if (NULL == peer->queue_head && NULL != peer->core_transmit)
1723 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1724 peer->core_transmit = NULL;
1726 fc = neighbor == t->next_hop ? &t->next_fc : &t->prev_fc;
1727 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
1729 GNUNET_SCHEDULER_cancel (fc->poll_task);
1730 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1736 * Destroy the peer_info and free any allocated resources linked to it
1738 * @param peer The peer_info to destroy.
1740 * @return GNUNET_OK on success
1743 peer_destroy (struct MeshPeer *peer)
1745 struct GNUNET_PeerIdentity id;
1746 struct MeshPeerPath *p;
1747 struct MeshPeerPath *nextp;
1750 GNUNET_PEER_resolve (peer->id, &id);
1751 GNUNET_PEER_change_rc (peer->id, -1);
1754 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, peer))
1757 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1758 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1760 if (NULL != peer->dhtget)
1762 GNUNET_DHT_get_stop (peer->dhtget);
1764 p = peer->path_head;
1768 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1772 for (i = 0; i < peer->ntunnels; i++)
1773 tunnel_destroy_empty (peer->tunnels[i]);
1774 GNUNET_array_grow (peer->tunnels, peer->ntunnels, 0);
1781 * Remove all paths that rely on a direct connection between p1 and p2
1782 * from the peer itself and notify all tunnels about it.
1784 * @param peer PeerInfo of affected peer.
1785 * @param p1 GNUNET_PEER_Id of one peer.
1786 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1789 * TODO: optimize (see below)
1792 peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
1795 struct MeshPeerPath *p;
1796 struct MeshPeerPath *next;
1797 struct MeshPeer *peer_d;
1799 unsigned int destroyed;
1802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1804 for (p = peer->path_head; NULL != p; p = next)
1807 for (i = 0; i < (p->length - 1); i++)
1809 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1810 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1812 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1822 for (i = 0; i < peer->ntunnels; i++)
1824 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1828 peer_d = peer_get_short (d);
1829 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1830 tunnel_use_path (peer->tunnels[i], next);
1831 peer_connect (peer_d, peer->tunnels[i]);
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1838 * Add the path to the peer and update the path used to reach it in case this
1841 * @param peer_info Destination peer to add the path to.
1842 * @param path New path to add. Last peer must be the peer in arg 1.
1843 * Path will be either used of freed if already known.
1844 * @param trusted Do we trust that this path is real?
1847 peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
1850 struct MeshPeerPath *aux;
1854 if ((NULL == peer_info) || (NULL == path))
1857 path_destroy (path);
1860 if (path->peers[path->length - 1] != peer_info->id)
1863 path_destroy (path);
1866 if (2 >= path->length && GNUNET_NO == trusted)
1868 /* Only allow CORE to tell us about direct paths */
1869 path_destroy (path);
1872 for (l = 1; l < path->length; l++)
1874 if (path->peers[l] == myid)
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1877 for (l2 = 0; l2 < path->length - l; l2++)
1879 path->peers[l2] = path->peers[l + l2];
1884 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1889 struct GNUNET_PeerIdentity id;
1891 GNUNET_PEER_resolve (peer_info->id, &id);
1892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1893 path->length, GNUNET_i2s (&id));
1896 l = path_get_length (path);
1899 path_destroy (path);
1903 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1904 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1906 l2 = path_get_length (aux);
1909 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1910 peer_info->path_tail, aux, path);
1915 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1917 path_destroy (path);
1922 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1929 * Add the path to the origin peer and update the path used to reach it in case
1930 * this is the shortest.
1931 * The path is given in peer_info -> destination, therefore we turn the path
1932 * upside down first.
1934 * @param peer_info Peer to add the path to, being the origin of the path.
1935 * @param path New path to add after being inversed.
1936 * Path will be either used or freed.
1937 * @param trusted Do we trust that this path is real?
1940 peer_add_path_to_origin (struct MeshPeer *peer_info,
1941 struct MeshPeerPath *path, int trusted)
1944 peer_add_path (peer_info, path, trusted);
1949 * Add a tunnel to the list of tunnels a peer participates in.
1950 * Update the tunnel's destination.
1952 * @param p Peer to add to.
1953 * @param t Tunnel to add.
1956 peer_add_tunnel (struct MeshPeer *p, struct MeshTunnel *t)
1960 GNUNET_break (t->dest == p->id);
1964 GNUNET_PEER_change_rc (t->dest, 1);
1965 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1970 * Remove a tunnel from the list of tunnels a peer participates in.
1971 * Free the tunnel's destination.
1973 * @param p Peer to clean.
1974 * @param t Tunnel to remove.
1977 peer_remove_tunnel (struct MeshPeer *p, struct MeshTunnel *t)
1981 if (t->dest == p->id)
1983 GNUNET_PEER_change_rc (t->dest, -1);
1986 for (i = 0; i < p->ntunnels; i++)
1988 if (p->tunnels[i] == t)
1990 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1991 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1999 * Function called if the connection to the peer has been stalled for a while,
2000 * possibly due to a missed ACK. Poll the peer about its ACK status.
2002 * @param cls Closure (poll ctx).
2003 * @param tc TaskContext.
2006 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2008 struct MeshFlowControl *fc = cls;
2009 struct GNUNET_MESH_Poll msg;
2010 struct MeshTunnel *t = fc->t;
2011 GNUNET_PEER_Id peer;
2013 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2014 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
2021 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2023 if (fc == &t->prev_fc)
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
2028 else if (fc == &t->next_fc)
2030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** peer: %s!\n",
2039 GNUNET_i2s(GNUNET_PEER_resolve2 (peer)));
2042 if (GNUNET_YES == t->destroy)
2049 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2050 msg.header.size = htons (sizeof (msg));
2051 msg.tid = htonl (t->id.tid);
2052 msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t));
2053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", ntohl (msg.pid));
2054 send_prebuilt_message (&msg.header, peer, t);
2055 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2056 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2062 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2063 * to obtain a local peer -> destination path and interning the peer ids.
2065 * @return Newly allocated and created path
2067 static struct MeshPeerPath *
2068 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2069 unsigned int get_path_length,
2070 const struct GNUNET_PeerIdentity *put_path,
2071 unsigned int put_path_length)
2073 struct MeshPeerPath *p;
2079 GNUNET_PEER_change_rc (myid, 1);
2080 i = get_path_length;
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2082 for (i--; i >= 0; i--)
2084 id = GNUNET_PEER_intern (&get_path[i]);
2085 if (p->length > 0 && id == p->peers[p->length - 1])
2087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2088 GNUNET_PEER_change_rc (id, -1);
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2093 GNUNET_i2s (&get_path[i]));
2095 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2096 p->peers[p->length - 1] = id;
2099 i = put_path_length;
2100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2101 for (i--; i >= 0; i--)
2103 id = GNUNET_PEER_intern (&put_path[i]);
2106 /* PUT path went through us, so discard the path up until now and start
2107 * from here to get a much shorter (and loop-free) path.
2112 if (p->length > 0 && id == p->peers[p->length - 1])
2114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2115 GNUNET_PEER_change_rc (id, -1);
2119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2120 GNUNET_i2s (&put_path[i]));
2122 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2123 p->peers[p->length - 1] = id;
2127 if (get_path_length > 0)
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2129 GNUNET_i2s (&get_path[0]));
2130 if (put_path_length > 0)
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2132 GNUNET_i2s (&put_path[0]));
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2135 for (i = 0; i < p->length; i++)
2137 struct GNUNET_PeerIdentity peer_id;
2139 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2141 GNUNET_i2s (&peer_id));
2149 * Adds a path to the peer_infos of all the peers in the path
2151 * @param p Path to process.
2152 * @param confirmed Whether we know if the path works or not.
2155 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2159 /* TODO: invert and add */
2160 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2161 for (i++; i < p->length; i++)
2163 struct MeshPeer *aux;
2164 struct MeshPeerPath *copy;
2166 aux = peer_get_short (p->peers[i]);
2167 copy = path_duplicate (p);
2168 copy->length = i + 1;
2169 peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2175 * Search for a tunnel among the incoming tunnels
2177 * @param tid the local id of the tunnel
2179 * @return tunnel handler, NULL if doesn't exist
2181 static struct MeshTunnel *
2182 tunnel_get_incoming (MESH_TunnelNumber tid)
2184 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2185 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
2190 * Search for a tunnel among the tunnels for a client
2192 * @param c the client whose tunnels to search in
2193 * @param tid the local id of the tunnel
2195 * @return tunnel handler, NULL if doesn't exist
2197 static struct MeshTunnel *
2198 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2200 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2202 GNUNET_break_op (0);
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TID %X not a local tid\n", tid);
2206 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2208 return tunnel_get_incoming (tid);
2212 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
2218 * Search for a tunnel by global ID using PEER_ID
2220 * @param pi owner of the tunnel
2221 * @param tid global tunnel number
2223 * @return tunnel handler, NULL if doesn't exist
2225 static struct MeshTunnel *
2226 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2228 struct MESH_TunnelID id;
2229 struct GNUNET_HashCode hash;
2234 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2235 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2240 * Search for a tunnel by global ID using full PeerIdentities
2242 * @param oid owner of the tunnel
2243 * @param tid global tunnel number
2245 * @return tunnel handler, NULL if doesn't exist
2247 static struct MeshTunnel *
2248 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2250 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2255 * Change the tunnel state.
2257 * @param t Tunnel whose ttate to change.
2258 * @param state New state.
2261 tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state)
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 "Tunnel %s[%X] state was %s\n",
2265 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid,
2266 GNUNET_MESH_DEBUG_S2S (t->state));
2267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268 "Tunnel %s[%X] state is now %s\n",
2269 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid,
2270 GNUNET_MESH_DEBUG_S2S (state));
2277 * Add a client to a tunnel, initializing all needed data structures.
2279 * @param t Tunnel to which add the client.
2280 * @param c Client which to add to the tunnel.
2283 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2285 if (NULL != t->client)
2291 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
2292 t->local_tid_dest, t,
2293 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2299 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
2300 t->local_tid_dest, t,
2301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2311 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
2313 unsigned int own_pos;
2315 for (own_pos = 0; own_pos < p->length; own_pos++)
2317 if (p->peers[own_pos] == myid)
2320 if (own_pos > p->length - 1)
2326 if (own_pos < p->length - 1)
2327 t->next_hop = p->peers[own_pos + 1];
2329 t->next_hop = p->peers[own_pos];
2330 GNUNET_PEER_change_rc (t->next_hop, 1);
2332 t->prev_hop = p->peers[own_pos - 1];
2334 t->prev_hop = p->peers[0];
2335 GNUNET_PEER_change_rc (t->prev_hop, 1);
2337 if (NULL != t->path)
2338 path_destroy (t->path);
2339 t->path = path_duplicate (p);
2342 if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
2343 GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
2344 t->fwd_maintenance_task =
2345 GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2346 &tunnel_fwd_keepalive, t);
2352 * Notifies a tunnel that a connection has broken that affects at least
2353 * some of its peers. Sends a notification towards the root of the tree.
2354 * In case the peer is the owner of the tree, notifies the client that owns
2355 * the tunnel and tries to reconnect.
2357 * @param t Tunnel affected.
2358 * @param p1 Peer that got disconnected from p2.
2359 * @param p2 Peer that got disconnected from p1.
2361 * @return Short ID of the peer disconnected (either p1 or p2).
2362 * 0 if the tunnel remained unaffected.
2364 static GNUNET_PEER_Id
2365 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2368 // if (myid != p1 && myid != p2) FIXME
2373 // if (tree_get_predecessor (t->tree) != 0)
2375 // /* We are the peer still connected, notify owner of the disconnection. */
2376 // struct GNUNET_MESH_PathBroken msg;
2377 // struct GNUNET_PeerIdentity neighbor;
2379 // msg.header.size = htons (sizeof (msg));
2380 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2381 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2382 // msg.tid = htonl (t->id.tid);
2383 // msg.peer1 = my_full_id;
2384 // GNUNET_PEER_resolve (pid, &msg.peer2);
2385 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2386 // send_prebuilt_message (&msg.header, &neighbor, t);
2393 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2395 * @param t Tunnel this is about.
2396 * @param fwd Is for FWD traffic? (ACK dest->owner)
2399 tunnel_send_data_ack (struct MeshTunnel *t, int fwd)
2401 struct GNUNET_MESH_DataACK msg;
2402 struct MeshTunnelReliability *rel;
2403 struct MeshReliableMessage *copy;
2408 rel = fwd ? t->bck_rel : t->fwd_rel;
2409 hop = fwd ? t->prev_hop : t->next_hop;
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 "send_data_ack for %u\n",
2414 if (GNUNET_NO == t->reliable)
2416 GNUNET_break_op (0);
2419 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2420 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2421 msg.header.size = htons (sizeof (msg));
2422 msg.tid = htonl (t->id.tid);
2423 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2424 msg.mid = htonl (rel->mid_recv - 1);
2426 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2428 delta = copy->mid - rel->mid_recv;
2431 mask = 0x1LL << delta;
2432 msg.futures |= mask;
2433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2434 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2435 copy->mid, delta, mask, msg.futures);
2437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2439 send_prebuilt_message (&msg.header, hop, t);
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2445 * Send an ACK informing the predecessor about the available buffer space.
2446 * In case there is no predecessor, inform the owning client.
2447 * If buffering is off, send only on behalf of children or self if endpoint.
2448 * If buffering is on, send when sent to children and buffer space is free.
2449 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2450 * the ACK itself goes "back" (towards root).
2452 * @param t Tunnel on which to send the ACK.
2453 * @param type Type of message that triggered the ACK transmission.
2454 * @param fwd Is this FWD ACK? (Going dest->owner)
2457 tunnel_send_ack (struct MeshTunnel *t, uint16_t type, int fwd)
2459 struct MeshTunnelReliability *rel;
2460 struct MeshFlowControl *next_fc;
2461 struct MeshFlowControl *prev_fc;
2462 struct MeshClient *c;
2463 struct MeshClient *o;
2469 rel = fwd ? t->fwd_rel : t->bck_rel;
2470 c = fwd ? t->client : t->owner;
2471 o = fwd ? t->owner : t->client;
2472 next_fc = fwd ? &t->next_fc : &t->prev_fc;
2473 prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2474 hop = fwd ? t->prev_hop : t->next_hop;
2478 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2479 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2482 GNUNET_MESH_DEBUG_M2S (type));
2483 if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c))
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2488 if (GNUNET_YES == t->reliable && NULL != c)
2489 tunnel_send_data_ack (t, fwd);
2491 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2492 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2493 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2494 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2496 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2497 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2498 t->force_ack = GNUNET_YES;
2504 /* Check if we need to transmit the ACK */
2506 prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 &&
2507 GNUNET_NO == t->force_ack)
2509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2511 " last pid recv: %u, last ack sent: %u\n",
2512 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2516 /* Ok, ACK might be necessary, what PID to ACK? */
2517 delta = t->queue_max - next_fc->queue_n;
2518 if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2519 delta_mid = rel->mid_sent - rel->head_sent->mid;
2522 if (0 > delta || (GNUNET_YES == t->reliable &&
2524 (10 < rel->n_sent || 64 <= delta_mid)))
2526 if (NULL != o && delta > 1)
2528 ack = prev_fc->last_pid_recv + delta;
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2531 " last pid %u, last ack %u, qmax %u, q %u\n",
2532 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2533 t->queue_max, next_fc->queue_n);
2534 if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2540 prev_fc->last_ack_sent = ack;
2542 send_local_ack (t, o, fwd);
2544 send_ack (t, hop, ack);
2546 GNUNET_break (GNUNET_YES == t->destroy);
2547 t->force_ack = GNUNET_NO;
2552 * Modify the mesh message TID from global to local and send to client.
2554 * @param t Tunnel on which to send the message.
2555 * @param msg Message to modify and send.
2556 * @param c Client to send to.
2557 * @param tid Tunnel ID to use (c can be both owner and client).
2560 tunnel_send_client_to_tid (struct MeshTunnel *t,
2561 const struct GNUNET_MESH_Data *msg,
2562 struct MeshClient *c, MESH_TunnelNumber tid)
2564 struct GNUNET_MESH_LocalData *copy;
2565 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2566 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2568 if (size < sizeof (struct GNUNET_MessageHeader))
2570 GNUNET_break_op (0);
2578 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2579 memcpy (©[1], &msg[1], size);
2580 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2581 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2582 copy->tid = htonl (tid);
2583 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2584 ©->header, GNUNET_NO);
2588 * Modify the unicast message TID from global to local and send to client.
2590 * @param t Tunnel on which to send the message.
2591 * @param msg Message to modify and send.
2592 * @param fwd Forward?
2595 tunnel_send_client_data (struct MeshTunnel *t,
2596 const struct GNUNET_MESH_Data *msg,
2600 tunnel_send_client_to_tid (t, msg, t->client, t->local_tid_dest);
2602 tunnel_send_client_to_tid (t, msg, t->owner, t->local_tid);
2607 * Send up to 64 buffered messages to the client for in order delivery.
2609 * @param t Tunnel on which to empty the message buffer.
2610 * @param c Client to send to.
2611 * @param rel Reliability structure to corresponding peer.
2612 * If rel == t->bck_rel, this is FWD data.
2615 tunnel_send_client_buffered_data (struct MeshTunnel *t, struct MeshClient *c,
2616 struct MeshTunnelReliability *rel)
2619 struct MeshReliableMessage *copy;
2620 struct MeshReliableMessage *next;
2622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2623 for (copy = rel->head_recv; NULL != copy; copy = next)
2626 if (copy->mid == rel->mid_recv)
2628 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2631 " have %u! now expecting %u\n",
2632 copy->mid, rel->mid_recv + 1);
2633 tunnel_send_client_data (t, msg, (rel == t->bck_rel));
2635 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 " don't have %u, next is %u\n",
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2652 * We have received a message out of order, buffer it until we receive
2653 * the missing one and we can feed the rest to the client.
2655 * @param t Tunnel to add to.
2656 * @param msg Message to buffer.
2657 * @param rel Reliability data to the corresponding direction.
2660 tunnel_add_buffered_data (struct MeshTunnel *t,
2661 const struct GNUNET_MESH_Data *msg,
2662 struct MeshTunnelReliability *rel)
2664 struct MeshReliableMessage *copy;
2665 struct MeshReliableMessage *prev;
2669 size = ntohs (msg->header.size);
2670 mid = ntohl (msg->mid);
2671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
2673 copy = GNUNET_malloc (sizeof (*copy) + size);
2676 memcpy (©[1], msg, size);
2678 // FIXME do something better than O(n), although n < 64...
2679 // FIXME start from the end (most messages are the latest ones)
2680 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
2683 if (GMC_is_pid_bigger (prev->mid, mid))
2685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2686 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2692 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
2698 * Destroy a reliable message after it has been acknowledged, either by
2699 * direct mid ACK or bitfield. Updates the appropriate data structures and
2700 * timers and frees all memory.
2702 * @param copy Message that is no longer needed: remote peer got it.
2705 tunnel_free_reliable_message (struct MeshReliableMessage *copy)
2707 struct MeshTunnelReliability *rel;
2708 struct GNUNET_TIME_Relative time;
2711 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2712 rel->expected_delay.rel_value *= 7;
2713 rel->expected_delay.rel_value += time.rel_value;
2714 rel->expected_delay.rel_value /= 8;
2716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
2717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
2718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
2719 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
2720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
2721 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
2723 rel->retry_timer = rel->expected_delay;
2724 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2730 * Destroy all reliable messages queued for a tunnel,
2731 * during a tunnel destruction.
2732 * Frees the reliability structure itself.
2734 * @param rel Reliability data for a tunnel.
2737 tunnel_free_reliable_all (struct MeshTunnelReliability *rel)
2739 struct MeshReliableMessage *copy;
2740 struct MeshReliableMessage *next;
2745 for (copy = rel->head_recv; NULL != copy; copy = next)
2748 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2751 for (copy = rel->head_sent; NULL != copy; copy = next)
2754 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2757 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
2758 GNUNET_SCHEDULER_cancel (rel->retry_task);
2764 * Mark future messages as ACK'd.
2766 * @param t Tunnel whose sent buffer to clean.
2767 * @param msg DataACK message with a bitfield of future ACK'd messages.
2768 * @param rel Reliability data.
2771 tunnel_free_sent_reliable (struct MeshTunnel *t,
2772 const struct GNUNET_MESH_DataACK *msg,
2773 struct MeshTunnelReliability *rel)
2775 struct MeshReliableMessage *copy;
2776 struct MeshReliableMessage *next;
2783 bitfield = msg->futures;
2784 mid = ntohl (msg->mid);
2785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2786 "free_sent_reliable %u %llX\n",
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2789 " rel %p, head %p\n",
2790 rel, rel->head_sent);
2791 for (i = 0, copy = rel->head_sent;
2792 i < 64 && NULL != copy && 0 != bitfield;
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2796 " trying bit %u (mid %u)\n",
2799 if (0 == (bitfield & mask))
2802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2803 /* Bit was set, clear the bit from the bitfield */
2806 /* The i-th bit was set. Do we have that copy? */
2807 /* Skip copies with mid < target */
2808 target = mid + i + 1;
2809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
2810 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
2813 /* Did we run out of copies? (previously freed, it's ok) */
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2820 /* Did we overshoot the target? (previously freed, it's ok) */
2821 if (GMC_is_pid_bigger (copy->mid, target))
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
2827 /* Now copy->mid == target, free it */
2829 tunnel_free_reliable_message (copy);
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
2837 * We haven't received an ACK after a certain time: restransmit the message.
2839 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2840 * @param tc TaskContext.
2843 tunnel_retransmit_message (void *cls,
2844 const struct GNUNET_SCHEDULER_TaskContext *tc)
2846 struct MeshTunnelReliability *rel = cls;
2847 struct MeshReliableMessage *copy;
2848 struct MeshFlowControl *fc;
2849 struct MeshPeerQueue *q;
2850 struct MeshPeer *pi;
2851 struct MeshTunnel *t;
2852 struct GNUNET_MESH_Data *payload;
2855 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2856 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2860 copy = rel->head_sent;
2867 /* Search the message to be retransmitted in the outgoing queue */
2868 payload = (struct GNUNET_MESH_Data *) ©[1];
2869 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2870 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2871 pi = peer_get_short (hop);
2872 for (q = pi->queue_head; NULL != q; q = q->next)
2874 if (ntohs (payload->header.type) == q->type)
2876 struct GNUNET_MESH_Data *queued_data = q->cls;
2878 if (queued_data->mid == payload->mid)
2883 /* Message not found in the queue */
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
2888 fc->last_ack_sent++;
2889 fc->last_pid_recv++;
2890 payload->pid = htonl (fc->last_pid_recv);
2891 send_prebuilt_message (&payload->header, hop, t);
2892 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %u\n", copy->mid);
2899 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2900 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2901 &tunnel_retransmit_message,
2907 * Send keepalive packets for a tunnel.
2909 * @param t Tunnel to keep alive..
2910 * @param fwd Is this a FWD keepalive? (owner -> dest).
2913 tunnel_keepalive (struct MeshTunnel *t, int fwd)
2915 struct GNUNET_MESH_TunnelKeepAlive *msg;
2916 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
2921 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
2922 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
2923 hop = fwd ? t->next_hop : t->prev_hop;
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2926 "sending %s keepalive for tunnel %d\n",
2927 fwd ? "FWD" : "BCK", t->id.tid);
2929 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
2930 msg->header.size = htons (size);
2931 msg->header.type = htons (type);
2932 msg->oid = *(GNUNET_PEER_resolve2 (t->id.oid));
2933 msg->tid = htonl (t->id.tid);
2934 send_prebuilt_message (&msg->header, hop, t);
2939 * Send create (PATH_CREATE/PATH_ACK) packets for a tunnel.
2941 * @param t Tunnel for which to send the message.
2942 * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
2945 tunnel_recreate (struct MeshTunnel *t, int fwd)
2947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2948 "sending path recreate for tunnel %s[%X]\n",
2949 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
2951 send_path_create (t);
2958 * Generic tunnel timer management.
2959 * Depending on the role of the peer in the tunnel will send the
2960 * appropriate message (build or keepalive)
2962 * @param t Tunnel to maintain.
2963 * @param fwd Is FWD?
2966 tunnel_maintain (struct MeshTunnel *t, int fwd)
2970 case MESH_TUNNEL_NEW:
2972 case MESH_TUNNEL_SEARCHING:
2973 /* TODO DHT GET with RO_BART */
2975 case MESH_TUNNEL_WAITING:
2976 tunnel_recreate (t, fwd);
2978 case MESH_TUNNEL_READY:
2979 tunnel_keepalive (t, fwd);
2988 tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2990 struct MeshTunnel *t = cls;
2992 t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2993 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
2997 tunnel_maintain (t, GNUNET_YES);
2998 t->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2999 &tunnel_fwd_keepalive,
3005 tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3007 struct MeshTunnel *t = cls;
3009 t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3010 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3014 tunnel_keepalive (t, GNUNET_NO);
3015 t->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
3016 &tunnel_bck_keepalive,
3022 * Send a message to all peers and clients in this tunnel that the tunnel
3023 * is no longer valid. If some peer or client should not receive the message,
3024 * should be zero'ed out before calling this function.
3026 * @param t The tunnel whose peers and clients to notify.
3029 tunnel_send_destroy (struct MeshTunnel *t)
3031 struct GNUNET_MESH_TunnelDestroy msg;
3032 struct GNUNET_PeerIdentity id;
3034 msg.header.size = htons (sizeof (msg));
3035 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3036 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3037 msg.tid = htonl (t->id.tid);
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3039 " sending tunnel destroy for tunnel: %s [%X]\n",
3040 GNUNET_i2s (&msg.oid), t->id.tid);
3042 if (NULL == t->client && 0 != t->next_hop)
3044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
3045 GNUNET_PEER_resolve (t->next_hop, &id);
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3047 " sending forward to %s\n",
3049 send_prebuilt_message (&msg.header, t->next_hop, t);
3051 if (NULL == t->owner && 0 != t->prev_hop)
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
3054 GNUNET_PEER_resolve (t->prev_hop, &id);
3055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3056 " sending back to %s\n",
3058 send_prebuilt_message (&msg.header, t->prev_hop, t);
3060 if (NULL != t->owner)
3062 send_local_tunnel_destroy (t, GNUNET_NO);
3064 if (NULL != t->client)
3066 send_local_tunnel_destroy (t, GNUNET_YES);
3071 tunnel_destroy (struct MeshTunnel *t)
3073 struct MeshClient *c;
3074 struct GNUNET_HashCode hash;
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3084 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3088 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3089 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3097 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
3109 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
3110 t->local_tid_dest, t))
3116 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
3117 t->local_tid_dest, t))
3124 if(GNUNET_YES == t->reliable)
3126 tunnel_free_reliable_all (t->fwd_rel);
3127 tunnel_free_reliable_all (t->bck_rel);
3129 if (0 != t->prev_hop)
3131 peer_cancel_queues (t->prev_hop, t);
3132 GNUNET_PEER_change_rc (t->prev_hop, -1);
3134 if (0 != t->next_hop)
3136 peer_cancel_queues (t->next_hop, t);
3137 GNUNET_PEER_change_rc (t->next_hop, -1);
3139 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task)
3141 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3142 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3144 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task)
3146 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3147 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3150 peer_remove_tunnel (peer_get_short (t->dest), t);
3153 if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
3154 GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
3155 if (GNUNET_SCHEDULER_NO_TASK != t->bck_maintenance_task)
3156 GNUNET_SCHEDULER_cancel (t->bck_maintenance_task);
3159 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3160 path_destroy (t->path);
3166 * Tunnel is empty: destroy it.
3168 * Notifies all participants (peers, cleints) about the destruction.
3170 * @param t Tunnel to destroy.
3173 tunnel_destroy_empty (struct MeshTunnel *t)
3177 struct GNUNET_PeerIdentity id;
3179 GNUNET_PEER_resolve (t->id.oid, &id);
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3181 "executing destruction of empty tunnel %s [%X]\n",
3182 GNUNET_i2s (&id), t->id.tid);
3186 if (GNUNET_NO == t->destroy)
3187 tunnel_send_destroy (t);
3188 if (0 == t->pending_messages)
3191 t->destroy = GNUNET_YES;
3195 * Initialize a Flow Control structure to the initial state.
3197 * @param fc Flow Control structure to initialize.
3200 fc_init (struct MeshFlowControl *fc)
3202 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3203 fc->last_pid_recv = (uint32_t) -1;
3204 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
3205 fc->last_ack_recv = (uint32_t) -1;
3206 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3207 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3212 * Create a new tunnel
3214 * @param owner Who is the owner of the tunnel (short ID).
3215 * @param tid Tunnel Number of the tunnel.
3216 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3217 * @param local Tunnel Number for the tunnel, for the client point of view.
3219 * @return A new initialized tunnel. NULL on error.
3221 static struct MeshTunnel *
3222 tunnel_new (GNUNET_PEER_Id owner,
3223 MESH_TunnelNumber tid,
3224 struct MeshClient *client,
3225 MESH_TunnelNumber local)
3227 struct MeshTunnel *t;
3228 struct GNUNET_HashCode hash;
3230 if (n_tunnels >= max_tunnels && NULL == client)
3233 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3236 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
3238 fc_init (&t->next_fc);
3239 fc_init (&t->prev_fc);
3242 t->local_tid = local;
3244 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3246 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3248 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3249 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3256 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3264 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
3266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3270 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3280 * Set options in a tunnel, extracted from a bit flag field
3282 * @param t Tunnel to set options to.
3283 * @param options Bit array in host byte order.
3286 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
3288 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
3289 GNUNET_YES : GNUNET_NO;
3290 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
3291 GNUNET_YES : GNUNET_NO;
3296 * Iterator for deleting each tunnel whose client endpoint disconnected.
3298 * @param cls Closure (client that has disconnected).
3299 * @param key The local tunnel id (used to access the hashmap).
3300 * @param value The value stored at the key (tunnel to destroy).
3302 * @return GNUNET_OK, keep iterating.
3305 tunnel_destroy_iterator (void *cls,
3309 struct MeshTunnel *t = value;
3310 struct MeshClient *c = cls;
3312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3313 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
3314 t->local_tid, t->local_tid_dest, c->id);
3315 client_delete_tunnel (c, t);
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
3320 if (0 != t->next_hop) { /* destroy could come before a path is used */
3321 GNUNET_PEER_change_rc (t->next_hop, -1);
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
3329 if (0 != t->prev_hop) { /* destroy could come before a path is used */
3330 GNUNET_PEER_change_rc (t->prev_hop, -1);
3335 tunnel_destroy_empty (t);
3342 * remove client's ports from the global hashmap on diconnect.
3344 * @param cls Closure (unused).
3346 * @param value ThClient structure.
3348 * @return GNUNET_OK, keep iterating.
3351 client_release_ports (void *cls,
3357 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
3358 if (GNUNET_YES != res)
3361 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3362 "Port %u by client %p was not registered.\n",
3369 * Timeout function due to lack of keepalive/traffic from the owner.
3370 * Destroys tunnel if called.
3372 * @param cls Closure (tunnel to destroy).
3373 * @param tc TaskContext
3376 tunnel_fwd_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3378 struct MeshTunnel *t = cls;
3380 t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3381 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3384 "Tunnel %s [%X] FWD timed out. Destroying.\n",
3385 GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3386 if (NULL != t->client)
3387 send_local_tunnel_destroy (t, GNUNET_YES);
3388 tunnel_destroy (t); /* Do not notify other */
3393 * Timeout function due to lack of keepalive/traffic from the destination.
3394 * Destroys tunnel if called.
3396 * @param cls Closure (tunnel to destroy).
3397 * @param tc TaskContext
3400 tunnel_bck_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3402 struct MeshTunnel *t = cls;
3404 t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3405 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3408 "Tunnel %s [%X] BCK timed out. Destroying.\n",
3409 GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3410 if (NULL != t->owner)
3411 send_local_tunnel_destroy (t, GNUNET_NO);
3412 tunnel_destroy (t); /* Do not notify other */
3417 * Resets the tunnel timeout task, some other message has done the task's job.
3418 * - For the first peer on the direction this means to send
3419 * a keepalive or a path confirmation message (either create or ACK).
3420 * - For all other peers, this means to destroy the tunnel,
3421 * due to lack of activity.
3422 * Starts the tiemout if no timeout was running (tunnel just created).
3424 * @param t Tunnel whose timeout to reset.
3425 * @param fwd Is this forward?
3427 * TODO use heap to improve efficiency of scheduler.
3430 tunnel_reset_timeout (struct MeshTunnel *t, int fwd)
3432 GNUNET_SCHEDULER_TaskIdentifier *ti;
3433 GNUNET_SCHEDULER_Task f;
3434 struct MeshClient *c;
3436 ti = fwd ? &t->fwd_maintenance_task : &t->bck_maintenance_task;
3437 c = fwd ? t->owner : t->client;
3439 if (GNUNET_SCHEDULER_NO_TASK != *ti)
3440 GNUNET_SCHEDULER_cancel (*ti);
3444 f = fwd ? &tunnel_fwd_keepalive : &tunnel_bck_keepalive;
3445 *ti = GNUNET_SCHEDULER_add_delayed (refresh_path_time, f, t);
3449 f = fwd ? &tunnel_fwd_timeout : &tunnel_bck_timeout;
3450 *ti = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3451 (refresh_path_time, 4),
3457 /******************************************************************************/
3458 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3459 /******************************************************************************/
3462 * Free a transmission that was already queued with all resources
3463 * associated to the request.
3465 * @param queue Queue handler to cancel.
3466 * @param clear_cls Is it necessary to free associated cls?
3469 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3471 struct MeshFlowControl *fc;
3473 if (GNUNET_YES == clear_cls)
3475 switch (queue->type)
3477 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3478 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3479 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3481 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3482 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3483 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3484 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3485 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3486 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3487 case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
3488 case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
3489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3490 " prebuilt message\n");
3491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3493 GNUNET_MESH_DEBUG_M2S (queue->type));
3495 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3501 " type %s unknown!\n",
3502 GNUNET_MESH_DEBUG_M2S (queue->type));
3504 GNUNET_free_non_null (queue->cls);
3506 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3507 queue->peer->queue_tail,
3510 /* Delete from appropriate fc in the tunnel */
3511 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3512 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3514 if (queue->peer->id == queue->tunnel->prev_hop)
3515 fc = &queue->tunnel->prev_fc;
3516 else if (queue->peer->id == queue->tunnel->next_hop)
3517 fc = &queue->tunnel->next_fc;
3521 GNUNET_free (queue);
3526 GNUNET_free (queue);
3531 * @brief Get the next transmittable message from the queue.
3533 * This will be the head, except in the case of being a data packet
3534 * not allowed by the destination peer.
3536 * @param peer Destination peer.
3538 * @return The next viable MeshPeerQueue element to send to that peer.
3539 * NULL when there are no transmittable messages.
3541 struct MeshPeerQueue *
3542 queue_get_next (const struct MeshPeer *peer)
3544 struct MeshPeerQueue *q;
3546 struct GNUNET_MESH_Data *dmsg;
3547 struct MeshTunnel* t;
3551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3552 for (q = peer->queue_head; NULL != q; q = q->next)
3555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3557 GNUNET_MESH_DEBUG_M2S (q->type));
3558 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3559 pid = ntohl (dmsg->pid);
3562 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3563 ack = t->next_fc.last_ack_recv;
3565 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3566 ack = t->prev_fc.last_ack_recv;
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3574 "* ACK: %u, PID: %u, MID: %u\n",
3575 ack, pid, ntohl (dmsg->mid));
3576 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3589 "* nothing found\n");
3595 queue_send (void *cls, size_t size, void *buf)
3597 struct MeshPeer *peer = cls;
3598 struct GNUNET_MessageHeader *msg;
3599 struct MeshPeerQueue *queue;
3600 struct MeshTunnel *t;
3601 struct GNUNET_PeerIdentity dst_id;
3602 struct MeshFlowControl *fc;
3607 peer->core_transmit = NULL;
3609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3611 if (NULL == buf || 0 == size)
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
3616 queue = queue_get_next (peer);
3618 /* Queue has no internal mesh traffic nor sendable payload */
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3622 if (NULL == peer->queue_head)
3623 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3629 GNUNET_PEER_resolve (peer->id, &dst_id);
3630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3632 GNUNET_i2s (&dst_id));
3633 /* Check if buffer size is enough for the message */
3634 if (queue->size > size)
3636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3637 "* not enough room, reissue\n");
3638 peer->core_transmit =
3639 GNUNET_CORE_notify_transmit_ready (core_handle,
3642 GNUNET_TIME_UNIT_FOREVER_REL,
3649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3652 GNUNET_assert (0 < t->pending_messages);
3653 t->pending_messages--;
3657 switch (queue->type)
3660 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3661 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3662 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3663 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3664 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3665 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3666 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3667 case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
3668 case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
3669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3671 GNUNET_MESH_DEBUG_M2S (queue->type));
3673 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3674 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3675 data_size = send_core_data_raw (queue->cls, size, buf);
3676 msg = (struct GNUNET_MessageHeader *) buf;
3677 type = ntohs (msg->type);
3679 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3681 data_size = send_core_path_create (queue->cls, size, buf);
3683 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3685 data_size = send_core_path_ack (queue->cls, size, buf);
3689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3690 "* type unknown: %u\n",
3695 if (0 < drop_percent &&
3696 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3699 "Dropping message of type %s\n",
3700 GNUNET_MESH_DEBUG_M2S(queue->type));
3703 /* Free queue, but cls was freed by send_core_* */
3704 queue_destroy (queue, GNUNET_NO);
3706 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3707 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3711 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3712 t->next_fc.last_pid_sent = pid;
3713 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
3714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3716 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3718 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3719 t->prev_fc.last_pid_sent = pid;
3720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3722 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3723 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
3729 /* If more data in queue, send next */
3730 queue = queue_get_next (peer);
3733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3734 if (NULL == peer->core_transmit) {
3735 peer->core_transmit =
3736 GNUNET_CORE_notify_transmit_ready(core_handle,
3739 GNUNET_TIME_UNIT_FOREVER_REL,
3747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3748 "* tmt rdy called somewhere else\n");
3751 if (peer->id == t->next_hop)
3753 else if (peer->id == t->prev_hop)
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n",
3759 peer->id, t->next_hop, t->prev_hop);
3762 if (NULL != peer->queue_head)
3764 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
3766 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3767 "* %s starting poll timeout\n",
3768 GNUNET_i2s (&my_full_id));
3769 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3775 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3777 GNUNET_SCHEDULER_cancel (fc->poll_task);
3778 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3781 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3792 * @brief Queue and pass message to core when possible.
3794 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3795 * accounts for it. In case the queue is full, the message is dropped and
3798 * Otherwise, message is treated as internal and allowed to go regardless of
3801 * @param cls Closure (@c type dependant). It will be used by queue_send to
3802 * build the message to be sent if not already prebuilt.
3803 * @param type Type of the message, 0 for a raw message.
3804 * @param size Size of the message.
3805 * @param dst Neighbor to send message to.
3806 * @param t Tunnel this message belongs to.
3809 queue_add (void *cls, uint16_t type, size_t size,
3810 struct MeshPeer *dst, struct MeshTunnel *t)
3812 struct MeshPeerQueue *queue;
3813 struct MeshFlowControl *fc;
3817 priority = GNUNET_NO;
3818 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3822 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3828 if (fc->queue_n >= t->queue_max)
3830 /* If this isn't a retransmission, drop the message */
3831 if (GNUNET_NO == t->reliable ||
3832 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3833 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3835 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3839 "queue full: %u/%u\n",
3840 fc->queue_n, t->queue_max);
3841 return; /* Drop this message */
3843 priority = GNUNET_YES;
3846 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
3847 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3848 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3852 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3858 if (GNUNET_YES == priority)
3860 struct GNUNET_MESH_Data *d;
3864 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3865 d = (struct GNUNET_MESH_Data *) queue->cls;
3867 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3869 if (queue->type != type)
3871 d = (struct GNUNET_MESH_Data *) queue->cls;
3878 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3880 if (NULL == dst->core_transmit)
3882 dst->core_transmit =
3883 GNUNET_CORE_notify_transmit_ready (core_handle,
3886 GNUNET_TIME_UNIT_FOREVER_REL,
3887 GNUNET_PEER_resolve2 (dst->id),
3892 t->pending_messages++;
3896 /******************************************************************************/
3897 /******************** MESH NETWORK HANDLERS **************************/
3898 /******************************************************************************/
3902 * Core handler for path creation
3904 * @param cls closure
3905 * @param message message
3906 * @param peer peer identity this notification is about
3908 * @return GNUNET_OK to keep the connection open,
3909 * GNUNET_SYSERR to close it (signal serious error)
3912 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3913 const struct GNUNET_MessageHeader *message)
3915 unsigned int own_pos;
3918 MESH_TunnelNumber tid;
3919 struct GNUNET_MESH_CreateTunnel *msg;
3920 struct GNUNET_PeerIdentity *pi;
3921 struct MeshPeerPath *path;
3922 struct MeshPeer *dest_peer_info;
3923 struct MeshPeer *orig_peer_info;
3924 struct MeshTunnel *t;
3926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3927 "Received a path create msg [%s]\n",
3928 GNUNET_i2s (&my_full_id));
3929 size = ntohs (message->size);
3930 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3932 GNUNET_break_op (0);
3936 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3937 if (size % sizeof (struct GNUNET_PeerIdentity))
3939 GNUNET_break_op (0);
3942 size /= sizeof (struct GNUNET_PeerIdentity);
3945 GNUNET_break_op (0);
3948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3949 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3951 tid = ntohl (msg->tid);
3952 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3954 " path is for tunnel %s[%X]:%u.\n",
3955 GNUNET_i2s (pi), tid, ntohl (msg->port));
3956 t = tunnel_get (pi, tid);
3957 if (NULL == t) /* might be a local tunnel */
3961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3962 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3968 t->port = ntohl (msg->port);
3969 opt = ntohl (msg->opt);
3970 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3972 t->nobuffer = GNUNET_YES;
3975 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3977 t->reliable = GNUNET_YES;
3979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3981 tunnel_reset_timeout (t, GNUNET_YES);
3982 tunnel_change_state (t, MESH_TUNNEL_WAITING);
3984 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3985 if (NULL == dest_peer_info)
3987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3988 " Creating PeerInfo for destination.\n");
3989 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeer));
3990 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3991 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3993 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3995 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3996 if (NULL == orig_peer_info)
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3999 " Creating PeerInfo for origin.\n");
4000 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeer));
4001 orig_peer_info->id = GNUNET_PEER_intern (pi);
4002 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4003 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4006 path = path_new (size);
4008 for (i = 0; i < size; i++)
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4011 GNUNET_i2s (&pi[i]));
4012 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4013 if (path->peers[i] == myid)
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4017 if (own_pos == 0 && path->peers[own_pos] != myid)
4019 /* create path: self not found in path through self */
4020 GNUNET_break_op (0);
4021 path_destroy (path);
4025 path_add_to_peers (path, GNUNET_NO);
4026 tunnel_use_path (t, path);
4028 peer_add_tunnel (dest_peer_info, t);
4030 if (own_pos == size - 1)
4032 struct MeshClient *c;
4034 /* Find target client */
4035 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
4038 /* TODO send reject */
4042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4043 peer_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
4044 /* This can be a retransmission due to a lost PATH ACK.
4045 * Check if we already have a destination client for the tunnel. */
4048 /* Assign local tid */
4049 while (NULL != tunnel_get_incoming (next_local_tid))
4050 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4051 t->local_tid_dest = next_local_tid++;
4052 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4054 if (GNUNET_YES == t->reliable)
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4057 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4059 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
4062 tunnel_add_client (t, c);
4063 send_local_tunnel_create (t);
4066 /* Eliminate tunnel when origin dies */
4067 tunnel_reset_timeout (t, GNUNET_YES);
4068 /* Keep tunnel alive in direction dest->owner*/
4069 tunnel_reset_timeout (t, GNUNET_NO);
4073 struct MeshPeerPath *path2;
4075 t->next_hop = path->peers[own_pos + 1];
4076 GNUNET_PEER_change_rc(t->next_hop, 1);
4078 /* It's for somebody else! Retransmit. */
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4080 path2 = path_duplicate (path);
4081 peer_add_path (dest_peer_info, path2, GNUNET_NO);
4082 peer_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4083 send_path_create (t);
4091 * Core handler for path ACKs
4093 * @param cls closure
4094 * @param message message
4095 * @param peer peer identity this notification is about
4097 * @return GNUNET_OK to keep the connection open,
4098 * GNUNET_SYSERR to close it (signal serious error)
4101 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4102 const struct GNUNET_MessageHeader *message)
4104 struct GNUNET_MESH_PathACK *msg;
4105 struct MeshPeer *peer_info;
4106 struct MeshPeerPath *p;
4107 struct MeshTunnel *t;
4109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4110 GNUNET_i2s (&my_full_id));
4111 msg = (struct GNUNET_MESH_PathACK *) message;
4112 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4115 /* TODO notify that we don't know the tunnel */
4116 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4118 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4122 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4124 peer_info = peer_get (&msg->peer_id);
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4126 GNUNET_i2s (&msg->peer_id));
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4130 /* Add path to peers? */
4134 path_add_to_peers (p, GNUNET_YES);
4140 tunnel_change_state (t, MESH_TUNNEL_READY);
4141 tunnel_reset_timeout (t, GNUNET_NO);
4142 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
4143 t->prev_fc.last_ack_sent = ntohl (msg->ack);
4145 /* Message for us? */
4146 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4149 if (NULL == t->owner)
4151 GNUNET_break_op (0);
4154 if (NULL != peer_info->dhtget)
4156 GNUNET_DHT_get_stop (peer_info->dhtget);
4157 peer_info->dhtget = NULL;
4159 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES);
4160 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO);
4164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4165 " not for us, retransmitting...\n");
4166 send_prebuilt_message (message, t->prev_hop, t);
4172 * Core handler for notifications of broken paths
4174 * @param cls closure
4175 * @param message message
4176 * @param peer peer identity this notification is about
4178 * @return GNUNET_OK to keep the connection open,
4179 * GNUNET_SYSERR to close it (signal serious error)
4182 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4183 const struct GNUNET_MessageHeader *message)
4185 struct GNUNET_MESH_PathBroken *msg;
4186 struct MeshTunnel *t;
4188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4189 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4190 msg = (struct GNUNET_MESH_PathBroken *) message;
4191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4192 GNUNET_i2s (&msg->peer1));
4193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4194 GNUNET_i2s (&msg->peer2));
4195 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4198 GNUNET_break_op (0);
4201 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4202 GNUNET_PEER_search (&msg->peer2));
4209 * Core handler for tunnel destruction
4211 * @param cls closure
4212 * @param message message
4213 * @param peer peer identity this notification is about
4215 * @return GNUNET_OK to keep the connection open,
4216 * GNUNET_SYSERR to close it (signal serious error)
4219 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4220 const struct GNUNET_MessageHeader *message)
4222 struct GNUNET_MESH_TunnelDestroy *msg;
4223 struct MeshTunnel *t;
4225 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4227 "Got a TUNNEL DESTROY packet from %s\n",
4229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4230 " for tunnel %s [%u]\n",
4231 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4232 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4235 /* Probably already got the message from another path,
4236 * destroyed the tunnel and retransmitted to children.
4239 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4243 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4246 t->local_tid, t->local_tid_dest);
4248 if (GNUNET_PEER_search (peer) == t->prev_hop)
4250 // TODO check owner's signature
4251 // TODO add owner's signatue to tunnel for retransmission
4252 peer_cancel_queues (t->prev_hop, t);
4253 GNUNET_PEER_change_rc (t->prev_hop, -1);
4256 else if (GNUNET_PEER_search (peer) == t->next_hop)
4258 // TODO check dest's signature
4259 // TODO add dest's signatue to tunnel for retransmission
4260 peer_cancel_queues (t->next_hop, t);
4261 GNUNET_PEER_change_rc (t->next_hop, -1);
4266 GNUNET_break_op (0);
4267 // TODO check both owner AND destination's signature to see which matches
4268 // TODO restransmit in appropriate direction
4271 tunnel_destroy_empty (t);
4273 // TODO: add timeout to destroy the tunnel anyway
4279 * Generic handler for mesh network payload traffic.
4281 * @param peer Peer identity this notification is about.
4282 * @param message Data message.
4283 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4285 * @return GNUNET_OK to keep the connection open,
4286 * GNUNET_SYSERR to close it (signal serious error)
4289 handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
4290 const struct GNUNET_MessageHeader *message,
4293 struct GNUNET_MESH_Data *msg;
4294 struct MeshFlowControl *fc;
4295 struct MeshTunnelReliability *rel;
4296 struct MeshTunnel *t;
4297 struct MeshClient *c;
4305 size = ntohs (message->size);
4307 sizeof (struct GNUNET_MESH_Data) +
4308 sizeof (struct GNUNET_MessageHeader))
4313 type =ntohs (message->type);
4314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
4315 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4316 msg = (struct GNUNET_MESH_Data *) message;
4317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4318 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4320 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4323 /* TODO notify back: we don't know this tunnel */
4324 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING tunnel unknown\n");
4329 /* Initialize FWD/BCK data */
4330 pid = ntohl (msg->pid);
4331 fc = fwd ? &t->prev_fc : &t->next_fc;
4332 c = fwd ? t->client : t->owner;
4333 rel = fwd ? t->bck_rel : t->fwd_rel;
4334 hop = fwd ? t->next_hop : t->prev_hop;
4335 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
4337 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
4338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4339 "WARNING Received PID %u, (prev %u), ACK %u\n",
4340 pid, fc->last_pid_recv, fc->last_ack_sent);
4344 tunnel_change_state (t, MESH_TUNNEL_READY);
4345 tunnel_reset_timeout (t, fwd);
4348 /* TODO signature verification */
4349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
4350 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4351 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
4355 mid = ntohl (msg->mid);
4356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4357 " pid %u (mid %u) not seen yet\n", pid, mid);
4358 fc->last_pid_recv = pid;
4360 if (GNUNET_NO == t->reliable ||
4361 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4362 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4365 "!!! RECV %u\n", ntohl (msg->mid));
4366 if (GNUNET_YES == t->reliable)
4368 /* Is this the exact next expected messasge? */
4369 if (mid == rel->mid_recv)
4371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4373 tunnel_send_client_data (t, msg, fwd);
4374 tunnel_send_client_buffered_data (t, c, rel);
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4379 tunnel_add_buffered_data (t, msg, rel);
4382 else /* Tunnel unreliable, send to clients directly */
4384 tunnel_send_client_data (t, msg, fwd);
4389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4390 " MID %u not expected (%u - %u), dropping!\n",
4391 ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
4396 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4398 " Pid %u not expected (%u+), dropping!\n",
4399 pid, fc->last_pid_recv + 1);
4401 tunnel_send_ack (t, type, fwd);
4404 fc->last_pid_recv = pid;
4407 GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO);
4408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n",
4409 GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid));
4410 return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */
4412 ttl = ntohl (msg->ttl);
4413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4416 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4418 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4425 send_prebuilt_message (message, hop, t);
4426 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4433 * Core handler for mesh network traffic going from the origin to a peer
4435 * @param cls Closure (unused).
4436 * @param message Message received.
4437 * @param peer Peer who sent the message.
4439 * @return GNUNET_OK to keep the connection open,
4440 * GNUNET_SYSERR to close it (signal serious error)
4443 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4444 const struct GNUNET_MessageHeader *message)
4446 return handle_mesh_data (peer, message, GNUNET_YES);
4450 * Core handler for mesh network traffic towards the owner of a tunnel.
4452 * @param cls Closure (unused).
4453 * @param message Message received.
4454 * @param peer Peer who sent the message.
4456 * @return GNUNET_OK to keep the connection open,
4457 * GNUNET_SYSERR to close it (signal serious error)
4460 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4461 const struct GNUNET_MessageHeader *message)
4463 return handle_mesh_data (peer, message, GNUNET_NO);
4468 * Core handler for mesh network traffic end-to-end ACKs.
4470 * @param cls Closure.
4471 * @param message Message.
4472 * @param peer Peer identity this notification is about.
4474 * @return GNUNET_OK to keep the connection open,
4475 * GNUNET_SYSERR to close it (signal serious error)
4478 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4479 const struct GNUNET_MessageHeader *message)
4481 struct GNUNET_MESH_DataACK *msg;
4482 struct MeshTunnelReliability *rel;
4483 struct MeshReliableMessage *copy;
4484 struct MeshReliableMessage *next;
4485 struct MeshTunnel *t;
4491 type = ntohs (message->type);
4492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4493 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4494 msg = (struct GNUNET_MESH_DataACK *) message;
4496 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4499 /* TODO notify that we dont know this tunnel (whom)? */
4500 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4503 ack = ntohl (msg->mid);
4504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4506 /* Is this a forward or backward ACK? */
4507 id = GNUNET_PEER_search (peer);
4508 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4511 if (NULL == t->owner)
4513 send_prebuilt_message (message, t->prev_hop, t);
4518 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4521 if (NULL == t->client)
4523 send_prebuilt_message (message, t->next_hop, t);
4530 GNUNET_break_op (0);
4534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4535 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4537 if (GMC_is_pid_bigger (copy->mid, ack))
4539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4540 tunnel_free_sent_reliable (t, msg, rel);
4544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4546 tunnel_free_reliable_message (copy);
4548 /* Once buffers have been free'd, send ACK */
4549 tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4551 /* If some message was free'd, update the retransmission delay*/
4552 if (GNUNET_YES == work)
4554 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4556 GNUNET_SCHEDULER_cancel (rel->retry_task);
4557 if (NULL == rel->head_sent)
4559 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4563 struct GNUNET_TIME_Absolute new_target;
4564 struct GNUNET_TIME_Relative delay;
4566 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4567 MESH_RETRANSMIT_MARGIN);
4568 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4570 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4572 GNUNET_SCHEDULER_add_delayed (delay,
4573 &tunnel_retransmit_message,
4584 * Core handler for mesh network traffic point-to-point acks.
4586 * @param cls closure
4587 * @param message message
4588 * @param peer peer identity this notification is about
4590 * @return GNUNET_OK to keep the connection open,
4591 * GNUNET_SYSERR to close it (signal serious error)
4594 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4595 const struct GNUNET_MessageHeader *message)
4597 struct GNUNET_MESH_ACK *msg;
4598 struct MeshTunnel *t;
4599 struct MeshFlowControl *fc;
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4605 msg = (struct GNUNET_MESH_ACK *) message;
4607 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4611 /* TODO notify that we dont know this tunnel (whom)? */
4612 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4615 ack = ntohl (msg->pid);
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4618 /* Is this a forward or backward ACK? */
4619 id = GNUNET_PEER_search (peer);
4620 if (t->next_hop == id)
4622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4625 else if (t->prev_hop == id)
4627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4632 GNUNET_break_op (0);
4636 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4637 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4639 GNUNET_SCHEDULER_cancel (fc->poll_task);
4640 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4641 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4644 fc->last_ack_recv = ack;
4645 peer_unlock_queue (id);
4646 tunnel_change_state (t, MESH_TUNNEL_READY);
4648 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
4655 * Core handler for mesh network traffic point-to-point ack polls.
4657 * @param cls closure
4658 * @param message message
4659 * @param peer peer identity this notification is about
4661 * @return GNUNET_OK to keep the connection open,
4662 * GNUNET_SYSERR to close it (signal serious error)
4665 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4666 const struct GNUNET_MessageHeader *message)
4668 struct GNUNET_MESH_Poll *msg;
4669 struct MeshTunnel *t;
4670 struct MeshFlowControl *fc;
4675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
4678 msg = (struct GNUNET_MESH_Poll *) message;
4680 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4684 /* TODO notify that we dont know this tunnel (whom)? */
4685 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4686 GNUNET_break_op (0);
4690 /* Is this a forward or backward ACK? */
4691 id = GNUNET_PEER_search (peer);
4692 pid = ntohl (msg->pid);
4693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4694 if (t->next_hop == id)
4696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4698 old = fc->last_pid_recv;
4700 else if (t->prev_hop == id)
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4704 old = fc->last_pid_recv;
4712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", fc->last_pid_recv);
4713 fc->last_pid_recv = pid;
4714 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id);
4716 if (GNUNET_YES == t->reliable)
4717 fc->last_pid_recv = old;
4724 * Core handler for mesh keepalives.
4726 * @param cls closure
4727 * @param message message
4728 * @param peer peer identity this notification is about
4729 * @return GNUNET_OK to keep the connection open,
4730 * GNUNET_SYSERR to close it (signal serious error)
4732 * TODO: Check who we got this from, to validate route.
4735 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4736 const struct GNUNET_MessageHeader *message)
4738 struct GNUNET_MESH_TunnelKeepAlive *msg;
4739 struct MeshTunnel *t;
4740 struct MeshClient *c;
4744 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4748 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4751 /* TODO notify that we dont know that tunnel */
4752 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4757 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
4758 GNUNET_YES : GNUNET_NO;
4759 c = fwd ? t->client : t->owner;
4760 hop = fwd ? t->next_hop : t->prev_hop;
4763 tunnel_change_state (t, MESH_TUNNEL_READY);
4764 tunnel_reset_timeout (t, fwd);
4765 if (NULL != c || 0 == hop || myid == hop)
4768 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4769 send_prebuilt_message (message, hop, t);
4776 * Functions to handle messages from core
4778 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4779 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4780 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4781 sizeof (struct GNUNET_MESH_PathBroken)},
4782 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4783 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4784 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4785 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4786 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4787 sizeof (struct GNUNET_MESH_DataACK)},
4788 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4789 sizeof (struct GNUNET_MESH_DataACK)},
4790 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
4791 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4792 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
4793 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4794 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4795 sizeof (struct GNUNET_MESH_ACK)},
4796 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4797 sizeof (struct GNUNET_MESH_Poll)},
4798 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4799 sizeof (struct GNUNET_MESH_PathACK)},
4805 * Function to process paths received for a new peer addition. The recorded
4806 * paths form the initial tunnel, which can be optimized later.
4807 * Called on each result obtained for the DHT search.
4809 * @param cls closure
4810 * @param exp when will this value expire
4811 * @param key key of the result
4812 * @param get_path path of the get request
4813 * @param get_path_length lenght of get_path
4814 * @param put_path path of the put request
4815 * @param put_path_length length of the put_path
4816 * @param type type of the result
4817 * @param size number of bytes in data
4818 * @param data pointer to the result data
4821 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4822 const struct GNUNET_HashCode * key,
4823 const struct GNUNET_PeerIdentity *get_path,
4824 unsigned int get_path_length,
4825 const struct GNUNET_PeerIdentity *put_path,
4826 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4827 size_t size, const void *data)
4829 struct MeshPeer *peer = cls;
4830 struct MeshPeerPath *p;
4831 struct GNUNET_PeerIdentity pi;
4834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4835 GNUNET_PEER_resolve (peer->id, &pi);
4836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4838 p = path_build_from_dht (get_path, get_path_length,
4839 put_path, put_path_length);
4840 path_add_to_peers (p, GNUNET_NO);
4842 for (i = 0; i < peer->ntunnels; i++)
4844 struct GNUNET_PeerIdentity id;
4846 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4848 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4849 peer->tunnels[i]->local_tid,
4850 peer->tunnels[i]->local_tid_dest);
4851 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4854 peer_connect (peer, peer->tunnels[i]);
4862 /******************************************************************************/
4863 /********************* MESH LOCAL HANDLES **************************/
4864 /******************************************************************************/
4868 * Handler for client connection.
4870 * @param cls Closure (unused).
4871 * @param client Client handler.
4874 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4876 struct MeshClient *c;
4880 c = GNUNET_malloc (sizeof (struct MeshClient));
4882 GNUNET_SERVER_client_keep (client);
4883 GNUNET_SERVER_client_set_user_context (client, c);
4884 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4889 * Handler for client disconnection
4891 * @param cls closure
4892 * @param client identification of the client; NULL
4893 * for the last call when the server is destroyed
4896 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4898 struct MeshClient *c;
4899 struct MeshClient *next;
4901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4908 c = client_get (client);
4911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
4913 GNUNET_SERVER_client_drop (c->handle);
4914 c->shutting_down = GNUNET_YES;
4915 if (NULL != c->own_tunnels)
4917 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4918 &tunnel_destroy_iterator, c);
4919 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4922 if (NULL != c->incoming_tunnels)
4924 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4925 &tunnel_destroy_iterator, c);
4926 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4929 if (NULL != c->ports)
4931 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
4932 &client_release_ports, c);
4933 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4936 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
4939 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4944 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4952 * Handler for new clients
4954 * @param cls closure
4955 * @param client identification of the client
4956 * @param message the actual message, which includes messages the client wants
4959 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4960 const struct GNUNET_MessageHeader *message)
4962 struct GNUNET_MESH_ClientConnect *cc_msg;
4963 struct MeshClient *c;
4968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4970 /* Check data sanity */
4971 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4972 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4973 if (0 != (size % sizeof (uint32_t)))
4976 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4979 size /= sizeof (uint32_t);
4981 /* Initialize new client structure */
4982 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4983 c->id = next_client_id++; /* overflow not important: just for debug */
4984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4990 p = (uint32_t *) &cc_msg[1];
4991 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4992 for (i = 0; i < size; i++)
4995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4997 /* store in client's hashmap */
4998 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4999 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
5000 /* store in global hashmap */
5001 /* FIXME only allow one client to have the port open,
5002 * have a backup hashmap with waiting clients */
5003 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
5004 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5008 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5009 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5010 GNUNET_SERVER_notification_context_add (nc, client);
5011 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5013 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5019 * Handler for requests of new tunnels
5021 * @param cls Closure.
5022 * @param client Identification of the client.
5023 * @param message The actual message.
5026 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5027 const struct GNUNET_MessageHeader *message)
5029 struct GNUNET_MESH_TunnelMessage *t_msg;
5030 struct MeshPeer *peer_info;
5031 struct MeshTunnel *t;
5032 struct MeshClient *c;
5033 MESH_TunnelNumber tid;
5035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5037 /* Sanity check for client registration */
5038 if (NULL == (c = client_get (client)))
5041 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5046 /* Message size sanity check */
5047 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5054 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
5056 GNUNET_i2s (&t_msg->peer), ntohl (t_msg->port));
5057 tid = ntohl (t_msg->tunnel_id);
5058 /* Sanity check for duplicate tunnel IDs */
5059 if (NULL != tunnel_get_by_local_id (c, tid))
5062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5067 while (NULL != tunnel_get_by_pi (myid, next_tid))
5068 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5069 t = tunnel_new (myid, next_tid, c, tid);
5070 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5074 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5077 t->port = ntohl (t_msg->port);
5078 tunnel_set_options (t, ntohl (t_msg->opt));
5079 if (GNUNET_YES == t->reliable)
5081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5082 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
5084 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
5087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
5088 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
5090 peer_info = peer_get (&t_msg->peer);
5091 peer_add_tunnel (peer_info, t);
5092 peer_connect (peer_info, t);
5093 tunnel_reset_timeout (t, GNUNET_YES);
5094 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5100 * Handler for requests of deleting tunnels
5102 * @param cls closure
5103 * @param client identification of the client
5104 * @param message the actual message
5107 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5108 const struct GNUNET_MessageHeader *message)
5110 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5111 struct MeshClient *c;
5112 struct MeshTunnel *t;
5113 MESH_TunnelNumber tid;
5115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5116 "Got a DESTROY TUNNEL from client!\n");
5118 /* Sanity check for client registration */
5119 if (NULL == (c = client_get (client)))
5122 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5127 /* Message sanity check */
5128 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5135 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5137 /* Retrieve tunnel */
5138 tid = ntohl (tunnel_msg->tunnel_id);
5139 t = tunnel_get_by_local_id (c, tid);
5142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5144 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5148 /* Cleanup after the tunnel */
5149 client_delete_tunnel (c, t);
5150 if (c == t->client && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV <= tid)
5154 else if (c == t->owner && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV > tid)
5156 peer_remove_tunnel (peer_get_short (t->dest), t);
5161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5162 " tunnel %X client %p (%p, %p)\n",
5163 tid, c, t->owner, t->client);
5167 /* The tunnel will be destroyed when the last message is transmitted. */
5168 tunnel_destroy_empty (t);
5170 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5176 * Handler for client traffic
5178 * @param cls closure
5179 * @param client identification of the client
5180 * @param message the actual message
5183 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
5184 const struct GNUNET_MessageHeader *message)
5186 struct GNUNET_MESH_LocalData *data_msg;
5187 struct MeshClient *c;
5188 struct MeshTunnel *t;
5189 struct MeshFlowControl *fc;
5190 MESH_TunnelNumber tid;
5193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5194 "Got data from a client!\n");
5196 /* Sanity check for client registration */
5197 if (NULL == (c = client_get (client)))
5200 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5205 data_msg = (struct GNUNET_MESH_LocalData *) message;
5207 /* Sanity check for message size */
5208 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
5209 if (size < sizeof (struct GNUNET_MessageHeader))
5212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5216 /* Tunnel exists? */
5217 tid = ntohl (data_msg->tid);
5218 t = tunnel_get_by_local_id (c, tid);
5222 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5226 /* Is the client in the tunnel? */
5227 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5229 t->owner->handle == client)
5231 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5233 t->client->handle == client) ) )
5236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5240 /* Ok, everything is correct, send the message
5241 * (pretend we got it from a mesh peer)
5244 struct GNUNET_MESH_Data *payload;
5245 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5247 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5248 if (GNUNET_YES == t->reliable)
5250 struct MeshTunnelReliability *rel;
5251 struct MeshReliableMessage *copy;
5253 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5254 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5255 + sizeof(struct GNUNET_MESH_Data)
5257 copy->mid = rel->mid_sent++;
5258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
5259 copy->timestamp = GNUNET_TIME_absolute_get ();
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
5263 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5264 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5267 GNUNET_TIME_relative_multiply (rel->expected_delay,
5268 MESH_RETRANSMIT_MARGIN);
5270 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5271 &tunnel_retransmit_message,
5274 payload = (struct GNUNET_MESH_Data *) ©[1];
5275 payload->mid = htonl (copy->mid);
5279 payload = (struct GNUNET_MESH_Data *) cbuf;
5280 payload->mid = htonl (fc->last_pid_recv + 1);
5282 memcpy (&payload[1], &data_msg[1], size);
5283 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5284 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5285 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5286 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5287 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5288 payload->tid = htonl (t->id.tid);
5289 payload->ttl = htonl (default_ttl);
5290 payload->pid = htonl (fc->last_pid_recv + 1);
5291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5292 " calling generic handler...\n");
5293 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5294 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5296 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5299 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5306 * Handler for client's ACKs for payload traffic.
5308 * @param cls Closure (unused).
5309 * @param client Identification of the client.
5310 * @param message The actual message.
5313 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5314 const struct GNUNET_MessageHeader *message)
5316 struct GNUNET_MESH_LocalAck *msg;
5317 struct MeshTunnel *t;
5318 struct MeshClient *c;
5319 MESH_TunnelNumber tid;
5321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5322 /* Sanity check for client registration */
5323 if (NULL == (c = client_get (client)))
5326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5331 msg = (struct GNUNET_MESH_LocalAck *) message;
5333 /* Tunnel exists? */
5334 tid = ntohl (msg->tunnel_id);
5335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5336 t = tunnel_get_by_local_id (c, tid);
5340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5346 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5347 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5349 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5350 t->prev_fc.last_ack_recv++;
5351 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5355 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5356 t->next_fc.last_ack_recv++;
5357 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5360 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5368 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5370 * @param cls Closure (client handle).
5371 * @param key Key (hashed tunnel ID, unused).
5372 * @param value Tunnel info.
5374 * @return GNUNET_YES, to keep iterating.
5377 monitor_all_tunnels_iterator (void *cls,
5378 const struct GNUNET_HashCode * key,
5381 struct GNUNET_SERVER_Client *client = cls;
5382 struct MeshTunnel *t = value;
5383 struct GNUNET_MESH_LocalMonitor *msg;
5385 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5386 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5387 msg->tunnel_id = htonl (t->id.tid);
5388 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5389 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5390 GNUNET_PEER_resolve (t->dest, &msg->destination);
5392 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5393 "* sending info about tunnel %s [%u]\n",
5394 GNUNET_i2s (&msg->owner), t->id.tid);
5396 GNUNET_SERVER_notification_context_unicast (nc, client,
5397 &msg->header, GNUNET_NO);
5403 * Handler for client's MONITOR request.
5405 * @param cls Closure (unused).
5406 * @param client Identification of the client.
5407 * @param message The actual message.
5410 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5411 const struct GNUNET_MessageHeader *message)
5413 struct MeshClient *c;
5415 /* Sanity check for client registration */
5416 if (NULL == (c = client_get (client)))
5419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5423 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5424 "Received get tunnels request from client %u\n",
5426 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5427 monitor_all_tunnels_iterator,
5429 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5430 "Get tunnels request from client %u completed\n",
5432 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5437 * Handler for client's MONITOR_TUNNEL request.
5439 * @param cls Closure (unused).
5440 * @param client Identification of the client.
5441 * @param message The actual message.
5444 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5445 const struct GNUNET_MessageHeader *message)
5447 const struct GNUNET_MESH_LocalMonitor *msg;
5448 struct GNUNET_MESH_LocalMonitor *resp;
5449 struct MeshClient *c;
5450 struct MeshTunnel *t;
5452 /* Sanity check for client registration */
5453 if (NULL == (c = client_get (client)))
5456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5460 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5461 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5462 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5465 ntohl (msg->tunnel_id));
5466 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5469 /* We don't know the tunnel FIXME */
5470 struct GNUNET_MESH_LocalMonitor warn;
5473 GNUNET_SERVER_notification_context_unicast (nc, client,
5476 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5480 /* Initialize context */
5481 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5483 GNUNET_PEER_resolve (t->dest, &resp->destination);
5484 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5485 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5486 &resp->header, GNUNET_NO);
5489 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5490 "Monitor tunnel request from client %u completed\n",
5492 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5497 * Functions to handle messages from clients
5499 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5500 {&handle_local_new_client, NULL,
5501 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5502 {&handle_local_tunnel_create, NULL,
5503 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5504 sizeof (struct GNUNET_MESH_TunnelMessage)},
5505 {&handle_local_tunnel_destroy, NULL,
5506 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5507 sizeof (struct GNUNET_MESH_TunnelMessage)},
5508 {&handle_local_data, NULL,
5509 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5510 {&handle_local_ack, NULL,
5511 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5512 sizeof (struct GNUNET_MESH_LocalAck)},
5513 {&handle_local_get_tunnels, NULL,
5514 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5515 sizeof (struct GNUNET_MessageHeader)},
5516 {&handle_local_show_tunnel, NULL,
5517 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5518 sizeof (struct GNUNET_MESH_LocalMonitor)},
5524 * Method called whenever a given peer connects.
5526 * @param cls closure
5527 * @param peer peer identity this notification is about
5530 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5532 struct MeshPeer *peer_info;
5533 struct MeshPeerPath *path;
5535 DEBUG_CONN ("Peer connected\n");
5536 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5537 peer_info = peer_get (peer);
5538 if (myid == peer_info->id)
5540 DEBUG_CONN (" (self)\n");
5541 path = path_new (1);
5545 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5546 path = path_new (2);
5547 path->peers[1] = peer_info->id;
5548 GNUNET_PEER_change_rc (peer_info->id, 1);
5549 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5551 path->peers[0] = myid;
5552 GNUNET_PEER_change_rc (myid, 1);
5553 peer_add_path (peer_info, path, GNUNET_YES);
5559 * Method called whenever a peer disconnects.
5561 * @param cls closure
5562 * @param peer peer identity this notification is about
5565 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5567 struct MeshPeer *pi;
5568 struct MeshPeerQueue *q;
5569 struct MeshPeerQueue *n;
5571 DEBUG_CONN ("Peer disconnected\n");
5572 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5582 /* TODO try to reroute this traffic instead */
5583 queue_destroy(q, GNUNET_YES);
5586 if (NULL != pi->core_transmit)
5588 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5589 pi->core_transmit = NULL;
5591 peer_remove_path (pi, pi->id, myid);
5594 DEBUG_CONN (" (self)\n");
5596 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5602 * Install server (service) handlers and start listening to clients.
5607 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5608 GNUNET_SERVER_connect_notify (server_handle,
5609 &handle_local_client_connect, NULL);
5610 GNUNET_SERVER_disconnect_notify (server_handle,
5611 &handle_local_client_disconnect, NULL);
5612 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5614 clients_head = NULL;
5615 clients_tail = NULL;
5617 GNUNET_SERVER_resume (server_handle);
5622 * To be called on core init/fail.
5624 * @param cls Closure (config)
5625 * @param server handle to the server for this service
5626 * @param identity the public identity of this peer
5629 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5630 const struct GNUNET_PeerIdentity *identity)
5632 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5636 GNUNET_break (core_handle == server);
5637 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5643 GNUNET_i2s (identity));
5644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5646 GNUNET_i2s (&my_full_id));
5647 GNUNET_CORE_disconnect (core_handle);
5648 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5649 NULL, /* Closure passed to MESH functions */
5650 &core_init, /* Call core_init once connected */
5651 &core_connect, /* Handle connects */
5652 &core_disconnect, /* remove peers on disconnects */
5653 NULL, /* Don't notify about all incoming messages */
5654 GNUNET_NO, /* For header only in notification */
5655 NULL, /* Don't notify about all outbound messages */
5656 GNUNET_NO, /* For header-only out notification */
5657 core_handlers); /* Register these handlers */
5666 /******************************************************************************/
5667 /************************ MAIN FUNCTIONS ****************************/
5668 /******************************************************************************/
5671 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5673 * @param cls closure
5674 * @param key current key code
5675 * @param value value in the hash map
5676 * @return GNUNET_YES if we should continue to iterate,
5680 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5682 struct MeshTunnel *t = value;
5689 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5691 * @param cls closure
5692 * @param key current key code
5693 * @param value value in the hash map
5694 * @return GNUNET_YES if we should continue to iterate,
5698 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5700 struct MeshPeer *p = value;
5701 struct MeshPeerQueue *q;
5702 struct MeshPeerQueue *n;
5710 queue_destroy(q, GNUNET_YES);
5720 * Task run during shutdown.
5726 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5730 if (core_handle != NULL)
5732 GNUNET_CORE_disconnect (core_handle);
5735 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5736 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5737 if (dht_handle != NULL)
5739 GNUNET_DHT_disconnect (dht_handle);
5744 GNUNET_SERVER_notification_context_destroy (nc);
5747 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5749 GNUNET_SCHEDULER_cancel (announce_id_task);
5750 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5757 * Process mesh requests.
5759 * @param cls closure
5760 * @param server the initialized server
5761 * @param c configuration to use
5764 run (void *cls, struct GNUNET_SERVER_Handle *server,
5765 const struct GNUNET_CONFIGURATION_Handle *c)
5768 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5771 server_handle = server;
5772 GNUNET_SERVER_suspend (server_handle);
5775 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5780 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5781 "mesh", "peer/privatekey");
5782 GNUNET_SCHEDULER_shutdown ();
5787 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5788 &refresh_path_time))
5790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5792 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5793 "mesh", "refresh path time");
5794 GNUNET_SCHEDULER_shutdown ();
5799 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5802 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5804 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5805 "mesh", "id announce time");
5806 GNUNET_SCHEDULER_shutdown ();
5811 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5814 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5816 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5817 "mesh", "connect timeout");
5818 GNUNET_SCHEDULER_shutdown ();
5823 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5828 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5829 "mesh", "max msgs queue");
5830 GNUNET_SCHEDULER_shutdown ();
5835 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5838 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5840 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5841 "mesh", "max tunnels");
5842 GNUNET_SCHEDULER_shutdown ();
5847 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5852 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5853 "mesh", "default ttl", 64);
5858 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5861 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5862 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5863 "mesh", "max peers", 1000);
5868 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5875 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5876 "Mesh is running with drop mode enabled. "
5877 "This is NOT a good idea! "
5878 "Remove the DROP_PERCENT option from your configuration.\n");
5882 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5883 &dht_replication_level))
5885 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5887 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5888 "mesh", "dht replication level", 3);
5889 dht_replication_level = 3;
5892 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5893 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5894 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5895 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5897 dht_handle = GNUNET_DHT_connect (c, 64);
5898 if (NULL == dht_handle)
5902 stats = GNUNET_STATISTICS_create ("mesh", c);
5904 /* Scheduled the task to clean up when shutdown is called */
5905 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5907 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5908 GNUNET_free (keyfile);
5909 GNUNET_assert (NULL != pk);
5910 my_private_key = pk;
5911 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5912 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5913 &my_full_id.hashPubKey);
5914 myid = GNUNET_PEER_intern (&my_full_id);
5915 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5916 "Mesh for peer [%s] starting\n",
5917 GNUNET_i2s(&my_full_id));
5919 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5920 NULL, /* Closure passed to MESH functions */
5921 &core_init, /* Call core_init once connected */
5922 &core_connect, /* Handle connects */
5923 &core_disconnect, /* remove peers on disconnects */
5924 NULL, /* Don't notify about all incoming messages */
5925 GNUNET_NO, /* For header only in notification */
5926 NULL, /* Don't notify about all outbound messages */
5927 GNUNET_NO, /* For header-only out notification */
5928 core_handlers); /* Register these handlers */
5929 if (NULL == core_handle)
5932 GNUNET_SCHEDULER_shutdown ();
5936 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5937 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5943 * The main function for the mesh service.
5945 * @param argc number of arguments from the command line
5946 * @param argv command line arguments
5947 * @return 0 ok, 1 on error
5950 main (int argc, char *const *argv)
5955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5956 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5958 ret = (GNUNET_OK == r) ? 0 : 1;
5959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");