2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
27 * - when sending in-order buffered data, wait for client ACKs
30 * - relay corking down to core
31 * - set ttl relative to path length
39 #include "mesh_protocol.h"
40 #include "mesh_path.h"
41 #include "block_mesh.h"
42 #include "gnunet_dht_service.h"
43 #include "gnunet_statistics_service.h"
45 #define MESH_BLOOM_SIZE 128
47 #define MESH_DEBUG_DHT GNUNET_NO
48 #define MESH_DEBUG_CONNECTION GNUNET_NO
49 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
51 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
52 GNUNET_TIME_UNIT_MINUTES,\
54 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
55 #define MESH_RETRANSMIT_MARGIN 4
57 #if MESH_DEBUG_CONNECTION
58 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
60 #define DEBUG_CONN(...)
64 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
66 #define DEBUG_DHT(...)
73 struct timespec __mesh_start;
74 struct timespec __mesh_end;
75 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
76 #define INTERVAL_END \
78 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
79 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
80 if (__diff < 0) __diff += 1000000000;\
84 #define INTERVAL_SHOW \
86 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
88 #define INTERVAL_START
93 /******************************************************************************/
94 /************************ DATA STRUCTURES ****************************/
95 /******************************************************************************/
97 /** FWD declaration */
103 * Struct containing info about a queued transmission to this peer
110 struct MeshPeerQueue *next;
115 struct MeshPeerQueue *prev;
118 * Peer this transmission is directed to.
120 struct MeshPeerInfo *peer;
123 * Tunnel this message belongs to.
125 struct MeshTunnel *tunnel;
128 * Pointer to info stucture used as cls.
138 * Size of the message
145 * Struct containing all information regarding a given peer
155 * Last time we heard from this peer
157 struct GNUNET_TIME_Absolute last_contact;
160 * Number of attempts to reconnect so far
162 int n_reconnect_attempts;
165 * Paths to reach the peer, ordered by ascending hop count
167 struct MeshPeerPath *path_head;
170 * Paths to reach the peer, ordered by ascending hop count
172 struct MeshPeerPath *path_tail;
175 * Handle to stop the DHT search for a path to this peer
177 struct GNUNET_DHT_GetHandle *dhtget;
180 * Array of tunnels this peer is the target of.
181 * Most probably a small amount, therefore not a hashmap.
182 * When the path to the peer changes, notify these tunnels to let them
183 * re-adjust their path trees.
185 struct MeshTunnel **tunnels;
188 * Number of tunnels this peers participates in
190 unsigned int ntunnels;
193 * Transmission queue to core DLL head
195 struct MeshPeerQueue *queue_head;
198 * Transmission queue to core DLL tail
200 struct MeshPeerQueue *queue_tail;
203 * How many messages are in the queue to this peer.
205 unsigned int queue_n;
208 * Handle for queued transmissions
210 struct GNUNET_CORE_TransmitHandle *core_transmit;
215 * Struct to encapsulate all the Flow Control information to a peer in the
216 * context of a tunnel: Same peer in different tunnels will have independent
217 * flow control structures, allowing to choke/free tunnels according to its
220 struct MeshFlowControl
223 * ID of the last packet sent towards the peer.
225 uint32_t last_pid_sent;
228 * ID of the last packet received from the peer.
230 uint32_t last_pid_recv;
233 * Last ACK sent to the peer (peer can't send more than this PID).
235 uint32_t last_ack_sent;
238 * Last ACK sent towards the origin (for traffic towards leaf node).
240 uint32_t last_ack_recv;
243 * How many payload messages are in the queue towards this peer.
248 * Task to poll the peer in case of a lost ACK causes stall.
250 GNUNET_SCHEDULER_TaskIdentifier poll_task;
253 * How frequently to poll for ACKs.
255 struct GNUNET_TIME_Relative poll_time;
258 * On which tunnel to poll.
259 * Using an explicit poll_ctx would not help memory wise,
260 * since the allocated context would have to be stored in the
261 * fc struct in order to free it upon cancelling poll_task.
263 struct MeshTunnel *t;
268 * Globally unique tunnel identification (owner + number)
269 * DO NOT USE OVER THE NETWORK
274 * Node that owns the tunnel
279 * Tunnel number to differentiate all the tunnels owned by the node oid
280 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
282 MESH_TunnelNumber tid;
287 * Data needed for reliable tunnel endpoint retransmission management.
289 struct MeshTunnelReliability;
293 * Info needed to retry a message in case it gets lost.
295 struct MeshReliableMessage
298 * Double linked list, FIFO style
300 struct MeshReliableMessage *next;
301 struct MeshReliableMessage *prev;
304 * Tunnel Reliability queue this message is in.
306 struct MeshTunnelReliability *rel;
309 * ID of the message (ACK needed to free)
314 * When was this message issued (to calculate ACK delay)
316 struct GNUNET_TIME_Absolute timestamp;
318 /* struct GNUNET_MESH_Data with payload */
322 struct MeshTunnelReliability
325 * Tunnel this is about.
327 struct MeshTunnel *t;
330 * DLL of messages sent and not yet ACK'd.
332 struct MeshReliableMessage *head_sent;
333 struct MeshReliableMessage *tail_sent;
346 * DLL of messages received out of order.
348 struct MeshReliableMessage *head_recv;
349 struct MeshReliableMessage *tail_recv;
357 * Task to resend/poll in case no ACK is received.
359 GNUNET_SCHEDULER_TaskIdentifier retry_task;
362 * Counter for exponential backoff.
364 struct GNUNET_TIME_Relative retry_timer;
367 * How long does it usually take to get an ACK.
369 struct GNUNET_TIME_Relative expected_delay;
374 * Struct containing all information regarding a tunnel
375 * For an intermediate node the improtant info used will be:
376 * - id Tunnel unique identification
377 * - paths[0] To know where to send it next
378 * - metainfo: ready, speeds, accounting
385 struct MESH_TunnelID id;
388 * Port of the tunnel.
393 * State of the tunnel.
395 enum MeshTunnelState state;
398 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
400 MESH_TunnelNumber local_tid;
403 * Local tunnel number for local destination clients (incoming number)
404 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
407 MESH_TunnelNumber local_tid_dest;
410 * Is the tunnel bufferless (minimum latency)?
415 * Is the tunnel reliable?
420 * Force sending ACK? Flag to allow duplicate ACK on POLL.
425 * How many messages do we accept in the forward queue.
427 unsigned int queue_max;
430 * Last time the tunnel was used
432 struct GNUNET_TIME_Absolute timestamp;
435 * Destination of the tunnel.
440 * Next hop in the tunnel. If 0, @c client must be set.
442 GNUNET_PEER_Id next_hop;
445 * Previous hop in the tunnel. If 0, @c owner must be set.
447 GNUNET_PEER_Id prev_hop;
450 * Flow control information about @c next_hop or @c client.
452 struct MeshFlowControl next_fc;
455 * Flow control information about @c prev_hop or @c owner.
457 struct MeshFlowControl prev_fc;
460 * Client owner of the tunnel, if any
462 struct MeshClient *owner;
465 * Client destination of the tunnel, if any.
467 struct MeshClient *client;
470 * Task to keep the used paths alive at the owner,
471 * time tunnel out on all the other peers.
473 GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
476 * Task to keep the used paths alive at the destination,
477 * time tunnel out on all the other peers.
479 GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
482 * Path being used for the tunnel.
484 struct MeshPeerPath *path;
487 * Flag to signal the destruction of the tunnel.
488 * If this is set GNUNET_YES the tunnel will be destroyed
489 * when the queue is empty.
494 * Total messages pending for this tunnels, payload or not.
496 unsigned int pending_messages;
500 * Only present (non-NULL) at the owner of a tunnel.
502 struct MeshTunnelReliability *fwd_rel;
506 * Only present (non-NULL) at the destination of a tunnel.
508 struct MeshTunnelReliability *bck_rel;
513 * Struct containing information about a client of the service
515 * TODO: add a list of 'waiting' ports
522 struct MeshClient *next;
527 struct MeshClient *prev;
530 * Tunnels that belong to this client, indexed by local id
532 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
535 * Tunnels this client has accepted, indexed by incoming local id
537 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
540 * Handle to communicate with the client
542 struct GNUNET_SERVER_Client *handle;
545 * Ports that this client has declared interest in.
546 * Indexed by port, contains *Client.
548 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
551 * Whether the client is active or shutting down (don't send confirmations
552 * to a client that is shutting down.
557 * ID of the client, mainly for debug messages
564 /******************************************************************************/
565 /************************ DEBUG FUNCTIONS ****************************/
566 /******************************************************************************/
570 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
571 * @param cls string to print
572 * @param success GNUNET_OK if the PUT was transmitted,
573 * GNUNET_NO on timeout,
574 * GNUNET_SYSERR on disconnect from service
575 * after the PUT message was transmitted
576 * (so we don't know if it was received or not)
581 mesh_debug (void *cls, int success)
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
591 /******************************************************************************/
592 /*********************** GLOBAL VARIABLES ****************************/
593 /******************************************************************************/
595 /************************** Configuration parameters **************************/
598 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
600 static struct GNUNET_TIME_Relative refresh_path_time;
603 * How often to PUT own ID in the DHT.
605 static struct GNUNET_TIME_Relative id_announce_time;
608 * Maximum time allowed to connect to a peer found by string.
610 static struct GNUNET_TIME_Relative connect_timeout;
613 * Default TTL for payload packets.
615 static unsigned long long default_ttl;
618 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
620 static unsigned long long dht_replication_level;
623 * How many tunnels are we willing to maintain.
624 * Local tunnels are always allowed, even if there are more tunnels than max.
626 static unsigned long long max_tunnels;
629 * How many messages *in total* are we willing to queue, divided by number of
630 * tunnels to get tunnel queue size.
632 static unsigned long long max_msgs_queue;
635 * How many peers do we want to remember?
637 static unsigned long long max_peers;
640 * Percentage of messages that will be dropped (for test purposes only).
642 static unsigned long long drop_percent;
644 /*************************** Static global variables **************************/
647 * DLL with all the clients, head.
649 static struct MeshClient *clients_head;
652 * DLL with all the clients, tail.
654 static struct MeshClient *clients_tail;
657 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
659 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
662 * Number of tunnels known.
664 static unsigned long long n_tunnels;
667 * Tunnels incoming, indexed by MESH_TunnelNumber
668 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
670 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
673 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
675 static struct GNUNET_CONTAINER_MultiHashMap *peers;
678 * Handle to communicate with transport
680 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
683 * Handle to communicate with core.
685 static struct GNUNET_CORE_Handle *core_handle;
690 static struct GNUNET_DHT_Handle *dht_handle;
695 static struct GNUNET_SERVER_Handle *server_handle;
698 * Handle to the statistics service.
700 static struct GNUNET_STATISTICS_Handle *stats;
703 * Notification context, to send messages to local clients.
705 static struct GNUNET_SERVER_NotificationContext *nc;
708 * Local peer own ID (memory efficient handle).
710 static GNUNET_PEER_Id myid;
713 * Local peer own ID (full value).
715 static struct GNUNET_PeerIdentity my_full_id;
720 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
725 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
728 * Tunnel ID for the next created tunnel (global tunnel number).
730 static MESH_TunnelNumber next_tid;
733 * Tunnel ID for the next incoming tunnel (local tunnel number).
735 static MESH_TunnelNumber next_local_tid;
738 * All ports clients of this peer have opened.
740 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
743 * Task to periodically announce itself in the network.
745 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
748 * Next ID to assign to a client.
750 unsigned int next_client_id;
753 /******************************************************************************/
754 /*********************** DECLARATIONS **************************/
755 /******************************************************************************/
758 * Function to process paths received for a new peer addition. The recorded
759 * paths form the initial tunnel, which can be optimized later.
760 * Called on each result obtained for the DHT search.
763 * @param exp when will this value expire
764 * @param key key of the result
765 * @param type type of the result
766 * @param size number of bytes in data
767 * @param data pointer to the result data
770 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
771 const struct GNUNET_HashCode * key,
772 const struct GNUNET_PeerIdentity *get_path,
773 unsigned int get_path_length,
774 const struct GNUNET_PeerIdentity *put_path,
775 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
776 size_t size, const void *data);
780 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
781 * and insert it in the appropriate structures if the peer is not known yet.
783 * @param peer Full identity of the peer.
785 * @return Existing or newly created peer info.
787 static struct MeshPeerInfo *
788 peer_get (const struct GNUNET_PeerIdentity *peer);
792 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
793 * and insert it in the appropriate structures if the peer is not known yet.
795 * @param peer Short identity of the peer.
797 * @return Existing or newly created peer info.
799 static struct MeshPeerInfo *
800 peer_get_short (const GNUNET_PEER_Id peer);
804 * Build a PeerPath from the paths returned from the DHT, reversing the paths
805 * to obtain a local peer -> destination path and interning the peer ids.
807 * @return Newly allocated and created path
809 static struct MeshPeerPath *
810 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
811 unsigned int get_path_length,
812 const struct GNUNET_PeerIdentity *put_path,
813 unsigned int put_path_length);
817 * Adds a path to the peer_infos of all the peers in the path
819 * @param p Path to process.
820 * @param confirmed Whether we know if the path works or not.
823 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
828 * Search for a tunnel by global ID using full PeerIdentities.
830 * @param oid owner of the tunnel.
831 * @param tid global tunnel number.
833 * @return tunnel handler, NULL if doesn't exist.
835 static struct MeshTunnel *
836 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
840 * Change the tunnel state.
842 * @param t Tunnel whose ttate to change.
843 * @param state New state.
846 tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state);
850 * Notify a tunnel that a connection has broken that affects at least
853 * @param t Tunnel affected.
854 * @param p1 Peer that got disconnected from p2.
855 * @param p2 Peer that got disconnected from p1.
857 * @return Short ID of the peer disconnected (either p1 or p2).
858 * 0 if the tunnel remained unaffected.
860 static GNUNET_PEER_Id
861 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
866 * Send FWD keepalive packets for a tunnel.
868 * @param cls Closure (tunnel for which to send the keepalive).
869 * @param tc Notification context.
872 tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
875 * Send BCK keepalive packets for a tunnel.
877 * @param cls Closure (tunnel for which to send the keepalive).
878 * @param tc Notification context.
881 tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
884 * @brief Use the given path for the tunnel.
885 * Update the next and prev hops (and RCs).
886 * (Re)start the path refresh in case the tunnel is locally owned.
888 * @param t Tunnel to update.
889 * @param p Path to use.
892 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
895 * Tunnel is empty: destroy it.
897 * Notifies all participants (peers, cleints) about the destruction.
899 * @param t Tunnel to destroy.
902 tunnel_destroy_empty (struct MeshTunnel *t);
905 * @brief Queue and pass message to core when possible.
907 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
908 * and accounts for it. In case the queue is full, the message is dropped and
911 * Otherwise, message is treated as internal and allowed to go regardless of
914 * @param cls Closure (@c type dependant). It will be used by queue_send to
915 * build the message to be sent if not already prebuilt.
916 * @param type Type of the message, 0 for a raw message.
917 * @param size Size of the message.
918 * @param dst Neighbor to send message to.
919 * @param t Tunnel this message belongs to.
922 queue_add (void *cls, uint16_t type, size_t size,
923 struct MeshPeerInfo *dst, struct MeshTunnel *t);
927 * Free a transmission that was already queued with all resources
928 * associated to the request.
930 * @param queue Queue handler to cancel.
931 * @param clear_cls Is it necessary to free associated cls?
934 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
938 * @brief Get the next transmittable message from the queue.
940 * This will be the head, except in the case of being a data packet
941 * not allowed by the destination peer.
943 * @param peer Destination peer.
945 * @return The next viable MeshPeerQueue element to send to that peer.
946 * NULL when there are no transmittable messages.
948 struct MeshPeerQueue *
949 queue_get_next (const struct MeshPeerInfo *peer);
953 * Core callback to write a queued packet to core buffer
955 * @param cls Closure (peer info).
956 * @param size Number of bytes available in buf.
957 * @param buf Where the to write the message.
959 * @return number of bytes written to buf
962 queue_send (void *cls, size_t size, void *buf);
966 * Dummy function to separate declarations from definitions in function list.
969 __mesh_divider______________________________________________________________();
973 GNUNET_MESH_DEBUG_S2S (enum MeshTunnelState s)
975 static char buf[128];
980 * Uninitialized status, should never appear in operation.
982 case MESH_TUNNEL_NEW: return "MESH_TUNNEL_NEW";
985 * Path to the peer not known yet
987 case MESH_TUNNEL_SEARCHING: return "MESH_TUNNEL_SEARCHING";
990 * Request sent, not yet answered.
992 case MESH_TUNNEL_WAITING: return "MESH_TUNNEL_WAITING";
995 * Peer connected and ready to accept data
997 case MESH_TUNNEL_READY: return "MESH_TUNNEL_READY";
1000 * Peer connected previosly but not responding
1002 case MESH_TUNNEL_RECONNECTING: return "MESH_TUNNEL_RECONNECTING";
1005 sprintf (buf, "%u (UNKNOWN STATE)", s);
1011 /******************************************************************************/
1012 /************************ PERIODIC FUNCTIONS ****************************/
1013 /******************************************************************************/
1016 * Periodically announce self id in the DHT
1018 * @param cls closure
1019 * @param tc task context
1022 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1024 struct PBlock block;
1026 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1028 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1032 * - Set data expiration in function of X
1033 * - Adapt X to churn
1035 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1037 block.id = my_full_id;
1038 GNUNET_DHT_put (dht_handle, /* DHT handle */
1039 &my_full_id.hashPubKey, /* Key to use */
1040 dht_replication_level, /* Replication level */
1041 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1042 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1043 sizeof (block), /* Size of the data */
1044 (const char *) &block, /* Data itself */
1045 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1046 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1047 NULL, /* Continuation */
1048 NULL); /* Continuation closure */
1050 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1054 /******************************************************************************/
1055 /****************** GENERAL HELPER FUNCTIONS ************************/
1056 /******************************************************************************/
1060 * Check if client has registered with the service and has not disconnected
1062 * @param client the client to check
1064 * @return non-NULL if client exists in the global DLL
1066 static struct MeshClient *
1067 client_get (struct GNUNET_SERVER_Client *client)
1069 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1074 * Deletes a tunnel from a client (either owner or destination). To be used on
1077 * @param c Client whose tunnel to delete.
1078 * @param t Tunnel which should be deleted.
1081 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1085 GNUNET_assert (GNUNET_YES ==
1086 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1092 GNUNET_assert (GNUNET_YES ==
1093 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1101 * Notify the appropriate client that a new incoming tunnel was created.
1103 * @param t Tunnel that was created.
1106 send_local_tunnel_create (struct MeshTunnel *t)
1108 struct GNUNET_MESH_TunnelMessage msg;
1110 if (NULL == t->client)
1112 msg.header.size = htons (sizeof (msg));
1113 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1114 msg.tunnel_id = htonl (t->local_tid_dest);
1115 msg.port = htonl (t->port);
1117 msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1118 msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1119 msg.opt = htonl (msg.opt);
1120 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1121 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1122 &msg.header, GNUNET_NO);
1127 * Notify a client that the incoming tunnel is no longer valid.
1129 * @param t Tunnel that is destroyed.
1130 * @param fwd Forward notification (owner->dest)?
1133 send_local_tunnel_destroy (struct MeshTunnel *t, int fwd)
1135 struct GNUNET_MESH_TunnelMessage msg;
1136 struct MeshClient *c;
1138 c = fwd ? t->client : t->owner;
1144 msg.header.size = htons (sizeof (msg));
1145 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1146 msg.tunnel_id = htonl (fwd ? t->local_tid_dest : t->local_tid);
1147 msg.port = htonl (0);
1148 memset (&msg.peer, 0, sizeof (msg.peer));
1149 msg.opt = htonl (0);
1150 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1151 &msg.header, GNUNET_NO);
1156 * Build a local ACK message and send it to a local client.
1158 * @param t Tunnel on which to send the ACK.
1159 * @param c Client to whom send the ACK.
1160 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
1163 send_local_ack (struct MeshTunnel *t,
1164 struct MeshClient *c,
1167 struct GNUNET_MESH_LocalAck msg;
1169 msg.header.size = htons (sizeof (msg));
1170 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1171 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
1172 GNUNET_SERVER_notification_context_unicast (nc,
1181 * Sends an already built message to a peer, properly registrating
1182 * all used resources.
1184 * @param message Message to send. Function makes a copy of it.
1185 * @param peer Short ID of the neighbor whom to send the message.
1186 * @param t Tunnel on which this message is transmitted.
1189 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1190 GNUNET_PEER_Id peer,
1191 struct MeshTunnel *t)
1193 struct GNUNET_PeerIdentity id;
1194 struct MeshPeerInfo *neighbor;
1195 struct MeshPeerPath *p;
1200 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1205 size = ntohs (message->size);
1206 data = GNUNET_malloc (size);
1207 memcpy (data, message, size);
1208 type = ntohs(message->type);
1209 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1210 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1212 struct GNUNET_MESH_Data *u;
1214 u = (struct GNUNET_MESH_Data *) data;
1215 u->ttl = htonl (ntohl (u->ttl) - 1);
1217 GNUNET_PEER_resolve (peer, &id);
1218 neighbor = peer_get (&id);
1219 for (p = neighbor->path_head; NULL != p; p = p->next)
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 " %s IS NOT DIRECTLY CONNECTED\n",
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 for (p = neighbor->path_head; NULL != p; p = p->next)
1237 struct GNUNET_PeerIdentity debug_id;
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1241 " path with %u hops through:\n",
1243 for (i = 0; i < p->length; i++)
1245 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 i, GNUNET_i2s(&debug_id));
1253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1254 " no direct connection to %s\n",
1259 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1270 * Sends a CREATE PATH message for a path to a peer, properly registrating
1271 * all used resources.
1273 * @param t Tunnel for which the path is created.
1276 send_path_create (struct MeshTunnel *t)
1278 struct MeshPeerInfo *neighbor;
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1281 neighbor = peer_get_short (t->next_hop);
1283 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1284 sizeof (struct GNUNET_MESH_CreateTunnel) +
1285 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1288 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1293 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1295 * @param t Tunnel which to confirm.
1298 send_path_ack (struct MeshTunnel *t)
1300 struct MeshPeerInfo *neighbor;
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send path ack\n");
1303 neighbor = peer_get_short (t->prev_hop);
1305 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1306 sizeof (struct GNUNET_MESH_PathACK),
1309 tunnel_change_state (t, MESH_TUNNEL_WAITING);
1314 * Build an ACK message and queue it to send to the given peer.
1316 * @param t Tunnel on which to send the ACK.
1317 * @param peer Peer to whom send the ACK.
1318 * @param ack Value of the ACK.
1321 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1323 struct GNUNET_MESH_ACK msg;
1325 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1326 msg.header.size = htons (sizeof (msg));
1327 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1328 msg.pid = htonl (ack);
1329 msg.tid = htonl (t->id.tid);
1331 send_prebuilt_message (&msg.header, peer, t);
1336 * Core callback to write a pre-constructed data packet to core buffer
1338 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1339 * @param size Number of bytes available in buf.
1340 * @param buf Where the to write the message.
1342 * @return number of bytes written to buf
1345 send_core_data_raw (void *cls, size_t size, void *buf)
1347 struct GNUNET_MessageHeader *msg = cls;
1350 GNUNET_assert (NULL != msg);
1351 total_size = ntohs (msg->size);
1353 if (total_size > size)
1358 memcpy (buf, msg, total_size);
1365 * Function to send a create path packet to a peer.
1367 * @param cls closure
1368 * @param size number of bytes available in buf
1369 * @param buf where the callee should write the message
1370 * @return number of bytes written to buf
1373 send_core_path_create (void *cls, size_t size, void *buf)
1375 struct MeshTunnel *t = cls;
1376 struct GNUNET_MESH_CreateTunnel *msg;
1377 struct GNUNET_PeerIdentity *peer_ptr;
1378 struct MeshPeerPath *p = t->path;
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
1385 sizeof (struct GNUNET_MESH_CreateTunnel) +
1386 p->length * sizeof (struct GNUNET_PeerIdentity);
1388 if (size < size_needed || NULL == buf)
1393 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
1394 msg->header.size = htons (size_needed);
1395 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1396 msg->tid = ntohl (t->id.tid);
1399 if (GNUNET_YES == t->nobuffer)
1400 opt |= GNUNET_MESH_OPTION_NOBUFFER;
1401 if (GNUNET_YES == t->reliable)
1402 opt |= GNUNET_MESH_OPTION_RELIABLE;
1403 msg->opt = htonl (opt);
1404 msg->port = htonl (t->port);
1406 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1407 for (i = 0; i < p->length; i++)
1409 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1413 "CREATE PATH (%u bytes long) sent!\n", size_needed);
1419 * Creates a path ack message in buf and frees all unused resources.
1421 * @param cls closure (MeshTransmissionDescriptor)
1422 * @param size number of bytes available in buf
1423 * @param buf where the callee should write the message
1424 * @return number of bytes written to buf
1427 send_core_path_ack (void *cls, size_t size, void *buf)
1429 struct MeshTunnel *t = cls;
1430 struct GNUNET_MESH_PathACK *msg = buf;
1432 GNUNET_assert (NULL != t);
1433 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1438 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
1439 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1440 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1441 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
1442 msg->tid = htonl (t->id.tid);
1443 msg->peer_id = my_full_id;
1444 msg->ack = htonl (t->prev_fc.last_ack_sent);
1446 /* TODO add signature */
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
1449 return sizeof (struct GNUNET_MESH_PathACK);
1454 * Iterator over all the peers to remove the oldest not-used entry.
1456 * @param cls Closure (unsued).
1457 * @param key ID of the peer.
1458 * @param value Peer_Info of the peer.
1463 peer_info_timeout (void *cls,
1464 const struct GNUNET_HashCode *key,
1471 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1472 * and insert it in the appropriate structures if the peer is not known yet.
1474 * @param peer Full identity of the peer.
1476 * @return Existing or newly created peer info.
1478 static struct MeshPeerInfo *
1479 peer_get (const struct GNUNET_PeerIdentity *peer)
1481 struct MeshPeerInfo *peer_info;
1483 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1484 if (NULL == peer_info)
1487 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1488 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1490 GNUNET_CONTAINER_multihashmap_iterate (peers,
1494 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1495 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1496 peer_info->id = GNUNET_PEER_intern (peer);
1498 peer_info->last_contact = GNUNET_TIME_absolute_get();
1505 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1506 * and insert it in the appropriate structures if the peer is not known yet.
1508 * @param peer Short identity of the peer.
1510 * @return Existing or newly created peer info.
1512 static struct MeshPeerInfo *
1513 peer_get_short (const GNUNET_PEER_Id peer)
1515 struct GNUNET_PeerIdentity id;
1517 GNUNET_PEER_resolve (peer, &id);
1518 return peer_get (&id);
1523 * Select which PID to POLL for, to compensate for lost messages.
1525 * @param pi Peer we want to poll.
1526 * @param t Tunnel about which we want to poll.
1528 * @return PID to use, either last sent or first_in_queue - 1
1531 peer_get_first_payload_pid (struct MeshPeerInfo *pi, struct MeshTunnel *t)
1533 struct MeshPeerQueue *q;
1536 type = pi->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST :
1537 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN;
1539 for (q = pi->queue_head; NULL != q; q = q->next)
1541 if (q->type == type && q->tunnel == t)
1543 struct GNUNET_MESH_Data *msg = q->cls;
1545 /* Pretend that the last one sent was the previous to this */
1546 return ntohl (msg->pid) - 1;
1550 /* No data in queue, use last sent */
1552 struct MeshFlowControl *fc;
1554 fc = pi->id == t->next_hop ? &t->next_fc : &t->prev_fc;
1555 return fc->last_pid_sent;
1561 * Choose the best path towards a peer considering the tunnel properties.
1563 * @param peer The destination peer.
1564 * @param t The tunnel the path is for.
1566 * @return Best current known path towards the peer, if any.
1568 static struct MeshPeerPath *
1569 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1571 struct MeshPeerPath *best_p;
1572 struct MeshPeerPath *p;
1573 unsigned int best_cost;
1576 best_p = p = peer->path_head;
1577 best_cost = cost = p->length;
1580 if ((cost = p->length) < best_cost)
1592 * Try to establish a new connection to this peer in the given tunnel.
1593 * If the peer doesn't have any path to it yet, try to get one.
1594 * If the peer already has some path, send a CREATE PATH towards it.
1596 * @param peer PeerInfo of the peer.
1597 * @param t Tunnel for which to create the path, if possible.
1600 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1602 struct MeshPeerPath *p;
1604 if (NULL != peer->path_head)
1606 p = peer_get_best_path (peer, t);
1607 tunnel_use_path (t, p);
1608 send_path_create (t);
1610 else if (NULL == peer->dhtget)
1612 struct GNUNET_PeerIdentity id;
1614 GNUNET_PEER_resolve (peer->id, &id);
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1617 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1618 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1619 &id.hashPubKey, /* key to search */
1620 dht_replication_level, /* replication level */
1621 GNUNET_DHT_RO_RECORD_ROUTE |
1622 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1624 0, /* xquery bits */
1625 &dht_get_id_handler, peer);
1626 tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "There is no path but the DHT GET is already started.\n");
1637 * @brief Re-initiate traffic to this peer if necessary.
1639 * Check if there is traffic queued towards this peer
1640 * and the core transmit handle is NULL (traffic was stalled).
1641 * If so, call core tmt rdy.
1643 * @param peer_id Short ID of peer to which initiate traffic.
1646 peer_unlock_queue (GNUNET_PEER_Id peer_id)
1648 struct MeshPeerInfo *peer;
1649 struct MeshPeerQueue *q;
1652 peer = peer_get_short (peer_id);
1653 if (NULL != peer->core_transmit)
1656 q = queue_get_next (peer);
1661 peer->core_transmit =
1662 GNUNET_CORE_notify_transmit_ready (core_handle,
1665 GNUNET_TIME_UNIT_FOREVER_REL,
1666 GNUNET_PEER_resolve2 (peer->id),
1674 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
1676 * @param t Tunnel which to cancel.
1677 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
1680 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
1682 struct MeshPeerInfo *peer_info;
1683 struct MeshPeerQueue *pq;
1684 struct MeshPeerQueue *next;
1687 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
1688 peer_info = peer_get_short (neighbor);
1689 for (pq = peer_info->queue_head; NULL != pq; pq = next)
1692 if (pq->tunnel == t)
1694 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
1695 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
1697 /* Should have been removed on destroy children */
1700 queue_destroy (pq, GNUNET_YES);
1703 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
1705 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
1706 peer_info->core_transmit = NULL;
1712 * Destroy the peer_info and free any allocated resources linked to it
1714 * @param pi The peer_info to destroy.
1716 * @return GNUNET_OK on success
1719 peer_info_destroy (struct MeshPeerInfo *pi)
1721 struct GNUNET_PeerIdentity id;
1722 struct MeshPeerPath *p;
1723 struct MeshPeerPath *nextp;
1726 GNUNET_PEER_resolve (pi->id, &id);
1727 GNUNET_PEER_change_rc (pi->id, -1);
1730 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1733 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1734 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1736 if (NULL != pi->dhtget)
1738 GNUNET_DHT_get_stop (pi->dhtget);
1744 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1748 for (i = 0; i < pi->ntunnels; i++)
1749 tunnel_destroy_empty (pi->tunnels[i]);
1750 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1757 * Remove all paths that rely on a direct connection between p1 and p2
1758 * from the peer itself and notify all tunnels about it.
1760 * @param peer PeerInfo of affected peer.
1761 * @param p1 GNUNET_PEER_Id of one peer.
1762 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1765 * TODO: optimize (see below)
1768 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1771 struct MeshPeerPath *p;
1772 struct MeshPeerPath *next;
1773 struct MeshPeerInfo *peer_d;
1775 unsigned int destroyed;
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1780 for (p = peer->path_head; NULL != p; p = next)
1783 for (i = 0; i < (p->length - 1); i++)
1785 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1786 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1788 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1798 for (i = 0; i < peer->ntunnels; i++)
1800 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1804 peer_d = peer_get_short (d);
1805 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1806 tunnel_use_path (peer->tunnels[i], next);
1807 peer_connect (peer_d, peer->tunnels[i]);
1809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1814 * Add the path to the peer and update the path used to reach it in case this
1817 * @param peer_info Destination peer to add the path to.
1818 * @param path New path to add. Last peer must be the peer in arg 1.
1819 * Path will be either used of freed if already known.
1820 * @param trusted Do we trust that this path is real?
1823 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1826 struct MeshPeerPath *aux;
1830 if ((NULL == peer_info) || (NULL == path))
1833 path_destroy (path);
1836 if (path->peers[path->length - 1] != peer_info->id)
1839 path_destroy (path);
1842 if (2 >= path->length && GNUNET_NO == trusted)
1844 /* Only allow CORE to tell us about direct paths */
1845 path_destroy (path);
1848 for (l = 1; l < path->length; l++)
1850 if (path->peers[l] == myid)
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1853 for (l2 = 0; l2 < path->length - l; l2++)
1855 path->peers[l2] = path->peers[l + l2];
1860 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1865 struct GNUNET_PeerIdentity id;
1867 GNUNET_PEER_resolve (peer_info->id, &id);
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1869 path->length, GNUNET_i2s (&id));
1872 l = path_get_length (path);
1875 path_destroy (path);
1879 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1880 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1882 l2 = path_get_length (aux);
1885 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1886 peer_info->path_tail, aux, path);
1891 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1893 path_destroy (path);
1898 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1905 * Add the path to the origin peer and update the path used to reach it in case
1906 * this is the shortest.
1907 * The path is given in peer_info -> destination, therefore we turn the path
1908 * upside down first.
1910 * @param peer_info Peer to add the path to, being the origin of the path.
1911 * @param path New path to add after being inversed.
1912 * Path will be either used or freed.
1913 * @param trusted Do we trust that this path is real?
1916 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1917 struct MeshPeerPath *path, int trusted)
1920 peer_info_add_path (peer_info, path, trusted);
1925 * Add a tunnel to the list of tunnels a peer participates in.
1926 * Update the tunnel's destination.
1928 * @param p Peer to add to.
1929 * @param t Tunnel to add.
1932 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1936 GNUNET_break (t->dest == p->id);
1940 GNUNET_PEER_change_rc (t->dest, 1);
1941 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1946 * Remove a tunnel from the list of tunnels a peer participates in.
1947 * Free the tunnel's destination.
1949 * @param p Peer to clean.
1950 * @param t Tunnel to remove.
1953 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1957 if (t->dest == p->id)
1959 GNUNET_PEER_change_rc (t->dest, -1);
1962 for (i = 0; i < p->ntunnels; i++)
1964 if (p->tunnels[i] == t)
1966 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1967 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1975 * Function called if the connection to the peer has been stalled for a while,
1976 * possibly due to a missed ACK. Poll the peer about its ACK status.
1978 * @param cls Closure (poll ctx).
1979 * @param tc TaskContext.
1982 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1984 struct MeshFlowControl *fc = cls;
1985 struct GNUNET_MESH_Poll msg;
1986 struct MeshTunnel *t = fc->t;
1987 GNUNET_PEER_Id peer;
1989 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1990 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1997 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1999 if (fc == &t->prev_fc)
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
2004 else if (fc == &t->next_fc)
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
2014 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2015 msg.header.size = htons (sizeof (msg));
2016 msg.tid = htonl (t->id.tid);
2017 msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t));
2018 send_prebuilt_message (&msg.header, peer, t);
2019 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
2020 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2026 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2027 * to obtain a local peer -> destination path and interning the peer ids.
2029 * @return Newly allocated and created path
2031 static struct MeshPeerPath *
2032 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2033 unsigned int get_path_length,
2034 const struct GNUNET_PeerIdentity *put_path,
2035 unsigned int put_path_length)
2037 struct MeshPeerPath *p;
2043 GNUNET_PEER_change_rc (myid, 1);
2044 i = get_path_length;
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2046 for (i--; i >= 0; i--)
2048 id = GNUNET_PEER_intern (&get_path[i]);
2049 if (p->length > 0 && id == p->peers[p->length - 1])
2051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2052 GNUNET_PEER_change_rc (id, -1);
2056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2057 GNUNET_i2s (&get_path[i]));
2059 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2060 p->peers[p->length - 1] = id;
2063 i = put_path_length;
2064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2065 for (i--; i >= 0; i--)
2067 id = GNUNET_PEER_intern (&put_path[i]);
2070 /* PUT path went through us, so discard the path up until now and start
2071 * from here to get a much shorter (and loop-free) path.
2076 if (p->length > 0 && id == p->peers[p->length - 1])
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2079 GNUNET_PEER_change_rc (id, -1);
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2084 GNUNET_i2s (&put_path[i]));
2086 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2087 p->peers[p->length - 1] = id;
2091 if (get_path_length > 0)
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2093 GNUNET_i2s (&get_path[0]));
2094 if (put_path_length > 0)
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2096 GNUNET_i2s (&put_path[0]));
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2099 for (i = 0; i < p->length; i++)
2101 struct GNUNET_PeerIdentity peer_id;
2103 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2105 GNUNET_i2s (&peer_id));
2113 * Adds a path to the peer_infos of all the peers in the path
2115 * @param p Path to process.
2116 * @param confirmed Whether we know if the path works or not.
2119 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2123 /* TODO: invert and add */
2124 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2125 for (i++; i < p->length; i++)
2127 struct MeshPeerInfo *aux;
2128 struct MeshPeerPath *copy;
2130 aux = peer_get_short (p->peers[i]);
2131 copy = path_duplicate (p);
2132 copy->length = i + 1;
2133 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2139 * Search for a tunnel among the incoming tunnels
2141 * @param tid the local id of the tunnel
2143 * @return tunnel handler, NULL if doesn't exist
2145 static struct MeshTunnel *
2146 tunnel_get_incoming (MESH_TunnelNumber tid)
2148 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2149 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
2154 * Search for a tunnel among the tunnels for a client
2156 * @param c the client whose tunnels to search in
2157 * @param tid the local id of the tunnel
2159 * @return tunnel handler, NULL if doesn't exist
2161 static struct MeshTunnel *
2162 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2164 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2166 GNUNET_break_op (0);
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TID %X not a local tid\n", tid);
2170 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2172 return tunnel_get_incoming (tid);
2176 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
2182 * Search for a tunnel by global ID using PEER_ID
2184 * @param pi owner of the tunnel
2185 * @param tid global tunnel number
2187 * @return tunnel handler, NULL if doesn't exist
2189 static struct MeshTunnel *
2190 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2192 struct MESH_TunnelID id;
2193 struct GNUNET_HashCode hash;
2198 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2199 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2204 * Search for a tunnel by global ID using full PeerIdentities
2206 * @param oid owner of the tunnel
2207 * @param tid global tunnel number
2209 * @return tunnel handler, NULL if doesn't exist
2211 static struct MeshTunnel *
2212 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2214 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2219 * Change the tunnel state.
2221 * @param t Tunnel whose ttate to change.
2222 * @param state New state.
2225 tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state)
2227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2228 "Tunnel %s[%X] state was %s\n",
2229 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid,
2230 GNUNET_MESH_DEBUG_S2S (t->state));
2231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2232 "Tunnel %s[%X] state is now %s\n",
2233 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid,
2234 GNUNET_MESH_DEBUG_S2S (state));
2241 * Add a client to a tunnel, initializing all needed data structures.
2243 * @param t Tunnel to which add the client.
2244 * @param c Client which to add to the tunnel.
2247 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2249 if (NULL != t->client)
2255 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
2256 t->local_tid_dest, t,
2257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2263 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
2264 t->local_tid_dest, t,
2265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2275 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
2277 unsigned int own_pos;
2279 for (own_pos = 0; own_pos < p->length; own_pos++)
2281 if (p->peers[own_pos] == myid)
2284 if (own_pos > p->length - 1)
2290 if (own_pos < p->length - 1)
2291 t->next_hop = p->peers[own_pos + 1];
2293 t->next_hop = p->peers[own_pos];
2294 GNUNET_PEER_change_rc (t->next_hop, 1);
2296 t->prev_hop = p->peers[own_pos - 1];
2298 t->prev_hop = p->peers[0];
2299 GNUNET_PEER_change_rc (t->prev_hop, 1);
2301 if (NULL != t->path)
2302 path_destroy (t->path);
2303 t->path = path_duplicate (p);
2306 if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
2307 GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
2308 t->fwd_maintenance_task =
2309 GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2310 &tunnel_fwd_keepalive, t);
2316 * Notifies a tunnel that a connection has broken that affects at least
2317 * some of its peers. Sends a notification towards the root of the tree.
2318 * In case the peer is the owner of the tree, notifies the client that owns
2319 * the tunnel and tries to reconnect.
2321 * @param t Tunnel affected.
2322 * @param p1 Peer that got disconnected from p2.
2323 * @param p2 Peer that got disconnected from p1.
2325 * @return Short ID of the peer disconnected (either p1 or p2).
2326 * 0 if the tunnel remained unaffected.
2328 static GNUNET_PEER_Id
2329 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2332 // if (myid != p1 && myid != p2) FIXME
2337 // if (tree_get_predecessor (t->tree) != 0)
2339 // /* We are the peer still connected, notify owner of the disconnection. */
2340 // struct GNUNET_MESH_PathBroken msg;
2341 // struct GNUNET_PeerIdentity neighbor;
2343 // msg.header.size = htons (sizeof (msg));
2344 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2345 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2346 // msg.tid = htonl (t->id.tid);
2347 // msg.peer1 = my_full_id;
2348 // GNUNET_PEER_resolve (pid, &msg.peer2);
2349 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2350 // send_prebuilt_message (&msg.header, &neighbor, t);
2357 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2359 * @param t Tunnel this is about.
2360 * @param fwd Is for FWD traffic? (ACK dest->owner)
2363 tunnel_send_data_ack (struct MeshTunnel *t, int fwd)
2365 struct GNUNET_MESH_DataACK msg;
2366 struct MeshTunnelReliability *rel;
2367 struct MeshReliableMessage *copy;
2372 rel = fwd ? t->bck_rel : t->fwd_rel;
2373 hop = fwd ? t->prev_hop : t->next_hop;
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2375 "send_data_ack for %u\n",
2378 if (GNUNET_NO == t->reliable)
2380 GNUNET_break_op (0);
2383 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2384 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2385 msg.header.size = htons (sizeof (msg));
2386 msg.tid = htonl (t->id.tid);
2387 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2388 msg.mid = htonl (rel->mid_recv - 1);
2390 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2392 delta = copy->mid - rel->mid_recv;
2395 mask = 0x1LL << delta;
2396 msg.futures |= mask;
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2398 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2399 copy->mid, delta, mask, msg.futures);
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2403 send_prebuilt_message (&msg.header, hop, t);
2404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2409 * Send an ACK informing the predecessor about the available buffer space.
2410 * In case there is no predecessor, inform the owning client.
2411 * If buffering is off, send only on behalf of children or self if endpoint.
2412 * If buffering is on, send when sent to children and buffer space is free.
2413 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2414 * the ACK itself goes "back" (towards root).
2416 * @param t Tunnel on which to send the ACK.
2417 * @param type Type of message that triggered the ACK transmission.
2418 * @param fwd Is this FWD ACK? (Going dest->owner)
2421 tunnel_send_ack (struct MeshTunnel *t, uint16_t type, int fwd)
2423 struct MeshTunnelReliability *rel;
2424 struct MeshFlowControl *next_fc;
2425 struct MeshFlowControl *prev_fc;
2426 struct MeshClient *c;
2427 struct MeshClient *o;
2433 rel = fwd ? t->fwd_rel : t->bck_rel;
2434 c = fwd ? t->client : t->owner;
2435 o = fwd ? t->owner : t->client;
2436 next_fc = fwd ? &t->next_fc : &t->prev_fc;
2437 prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2438 hop = fwd ? t->prev_hop : t->next_hop;
2442 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2443 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 GNUNET_MESH_DEBUG_M2S (type));
2447 if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c))
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2452 if (GNUNET_YES == t->reliable && NULL != c)
2453 tunnel_send_data_ack (t, fwd);
2455 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2456 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2457 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2458 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2460 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2461 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2462 t->force_ack = GNUNET_YES;
2468 /* Check if we need to transmit the ACK */
2470 prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 &&
2471 GNUNET_NO == t->force_ack)
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2475 " last pid recv: %u, last ack sent: %u\n",
2476 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2480 /* Ok, ACK might be necessary, what PID to ACK? */
2481 delta = t->queue_max - next_fc->queue_n;
2482 if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2483 delta_mid = rel->mid_sent - rel->head_sent->mid;
2486 if (0 > delta || (GNUNET_YES == t->reliable &&
2488 (10 < rel->n_sent || 64 <= delta_mid)))
2490 if (NULL != o && delta > 1)
2492 ack = prev_fc->last_pid_recv + delta;
2493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 " last pid %u, last ack %u, qmax %u, q %u\n",
2496 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2497 t->queue_max, next_fc->queue_n);
2498 if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2504 prev_fc->last_ack_sent = ack;
2506 send_local_ack (t, o, fwd);
2508 send_ack (t, hop, ack);
2510 GNUNET_break (GNUNET_YES == t->destroy);
2511 t->force_ack = GNUNET_NO;
2516 * Modify the mesh message TID from global to local and send to client.
2518 * @param t Tunnel on which to send the message.
2519 * @param msg Message to modify and send.
2520 * @param c Client to send to.
2521 * @param tid Tunnel ID to use (c can be both owner and client).
2524 tunnel_send_client_to_tid (struct MeshTunnel *t,
2525 const struct GNUNET_MESH_Data *msg,
2526 struct MeshClient *c, MESH_TunnelNumber tid)
2528 struct GNUNET_MESH_LocalData *copy;
2529 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2530 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2532 if (size < sizeof (struct GNUNET_MessageHeader))
2534 GNUNET_break_op (0);
2542 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2543 memcpy (©[1], &msg[1], size);
2544 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2545 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2546 copy->tid = htonl (tid);
2547 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2548 ©->header, GNUNET_NO);
2552 * Modify the unicast 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 fwd Forward?
2559 tunnel_send_client_data (struct MeshTunnel *t,
2560 const struct GNUNET_MESH_Data *msg,
2564 tunnel_send_client_to_tid (t, msg, t->client, t->local_tid_dest);
2566 tunnel_send_client_to_tid (t, msg, t->owner, t->local_tid);
2571 * Send up to 64 buffered messages to the client for in order delivery.
2573 * @param t Tunnel on which to empty the message buffer.
2574 * @param c Client to send to.
2575 * @param rel Reliability structure to corresponding peer.
2576 * If rel == t->bck_rel, this is FWD data.
2579 tunnel_send_client_buffered_data (struct MeshTunnel *t, struct MeshClient *c,
2580 struct MeshTunnelReliability *rel)
2583 struct MeshReliableMessage *copy;
2584 struct MeshReliableMessage *next;
2586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2587 for (copy = rel->head_recv; NULL != copy; copy = next)
2590 if (copy->mid == rel->mid_recv)
2592 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2595 " have %u! now expecting %u\n",
2596 copy->mid, rel->mid_recv + 1);
2597 tunnel_send_client_data (t, msg, (rel == t->bck_rel));
2599 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 " don't have %u, next is %u\n",
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2616 * We have received a message out of order, buffer it until we receive
2617 * the missing one and we can feed the rest to the client.
2619 * @param t Tunnel to add to.
2620 * @param msg Message to buffer.
2621 * @param rel Reliability data to the corresponding direction.
2624 tunnel_add_buffered_data (struct MeshTunnel *t,
2625 const struct GNUNET_MESH_Data *msg,
2626 struct MeshTunnelReliability *rel)
2628 struct MeshReliableMessage *copy;
2629 struct MeshReliableMessage *prev;
2633 size = ntohs (msg->header.size);
2634 mid = ntohl (msg->mid);
2635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
2637 copy = GNUNET_malloc (sizeof (*copy) + size);
2640 memcpy (©[1], msg, size);
2642 // FIXME do something better than O(n), although n < 64...
2643 // FIXME start from the end (most messages are the latest ones)
2644 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
2647 if (GMC_is_pid_bigger (prev->mid, mid))
2649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2650 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2656 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
2662 * Destroy a reliable message after it has been acknowledged, either by
2663 * direct mid ACK or bitfield. Updates the appropriate data structures and
2664 * timers and frees all memory.
2666 * @param copy Message that is no longer needed: remote peer got it.
2669 tunnel_free_reliable_message (struct MeshReliableMessage *copy)
2671 struct MeshTunnelReliability *rel;
2672 struct GNUNET_TIME_Relative time;
2675 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2676 rel->expected_delay.rel_value *= 7;
2677 rel->expected_delay.rel_value += time.rel_value;
2678 rel->expected_delay.rel_value /= 8;
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
2683 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
2685 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
2687 rel->retry_timer = rel->expected_delay;
2688 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2694 * Destroy all reliable messages queued for a tunnel,
2695 * during a tunnel destruction.
2696 * Frees the reliability structure itself.
2698 * @param rel Reliability data for a tunnel.
2701 tunnel_free_reliable_all (struct MeshTunnelReliability *rel)
2703 struct MeshReliableMessage *copy;
2704 struct MeshReliableMessage *next;
2709 for (copy = rel->head_recv; NULL != copy; copy = next)
2712 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2715 for (copy = rel->head_sent; NULL != copy; copy = next)
2718 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2721 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
2722 GNUNET_SCHEDULER_cancel (rel->retry_task);
2728 * Mark future messages as ACK'd.
2730 * @param t Tunnel whose sent buffer to clean.
2731 * @param msg DataACK message with a bitfield of future ACK'd messages.
2732 * @param rel Reliability data.
2735 tunnel_free_sent_reliable (struct MeshTunnel *t,
2736 const struct GNUNET_MESH_DataACK *msg,
2737 struct MeshTunnelReliability *rel)
2739 struct MeshReliableMessage *copy;
2740 struct MeshReliableMessage *next;
2747 bitfield = msg->futures;
2748 mid = ntohl (msg->mid);
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2750 "free_sent_reliable %u %llX\n",
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2753 " rel %p, head %p\n",
2754 rel, rel->head_sent);
2755 for (i = 0, copy = rel->head_sent;
2756 i < 64 && NULL != copy && 0 != bitfield;
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2760 " trying bit %u (mid %u)\n",
2763 if (0 == (bitfield & mask))
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2767 /* Bit was set, clear the bit from the bitfield */
2770 /* The i-th bit was set. Do we have that copy? */
2771 /* Skip copies with mid < target */
2772 target = mid + i + 1;
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
2774 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
2777 /* Did we run out of copies? (previously freed, it's ok) */
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2784 /* Did we overshoot the target? (previously freed, it's ok) */
2785 if (GMC_is_pid_bigger (copy->mid, target))
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
2791 /* Now copy->mid == target, free it */
2793 tunnel_free_reliable_message (copy);
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
2801 * We haven't received an ACK after a certain time: restransmit the message.
2803 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2804 * @param tc TaskContext.
2807 tunnel_retransmit_message (void *cls,
2808 const struct GNUNET_SCHEDULER_TaskContext *tc)
2810 struct MeshTunnelReliability *rel = cls;
2811 struct MeshReliableMessage *copy;
2812 struct MeshFlowControl *fc;
2813 struct MeshPeerQueue *q;
2814 struct MeshPeerInfo *pi;
2815 struct MeshTunnel *t;
2816 struct GNUNET_MESH_Data *payload;
2819 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2820 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2824 copy = rel->head_sent;
2831 /* Search the message to be retransmitted in the outgoing queue */
2832 payload = (struct GNUNET_MESH_Data *) ©[1];
2833 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2834 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2835 pi = peer_get_short (hop);
2836 for (q = pi->queue_head; NULL != q; q = q->next)
2838 if (ntohs (payload->header.type) == q->type)
2840 struct GNUNET_MESH_Data *queued_data = q->cls;
2842 if (queued_data->mid == payload->mid)
2847 /* Message not found in the queue */
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
2852 fc->last_ack_sent++;
2853 fc->last_pid_recv++;
2854 payload->pid = htonl (fc->last_pid_recv);
2855 send_prebuilt_message (&payload->header, hop, t);
2856 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %u\n", copy->mid);
2863 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2864 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2865 &tunnel_retransmit_message,
2871 * Send keepalive packets for a tunnel.
2873 * @param t Tunnel to keep alive..
2874 * @param fwd Is this a FWD keepalive? (owner -> dest).
2877 tunnel_keepalive (struct MeshTunnel *t, int fwd)
2879 struct GNUNET_MESH_TunnelKeepAlive *msg;
2880 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
2885 type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
2886 GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
2887 hop = fwd ? t->next_hop : t->prev_hop;
2889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2890 "sending %s keepalive for tunnel %d\n",
2891 fwd ? "FWD" : "BCK", t->id.tid);
2893 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
2894 msg->header.size = htons (size);
2895 msg->header.type = htons (type);
2896 msg->oid = *(GNUNET_PEER_resolve2 (t->id.oid));
2897 msg->tid = htonl (t->id.tid);
2898 send_prebuilt_message (&msg->header, hop, t);
2903 * Send keepalive packets for a tunnel.
2905 * @param cls Closure (tunnel for which to send the keepalive).
2906 * @param tc Notification context.
2909 tunnel_recreate (struct MeshTunnel *t, int fwd)
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2912 "sending path recreate for tunnel %s[%X]\n",
2913 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
2915 send_path_create (t);
2922 * Generic tunnel timer management.
2923 * Depending on the role of the peer in the tunnel will send the
2924 * appropriate message (build or keepalive)
2926 * @param t Tunnel to maintain.
2930 tunnel_maintain (struct MeshTunnel *t, int fwd)
2934 case MESH_TUNNEL_NEW:
2936 case MESH_TUNNEL_SEARCHING:
2937 /* TODO DHT GET with RO_BART */
2939 case MESH_TUNNEL_WAITING:
2940 tunnel_recreate (t, fwd);
2942 case MESH_TUNNEL_READY:
2943 tunnel_keepalive (t, fwd);
2952 tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2954 struct MeshTunnel *t = cls;
2956 t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2957 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
2961 tunnel_maintain (t, GNUNET_YES);
2962 t->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2963 &tunnel_fwd_keepalive,
2969 tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2971 struct MeshTunnel *t = cls;
2973 t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2974 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
2978 tunnel_keepalive (t, GNUNET_NO);
2979 t->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2980 &tunnel_bck_keepalive,
2986 * Send a message to all peers and clients in this tunnel that the tunnel
2987 * is no longer valid. If some peer or client should not receive the message,
2988 * should be zero'ed out before calling this function.
2990 * @param t The tunnel whose peers and clients to notify.
2993 tunnel_send_destroy (struct MeshTunnel *t)
2995 struct GNUNET_MESH_TunnelDestroy msg;
2996 struct GNUNET_PeerIdentity id;
2998 msg.header.size = htons (sizeof (msg));
2999 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3000 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3001 msg.tid = htonl (t->id.tid);
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3003 " sending tunnel destroy for tunnel: %s [%X]\n",
3004 GNUNET_i2s (&msg.oid), t->id.tid);
3006 if (NULL == t->client && 0 != t->next_hop)
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
3009 GNUNET_PEER_resolve (t->next_hop, &id);
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3011 " sending forward to %s\n",
3013 send_prebuilt_message (&msg.header, t->next_hop, t);
3015 if (NULL == t->owner && 0 != t->prev_hop)
3017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
3018 GNUNET_PEER_resolve (t->prev_hop, &id);
3019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3020 " sending back to %s\n",
3022 send_prebuilt_message (&msg.header, t->prev_hop, t);
3024 if (NULL != t->owner)
3026 send_local_tunnel_destroy (t, GNUNET_NO);
3028 if (NULL != t->client)
3030 send_local_tunnel_destroy (t, GNUNET_YES);
3036 * Destroy the tunnel.
3038 * This function does not generate any warning traffic to clients or peers.
3041 * Remove the tunnel from peer_info's and clients' hashmaps.
3042 * Cancel messages belonging to this tunnel queued to neighbors.
3043 * Free any allocated resources linked to the tunnel.
3045 * @param t the tunnel to destroy
3047 * @return GNUNET_OK on success
3050 tunnel_destroy (struct MeshTunnel *t)
3052 struct MeshClient *c;
3053 struct GNUNET_HashCode hash;
3062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3063 GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3067 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3068 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3076 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
3088 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
3089 t->local_tid_dest, t))
3095 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
3096 t->local_tid_dest, t))
3103 if(GNUNET_YES == t->reliable)
3105 tunnel_free_reliable_all (t->fwd_rel);
3106 tunnel_free_reliable_all (t->bck_rel);
3108 if (0 != t->prev_hop)
3110 peer_cancel_queues (t->prev_hop, t);
3111 GNUNET_PEER_change_rc (t->prev_hop, -1);
3113 if (0 != t->next_hop)
3115 peer_cancel_queues (t->next_hop, t);
3116 GNUNET_PEER_change_rc (t->next_hop, -1);
3118 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task)
3120 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3121 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3123 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task)
3125 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3126 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3129 peer_info_remove_tunnel (peer_get_short (t->dest), t);
3132 if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
3133 GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
3134 if (GNUNET_SCHEDULER_NO_TASK != t->bck_maintenance_task)
3135 GNUNET_SCHEDULER_cancel (t->bck_maintenance_task);
3138 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3139 path_destroy (t->path);
3145 * Tunnel is empty: destroy it.
3147 * Notifies all participants (peers, cleints) about the destruction.
3149 * @param t Tunnel to destroy.
3152 tunnel_destroy_empty (struct MeshTunnel *t)
3156 struct GNUNET_PeerIdentity id;
3158 GNUNET_PEER_resolve (t->id.oid, &id);
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3160 "executing destruction of empty tunnel %s [%X]\n",
3161 GNUNET_i2s (&id), t->id.tid);
3165 if (GNUNET_NO == t->destroy)
3166 tunnel_send_destroy (t);
3167 if (0 == t->pending_messages)
3170 t->destroy = GNUNET_YES;
3174 * Initialize a Flow Control structure to the initial state.
3176 * @param fc Flow Control structure to initialize.
3179 fc_init (struct MeshFlowControl *fc)
3181 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
3182 fc->last_pid_recv = (uint32_t) -1;
3183 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
3184 fc->last_ack_recv = (uint32_t) -1;
3185 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3186 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
3191 * Create a new tunnel
3193 * @param owner Who is the owner of the tunnel (short ID).
3194 * @param tid Tunnel Number of the tunnel.
3195 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3196 * @param local Tunnel Number for the tunnel, for the client point of view.
3198 * @return A new initialized tunnel. NULL on error.
3200 static struct MeshTunnel *
3201 tunnel_new (GNUNET_PEER_Id owner,
3202 MESH_TunnelNumber tid,
3203 struct MeshClient *client,
3204 MESH_TunnelNumber local)
3206 struct MeshTunnel *t;
3207 struct GNUNET_HashCode hash;
3209 if (n_tunnels >= max_tunnels && NULL == client)
3212 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3215 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
3217 fc_init (&t->next_fc);
3218 fc_init (&t->prev_fc);
3221 t->local_tid = local;
3223 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3225 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3227 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3228 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3235 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3243 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
3245 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3249 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3259 * Set options in a tunnel, extracted from a bit flag field
3261 * @param t Tunnel to set options to.
3262 * @param options Bit array in host byte order.
3265 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
3267 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
3268 GNUNET_YES : GNUNET_NO;
3269 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
3270 GNUNET_YES : GNUNET_NO;
3275 * Iterator for deleting each tunnel whose client endpoint disconnected.
3277 * @param cls Closure (client that has disconnected).
3278 * @param key The local tunnel id (used to access the hashmap).
3279 * @param value The value stored at the key (tunnel to destroy).
3281 * @return GNUNET_OK, keep iterating.
3284 tunnel_destroy_iterator (void *cls,
3288 struct MeshTunnel *t = value;
3289 struct MeshClient *c = cls;
3291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3292 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
3293 t->local_tid, t->local_tid_dest, c->id);
3294 client_delete_tunnel (c, t);
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
3299 if (0 != t->next_hop) { /* destroy could come before a path is used */
3300 GNUNET_PEER_change_rc (t->next_hop, -1);
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
3308 if (0 != t->prev_hop) { /* destroy could come before a path is used */
3309 GNUNET_PEER_change_rc (t->prev_hop, -1);
3314 tunnel_destroy_empty (t);
3321 * remove client's ports from the global hashmap on diconnect.
3323 * @param cls Closure (unused).
3325 * @param value ThClient structure.
3327 * @return GNUNET_OK, keep iterating.
3330 client_release_ports (void *cls,
3336 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
3337 if (GNUNET_YES != res)
3340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3341 "Port %u by client %p was not registered.\n",
3348 * Timeout function due to lack of keepalive/traffic from the owner.
3349 * Destroys tunnel if called.
3351 * @param cls Closure (tunnel to destroy).
3352 * @param tc TaskContext
3355 tunnel_fwd_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3357 struct MeshTunnel *t = cls;
3359 t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3360 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3363 "Tunnel %s [%X] FWD timed out. Destroying.\n",
3364 GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3365 if (NULL != t->client)
3366 send_local_tunnel_destroy (t, GNUNET_YES);
3367 tunnel_destroy (t); /* Do not notify other */
3372 * Timeout function due to lack of keepalive/traffic from the destination.
3373 * Destroys tunnel if called.
3375 * @param cls Closure (tunnel to destroy).
3376 * @param tc TaskContext
3379 tunnel_bck_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3381 struct MeshTunnel *t = cls;
3383 t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3384 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3387 "Tunnel %s [%X] BCK timed out. Destroying.\n",
3388 GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
3389 if (NULL != t->owner)
3390 send_local_tunnel_destroy (t, GNUNET_NO);
3391 tunnel_destroy (t); /* Do not notify other */
3396 * Resets the tunnel timeout task, some other message has done the task's job.
3397 * - For the first peer on the direction this means to send
3398 * a keepalive or a path confirmation message (either create or ACK).
3399 * - For all other peers, this means to destroy the tunnel,
3400 * due to lack of activity.
3401 * Starts the tiemout if no timeout was running (tunnel just created).
3403 * @param t Tunnel whose timeout to reset.
3404 * @param fwd Is this forward?
3406 * TODO use heap to improve efficiency of scheduler.
3409 tunnel_reset_timeout (struct MeshTunnel *t, int fwd)
3411 GNUNET_SCHEDULER_TaskIdentifier *ti;
3412 GNUNET_SCHEDULER_Task f;
3413 struct MeshClient *c;
3415 ti = fwd ? &t->fwd_maintenance_task : &t->bck_maintenance_task;
3416 c = fwd ? t->owner : t->client;
3418 if (GNUNET_SCHEDULER_NO_TASK != *ti)
3419 GNUNET_SCHEDULER_cancel (*ti);
3423 f = fwd ? &tunnel_fwd_keepalive : &tunnel_bck_keepalive;
3424 *ti = GNUNET_SCHEDULER_add_delayed (refresh_path_time, f, t);
3428 f = fwd ? &tunnel_fwd_timeout : &tunnel_bck_timeout;
3429 *ti = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3430 (refresh_path_time, 4),
3436 /******************************************************************************/
3437 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3438 /******************************************************************************/
3441 * Free a transmission that was already queued with all resources
3442 * associated to the request.
3444 * @param queue Queue handler to cancel.
3445 * @param clear_cls Is it necessary to free associated cls?
3448 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3450 struct MeshFlowControl *fc;
3452 if (GNUNET_YES == clear_cls)
3454 switch (queue->type)
3456 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3457 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3458 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3460 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3461 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3462 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3463 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3464 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3465 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3466 case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
3467 case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3469 " prebuilt message\n");
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3472 GNUNET_MESH_DEBUG_M2S (queue->type));
3474 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3480 " type %s unknown!\n",
3481 GNUNET_MESH_DEBUG_M2S (queue->type));
3483 GNUNET_free_non_null (queue->cls);
3485 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3486 queue->peer->queue_tail,
3489 /* Delete from appropriate fc in the tunnel */
3490 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3491 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3493 if (queue->peer->id == queue->tunnel->prev_hop)
3494 fc = &queue->tunnel->prev_fc;
3495 else if (queue->peer->id == queue->tunnel->next_hop)
3496 fc = &queue->tunnel->next_fc;
3500 GNUNET_free (queue);
3505 GNUNET_free (queue);
3510 * @brief Get the next transmittable message from the queue.
3512 * This will be the head, except in the case of being a data packet
3513 * not allowed by the destination peer.
3515 * @param peer Destination peer.
3517 * @return The next viable MeshPeerQueue element to send to that peer.
3518 * NULL when there are no transmittable messages.
3520 struct MeshPeerQueue *
3521 queue_get_next (const struct MeshPeerInfo *peer)
3523 struct MeshPeerQueue *q;
3525 struct GNUNET_MESH_Data *dmsg;
3526 struct MeshTunnel* t;
3530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3531 for (q = peer->queue_head; NULL != q; q = q->next)
3534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3536 GNUNET_MESH_DEBUG_M2S (q->type));
3537 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3538 pid = ntohl (dmsg->pid);
3541 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3542 ack = t->next_fc.last_ack_recv;
3544 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3545 ack = t->prev_fc.last_ack_recv;
3548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3553 "* ACK: %u, PID: %u, MID: %u\n",
3554 ack, pid, ntohl (dmsg->mid));
3555 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3568 "* nothing found\n");
3574 queue_send (void *cls, size_t size, void *buf)
3576 struct MeshPeerInfo *peer = cls;
3577 struct GNUNET_MessageHeader *msg;
3578 struct MeshPeerQueue *queue;
3579 struct MeshTunnel *t;
3580 struct GNUNET_PeerIdentity dst_id;
3581 struct MeshFlowControl *fc;
3586 peer->core_transmit = NULL;
3588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3589 queue = queue_get_next (peer);
3591 /* Queue has no internal mesh traffic nor sendable payload */
3594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3595 if (NULL == peer->queue_head)
3596 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3601 GNUNET_PEER_resolve (peer->id, &dst_id);
3602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3604 GNUNET_i2s (&dst_id));
3605 /* Check if buffer size is enough for the message */
3606 if (queue->size > size)
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3609 "* not enough room, reissue\n");
3610 peer->core_transmit =
3611 GNUNET_CORE_notify_transmit_ready (core_handle,
3614 GNUNET_TIME_UNIT_FOREVER_REL,
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3624 GNUNET_assert (0 < t->pending_messages);
3625 t->pending_messages--;
3629 switch (queue->type)
3632 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3633 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3634 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3635 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3636 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3637 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3638 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3639 case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
3640 case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3643 GNUNET_MESH_DEBUG_M2S (queue->type));
3645 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3646 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3647 data_size = send_core_data_raw (queue->cls, size, buf);
3648 msg = (struct GNUNET_MessageHeader *) buf;
3649 type = ntohs (msg->type);
3651 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3653 data_size = send_core_path_create (queue->cls, size, buf);
3655 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3657 data_size = send_core_path_ack (queue->cls, size, buf);
3661 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3662 "* type unknown: %u\n",
3667 if (0 < drop_percent &&
3668 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3670 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3671 "Dropping message of type %s\n",
3672 GNUNET_MESH_DEBUG_M2S(queue->type));
3675 /* Free queue, but cls was freed by send_core_* */
3676 queue_destroy (queue, GNUNET_NO);
3678 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3679 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3683 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3684 t->next_fc.last_pid_sent = pid;
3685 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
3686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3688 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3690 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3691 t->prev_fc.last_pid_sent = pid;
3692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3694 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3695 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
3701 /* If more data in queue, send next */
3702 queue = queue_get_next (peer);
3705 struct GNUNET_PeerIdentity id;
3707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3708 GNUNET_PEER_resolve (peer->id, &id);
3709 peer->core_transmit =
3710 GNUNET_CORE_notify_transmit_ready(core_handle,
3713 GNUNET_TIME_UNIT_FOREVER_REL,
3719 if (peer->id == t->next_hop)
3721 else if (peer->id == t->prev_hop)
3726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n",
3727 peer->id, t->next_hop, t->prev_hop);
3730 if (NULL != peer->queue_head)
3732 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
3734 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3735 "* %s starting poll timeout\n",
3736 GNUNET_i2s (&my_full_id));
3737 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3743 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3745 GNUNET_SCHEDULER_cancel (fc->poll_task);
3746 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3749 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3760 * @brief Queue and pass message to core when possible.
3762 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3763 * accounts for it. In case the queue is full, the message is dropped and
3766 * Otherwise, message is treated as internal and allowed to go regardless of
3769 * @param cls Closure (@c type dependant). It will be used by queue_send to
3770 * build the message to be sent if not already prebuilt.
3771 * @param type Type of the message, 0 for a raw message.
3772 * @param size Size of the message.
3773 * @param dst Neighbor to send message to.
3774 * @param t Tunnel this message belongs to.
3777 queue_add (void *cls, uint16_t type, size_t size,
3778 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3780 struct MeshPeerQueue *queue;
3781 struct GNUNET_PeerIdentity id;
3782 struct MeshFlowControl *fc;
3786 priority = GNUNET_NO;
3787 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3791 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3797 if (fc->queue_n >= t->queue_max)
3799 /* If this isn't a retransmission, drop the message */
3800 if (GNUNET_NO == t->reliable ||
3801 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3802 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3804 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3808 "queue full: %u/%u\n",
3809 fc->queue_n, t->queue_max);
3810 return; /* Drop this message */
3812 priority = GNUNET_YES;
3815 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
3816 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3817 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3821 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3827 if (GNUNET_YES == priority)
3829 struct GNUNET_MESH_Data *d;
3833 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3834 d = (struct GNUNET_MESH_Data *) queue->cls;
3836 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3838 if (queue->type != type)
3840 d = (struct GNUNET_MESH_Data *) queue->cls;
3847 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3849 if (NULL == dst->core_transmit)
3851 GNUNET_PEER_resolve (dst->id, &id);
3852 dst->core_transmit =
3853 GNUNET_CORE_notify_transmit_ready (core_handle,
3856 GNUNET_TIME_UNIT_FOREVER_REL,
3862 t->pending_messages++;
3866 /******************************************************************************/
3867 /******************** MESH NETWORK HANDLERS **************************/
3868 /******************************************************************************/
3872 * Core handler for path creation
3874 * @param cls closure
3875 * @param message message
3876 * @param peer peer identity this notification is about
3878 * @return GNUNET_OK to keep the connection open,
3879 * GNUNET_SYSERR to close it (signal serious error)
3882 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3883 const struct GNUNET_MessageHeader *message)
3885 unsigned int own_pos;
3888 MESH_TunnelNumber tid;
3889 struct GNUNET_MESH_CreateTunnel *msg;
3890 struct GNUNET_PeerIdentity *pi;
3891 struct MeshPeerPath *path;
3892 struct MeshPeerInfo *dest_peer_info;
3893 struct MeshPeerInfo *orig_peer_info;
3894 struct MeshTunnel *t;
3896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3897 "Received a path create msg [%s]\n",
3898 GNUNET_i2s (&my_full_id));
3899 size = ntohs (message->size);
3900 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3902 GNUNET_break_op (0);
3906 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3907 if (size % sizeof (struct GNUNET_PeerIdentity))
3909 GNUNET_break_op (0);
3912 size /= sizeof (struct GNUNET_PeerIdentity);
3915 GNUNET_break_op (0);
3918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3919 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3921 tid = ntohl (msg->tid);
3922 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3924 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3925 t = tunnel_get (pi, tid);
3926 if (NULL == t) /* might be a local tunnel */
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3931 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3937 t->port = ntohl (msg->port);
3938 opt = ntohl (msg->opt);
3939 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3941 t->nobuffer = GNUNET_YES;
3944 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3946 t->reliable = GNUNET_YES;
3948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3950 tunnel_reset_timeout (t, GNUNET_YES);
3951 tunnel_change_state (t, MESH_TUNNEL_WAITING);
3953 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3954 if (NULL == dest_peer_info)
3956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3957 " Creating PeerInfo for destination.\n");
3958 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3959 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3960 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3962 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3964 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3965 if (NULL == orig_peer_info)
3967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3968 " Creating PeerInfo for origin.\n");
3969 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3970 orig_peer_info->id = GNUNET_PEER_intern (pi);
3971 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3972 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3975 path = path_new (size);
3977 for (i = 0; i < size; i++)
3979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3980 GNUNET_i2s (&pi[i]));
3981 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3982 if (path->peers[i] == myid)
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3986 if (own_pos == 0 && path->peers[own_pos] != myid)
3988 /* create path: self not found in path through self */
3989 GNUNET_break_op (0);
3990 path_destroy (path);
3994 path_add_to_peers (path, GNUNET_NO);
3995 tunnel_use_path (t, path);
3997 peer_info_add_tunnel (dest_peer_info, t);
3999 if (own_pos == size - 1)
4001 struct MeshClient *c;
4003 /* Find target client */
4004 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
4007 /* TODO send reject */
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4012 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
4014 /* Assign local tid */
4015 while (NULL != tunnel_get_incoming (next_local_tid))
4016 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4017 t->local_tid_dest = next_local_tid++;
4018 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4020 if (GNUNET_YES == t->reliable)
4022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4023 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4025 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
4028 tunnel_add_client (t, c);
4029 send_local_tunnel_create (t);
4031 /* Eliminate tunnel when origin dies */
4032 tunnel_reset_timeout (t, GNUNET_YES);
4033 /* Keep tunnel alive in direction dest->owner*/
4034 tunnel_reset_timeout (t, GNUNET_NO);
4038 struct MeshPeerPath *path2;
4040 t->next_hop = path->peers[own_pos + 1];
4041 GNUNET_PEER_change_rc(t->next_hop, 1);
4043 /* It's for somebody else! Retransmit. */
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4045 path2 = path_duplicate (path);
4046 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4047 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4048 send_path_create (t);
4056 * Core handler for path ACKs
4058 * @param cls closure
4059 * @param message message
4060 * @param peer peer identity this notification is about
4062 * @return GNUNET_OK to keep the connection open,
4063 * GNUNET_SYSERR to close it (signal serious error)
4066 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4067 const struct GNUNET_MessageHeader *message)
4069 struct GNUNET_MESH_PathACK *msg;
4070 struct MeshPeerInfo *peer_info;
4071 struct MeshPeerPath *p;
4072 struct MeshTunnel *t;
4074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4075 GNUNET_i2s (&my_full_id));
4076 msg = (struct GNUNET_MESH_PathACK *) message;
4077 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4080 /* TODO notify that we don't know the tunnel */
4081 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4083 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4087 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4089 peer_info = peer_get (&msg->peer_id);
4090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4091 GNUNET_i2s (&msg->peer_id));
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4095 /* Add path to peers? */
4099 path_add_to_peers (p, GNUNET_YES);
4105 tunnel_change_state (t, MESH_TUNNEL_READY);
4106 tunnel_reset_timeout (t, GNUNET_NO);
4107 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
4108 t->prev_fc.last_ack_sent = ntohl (msg->ack);
4110 /* Message for us? */
4111 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4114 if (NULL == t->owner)
4116 GNUNET_break_op (0);
4119 if (NULL != peer_info->dhtget)
4121 GNUNET_DHT_get_stop (peer_info->dhtget);
4122 peer_info->dhtget = NULL;
4124 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES);
4125 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO);
4129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4130 " not for us, retransmitting...\n");
4131 send_prebuilt_message (message, t->prev_hop, t);
4137 * Core handler for notifications of broken paths
4139 * @param cls closure
4140 * @param message message
4141 * @param peer peer identity this notification is about
4143 * @return GNUNET_OK to keep the connection open,
4144 * GNUNET_SYSERR to close it (signal serious error)
4147 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4148 const struct GNUNET_MessageHeader *message)
4150 struct GNUNET_MESH_PathBroken *msg;
4151 struct MeshTunnel *t;
4153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4154 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4155 msg = (struct GNUNET_MESH_PathBroken *) message;
4156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4157 GNUNET_i2s (&msg->peer1));
4158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4159 GNUNET_i2s (&msg->peer2));
4160 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4163 GNUNET_break_op (0);
4166 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4167 GNUNET_PEER_search (&msg->peer2));
4174 * Core handler for tunnel destruction
4176 * @param cls closure
4177 * @param message message
4178 * @param peer peer identity this notification is about
4180 * @return GNUNET_OK to keep the connection open,
4181 * GNUNET_SYSERR to close it (signal serious error)
4184 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4185 const struct GNUNET_MessageHeader *message)
4187 struct GNUNET_MESH_TunnelDestroy *msg;
4188 struct MeshTunnel *t;
4190 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4192 "Got a TUNNEL DESTROY packet from %s\n",
4194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4195 " for tunnel %s [%u]\n",
4196 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4197 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4200 /* Probably already got the message from another path,
4201 * destroyed the tunnel and retransmitted to children.
4204 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4208 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4211 t->local_tid, t->local_tid_dest);
4213 if (GNUNET_PEER_search (peer) == t->prev_hop)
4215 // TODO check owner's signature
4216 // TODO add owner's signatue to tunnel for retransmission
4217 peer_cancel_queues (t->prev_hop, t);
4218 GNUNET_PEER_change_rc (t->prev_hop, -1);
4221 else if (GNUNET_PEER_search (peer) == t->next_hop)
4223 // TODO check dest's signature
4224 // TODO add dest's signatue to tunnel for retransmission
4225 peer_cancel_queues (t->next_hop, t);
4226 GNUNET_PEER_change_rc (t->next_hop, -1);
4231 GNUNET_break_op (0);
4232 // TODO check both owner AND destination's signature to see which matches
4233 // TODO restransmit in appropriate direction
4236 tunnel_destroy_empty (t);
4238 // TODO: add timeout to destroy the tunnel anyway
4244 * Generic handler for mesh network payload traffic.
4246 * @param peer Peer identity this notification is about.
4247 * @param message Data message.
4248 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
4250 * @return GNUNET_OK to keep the connection open,
4251 * GNUNET_SYSERR to close it (signal serious error)
4254 handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
4255 const struct GNUNET_MessageHeader *message,
4258 struct GNUNET_MESH_Data *msg;
4259 struct MeshFlowControl *fc;
4260 struct MeshTunnelReliability *rel;
4261 struct MeshTunnel *t;
4262 struct MeshClient *c;
4270 size = ntohs (message->size);
4272 sizeof (struct GNUNET_MESH_Data) +
4273 sizeof (struct GNUNET_MessageHeader))
4278 type =ntohs (message->type);
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
4280 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4281 msg = (struct GNUNET_MESH_Data *) message;
4282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4283 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4285 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4288 /* TODO notify back: we don't know this tunnel */
4289 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4290 GNUNET_break_op (0);
4294 /* Initialize FWD/BCK data */
4295 pid = ntohl (msg->pid);
4296 fc = fwd ? &t->prev_fc : &t->next_fc;
4297 c = fwd ? t->client : t->owner;
4298 rel = fwd ? t->bck_rel : t->fwd_rel;
4299 hop = fwd ? t->next_hop : t->prev_hop;
4300 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
4302 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
4303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4304 "WARNING Received PID %u, (prev %u), ACK %u\n",
4305 pid, fc->last_pid_recv, fc->last_ack_sent);
4309 tunnel_change_state (t, MESH_TUNNEL_READY);
4310 tunnel_reset_timeout (t, fwd);
4313 /* TODO signature verification */
4314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
4315 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4316 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
4320 mid = ntohl (msg->mid);
4321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4322 " pid %u (mid %u) not seen yet\n", pid, mid);
4323 fc->last_pid_recv = pid;
4325 if (GNUNET_NO == t->reliable ||
4326 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4327 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4330 "!!! RECV %u\n", ntohl (msg->mid));
4331 if (GNUNET_YES == t->reliable)
4333 /* Is this the exact next expected messasge? */
4334 if (mid == rel->mid_recv)
4336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4338 tunnel_send_client_data (t, msg, fwd);
4339 tunnel_send_client_buffered_data (t, c, rel);
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4344 tunnel_add_buffered_data (t, msg, rel);
4347 else /* Tunnel unreliable, send to clients directly */
4349 tunnel_send_client_data (t, msg, fwd);
4354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4355 " MID %u not expected (%u - %u), dropping!\n",
4356 ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
4361 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4363 " Pid %u not expected (%u+), dropping!\n",
4364 pid, fc->last_pid_recv + 1);
4366 tunnel_send_ack (t, type, fwd);
4369 fc->last_pid_recv = pid;
4372 GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO);
4373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n",
4374 GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid));
4375 return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */
4377 ttl = ntohl (msg->ttl);
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4381 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4383 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4390 send_prebuilt_message (message, hop, t);
4391 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4398 * Core handler for mesh network traffic going from the origin to a peer
4400 * @param cls Closure (unused).
4401 * @param message Message received.
4402 * @param peer Peer who sent the message.
4404 * @return GNUNET_OK to keep the connection open,
4405 * GNUNET_SYSERR to close it (signal serious error)
4408 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4409 const struct GNUNET_MessageHeader *message)
4411 return handle_mesh_data (peer, message, GNUNET_YES);
4415 * Core handler for mesh network traffic towards the owner of a tunnel.
4417 * @param cls Closure (unused).
4418 * @param message Message received.
4419 * @param peer Peer who sent the message.
4421 * @return GNUNET_OK to keep the connection open,
4422 * GNUNET_SYSERR to close it (signal serious error)
4425 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4426 const struct GNUNET_MessageHeader *message)
4428 return handle_mesh_data (peer, message, GNUNET_NO);
4433 * Core handler for mesh network traffic end-to-end ACKs.
4435 * @param cls Closure.
4436 * @param message Message.
4437 * @param peer Peer identity this notification is about.
4439 * @return GNUNET_OK to keep the connection open,
4440 * GNUNET_SYSERR to close it (signal serious error)
4443 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4444 const struct GNUNET_MessageHeader *message)
4446 struct GNUNET_MESH_DataACK *msg;
4447 struct MeshTunnelReliability *rel;
4448 struct MeshReliableMessage *copy;
4449 struct MeshReliableMessage *next;
4450 struct MeshTunnel *t;
4456 type = ntohs (message->type);
4457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4458 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4459 msg = (struct GNUNET_MESH_DataACK *) message;
4461 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4464 /* TODO notify that we dont know this tunnel (whom)? */
4465 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4468 ack = ntohl (msg->mid);
4469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4471 /* Is this a forward or backward ACK? */
4472 id = GNUNET_PEER_search (peer);
4473 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4476 if (NULL == t->owner)
4478 send_prebuilt_message (message, t->prev_hop, t);
4483 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4486 if (NULL == t->client)
4488 send_prebuilt_message (message, t->next_hop, t);
4495 GNUNET_break_op (0);
4499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4500 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4502 if (GMC_is_pid_bigger (copy->mid, ack))
4504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4505 tunnel_free_sent_reliable (t, msg, rel);
4509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4511 tunnel_free_reliable_message (copy);
4513 /* Once buffers have been free'd, send ACK */
4514 tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4516 /* If some message was free'd, update the retransmission delay*/
4517 if (GNUNET_YES == work)
4519 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4521 GNUNET_SCHEDULER_cancel (rel->retry_task);
4522 if (NULL == rel->head_sent)
4524 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4528 struct GNUNET_TIME_Absolute new_target;
4529 struct GNUNET_TIME_Relative delay;
4531 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4532 MESH_RETRANSMIT_MARGIN);
4533 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4535 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4537 GNUNET_SCHEDULER_add_delayed (delay,
4538 &tunnel_retransmit_message,
4549 * Core handler for mesh network traffic point-to-point acks.
4551 * @param cls closure
4552 * @param message message
4553 * @param peer peer identity this notification is about
4555 * @return GNUNET_OK to keep the connection open,
4556 * GNUNET_SYSERR to close it (signal serious error)
4559 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4560 const struct GNUNET_MessageHeader *message)
4562 struct GNUNET_MESH_ACK *msg;
4563 struct MeshTunnel *t;
4564 struct MeshFlowControl *fc;
4568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4570 msg = (struct GNUNET_MESH_ACK *) message;
4572 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4576 /* TODO notify that we dont know this tunnel (whom)? */
4577 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4580 ack = ntohl (msg->pid);
4581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4583 /* Is this a forward or backward ACK? */
4584 id = GNUNET_PEER_search (peer);
4585 if (t->next_hop == id)
4587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4590 else if (t->prev_hop == id)
4592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4597 GNUNET_break_op (0);
4601 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4602 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4604 GNUNET_SCHEDULER_cancel (fc->poll_task);
4605 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4606 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4609 fc->last_ack_recv = ack;
4610 peer_unlock_queue (id);
4611 tunnel_change_state (t, MESH_TUNNEL_READY);
4613 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
4620 * Core handler for mesh network traffic point-to-point ack polls.
4622 * @param cls closure
4623 * @param message message
4624 * @param peer peer identity this notification is about
4626 * @return GNUNET_OK to keep the connection open,
4627 * GNUNET_SYSERR to close it (signal serious error)
4630 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4631 const struct GNUNET_MessageHeader *message)
4633 struct GNUNET_MESH_Poll *msg;
4634 struct MeshTunnel *t;
4635 struct MeshFlowControl *fc;
4640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4643 msg = (struct GNUNET_MESH_Poll *) message;
4645 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4649 /* TODO notify that we dont know this tunnel (whom)? */
4650 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4651 GNUNET_break_op (0);
4655 /* Is this a forward or backward ACK? */
4656 id = GNUNET_PEER_search (peer);
4657 pid = ntohl (msg->pid);
4658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4659 if (t->next_hop == id)
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4663 old = fc->last_pid_recv;
4665 else if (t->prev_hop == id)
4667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4669 old = fc->last_pid_recv;
4677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", fc->last_pid_recv);
4678 fc->last_pid_recv = pid;
4679 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id);
4681 if (GNUNET_YES == t->reliable)
4682 fc->last_pid_recv = old;
4689 * Core handler for mesh keepalives.
4691 * @param cls closure
4692 * @param message message
4693 * @param peer peer identity this notification is about
4694 * @return GNUNET_OK to keep the connection open,
4695 * GNUNET_SYSERR to close it (signal serious error)
4697 * TODO: Check who we got this from, to validate route.
4700 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4701 const struct GNUNET_MessageHeader *message)
4703 struct GNUNET_MESH_TunnelKeepAlive *msg;
4704 struct MeshTunnel *t;
4705 struct MeshClient *c;
4709 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4713 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4716 /* TODO notify that we dont know that tunnel */
4717 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4722 fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
4723 GNUNET_YES : GNUNET_NO;
4724 c = fwd ? t->client : t->owner;
4725 hop = fwd ? t->next_hop : t->prev_hop;
4728 tunnel_change_state (t, MESH_TUNNEL_READY);
4729 tunnel_reset_timeout (t, fwd);
4730 if (NULL != c || 0 == hop || myid == hop)
4733 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4734 send_prebuilt_message (message, hop, t);
4741 * Functions to handle messages from core
4743 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4744 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4745 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4746 sizeof (struct GNUNET_MESH_PathBroken)},
4747 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4748 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4749 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4750 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4751 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4752 sizeof (struct GNUNET_MESH_DataACK)},
4753 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4754 sizeof (struct GNUNET_MESH_DataACK)},
4755 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
4756 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4757 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
4758 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4759 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4760 sizeof (struct GNUNET_MESH_ACK)},
4761 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4762 sizeof (struct GNUNET_MESH_Poll)},
4763 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4764 sizeof (struct GNUNET_MESH_PathACK)},
4770 * Function to process paths received for a new peer addition. The recorded
4771 * paths form the initial tunnel, which can be optimized later.
4772 * Called on each result obtained for the DHT search.
4774 * @param cls closure
4775 * @param exp when will this value expire
4776 * @param key key of the result
4777 * @param get_path path of the get request
4778 * @param get_path_length lenght of get_path
4779 * @param put_path path of the put request
4780 * @param put_path_length length of the put_path
4781 * @param type type of the result
4782 * @param size number of bytes in data
4783 * @param data pointer to the result data
4786 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4787 const struct GNUNET_HashCode * key,
4788 const struct GNUNET_PeerIdentity *get_path,
4789 unsigned int get_path_length,
4790 const struct GNUNET_PeerIdentity *put_path,
4791 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4792 size_t size, const void *data)
4794 struct MeshPeerInfo *peer = cls;
4795 struct MeshPeerPath *p;
4796 struct GNUNET_PeerIdentity pi;
4799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4800 GNUNET_PEER_resolve (peer->id, &pi);
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4803 p = path_build_from_dht (get_path, get_path_length,
4804 put_path, put_path_length);
4805 path_add_to_peers (p, GNUNET_NO);
4807 for (i = 0; i < peer->ntunnels; i++)
4809 struct GNUNET_PeerIdentity id;
4811 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4813 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4814 peer->tunnels[i]->local_tid,
4815 peer->tunnels[i]->local_tid_dest);
4816 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4819 peer_connect (peer, peer->tunnels[i]);
4827 /******************************************************************************/
4828 /********************* MESH LOCAL HANDLES **************************/
4829 /******************************************************************************/
4833 * Handler for client connection.
4835 * @param cls Closure (unused).
4836 * @param client Client handler.
4839 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4841 struct MeshClient *c;
4845 c = GNUNET_malloc (sizeof (struct MeshClient));
4847 GNUNET_SERVER_client_keep (client);
4848 GNUNET_SERVER_client_set_user_context (client, c);
4849 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4854 * Handler for client disconnection
4856 * @param cls closure
4857 * @param client identification of the client; NULL
4858 * for the last call when the server is destroyed
4861 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4863 struct MeshClient *c;
4864 struct MeshClient *next;
4866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4873 c = client_get (client);
4876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
4878 GNUNET_SERVER_client_drop (c->handle);
4879 c->shutting_down = GNUNET_YES;
4880 if (NULL != c->own_tunnels)
4882 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4883 &tunnel_destroy_iterator, c);
4884 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4887 if (NULL != c->incoming_tunnels)
4889 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4890 &tunnel_destroy_iterator, c);
4891 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4894 if (NULL != c->ports)
4896 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
4897 &client_release_ports, c);
4898 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4901 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
4904 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4909 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4917 * Handler for new clients
4919 * @param cls closure
4920 * @param client identification of the client
4921 * @param message the actual message, which includes messages the client wants
4924 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4925 const struct GNUNET_MessageHeader *message)
4927 struct GNUNET_MESH_ClientConnect *cc_msg;
4928 struct MeshClient *c;
4933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4935 /* Check data sanity */
4936 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4937 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4938 if (0 != (size % sizeof (uint32_t)))
4941 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4944 size /= sizeof (uint32_t);
4946 /* Initialize new client structure */
4947 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4948 c->id = next_client_id++; /* overflow not important: just for debug */
4949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4955 p = (uint32_t *) &cc_msg[1];
4956 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4957 for (i = 0; i < size; i++)
4960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4962 /* store in client's hashmap */
4963 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4964 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4965 /* store in global hashmap */
4966 /* FIXME only allow one client to have the port open,
4967 * have a backup hashmap with waiting clients */
4968 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4969 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4973 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4974 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4975 GNUNET_SERVER_notification_context_add (nc, client);
4976 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4978 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4984 * Handler for requests of new tunnels
4986 * @param cls Closure.
4987 * @param client Identification of the client.
4988 * @param message The actual message.
4991 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4992 const struct GNUNET_MessageHeader *message)
4994 struct GNUNET_MESH_TunnelMessage *t_msg;
4995 struct MeshPeerInfo *peer_info;
4996 struct MeshTunnel *t;
4997 struct MeshClient *c;
4998 MESH_TunnelNumber tid;
5000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5002 /* Sanity check for client registration */
5003 if (NULL == (c = client_get (client)))
5006 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5011 /* Message size sanity check */
5012 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5015 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5019 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
5021 GNUNET_i2s (&t_msg->peer));
5022 tid = ntohl (t_msg->tunnel_id);
5023 /* Sanity check for duplicate tunnel IDs */
5024 if (NULL != tunnel_get_by_local_id (c, tid))
5027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5032 while (NULL != tunnel_get_by_pi (myid, next_tid))
5033 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5034 t = tunnel_new (myid, next_tid, c, tid);
5035 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5039 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5042 t->port = ntohl (t_msg->port);
5043 tunnel_set_options (t, ntohl (t_msg->opt));
5044 if (GNUNET_YES == t->reliable)
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
5047 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
5049 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
5052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
5053 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
5055 peer_info = peer_get (&t_msg->peer);
5056 peer_info_add_tunnel (peer_info, t);
5057 peer_connect (peer_info, t);
5058 tunnel_reset_timeout (t, GNUNET_YES);
5059 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5065 * Handler for requests of deleting tunnels
5067 * @param cls closure
5068 * @param client identification of the client
5069 * @param message the actual message
5072 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5073 const struct GNUNET_MessageHeader *message)
5075 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5076 struct MeshClient *c;
5077 struct MeshTunnel *t;
5078 MESH_TunnelNumber tid;
5080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5081 "Got a DESTROY TUNNEL from client!\n");
5083 /* Sanity check for client registration */
5084 if (NULL == (c = client_get (client)))
5087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5092 /* Message sanity check */
5093 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5096 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5100 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5102 /* Retrieve tunnel */
5103 tid = ntohl (tunnel_msg->tunnel_id);
5104 t = tunnel_get_by_local_id (c, tid);
5107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5113 /* Cleanup after the tunnel */
5114 client_delete_tunnel (c, t);
5115 if (c == t->client && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV <= tid)
5119 else if (c == t->owner && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV > tid)
5121 peer_info_remove_tunnel (peer_get_short (t->dest), t);
5126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5127 " tunnel %X client %p (%p, %p)\n",
5128 tid, c, t->owner, t->client);
5132 /* The tunnel will be destroyed when the last message is transmitted. */
5133 tunnel_destroy_empty (t);
5135 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5141 * Handler for client traffic
5143 * @param cls closure
5144 * @param client identification of the client
5145 * @param message the actual message
5148 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
5149 const struct GNUNET_MessageHeader *message)
5151 struct GNUNET_MESH_LocalData *data_msg;
5152 struct MeshClient *c;
5153 struct MeshTunnel *t;
5154 struct MeshFlowControl *fc;
5155 MESH_TunnelNumber tid;
5158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5159 "Got data from a client!\n");
5161 /* Sanity check for client registration */
5162 if (NULL == (c = client_get (client)))
5165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5170 data_msg = (struct GNUNET_MESH_LocalData *) message;
5172 /* Sanity check for message size */
5173 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
5174 if (size < sizeof (struct GNUNET_MessageHeader))
5177 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5181 /* Tunnel exists? */
5182 tid = ntohl (data_msg->tid);
5183 t = tunnel_get_by_local_id (c, tid);
5187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5191 /* Is the client in the tunnel? */
5192 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5194 t->owner->handle == client)
5196 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5198 t->client->handle == client) ) )
5201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5205 /* Ok, everything is correct, send the message
5206 * (pretend we got it from a mesh peer)
5209 struct GNUNET_MESH_Data *payload;
5210 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5212 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5213 if (GNUNET_YES == t->reliable)
5215 struct MeshTunnelReliability *rel;
5216 struct MeshReliableMessage *copy;
5218 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5219 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5220 + sizeof(struct GNUNET_MESH_Data)
5222 copy->mid = rel->mid_sent++;
5223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
5224 copy->timestamp = GNUNET_TIME_absolute_get ();
5227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
5228 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5229 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5232 GNUNET_TIME_relative_multiply (rel->expected_delay,
5233 MESH_RETRANSMIT_MARGIN);
5235 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5236 &tunnel_retransmit_message,
5239 payload = (struct GNUNET_MESH_Data *) ©[1];
5240 payload->mid = htonl (copy->mid);
5244 payload = (struct GNUNET_MESH_Data *) cbuf;
5245 payload->mid = htonl (fc->last_pid_recv + 1);
5247 memcpy (&payload[1], &data_msg[1], size);
5248 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5249 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5250 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5251 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5252 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5253 payload->tid = htonl (t->id.tid);
5254 payload->ttl = htonl (default_ttl);
5255 payload->pid = htonl (fc->last_pid_recv + 1);
5256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5257 " calling generic handler...\n");
5258 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5259 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5261 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5264 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5271 * Handler for client's ACKs for payload traffic.
5273 * @param cls Closure (unused).
5274 * @param client Identification of the client.
5275 * @param message The actual message.
5278 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5279 const struct GNUNET_MessageHeader *message)
5281 struct GNUNET_MESH_LocalAck *msg;
5282 struct MeshTunnel *t;
5283 struct MeshClient *c;
5284 MESH_TunnelNumber tid;
5286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5287 /* Sanity check for client registration */
5288 if (NULL == (c = client_get (client)))
5291 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5296 msg = (struct GNUNET_MESH_LocalAck *) message;
5298 /* Tunnel exists? */
5299 tid = ntohl (msg->tunnel_id);
5300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5301 t = tunnel_get_by_local_id (c, tid);
5305 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5307 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5311 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5312 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5314 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5315 t->prev_fc.last_ack_recv++;
5316 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5320 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5321 t->next_fc.last_ack_recv++;
5322 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5325 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5333 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5335 * @param cls Closure (client handle).
5336 * @param key Key (hashed tunnel ID, unused).
5337 * @param value Tunnel info.
5339 * @return GNUNET_YES, to keep iterating.
5342 monitor_all_tunnels_iterator (void *cls,
5343 const struct GNUNET_HashCode * key,
5346 struct GNUNET_SERVER_Client *client = cls;
5347 struct MeshTunnel *t = value;
5348 struct GNUNET_MESH_LocalMonitor *msg;
5350 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5351 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5352 msg->tunnel_id = htonl (t->id.tid);
5353 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5354 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5355 GNUNET_PEER_resolve (t->dest, &msg->destination);
5357 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5358 "* sending info about tunnel %s [%u]\n",
5359 GNUNET_i2s (&msg->owner), t->id.tid);
5361 GNUNET_SERVER_notification_context_unicast (nc, client,
5362 &msg->header, GNUNET_NO);
5368 * Handler for client's MONITOR request.
5370 * @param cls Closure (unused).
5371 * @param client Identification of the client.
5372 * @param message The actual message.
5375 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5376 const struct GNUNET_MessageHeader *message)
5378 struct MeshClient *c;
5380 /* Sanity check for client registration */
5381 if (NULL == (c = client_get (client)))
5384 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5388 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5389 "Received get tunnels request from client %u\n",
5391 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5392 monitor_all_tunnels_iterator,
5394 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5395 "Get tunnels request from client %u completed\n",
5397 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5402 * Handler for client's MONITOR_TUNNEL request.
5404 * @param cls Closure (unused).
5405 * @param client Identification of the client.
5406 * @param message The actual message.
5409 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5410 const struct GNUNET_MessageHeader *message)
5412 const struct GNUNET_MESH_LocalMonitor *msg;
5413 struct GNUNET_MESH_LocalMonitor *resp;
5414 struct MeshClient *c;
5415 struct MeshTunnel *t;
5417 /* Sanity check for client registration */
5418 if (NULL == (c = client_get (client)))
5421 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5425 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5426 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5427 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5430 ntohl (msg->tunnel_id));
5431 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5434 /* We don't know the tunnel FIXME */
5435 struct GNUNET_MESH_LocalMonitor warn;
5438 GNUNET_SERVER_notification_context_unicast (nc, client,
5441 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5445 /* Initialize context */
5446 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5448 GNUNET_PEER_resolve (t->dest, &resp->destination);
5449 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5450 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5451 &resp->header, GNUNET_NO);
5454 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5455 "Monitor tunnel request from client %u completed\n",
5457 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5462 * Functions to handle messages from clients
5464 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5465 {&handle_local_new_client, NULL,
5466 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5467 {&handle_local_tunnel_create, NULL,
5468 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5469 sizeof (struct GNUNET_MESH_TunnelMessage)},
5470 {&handle_local_tunnel_destroy, NULL,
5471 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5472 sizeof (struct GNUNET_MESH_TunnelMessage)},
5473 {&handle_local_data, NULL,
5474 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5475 {&handle_local_ack, NULL,
5476 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5477 sizeof (struct GNUNET_MESH_LocalAck)},
5478 {&handle_local_get_tunnels, NULL,
5479 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5480 sizeof (struct GNUNET_MessageHeader)},
5481 {&handle_local_show_tunnel, NULL,
5482 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5483 sizeof (struct GNUNET_MESH_LocalMonitor)},
5489 * Method called whenever a given peer connects.
5491 * @param cls closure
5492 * @param peer peer identity this notification is about
5495 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5497 struct MeshPeerInfo *peer_info;
5498 struct MeshPeerPath *path;
5500 DEBUG_CONN ("Peer connected\n");
5501 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5502 peer_info = peer_get (peer);
5503 if (myid == peer_info->id)
5505 DEBUG_CONN (" (self)\n");
5506 path = path_new (1);
5510 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5511 path = path_new (2);
5512 path->peers[1] = peer_info->id;
5513 GNUNET_PEER_change_rc (peer_info->id, 1);
5514 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5516 path->peers[0] = myid;
5517 GNUNET_PEER_change_rc (myid, 1);
5518 peer_info_add_path (peer_info, path, GNUNET_YES);
5524 * Method called whenever a peer disconnects.
5526 * @param cls closure
5527 * @param peer peer identity this notification is about
5530 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5532 struct MeshPeerInfo *pi;
5533 struct MeshPeerQueue *q;
5534 struct MeshPeerQueue *n;
5536 DEBUG_CONN ("Peer disconnected\n");
5537 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5547 /* TODO try to reroute this traffic instead */
5548 queue_destroy(q, GNUNET_YES);
5551 if (NULL != pi->core_transmit)
5553 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5554 pi->core_transmit = NULL;
5556 peer_info_remove_path (pi, pi->id, myid);
5559 DEBUG_CONN (" (self)\n");
5561 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5567 * Install server (service) handlers and start listening to clients.
5572 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5573 GNUNET_SERVER_connect_notify (server_handle,
5574 &handle_local_client_connect, NULL);
5575 GNUNET_SERVER_disconnect_notify (server_handle,
5576 &handle_local_client_disconnect, NULL);
5577 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5579 clients_head = NULL;
5580 clients_tail = NULL;
5582 GNUNET_SERVER_resume (server_handle);
5587 * To be called on core init/fail.
5589 * @param cls Closure (config)
5590 * @param server handle to the server for this service
5591 * @param identity the public identity of this peer
5594 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5595 const struct GNUNET_PeerIdentity *identity)
5597 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5601 GNUNET_break (core_handle == server);
5602 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5608 GNUNET_i2s (identity));
5609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5611 GNUNET_i2s (&my_full_id));
5612 GNUNET_CORE_disconnect (core_handle);
5613 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5614 NULL, /* Closure passed to MESH functions */
5615 &core_init, /* Call core_init once connected */
5616 &core_connect, /* Handle connects */
5617 &core_disconnect, /* remove peers on disconnects */
5618 NULL, /* Don't notify about all incoming messages */
5619 GNUNET_NO, /* For header only in notification */
5620 NULL, /* Don't notify about all outbound messages */
5621 GNUNET_NO, /* For header-only out notification */
5622 core_handlers); /* Register these handlers */
5631 /******************************************************************************/
5632 /************************ MAIN FUNCTIONS ****************************/
5633 /******************************************************************************/
5636 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5638 * @param cls closure
5639 * @param key current key code
5640 * @param value value in the hash map
5641 * @return GNUNET_YES if we should continue to iterate,
5645 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5647 struct MeshTunnel *t = value;
5654 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5656 * @param cls closure
5657 * @param key current key code
5658 * @param value value in the hash map
5659 * @return GNUNET_YES if we should continue to iterate,
5663 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5665 struct MeshPeerInfo *p = value;
5666 struct MeshPeerQueue *q;
5667 struct MeshPeerQueue *n;
5675 queue_destroy(q, GNUNET_YES);
5679 peer_info_destroy (p);
5685 * Task run during shutdown.
5691 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5695 if (core_handle != NULL)
5697 GNUNET_CORE_disconnect (core_handle);
5700 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5701 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5702 if (dht_handle != NULL)
5704 GNUNET_DHT_disconnect (dht_handle);
5709 GNUNET_SERVER_notification_context_destroy (nc);
5712 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5714 GNUNET_SCHEDULER_cancel (announce_id_task);
5715 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5722 * Process mesh requests.
5724 * @param cls closure
5725 * @param server the initialized server
5726 * @param c configuration to use
5729 run (void *cls, struct GNUNET_SERVER_Handle *server,
5730 const struct GNUNET_CONFIGURATION_Handle *c)
5733 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5736 server_handle = server;
5737 GNUNET_SERVER_suspend (server_handle);
5740 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5745 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5746 "mesh", "peer/privatekey");
5747 GNUNET_SCHEDULER_shutdown ();
5752 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5753 &refresh_path_time))
5755 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5757 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5758 "mesh", "refresh path time");
5759 GNUNET_SCHEDULER_shutdown ();
5764 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5767 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5769 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5770 "mesh", "id announce time");
5771 GNUNET_SCHEDULER_shutdown ();
5776 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5781 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5782 "mesh", "connect timeout");
5783 GNUNET_SCHEDULER_shutdown ();
5788 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5791 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5793 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5794 "mesh", "max msgs queue");
5795 GNUNET_SCHEDULER_shutdown ();
5800 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5803 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5805 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5806 "mesh", "max tunnels");
5807 GNUNET_SCHEDULER_shutdown ();
5812 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5815 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5817 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5818 "mesh", "default ttl", 64);
5823 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5826 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5827 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5828 "mesh", "max peers", 1000);
5833 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5840 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5841 "Mesh is running with drop mode enabled. "
5842 "This is NOT a good idea! "
5843 "Remove the DROP_PERCENT option from your configuration.\n");
5847 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5848 &dht_replication_level))
5850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5852 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5853 "mesh", "dht replication level", 3);
5854 dht_replication_level = 3;
5857 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5858 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5859 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5860 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5862 dht_handle = GNUNET_DHT_connect (c, 64);
5863 if (NULL == dht_handle)
5867 stats = GNUNET_STATISTICS_create ("mesh", c);
5869 /* Scheduled the task to clean up when shutdown is called */
5870 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5872 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5873 GNUNET_free (keyfile);
5874 GNUNET_assert (NULL != pk);
5875 my_private_key = pk;
5876 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5877 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5878 &my_full_id.hashPubKey);
5879 myid = GNUNET_PEER_intern (&my_full_id);
5880 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5881 "Mesh for peer [%s] starting\n",
5882 GNUNET_i2s(&my_full_id));
5884 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5885 NULL, /* Closure passed to MESH functions */
5886 &core_init, /* Call core_init once connected */
5887 &core_connect, /* Handle connects */
5888 &core_disconnect, /* remove peers on disconnects */
5889 NULL, /* Don't notify about all incoming messages */
5890 GNUNET_NO, /* For header only in notification */
5891 NULL, /* Don't notify about all outbound messages */
5892 GNUNET_NO, /* For header-only out notification */
5893 core_handlers); /* Register these handlers */
5894 if (NULL == core_handle)
5897 GNUNET_SCHEDULER_shutdown ();
5901 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5902 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5908 * The main function for the mesh service.
5910 * @param argc number of arguments from the command line
5911 * @param argv command line arguments
5912 * @return 0 ok, 1 on error
5915 main (int argc, char *const *argv)
5920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5921 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5923 ret = (GNUNET_OK == r) ? 0 : 1;
5924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");