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
28 * - refactor unicast to make generic handling, assigning *rel and *fc
31 * - relay corking down to core
32 * - set ttl relative to path length
40 #include "mesh_protocol.h"
41 #include "mesh_path.h"
42 #include "block_mesh.h"
43 #include "gnunet_dht_service.h"
44 #include "gnunet_statistics_service.h"
46 #define MESH_BLOOM_SIZE 128
48 #define MESH_DEBUG_DHT GNUNET_NO
49 #define MESH_DEBUG_CONNECTION GNUNET_NO
50 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
52 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
53 GNUNET_TIME_UNIT_MINUTES,\
55 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
56 #define MESH_RETRANSMIT_MARGIN 4
58 #if MESH_DEBUG_CONNECTION
59 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
61 #define DEBUG_CONN(...)
65 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
67 #define DEBUG_DHT(...)
74 struct timespec __mesh_start;
75 struct timespec __mesh_end;
76 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
77 #define INTERVAL_END \
79 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
80 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
81 if (__diff < 0) __diff += 1000000000;\
85 #define INTERVAL_SHOW \
87 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
89 #define INTERVAL_START
94 /******************************************************************************/
95 /************************ DATA STRUCTURES ****************************/
96 /******************************************************************************/
98 /** FWD declaration */
104 * Struct containing info about a queued transmission to this peer
111 struct MeshPeerQueue *next;
116 struct MeshPeerQueue *prev;
119 * Peer this transmission is directed to.
121 struct MeshPeerInfo *peer;
124 * Tunnel this message belongs to.
126 struct MeshTunnel *tunnel;
129 * Pointer to info stucture used as cls.
139 * Size of the message
146 * Struct containing all information regarding a given peer
156 * Last time we heard from this peer
158 struct GNUNET_TIME_Absolute last_contact;
161 * Number of attempts to reconnect so far
163 int n_reconnect_attempts;
166 * Paths to reach the peer, ordered by ascending hop count
168 struct MeshPeerPath *path_head;
171 * Paths to reach the peer, ordered by ascending hop count
173 struct MeshPeerPath *path_tail;
176 * Handle to stop the DHT search for a path to this peer
178 struct GNUNET_DHT_GetHandle *dhtget;
181 * Array of tunnels this peer is the target of.
182 * Most probably a small amount, therefore not a hashmap.
183 * When the path to the peer changes, notify these tunnels to let them
184 * re-adjust their path trees.
186 struct MeshTunnel **tunnels;
189 * Number of tunnels this peers participates in
191 unsigned int ntunnels;
194 * Transmission queue to core DLL head
196 struct MeshPeerQueue *queue_head;
199 * Transmission queue to core DLL tail
201 struct MeshPeerQueue *queue_tail;
204 * How many messages are in the queue to this peer.
206 unsigned int queue_n;
209 * Handle for queued transmissions
211 struct GNUNET_CORE_TransmitHandle *core_transmit;
216 * Struct to encapsulate all the Flow Control information to a peer in the
217 * context of a tunnel: Same peer in different tunnels will have independent
218 * flow control structures, allowing to choke/free tunnels according to its
221 struct MeshFlowControl
224 * ID of the last packet sent towards the peer.
226 uint32_t last_pid_sent;
229 * ID of the last packet received from the peer.
231 uint32_t last_pid_recv;
234 * Last ACK sent to the peer (peer can't send more than this PID).
236 uint32_t last_ack_sent;
239 * Last ACK sent towards the origin (for traffic towards leaf node).
241 uint32_t last_ack_recv;
244 * How many payload messages are in the queue towards this peer.
249 * Task to poll the peer in case of a lost ACK causes stall.
251 GNUNET_SCHEDULER_TaskIdentifier poll_task;
254 * How frequently to poll for ACKs.
256 struct GNUNET_TIME_Relative poll_time;
259 * On which tunnel to poll.
260 * Using an explicit poll_ctx would not help memory wise,
261 * since the allocated context would have to be stored in the
262 * fc struct in order to free it upon cancelling poll_task.
264 struct MeshTunnel *t;
269 * Globally unique tunnel identification (owner + number)
270 * DO NOT USE OVER THE NETWORK
275 * Node that owns the tunnel
280 * Tunnel number to differentiate all the tunnels owned by the node oid
281 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
283 MESH_TunnelNumber tid;
288 * Data needed for reliable tunnel endpoint retransmission management.
290 struct MeshTunnelReliability;
294 * Info needed to retry a message in case it gets lost.
296 struct MeshReliableMessage
299 * Double linked list, FIFO style
301 struct MeshReliableMessage *next;
302 struct MeshReliableMessage *prev;
305 * Tunnel Reliability queue this message is in.
307 struct MeshTunnelReliability *rel;
310 * ID of the message (ACK needed to free)
315 * When was this message issued (to calculate ACK delay) FIXME update with traffic
317 struct GNUNET_TIME_Absolute timestamp;
319 /* struct GNUNET_MESH_Data with payload */
323 struct MeshTunnelReliability
326 * Tunnel this is about.
328 struct MeshTunnel *t;
331 * DLL of messages sent and not yet ACK'd.
333 struct MeshReliableMessage *head_sent;
334 struct MeshReliableMessage *tail_sent;
347 * DLL of messages received out of order.
349 struct MeshReliableMessage *head_recv;
350 struct MeshReliableMessage *tail_recv;
358 * Task to resend/poll in case no ACK is received.
360 GNUNET_SCHEDULER_TaskIdentifier retry_task;
363 * Counter for exponential backoff.
365 struct GNUNET_TIME_Relative retry_timer;
368 * How long does it usually take to get an ACK. FIXME update with traffic
370 struct GNUNET_TIME_Relative expected_delay;
375 * Struct containing all information regarding a tunnel
376 * For an intermediate node the improtant info used will be:
377 * - id Tunnel unique identification
378 * - paths[0] To know where to send it next
379 * - metainfo: ready, speeds, accounting
386 struct MESH_TunnelID id;
389 * Port of the tunnel.
394 * State of the tunnel.
396 enum MeshTunnelState state;
399 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
401 MESH_TunnelNumber local_tid;
404 * Local tunnel number for local destination clients (incoming number)
405 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
408 MESH_TunnelNumber local_tid_dest;
411 * Is the tunnel bufferless (minimum latency)?
416 * Is the tunnel reliable?
421 * Force sending ACK? Flag to allow duplicate ACK on POLL.
426 * How many messages do we accept in the forward queue.
428 unsigned int queue_max;
431 * Last time the tunnel was used
433 struct GNUNET_TIME_Absolute timestamp;
436 * Destination of the tunnel.
441 * Next hop in the tunnel. If 0, @c client must be set.
443 GNUNET_PEER_Id next_hop;
446 * Previous hop in the tunnel. If 0, @c owner must be set.
448 GNUNET_PEER_Id prev_hop;
451 * Flow control information about @c next_hop or @c client.
453 struct MeshFlowControl next_fc;
456 * Flow control information about @c prev_hop or @c owner.
458 struct MeshFlowControl prev_fc;
461 * Client owner of the tunnel, if any
463 struct MeshClient *owner;
466 * Client destination of the tunnel, if any.
468 struct MeshClient *client;
471 * Task to keep the used paths alive at the owner,
472 * time tunnel out on all the other peers.
474 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
477 * Path being used for the tunnel.
479 struct MeshPeerPath *path;
482 * Flag to signal the destruction of the tunnel.
483 * If this is set GNUNET_YES the tunnel will be destroyed
484 * when the queue is empty.
489 * Total messages pending for this tunnels, payload or not.
491 unsigned int pending_messages;
495 * Only present (non-NULL) at the owner of a tunnel.
497 struct MeshTunnelReliability *fwd_rel;
501 * Only present (non-NULL) at the destination of a tunnel.
503 struct MeshTunnelReliability *bck_rel;
508 * Struct containing information about a client of the service
510 * TODO: add a list of 'waiting' ports
517 struct MeshClient *next;
522 struct MeshClient *prev;
525 * Tunnels that belong to this client, indexed by local id
527 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
530 * Tunnels this client has accepted, indexed by incoming local id
532 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
535 * Handle to communicate with the client
537 struct GNUNET_SERVER_Client *handle;
540 * Ports that this client has declared interest in.
541 * Indexed by port, contains *Client.
543 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
546 * Whether the client is active or shutting down (don't send confirmations
547 * to a client that is shutting down.
552 * ID of the client, mainly for debug messages
559 /******************************************************************************/
560 /************************ DEBUG FUNCTIONS ****************************/
561 /******************************************************************************/
565 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
566 * @param cls string to print
567 * @param success GNUNET_OK if the PUT was transmitted,
568 * GNUNET_NO on timeout,
569 * GNUNET_SYSERR on disconnect from service
570 * after the PUT message was transmitted
571 * (so we don't know if it was received or not)
576 mesh_debug (void *cls, int success)
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
585 unsigned int debug_fwd_ack;
586 unsigned int debug_bck_ack;
590 /******************************************************************************/
591 /*********************** GLOBAL VARIABLES ****************************/
592 /******************************************************************************/
594 /************************** Configuration parameters **************************/
597 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
599 static struct GNUNET_TIME_Relative refresh_path_time;
602 * How often to PUT own ID in the DHT.
604 static struct GNUNET_TIME_Relative id_announce_time;
607 * Maximum time allowed to connect to a peer found by string.
609 static struct GNUNET_TIME_Relative connect_timeout;
612 * Default TTL for payload packets.
614 static unsigned long long default_ttl;
617 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
619 static unsigned long long dht_replication_level;
622 * How many tunnels are we willing to maintain.
623 * Local tunnels are always allowed, even if there are more tunnels than max.
625 static unsigned long long max_tunnels;
628 * How many messages *in total* are we willing to queue, divided by number of
629 * tunnels to get tunnel queue size.
631 static unsigned long long max_msgs_queue;
634 * How many peers do we want to remember?
636 static unsigned long long max_peers;
639 * Percentage of messages that will be dropped (for test purposes only).
641 static unsigned long long drop_percent;
643 /*************************** Static global variables **************************/
646 * DLL with all the clients, head.
648 static struct MeshClient *clients_head;
651 * DLL with all the clients, tail.
653 static struct MeshClient *clients_tail;
656 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
658 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
661 * Number of tunnels known.
663 static unsigned long long n_tunnels;
666 * Tunnels incoming, indexed by MESH_TunnelNumber
667 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
669 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
672 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
674 static struct GNUNET_CONTAINER_MultiHashMap *peers;
677 * Handle to communicate with transport
679 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
682 * Handle to communicate with core.
684 static struct GNUNET_CORE_Handle *core_handle;
689 static struct GNUNET_DHT_Handle *dht_handle;
694 static struct GNUNET_SERVER_Handle *server_handle;
697 * Handle to the statistics service.
699 static struct GNUNET_STATISTICS_Handle *stats;
702 * Notification context, to send messages to local clients.
704 static struct GNUNET_SERVER_NotificationContext *nc;
707 * Local peer own ID (memory efficient handle).
709 static GNUNET_PEER_Id myid;
712 * Local peer own ID (full value).
714 static struct GNUNET_PeerIdentity my_full_id;
719 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
724 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
727 * Tunnel ID for the next created tunnel (global tunnel number).
729 static MESH_TunnelNumber next_tid;
732 * Tunnel ID for the next incoming tunnel (local tunnel number).
734 static MESH_TunnelNumber next_local_tid;
737 * All ports clients of this peer have opened.
739 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
742 * Task to periodically announce itself in the network.
744 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
747 * Next ID to assign to a client.
749 unsigned int next_client_id;
752 /******************************************************************************/
753 /*********************** DECLARATIONS **************************/
754 /******************************************************************************/
757 * Function to process paths received for a new peer addition. The recorded
758 * paths form the initial tunnel, which can be optimized later.
759 * Called on each result obtained for the DHT search.
762 * @param exp when will this value expire
763 * @param key key of the result
764 * @param type type of the result
765 * @param size number of bytes in data
766 * @param data pointer to the result data
769 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
770 const struct GNUNET_HashCode * key,
771 const struct GNUNET_PeerIdentity *get_path,
772 unsigned int get_path_length,
773 const struct GNUNET_PeerIdentity *put_path,
774 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
775 size_t size, const void *data);
779 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
780 * and insert it in the appropriate structures if the peer is not known yet.
782 * @param peer Full identity of the peer.
784 * @return Existing or newly created peer info.
786 static struct MeshPeerInfo *
787 peer_get (const struct GNUNET_PeerIdentity *peer);
791 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
792 * and insert it in the appropriate structures if the peer is not known yet.
794 * @param peer Short identity of the peer.
796 * @return Existing or newly created peer info.
798 static struct MeshPeerInfo *
799 peer_get_short (const GNUNET_PEER_Id peer);
803 * Build a PeerPath from the paths returned from the DHT, reversing the paths
804 * to obtain a local peer -> destination path and interning the peer ids.
806 * @return Newly allocated and created path
808 static struct MeshPeerPath *
809 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
810 unsigned int get_path_length,
811 const struct GNUNET_PeerIdentity *put_path,
812 unsigned int put_path_length);
816 * Adds a path to the peer_infos of all the peers in the path
818 * @param p Path to process.
819 * @param confirmed Whether we know if the path works or not.
822 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
827 * Search for a tunnel by global ID using full PeerIdentities.
829 * @param oid owner of the tunnel.
830 * @param tid global tunnel number.
832 * @return tunnel handler, NULL if doesn't exist.
834 static struct MeshTunnel *
835 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
839 * Notify a tunnel that a connection has broken that affects at least
842 * @param t Tunnel affected.
843 * @param p1 Peer that got disconnected from p2.
844 * @param p2 Peer that got disconnected from p1.
846 * @return Short ID of the peer disconnected (either p1 or p2).
847 * 0 if the tunnel remained unaffected.
849 static GNUNET_PEER_Id
850 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
855 * @brief Use the given path for the tunnel.
856 * Update the next and prev hops (and RCs).
857 * (Re)start the path refresh in case the tunnel is locally owned.
859 * @param t Tunnel to update.
860 * @param p Path to use.
863 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
866 * Tunnel is empty: destroy it.
868 * Notifies all participants (peers, cleints) about the destruction.
870 * @param t Tunnel to destroy.
873 tunnel_destroy_empty (struct MeshTunnel *t);
876 * @brief Queue and pass message to core when possible.
878 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
879 * and accounts for it. In case the queue is full, the message is dropped and
882 * Otherwise, message is treated as internal and allowed to go regardless of
885 * @param cls Closure (@c type dependant). It will be used by queue_send to
886 * build the message to be sent if not already prebuilt.
887 * @param type Type of the message, 0 for a raw message.
888 * @param size Size of the message.
889 * @param dst Neighbor to send message to.
890 * @param t Tunnel this message belongs to.
893 queue_add (void *cls, uint16_t type, size_t size,
894 struct MeshPeerInfo *dst, struct MeshTunnel *t);
898 * Free a transmission that was already queued with all resources
899 * associated to the request.
901 * @param queue Queue handler to cancel.
902 * @param clear_cls Is it necessary to free associated cls?
905 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
909 * @brief Get the next transmittable message from the queue.
911 * This will be the head, except in the case of being a data packet
912 * not allowed by the destination peer.
914 * @param peer Destination peer.
916 * @return The next viable MeshPeerQueue element to send to that peer.
917 * NULL when there are no transmittable messages.
919 struct MeshPeerQueue *
920 queue_get_next (const struct MeshPeerInfo *peer);
924 * Core callback to write a queued packet to core buffer
926 * @param cls Closure (peer info).
927 * @param size Number of bytes available in buf.
928 * @param buf Where the to write the message.
930 * @return number of bytes written to buf
933 queue_send (void *cls, size_t size, void *buf);
936 /******************************************************************************/
937 /************************ PERIODIC FUNCTIONS ****************************/
938 /******************************************************************************/
941 * Periodically announce self id in the DHT
944 * @param tc task context
947 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
951 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
953 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
957 * - Set data expiration in function of X
960 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
962 block.id = my_full_id;
963 GNUNET_DHT_put (dht_handle, /* DHT handle */
964 &my_full_id.hashPubKey, /* Key to use */
965 dht_replication_level, /* Replication level */
966 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
967 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
968 sizeof (block), /* Size of the data */
969 (const char *) &block, /* Data itself */
970 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
971 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
972 NULL, /* Continuation */
973 NULL); /* Continuation closure */
975 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
979 /******************************************************************************/
980 /****************** GENERAL HELPER FUNCTIONS ************************/
981 /******************************************************************************/
985 * Check if client has registered with the service and has not disconnected
987 * @param client the client to check
989 * @return non-NULL if client exists in the global DLL
991 static struct MeshClient *
992 client_get (struct GNUNET_SERVER_Client *client)
994 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
999 * Deletes a tunnel from a client (either owner or destination). To be used on
1002 * @param c Client whose tunnel to delete.
1003 * @param t Tunnel which should be deleted.
1006 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1010 GNUNET_assert (GNUNET_YES ==
1011 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1017 GNUNET_assert (GNUNET_YES ==
1018 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1025 * Notify the appropriate client that a new incoming tunnel was created.
1027 * @param t Tunnel that was created.
1030 send_client_tunnel_create (struct MeshTunnel *t)
1032 struct GNUNET_MESH_TunnelMessage msg;
1034 if (NULL == t->client)
1036 msg.header.size = htons (sizeof (msg));
1037 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1038 msg.tunnel_id = htonl (t->local_tid_dest);
1039 msg.port = htonl (t->port);
1041 msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1042 msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1043 msg.opt = htonl (msg.opt);
1044 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1045 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1046 &msg.header, GNUNET_NO);
1051 * Notify dest client that the incoming tunnel is no longer valid.
1053 * @param c Client to notify..
1054 * @param t Tunnel that is destroyed.
1057 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
1059 struct GNUNET_MESH_TunnelMessage msg;
1066 if (c != t->client && c != t->owner)
1071 msg.header.size = htons (sizeof (msg));
1072 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1073 msg.tunnel_id = htonl (t->local_tid_dest);
1074 msg.port = htonl (0);
1075 memset(&msg.peer, 0, sizeof (msg.peer));
1076 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1077 &msg.header, GNUNET_NO);
1082 * Iterator over all the peers to remove the oldest not-used entry.
1084 * @param cls Closure (unsued).
1085 * @param key ID of the peer.
1086 * @param value Peer_Info of the peer.
1091 peer_info_timeout (void *cls,
1092 const struct GNUNET_HashCode *key,
1099 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1100 * and insert it in the appropriate structures if the peer is not known yet.
1102 * @param peer Full identity of the peer.
1104 * @return Existing or newly created peer info.
1106 static struct MeshPeerInfo *
1107 peer_get (const struct GNUNET_PeerIdentity *peer)
1109 struct MeshPeerInfo *peer_info;
1111 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1112 if (NULL == peer_info)
1115 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1116 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1118 GNUNET_CONTAINER_multihashmap_iterate (peers,
1122 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1123 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1124 peer_info->id = GNUNET_PEER_intern (peer);
1126 peer_info->last_contact = GNUNET_TIME_absolute_get();
1133 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1134 * and insert it in the appropriate structures if the peer is not known yet.
1136 * @param peer Short identity of the peer.
1138 * @return Existing or newly created peer info.
1140 static struct MeshPeerInfo *
1141 peer_get_short (const GNUNET_PEER_Id peer)
1143 struct GNUNET_PeerIdentity id;
1145 GNUNET_PEER_resolve (peer, &id);
1146 return peer_get (&id);
1151 * Select which PID to POLL for, to compensate for lost messages.
1153 * @param pi Peer we want to poll.
1154 * @param t Tunnel about which we want to poll.
1156 * @return PID to use, either last sent or first_in_queue - 1
1159 peer_get_first_payload_pid (struct MeshPeerInfo *pi, struct MeshTunnel *t)
1161 struct MeshPeerQueue *q;
1164 type = pi->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST :
1165 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN;
1167 for (q = pi->queue_head; NULL != q; q = q->next)
1169 if (q->type == type && q->tunnel == t)
1171 struct GNUNET_MESH_Data *msg = q->cls;
1173 /* Pretend that the last one sent was the previous to this */
1174 return ntohl (msg->pid) - 1;
1178 /* No data in queue, use last sent */
1180 struct MeshFlowControl *fc;
1182 fc = pi->id == t->next_hop ? &t->next_fc : &t->prev_fc;
1183 return fc->last_pid_sent;
1189 * Choose the best path towards a peer considering the tunnel properties.
1191 * @param peer The destination peer.
1192 * @param t The tunnel the path is for.
1194 * @return Best current known path towards the peer, if any.
1196 static struct MeshPeerPath *
1197 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1199 struct MeshPeerPath *best_p;
1200 struct MeshPeerPath *p;
1201 unsigned int best_cost;
1204 best_p = p = peer->path_head;
1205 best_cost = cost = p->length;
1208 if ((cost = p->length) < best_cost)
1219 * Core callback to write a pre-constructed data packet to core buffer
1221 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1222 * @param size Number of bytes available in buf.
1223 * @param buf Where the to write the message.
1225 * @return number of bytes written to buf
1228 send_core_data_raw (void *cls, size_t size, void *buf)
1230 struct GNUNET_MessageHeader *msg = cls;
1233 GNUNET_assert (NULL != msg);
1234 total_size = ntohs (msg->size);
1236 if (total_size > size)
1241 memcpy (buf, msg, total_size);
1248 * Sends an already built message to a peer, properly registrating
1249 * all used resources.
1251 * @param message Message to send. Function makes a copy of it.
1252 * @param peer Short ID of the neighbor whom to send the message.
1253 * @param t Tunnel on which this message is transmitted.
1256 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1257 GNUNET_PEER_Id peer,
1258 struct MeshTunnel *t)
1260 struct GNUNET_PeerIdentity id;
1261 struct MeshPeerInfo *neighbor;
1262 struct MeshPeerPath *p;
1267 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1272 size = ntohs (message->size);
1273 data = GNUNET_malloc (size);
1274 memcpy (data, message, size);
1275 type = ntohs(message->type);
1276 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1277 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1279 struct GNUNET_MESH_Data *u;
1281 u = (struct GNUNET_MESH_Data *) data;
1282 u->ttl = htonl (ntohl (u->ttl) - 1);
1284 GNUNET_PEER_resolve (peer, &id);
1285 neighbor = peer_get (&id);
1286 for (p = neighbor->path_head; NULL != p; p = p->next)
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 " %s IS NOT DIRECTLY CONNECTED\n",
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 for (p = neighbor->path_head; NULL != p; p = p->next)
1304 struct GNUNET_PeerIdentity debug_id;
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 " path with %u hops through:\n",
1310 for (i = 0; i < p->length; i++)
1312 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 i, GNUNET_i2s(&debug_id));
1319 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1321 " no direct connection to %s\n",
1326 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1337 * Sends a CREATE PATH message for a path to a peer, properly registrating
1338 * all used resources.
1340 * @param t Tunnel for which the path is created.
1343 send_create_path (struct MeshTunnel *t)
1345 struct MeshPeerInfo *neighbor;
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1348 neighbor = peer_get_short (t->next_hop);
1350 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1351 sizeof (struct GNUNET_MESH_CreateTunnel) +
1352 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1355 t->state = MESH_TUNNEL_WAITING;
1360 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1362 * @param t Tunnel which to confirm.
1365 send_path_ack (struct MeshTunnel *t)
1367 struct MeshPeerInfo *peer;
1369 peer = peer_get_short (t->prev_hop);
1372 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1373 sizeof (struct GNUNET_MESH_PathACK),
1380 * Try to establish a new connection to this peer in the given tunnel.
1381 * If the peer doesn't have any path to it yet, try to get one.
1382 * If the peer already has some path, send a CREATE PATH towards it.
1384 * @param peer PeerInfo of the peer.
1385 * @param t Tunnel for which to create the path, if possible.
1388 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1390 struct MeshPeerPath *p;
1392 if (NULL != peer->path_head)
1394 p = peer_get_best_path (peer, t);
1395 tunnel_use_path (t, p);
1396 send_create_path (t);
1398 else if (NULL == peer->dhtget)
1400 struct GNUNET_PeerIdentity id;
1402 GNUNET_PEER_resolve (peer->id, &id);
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1404 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1405 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1406 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1407 &id.hashPubKey, /* key to search */
1408 dht_replication_level, /* replication level */
1409 GNUNET_DHT_RO_RECORD_ROUTE |
1410 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1412 0, /* xquery bits */
1413 &dht_get_id_handler, peer);
1414 t->state = MESH_TUNNEL_SEARCHING;
1418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1419 "There is no path but the DHT GET is already started.\n");
1425 * Destroy the peer_info and free any allocated resources linked to it
1427 * @param pi The peer_info to destroy.
1429 * @return GNUNET_OK on success
1432 peer_info_destroy (struct MeshPeerInfo *pi)
1434 struct GNUNET_PeerIdentity id;
1435 struct MeshPeerPath *p;
1436 struct MeshPeerPath *nextp;
1439 GNUNET_PEER_resolve (pi->id, &id);
1440 GNUNET_PEER_change_rc (pi->id, -1);
1443 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1446 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1447 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1449 if (NULL != pi->dhtget)
1451 GNUNET_DHT_get_stop (pi->dhtget);
1457 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1461 for (i = 0; i < pi->ntunnels; i++)
1462 tunnel_destroy_empty (pi->tunnels[i]);
1463 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1470 * Remove all paths that rely on a direct connection between p1 and p2
1471 * from the peer itself and notify all tunnels about it.
1473 * @param peer PeerInfo of affected peer.
1474 * @param p1 GNUNET_PEER_Id of one peer.
1475 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1478 * TODO: optimize (see below)
1481 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1484 struct MeshPeerPath *p;
1485 struct MeshPeerPath *next;
1486 struct MeshPeerInfo *peer_d;
1488 unsigned int destroyed;
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1493 for (p = peer->path_head; NULL != p; p = next)
1496 for (i = 0; i < (p->length - 1); i++)
1498 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1499 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1501 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1511 for (i = 0; i < peer->ntunnels; i++)
1513 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1517 peer_d = peer_get_short (d);
1518 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1519 tunnel_use_path (peer->tunnels[i], next);
1520 peer_connect (peer_d, peer->tunnels[i]);
1522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1527 * Add the path to the peer and update the path used to reach it in case this
1530 * @param peer_info Destination peer to add the path to.
1531 * @param path New path to add. Last peer must be the peer in arg 1.
1532 * Path will be either used of freed if already known.
1533 * @param trusted Do we trust that this path is real?
1536 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1539 struct MeshPeerPath *aux;
1543 if ((NULL == peer_info) || (NULL == path))
1546 path_destroy (path);
1549 if (path->peers[path->length - 1] != peer_info->id)
1552 path_destroy (path);
1555 if (2 >= path->length && GNUNET_NO == trusted)
1557 /* Only allow CORE to tell us about direct paths */
1558 path_destroy (path);
1561 for (l = 1; l < path->length; l++)
1563 if (path->peers[l] == myid)
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1566 for (l2 = 0; l2 < path->length - l; l2++)
1568 path->peers[l2] = path->peers[l + l2];
1573 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1578 struct GNUNET_PeerIdentity id;
1580 GNUNET_PEER_resolve (peer_info->id, &id);
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1582 path->length, GNUNET_i2s (&id));
1585 l = path_get_length (path);
1588 path_destroy (path);
1592 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1593 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1595 l2 = path_get_length (aux);
1598 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1599 peer_info->path_tail, aux, path);
1604 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1606 path_destroy (path);
1611 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1618 * Add the path to the origin peer and update the path used to reach it in case
1619 * this is the shortest.
1620 * The path is given in peer_info -> destination, therefore we turn the path
1621 * upside down first.
1623 * @param peer_info Peer to add the path to, being the origin of the path.
1624 * @param path New path to add after being inversed.
1625 * Path will be either used or freed.
1626 * @param trusted Do we trust that this path is real?
1629 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1630 struct MeshPeerPath *path, int trusted)
1633 peer_info_add_path (peer_info, path, trusted);
1638 * Add a tunnel to the list of tunnels a peer participates in.
1639 * Update the tunnel's destination.
1641 * @param p Peer to add to.
1642 * @param t Tunnel to add.
1645 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1649 GNUNET_break (t->dest == p->id);
1653 GNUNET_PEER_change_rc (t->dest, 1);
1654 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1659 * Remove a tunnel from the list of tunnels a peer participates in.
1660 * Free the tunnel's destination.
1662 * @param p Peer to clean.
1663 * @param t Tunnel to remove.
1666 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1670 if (t->dest == p->id)
1672 GNUNET_PEER_change_rc (t->dest, -1);
1675 for (i = 0; i < p->ntunnels; i++)
1677 if (p->tunnels[i] == t)
1679 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1680 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1688 * Function called if the connection to the peer has been stalled for a while,
1689 * possibly due to a missed ACK. Poll the peer about its ACK status.
1691 * @param cls Closure (poll ctx).
1692 * @param tc TaskContext.
1695 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1697 struct MeshFlowControl *fc = cls;
1698 struct GNUNET_MESH_Poll msg;
1699 struct MeshTunnel *t = fc->t;
1700 GNUNET_PEER_Id peer;
1702 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1703 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1710 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1712 if (fc == &t->prev_fc)
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1717 else if (fc == &t->next_fc)
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1727 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1728 msg.header.size = htons (sizeof (msg));
1729 msg.tid = htonl (t->id.tid);
1730 msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t));
1731 send_prebuilt_message (&msg.header, peer, t);
1732 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1733 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1739 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1740 * to obtain a local peer -> destination path and interning the peer ids.
1742 * @return Newly allocated and created path
1744 static struct MeshPeerPath *
1745 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1746 unsigned int get_path_length,
1747 const struct GNUNET_PeerIdentity *put_path,
1748 unsigned int put_path_length)
1750 struct MeshPeerPath *p;
1756 GNUNET_PEER_change_rc (myid, 1);
1757 i = get_path_length;
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1759 for (i--; i >= 0; i--)
1761 id = GNUNET_PEER_intern (&get_path[i]);
1762 if (p->length > 0 && id == p->peers[p->length - 1])
1764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1765 GNUNET_PEER_change_rc (id, -1);
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1770 GNUNET_i2s (&get_path[i]));
1772 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1773 p->peers[p->length - 1] = id;
1776 i = put_path_length;
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1778 for (i--; i >= 0; i--)
1780 id = GNUNET_PEER_intern (&put_path[i]);
1783 /* PUT path went through us, so discard the path up until now and start
1784 * from here to get a much shorter (and loop-free) path.
1789 if (p->length > 0 && id == p->peers[p->length - 1])
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1792 GNUNET_PEER_change_rc (id, -1);
1796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1797 GNUNET_i2s (&put_path[i]));
1799 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1800 p->peers[p->length - 1] = id;
1804 if (get_path_length > 0)
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1806 GNUNET_i2s (&get_path[0]));
1807 if (put_path_length > 0)
1808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1809 GNUNET_i2s (&put_path[0]));
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1812 for (i = 0; i < p->length; i++)
1814 struct GNUNET_PeerIdentity peer_id;
1816 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1818 GNUNET_i2s (&peer_id));
1826 * Adds a path to the peer_infos of all the peers in the path
1828 * @param p Path to process.
1829 * @param confirmed Whether we know if the path works or not.
1832 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1836 /* TODO: invert and add */
1837 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1838 for (i++; i < p->length; i++)
1840 struct MeshPeerInfo *aux;
1841 struct MeshPeerPath *copy;
1843 aux = peer_get_short (p->peers[i]);
1844 copy = path_duplicate (p);
1845 copy->length = i + 1;
1846 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1852 * Send keepalive packets for a peer
1854 * @param cls Closure (tunnel for which to send the keepalive).
1855 * @param tc Notification context.
1858 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1862 * Search for a tunnel among the incoming tunnels
1864 * @param tid the local id of the tunnel
1866 * @return tunnel handler, NULL if doesn't exist
1868 static struct MeshTunnel *
1869 tunnel_get_incoming (MESH_TunnelNumber tid)
1871 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1872 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1877 * Search for a tunnel among the tunnels for a client
1879 * @param c the client whose tunnels to search in
1880 * @param tid the local id of the tunnel
1882 * @return tunnel handler, NULL if doesn't exist
1884 static struct MeshTunnel *
1885 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1887 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1889 return tunnel_get_incoming (tid);
1893 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1894 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1900 * Search for a tunnel by global ID using PEER_ID
1902 * @param pi owner of the tunnel
1903 * @param tid global tunnel number
1905 * @return tunnel handler, NULL if doesn't exist
1907 static struct MeshTunnel *
1908 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1910 struct MESH_TunnelID id;
1911 struct GNUNET_HashCode hash;
1916 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1917 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1922 * Search for a tunnel by global ID using full PeerIdentities
1924 * @param oid owner of the tunnel
1925 * @param tid global tunnel number
1927 * @return tunnel handler, NULL if doesn't exist
1929 static struct MeshTunnel *
1930 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1932 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1937 * Add a client to a tunnel, initializing all needed data structures.
1939 * @param t Tunnel to which add the client.
1940 * @param c Client which to add to the tunnel.
1943 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1945 if (NULL != t->client)
1951 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1952 t->local_tid_dest, t,
1953 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1959 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1960 t->local_tid_dest, t,
1961 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1971 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1973 unsigned int own_pos;
1975 for (own_pos = 0; own_pos < p->length; own_pos++)
1977 if (p->peers[own_pos] == myid)
1980 if (own_pos > p->length - 1)
1986 if (own_pos < p->length - 1)
1987 t->next_hop = p->peers[own_pos + 1];
1989 t->next_hop = p->peers[own_pos];
1990 GNUNET_PEER_change_rc (t->next_hop, 1);
1992 t->prev_hop = p->peers[own_pos - 1];
1994 t->prev_hop = p->peers[0];
1995 GNUNET_PEER_change_rc (t->prev_hop, 1);
1997 if (NULL != t->path)
1998 path_destroy (t->path);
1999 t->path = path_duplicate (p);
2002 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2003 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2004 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2011 * Notifies a tunnel that a connection has broken that affects at least
2012 * some of its peers. Sends a notification towards the root of the tree.
2013 * In case the peer is the owner of the tree, notifies the client that owns
2014 * the tunnel and tries to reconnect.
2016 * @param t Tunnel affected.
2017 * @param p1 Peer that got disconnected from p2.
2018 * @param p2 Peer that got disconnected from p1.
2020 * @return Short ID of the peer disconnected (either p1 or p2).
2021 * 0 if the tunnel remained unaffected.
2023 static GNUNET_PEER_Id
2024 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2027 // if (myid != p1 && myid != p2) FIXME
2032 // if (tree_get_predecessor (t->tree) != 0)
2034 // /* We are the peer still connected, notify owner of the disconnection. */
2035 // struct GNUNET_MESH_PathBroken msg;
2036 // struct GNUNET_PeerIdentity neighbor;
2038 // msg.header.size = htons (sizeof (msg));
2039 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2040 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2041 // msg.tid = htonl (t->id.tid);
2042 // msg.peer1 = my_full_id;
2043 // GNUNET_PEER_resolve (pid, &msg.peer2);
2044 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2045 // send_prebuilt_message (&msg.header, &neighbor, t);
2052 * Build a local ACK message and send it to a local client.
2054 * @param t Tunnel on which to send the ACK.
2055 * @param c Client to whom send the ACK.
2056 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
2059 send_local_ack (struct MeshTunnel *t,
2060 struct MeshClient *c,
2063 struct GNUNET_MESH_LocalAck msg;
2065 msg.header.size = htons (sizeof (msg));
2066 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2067 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
2068 GNUNET_SERVER_notification_context_unicast (nc,
2075 * Build an ACK message and queue it to send to the given peer.
2077 * @param t Tunnel on which to send the ACK.
2078 * @param peer Peer to whom send the ACK.
2079 * @param ack Value of the ACK.
2082 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2084 struct GNUNET_MESH_ACK msg;
2086 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2087 msg.header.size = htons (sizeof (msg));
2088 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2089 msg.pid = htonl (ack);
2090 msg.tid = htonl (t->id.tid);
2092 send_prebuilt_message (&msg.header, peer, t);
2097 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2099 * @param t Tunnel this is about.
2102 tunnel_send_fwd_data_ack (struct MeshTunnel *t)
2104 struct GNUNET_MESH_DataACK msg;
2105 struct MeshTunnelReliability *rel;
2106 struct MeshReliableMessage *copy;
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 "send_fwd_data_ack for %llu\n",
2112 t->bck_rel->mid_recv - 1);
2114 if (GNUNET_NO == t->reliable)
2116 GNUNET_break_op (0);
2119 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
2120 msg.header.size = htons (sizeof (msg));
2121 msg.tid = htonl (t->id.tid);
2122 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2123 msg.mid = GNUNET_htonll (t->bck_rel->mid_recv - 1);
2126 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2128 delta = copy->mid - t->bck_rel->mid_recv;
2131 mask = 0x1LL << delta;
2132 msg.futures |= mask;
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2134 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2135 copy->mid, delta, mask, msg.futures);
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2139 send_prebuilt_message (&msg.header, t->prev_hop, t);
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_fwd_data_ack END\n");
2145 * Send an end-to-end BCK ACK message for the most recent in-sequence payload.
2147 * @param t Tunnel this is about.
2150 tunnel_send_bck_data_ack (struct MeshTunnel *t)
2152 struct GNUNET_MESH_DataACK msg;
2154 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2155 msg.header.size = htons (sizeof (msg));
2156 msg.tid = htonl (t->id.tid);
2157 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2158 msg.mid = GNUNET_htonll (t->fwd_rel->mid_recv - 1);
2159 msg.futures = 0; // FIXME set bits of other newer messages received
2161 send_prebuilt_message (&msg.header, t->next_hop, t);
2166 * Send an ACK informing the predecessor about the available buffer space.
2167 * In case there is no predecessor, inform the owning client.
2168 * If buffering is off, send only on behalf of children or self if endpoint.
2169 * If buffering is on, send when sent to children and buffer space is free.
2170 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2171 * the ACK itself goes "back" (towards root).
2173 * @param t Tunnel on which to send the ACK.
2174 * @param type Type of message that triggered the ACK transmission.
2177 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2179 struct MeshTunnelReliability *rel = t->fwd_rel;
2184 /* Is it after unicast retransmission? */
2187 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2189 "ACK due to FWD DATA retransmission\n");
2190 if (GNUNET_YES == t->nobuffer)
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2195 if (GNUNET_YES == t->reliable && NULL != t->client)
2196 tunnel_send_fwd_data_ack (t);
2198 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2199 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2200 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2202 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2203 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2204 t->force_ack = GNUNET_YES;
2210 /* Check if we need to transmit the ACK */
2211 if (0 && NULL == t->owner &&
2212 t->queue_max > t->next_fc.queue_n * 4 &&
2213 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2214 GNUNET_NO == t->force_ack)
2216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, buffer free\n");
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2218 " t->qmax: %u, t->qn: %u\n",
2219 t->queue_max, t->next_fc.queue_n);
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 " t->pid: %u, t->ack: %u\n",
2222 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2226 /* Ok, ACK might be necessary, what PID to ACK? */
2227 delta = t->queue_max - t->next_fc.queue_n;
2228 if (NULL != t->owner && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2229 delta_mid = rel->mid_sent - rel->head_sent->mid;
2232 if (0 > delta || (GNUNET_YES == t->reliable &&
2234 (rel->n_sent > 10 || delta_mid > 64)))
2236 if (NULL != t->owner && delta > 1)
2238 ack = t->prev_fc.last_pid_recv + delta;
2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK %u\n", ack);
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2241 " last pid %u, last ack %u, qmax %u, q %u\n",
2242 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent,
2243 t->queue_max, t->next_fc.queue_n);
2244 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2250 t->prev_fc.last_ack_sent = ack;
2251 if (NULL != t->owner)
2252 send_local_ack (t, t->owner, GNUNET_YES);
2253 else if (0 != t->prev_hop)
2254 send_ack (t, t->prev_hop, ack);
2258 t->force_ack = GNUNET_NO;
2263 * Send an ACK informing the children node/client about the available
2265 * If buffering is off, send only on behalf of root (can be self).
2266 * If buffering is on, send when sent to predecessor and buffer space is free.
2267 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2268 * the ACK itself goes "forward" (towards children/clients).
2270 * @param t Tunnel on which to send the ACK.
2271 * @param type Type of message that triggered the ACK transmission.
2274 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2278 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2279 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2280 /* Is it after data to_origin retransmission? */
2283 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2284 if (GNUNET_YES == t->nobuffer)
2286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2287 " Not sending ACK, nobuffer + traffic\n");
2290 if (GNUNET_YES == t->reliable && NULL != t->owner)
2291 tunnel_send_bck_data_ack (t);
2293 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2294 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2296 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2297 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2298 t->force_ack = GNUNET_YES;
2304 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2306 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2308 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2311 " Not sending ACK, not needed, last ack sent was %u\n",
2312 t->next_fc.last_ack_sent);
2315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2316 " Sending BCK ACK %u (last sent: %u)\n",
2317 ack, t->next_fc.last_ack_sent);
2318 t->next_fc.last_ack_sent = ack;
2320 if (NULL != t->client)
2321 send_local_ack (t, t->client, GNUNET_NO);
2322 else if (0 != t->next_hop)
2323 send_ack (t, t->next_hop, ack);
2326 t->force_ack = GNUNET_NO;
2331 * Modify the mesh message TID from global to local and send to client.
2333 * @param t Tunnel on which to send the message.
2334 * @param msg Message to modify and send.
2335 * @param c Client to send to.
2336 * @param tid Tunnel ID to use (c can be both owner and client).
2339 tunnel_send_client_data (struct MeshTunnel *t,
2340 const struct GNUNET_MESH_Data *msg,
2341 struct MeshClient *c, MESH_TunnelNumber tid)
2343 struct GNUNET_MESH_LocalData *copy;
2344 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2345 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2347 if (size < sizeof (struct GNUNET_MessageHeader))
2349 GNUNET_break_op (0);
2357 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2358 memcpy (©[1], &msg[1], size);
2359 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2360 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2361 copy->tid = htonl (tid);
2362 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2363 ©->header, GNUNET_NO);
2367 * Modify the unicast message TID from global to local and send to client.
2369 * @param t Tunnel on which to send the message.
2370 * @param msg Message to modify and send.
2373 tunnel_send_client_ucast (struct MeshTunnel *t,
2374 const struct GNUNET_MESH_Data *msg)
2376 tunnel_send_client_data (t, msg, t->client, t->local_tid_dest);
2381 * Send up to 64 buffered messages to the client for in order delivery.
2383 * @param t Tunnel on which to empty the message buffer.
2386 tunnel_send_client_buffered_ucast (struct MeshTunnel *t)
2388 struct MeshTunnelReliability *rel;
2389 struct MeshReliableMessage *copy;
2390 struct MeshReliableMessage *next;
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_unicast\n");
2394 for (copy = rel->head_recv; NULL != copy; copy = next)
2397 if (copy->mid == rel->mid_recv)
2399 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 " have %llu! now expecting %llu\n",
2403 copy->mid, rel->mid_recv + 1LL);
2404 tunnel_send_client_ucast (t, msg);
2406 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2412 " don't have %llu, (%llu)\n",
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_unicast END\n");
2423 * We have received a message out of order, buffer it until we receive
2424 * the missing one and we can feed the rest to the client.
2427 tunnel_add_buffer_ucast (struct MeshTunnel *t,
2428 const struct GNUNET_MESH_Data *msg)
2430 struct MeshTunnelReliability *rel;
2431 struct MeshReliableMessage *copy;
2432 struct MeshReliableMessage *prev;
2437 size = ntohs (msg->header.size);
2438 mid = GNUNET_ntohll (msg->mid);
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffer_ucast %llu\n", mid);
2441 copy = GNUNET_malloc (sizeof (*copy) + size);
2444 memcpy (©[1], msg, size);
2446 // FIXME do something better than O(n), although n < 64...
2447 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %llu\n", prev->mid);
2450 if (mid < prev->mid)
2452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2453 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2459 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffer_ucast END\n");
2465 * Destroy a reliable message after it has been acknowledged, either by
2466 * direct mid ACK or bitfield. Updates the appropriate data structures and
2467 * timers and frees all memory.
2469 * @param copy Message that is no longer needed: remote peer got it.
2472 tunnel_free_reliable_message (struct MeshReliableMessage *copy)
2474 struct MeshTunnelReliability *rel;
2475 struct GNUNET_TIME_Relative time;
2478 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2479 rel->expected_delay.rel_value *= 7;
2480 rel->expected_delay.rel_value += time.rel_value;
2481 rel->expected_delay.rel_value /= 8;
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %llu\n", copy->mid);
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
2486 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
2488 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
2490 rel->retry_timer = rel->expected_delay;
2491 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2497 * Mark future messages as ACK'd.
2499 * @param t Tunnel whose sent buffer to clean.
2500 * @param msg DataACK message with a bitfield of future ACK'd messages.
2503 tunnel_free_buffer_ucast (struct MeshTunnel *t,
2504 const struct GNUNET_MESH_DataACK *msg)
2506 struct MeshTunnelReliability *rel;
2507 struct MeshReliableMessage *copy;
2508 struct MeshReliableMessage *next;
2515 bitfield = msg->futures;
2516 mid = GNUNET_ntohll (msg->mid);
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519 "free_sent_buffer %llu %llX\n",
2521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2522 " rel %p, head %p\n",
2523 rel, rel->head_sent);
2524 for (i = 0, copy = rel->head_sent;
2525 i < 64 && NULL != copy && 0 != bitfield;
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2529 " trying bit %u (mid %llu)\n",
2532 if (0 == (bitfield & mask))
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2536 /* Bit was set, clear the bit from the bitfield */
2539 /* The i-th bit was set. Do we have that copy? */
2540 /* Skip copies with mid < target */
2541 target = mid + i + 1;
2542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %llu\n", target);
2543 while (NULL != copy && copy->mid < target)
2546 /* Did we run out of copies? (previously freed, it's ok) */
2549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2553 /* Did we overshoot the target? (previously freed, it's ok) */
2554 if (copy->mid > target)
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %llu\n", copy->mid);
2560 /* Now copy->mid == target, free it */
2562 tunnel_free_reliable_message (copy);
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_buffer END\n");
2570 * Modify the to_origin message TID from global to local and send to client.
2572 * @param t Tunnel on which to send the message.
2573 * @param msg Message to modify and send.
2576 tunnel_send_client_to_orig (struct MeshTunnel *t,
2577 const struct GNUNET_MESH_Data *msg)
2579 tunnel_send_client_data (t, msg, t->owner, t->local_tid);
2584 * We haven't received an ACK after a certain time: restransmit the message.
2586 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2587 * @param tc TaskContext.
2590 tunnel_retransmit_message (void *cls,
2591 const struct GNUNET_SCHEDULER_TaskContext *tc)
2593 struct MeshTunnelReliability *rel = cls;
2594 struct MeshReliableMessage *copy;
2595 struct MeshFlowControl *fc;
2596 struct MeshPeerQueue *q;
2597 struct MeshPeerInfo *pi;
2598 struct MeshTunnel *t;
2599 struct GNUNET_MESH_Data *payload;
2602 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2603 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2607 copy = rel->head_sent;
2614 /* Search the message to be retransmitted in the outgoing queue */
2615 payload = (struct GNUNET_MESH_Data *) ©[1];
2616 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2617 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2618 pi = peer_get_short (hop);
2619 for (q = pi->queue_head; NULL != q; q = q->next)
2621 if (ntohs (payload->header.type) == q->type)
2623 struct GNUNET_MESH_Data *queued_data = q->cls;
2625 if (queued_data->mid == payload->mid)
2630 /* Message not found in the queue */
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %llu\n", copy->mid);
2635 fc->last_ack_sent++;
2636 fc->last_pid_recv++;
2637 payload->pid = htonl (fc->last_pid_recv);
2638 send_prebuilt_message (&payload->header, hop, t);
2639 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %llu\n", copy->mid);
2646 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2647 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2648 &tunnel_retransmit_message,
2654 * @brief Re-initiate traffic to this peer if necessary.
2656 * Check if there is traffic queued towards this peer
2657 * and the core transmit handle is NULL (traffic was stalled).
2658 * If so, call core tmt rdy.
2660 * @param peer_id Short ID of peer to which initiate traffic.
2663 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2665 struct MeshPeerInfo *peer;
2666 struct GNUNET_PeerIdentity id;
2667 struct MeshPeerQueue *q;
2670 peer = peer_get_short (peer_id);
2671 if (NULL != peer->core_transmit)
2674 q = queue_get_next (peer);
2677 /* Might br multicast traffic already sent to this particular peer but
2678 * not to other children in this tunnel.
2679 * This way t->queue_n would be > 0 but the queue of this particular peer
2685 GNUNET_PEER_resolve (peer->id, &id);
2686 peer->core_transmit =
2687 GNUNET_CORE_notify_transmit_ready(core_handle,
2690 GNUNET_TIME_UNIT_FOREVER_REL,
2700 * Send a message to all peers and clients in this tunnel that the tunnel
2701 * is no longer valid. If some peer or client should not receive the message,
2702 * should be zero'ed out before calling this function.
2704 * @param t The tunnel whose peers and clients to notify.
2707 tunnel_send_destroy (struct MeshTunnel *t)
2709 struct GNUNET_MESH_TunnelDestroy msg;
2710 struct GNUNET_PeerIdentity id;
2712 msg.header.size = htons (sizeof (msg));
2713 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2714 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2715 msg.tid = htonl (t->id.tid);
2716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2717 " sending tunnel destroy for tunnel: %s [%X]\n",
2718 GNUNET_i2s (&msg.oid), t->id.tid);
2720 if (NULL == t->client && 0 != t->next_hop)
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2723 GNUNET_PEER_resolve (t->next_hop, &id);
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2725 " sending forward to %s\n",
2727 send_prebuilt_message (&msg.header, t->next_hop, t);
2729 if (NULL == t->owner && 0 != t->prev_hop)
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2732 GNUNET_PEER_resolve (t->prev_hop, &id);
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2734 " sending back to %s\n",
2736 send_prebuilt_message (&msg.header, t->prev_hop, t);
2738 if (NULL != t->owner)
2740 send_client_tunnel_destroy (t->owner, t);
2742 if (NULL != t->client)
2744 send_client_tunnel_destroy (t->client, t);
2750 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2752 * @param t Tunnel which to cancel.
2753 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2756 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2758 struct MeshPeerInfo *peer_info;
2759 struct MeshPeerQueue *pq;
2760 struct MeshPeerQueue *next;
2763 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2764 peer_info = peer_get_short (neighbor);
2765 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2768 if (pq->tunnel == t)
2770 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2771 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2773 /* Should have been removed on destroy children */
2776 queue_destroy (pq, GNUNET_YES);
2779 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2781 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2782 peer_info->core_transmit = NULL;
2788 * Destroy the tunnel.
2790 * This function does not generate any warning traffic to clients or peers.
2793 * Remove the tunnel from peer_info's and clients' hashmaps.
2794 * Cancel messages belonging to this tunnel queued to neighbors.
2795 * Free any allocated resources linked to the tunnel.
2797 * @param t the tunnel to destroy
2799 * @return GNUNET_OK on success
2802 tunnel_destroy (struct MeshTunnel *t)
2804 struct MeshClient *c;
2805 struct GNUNET_HashCode hash;
2815 struct GNUNET_PeerIdentity id;
2817 GNUNET_PEER_resolve (t->id.oid, &id);
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2819 GNUNET_i2s (&id), t->id.tid);
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2825 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2826 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2835 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2842 if (NULL != t->client)
2846 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2847 t->local_tid_dest, t))
2853 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2854 t->local_tid_dest, t))
2861 if (0 != t->prev_hop)
2863 peer_cancel_queues (t->prev_hop, t);
2864 GNUNET_PEER_change_rc (t->prev_hop, -1);
2866 if (0 != t->next_hop)
2868 peer_cancel_queues (t->next_hop, t);
2869 GNUNET_PEER_change_rc (t->next_hop, -1);
2872 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2875 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2876 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2879 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2880 path_destroy (t->path);
2886 * Tunnel is empty: destroy it.
2888 * Notifies all participants (peers, cleints) about the destruction.
2890 * @param t Tunnel to destroy.
2893 tunnel_destroy_empty (struct MeshTunnel *t)
2897 struct GNUNET_PeerIdentity id;
2899 GNUNET_PEER_resolve (t->id.oid, &id);
2900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2901 "executing destruction of empty tunnel %s [%X]\n",
2902 GNUNET_i2s (&id), t->id.tid);
2906 if (GNUNET_NO == t->destroy)
2907 tunnel_send_destroy (t);
2908 if (0 == t->pending_messages)
2911 t->destroy = GNUNET_YES;
2915 * Initialize a Flow Control structure to the initial state.
2917 * @param fc Flow Control structure to initialize.
2920 fc_init (struct MeshFlowControl *fc)
2922 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2923 fc->last_pid_recv = (uint32_t) -1;
2924 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2925 fc->last_ack_recv = (uint32_t) -1;
2926 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2927 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2932 * Create a new tunnel
2934 * @param owner Who is the owner of the tunnel (short ID).
2935 * @param tid Tunnel Number of the tunnel.
2936 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2937 * @param local Tunnel Number for the tunnel, for the client point of view.
2939 * @return A new initialized tunnel. NULL on error.
2941 static struct MeshTunnel *
2942 tunnel_new (GNUNET_PEER_Id owner,
2943 MESH_TunnelNumber tid,
2944 struct MeshClient *client,
2945 MESH_TunnelNumber local)
2947 struct MeshTunnel *t;
2948 struct GNUNET_HashCode hash;
2950 if (n_tunnels >= max_tunnels && NULL == client)
2953 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2956 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2958 fc_init (&t->next_fc);
2959 fc_init (&t->prev_fc);
2960 t->local_tid = local;
2962 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2964 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2966 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2967 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2974 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2982 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2984 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2988 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2998 * Set options in a tunnel, extracted from a bit flag field
3000 * @param t Tunnel to set options to.
3001 * @param options Bit array in host byte order.
3004 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
3006 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
3007 GNUNET_YES : GNUNET_NO;
3008 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
3009 GNUNET_YES : GNUNET_NO;
3014 * Iterator for deleting each tunnel whose client endpoint disconnected.
3016 * @param cls Closure (client that has disconnected).
3017 * @param key The local tunnel id (used to access the hashmap).
3018 * @param value The value stored at the key (tunnel to destroy).
3020 * @return GNUNET_OK, keep iterating.
3023 tunnel_destroy_iterator (void *cls,
3027 struct MeshTunnel *t = value;
3028 struct MeshClient *c = cls;
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3031 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
3032 t->local_tid, t->local_tid_dest, c->id);
3033 client_delete_tunnel (c, t);
3036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
3038 if (0 != t->next_hop) { /* destroy could come before a path is used */
3039 GNUNET_PEER_change_rc (t->next_hop, -1);
3045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
3047 if (0 != t->prev_hop) { /* destroy could come before a path is used */
3048 GNUNET_PEER_change_rc (t->prev_hop, -1);
3053 tunnel_destroy_empty (t);
3060 * Timeout function, destroys tunnel if called
3062 * @param cls Closure (tunnel to destroy).
3063 * @param tc TaskContext
3066 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3068 struct MeshTunnel *t = cls;
3069 struct GNUNET_PeerIdentity id;
3071 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3072 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3074 GNUNET_PEER_resolve(t->id.oid, &id);
3075 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3076 "Tunnel %s [%X] timed out. Destroying.\n",
3077 GNUNET_i2s(&id), t->id.tid);
3078 if (NULL != t->client)
3079 send_client_tunnel_destroy (t->client, t);
3080 tunnel_destroy (t); /* Do not notify other */
3085 * Resets the tunnel timeout. Starts it if no timeout was running.
3087 * @param t Tunnel whose timeout to reset.
3089 * TODO use heap to improve efficiency of scheduler.
3092 tunnel_reset_timeout (struct MeshTunnel *t)
3094 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
3096 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
3097 GNUNET_SCHEDULER_cancel (t->maintenance_task);
3098 t->maintenance_task =
3099 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3100 (refresh_path_time, 4), &tunnel_timeout, t);
3104 /******************************************************************************/
3105 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3106 /******************************************************************************/
3109 * Function to send a create path packet to a peer.
3111 * @param cls closure
3112 * @param size number of bytes available in buf
3113 * @param buf where the callee should write the message
3114 * @return number of bytes written to buf
3117 send_core_path_create (void *cls, size_t size, void *buf)
3119 struct MeshTunnel *t = cls;
3120 struct GNUNET_MESH_CreateTunnel *msg;
3121 struct GNUNET_PeerIdentity *peer_ptr;
3122 struct MeshPeerPath *p = t->path;
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3129 sizeof (struct GNUNET_MESH_CreateTunnel) +
3130 p->length * sizeof (struct GNUNET_PeerIdentity);
3132 if (size < size_needed || NULL == buf)
3137 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
3138 msg->header.size = htons (size_needed);
3139 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3140 msg->tid = ntohl (t->id.tid);
3143 if (GNUNET_YES == t->nobuffer)
3144 opt |= GNUNET_MESH_OPTION_NOBUFFER;
3145 if (GNUNET_YES == t->reliable)
3146 opt |= GNUNET_MESH_OPTION_RELIABLE;
3147 msg->opt = htonl (opt);
3148 msg->port = htonl (t->port);
3150 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3151 for (i = 0; i < p->length; i++)
3153 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3157 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3163 * Creates a path ack message in buf and frees all unused resources.
3165 * @param cls closure (MeshTransmissionDescriptor)
3166 * @param size number of bytes available in buf
3167 * @param buf where the callee should write the message
3168 * @return number of bytes written to buf
3171 send_core_path_ack (void *cls, size_t size, void *buf)
3173 struct MeshTunnel *t = cls;
3174 struct GNUNET_MESH_PathACK *msg = buf;
3176 GNUNET_assert (NULL != t);
3177 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3182 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
3183 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3184 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3185 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
3186 msg->tid = htonl (t->id.tid);
3187 msg->peer_id = my_full_id;
3188 msg->ack = htonl (t->prev_fc.last_ack_sent);
3190 /* TODO add signature */
3192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3193 return sizeof (struct GNUNET_MESH_PathACK);
3198 * Free a transmission that was already queued with all resources
3199 * associated to the request.
3201 * @param queue Queue handler to cancel.
3202 * @param clear_cls Is it necessary to free associated cls?
3205 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3207 struct MeshFlowControl *fc;
3209 if (GNUNET_YES == clear_cls)
3211 switch (queue->type)
3213 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3214 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3215 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3217 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3218 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3219 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3220 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3221 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3223 " prebuilt message\n");
3224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3226 GNUNET_MESH_DEBUG_M2S (queue->type));
3228 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3234 " type %s unknown!\n",
3235 GNUNET_MESH_DEBUG_M2S (queue->type));
3237 GNUNET_free_non_null (queue->cls);
3239 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3240 queue->peer->queue_tail,
3243 /* Delete from appropriate fc in the tunnel */
3244 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3245 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3247 if (queue->peer->id == queue->tunnel->prev_hop)
3248 fc = &queue->tunnel->prev_fc;
3249 else if (queue->peer->id == queue->tunnel->next_hop)
3250 fc = &queue->tunnel->next_fc;
3254 GNUNET_free (queue);
3259 GNUNET_free (queue);
3264 * @brief Get the next transmittable message from the queue.
3266 * This will be the head, except in the case of being a data packet
3267 * not allowed by the destination peer.
3269 * @param peer Destination peer.
3271 * @return The next viable MeshPeerQueue element to send to that peer.
3272 * NULL when there are no transmittable messages.
3274 struct MeshPeerQueue *
3275 queue_get_next (const struct MeshPeerInfo *peer)
3277 struct MeshPeerQueue *q;
3279 struct GNUNET_MESH_Data *dmsg;
3280 struct MeshTunnel* t;
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3285 for (q = peer->queue_head; NULL != q; q = q->next)
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3290 GNUNET_MESH_DEBUG_M2S (q->type));
3291 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3292 pid = ntohl (dmsg->pid);
3295 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3296 ack = t->next_fc.last_ack_recv;
3298 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3299 ack = t->prev_fc.last_ack_recv;
3302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3307 "* ACK: %u, PID: %u, MID: %llu\n",
3308 ack, pid, GNUNET_ntohll (dmsg->mid));
3309 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3322 "* nothing found\n");
3328 queue_send (void *cls, size_t size, void *buf)
3330 struct MeshPeerInfo *peer = cls;
3331 struct GNUNET_MessageHeader *msg;
3332 struct MeshPeerQueue *queue;
3333 struct MeshTunnel *t;
3334 struct GNUNET_PeerIdentity dst_id;
3335 struct MeshFlowControl *fc;
3340 peer->core_transmit = NULL;
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3343 queue = queue_get_next (peer);
3345 /* Queue has no internal mesh traffic nor sendable payload */
3348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3349 if (NULL == peer->queue_head)
3350 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3355 GNUNET_PEER_resolve (peer->id, &dst_id);
3356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3358 GNUNET_i2s (&dst_id));
3359 /* Check if buffer size is enough for the message */
3360 if (queue->size > size)
3362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3363 "* not enough room, reissue\n");
3364 peer->core_transmit =
3365 GNUNET_CORE_notify_transmit_ready (core_handle,
3368 GNUNET_TIME_UNIT_FOREVER_REL,
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3378 GNUNET_assert (0 < t->pending_messages);
3379 t->pending_messages--;
3383 switch (queue->type)
3386 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3387 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3388 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3389 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3390 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3391 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3392 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3393 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3396 GNUNET_MESH_DEBUG_M2S (queue->type));
3398 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3399 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3400 data_size = send_core_data_raw (queue->cls, size, buf);
3401 msg = (struct GNUNET_MessageHeader *) buf;
3402 type = ntohs (msg->type);
3404 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3406 data_size = send_core_path_create (queue->cls, size, buf);
3408 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3410 data_size = send_core_path_ack (queue->cls, size, buf);
3414 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3415 "* type unknown: %u\n",
3420 if (0 < drop_percent &&
3421 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3424 "Dropping message of type %s\n",
3425 GNUNET_MESH_DEBUG_M2S(queue->type));
3428 /* Free queue, but cls was freed by send_core_* */
3429 queue_destroy (queue, GNUNET_NO);
3431 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3432 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3436 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3437 t->next_fc.last_pid_sent = pid;
3438 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3441 GNUNET_ntohll ( ((struct GNUNET_MESH_Data *) buf)->mid ));
3443 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3444 t->prev_fc.last_pid_sent = pid;
3445 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3451 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3457 /* If more data in queue, send next */
3458 queue = queue_get_next (peer);
3461 struct GNUNET_PeerIdentity id;
3463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3464 GNUNET_PEER_resolve (peer->id, &id);
3465 peer->core_transmit =
3466 GNUNET_CORE_notify_transmit_ready(core_handle,
3469 GNUNET_TIME_UNIT_FOREVER_REL,
3475 if (NULL != peer->queue_head)
3477 if (peer->id == t->next_hop)
3479 else if (peer->id == t->prev_hop)
3486 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
3488 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3489 "* %s starting poll timeout\n",
3490 GNUNET_i2s (&my_full_id));
3492 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3502 * @brief Queue and pass message to core when possible.
3504 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3505 * accounts for it. In case the queue is full, the message is dropped and
3508 * Otherwise, message is treated as internal and allowed to go regardless of
3511 * @param cls Closure (@c type dependant). It will be used by queue_send to
3512 * build the message to be sent if not already prebuilt.
3513 * @param type Type of the message, 0 for a raw message.
3514 * @param size Size of the message.
3515 * @param dst Neighbor to send message to.
3516 * @param t Tunnel this message belongs to.
3519 queue_add (void *cls, uint16_t type, size_t size,
3520 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3522 struct MeshPeerQueue *queue;
3523 struct GNUNET_PeerIdentity id;
3524 struct MeshFlowControl *fc;
3528 priority = GNUNET_NO;
3529 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3533 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3539 if (fc->queue_n >= t->queue_max)
3541 /* If this isn't a retransmission, drop the message */
3542 if (GNUNET_NO == t->reliable ||
3543 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3544 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3546 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3550 "queue full: %u/%u\n",
3551 fc->queue_n, t->queue_max);
3552 return; /* Drop this message */
3554 priority = GNUNET_YES;
3558 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3564 if (GNUNET_YES == priority)
3566 struct GNUNET_MESH_Data *d;
3570 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3571 d = (struct GNUNET_MESH_Data *) queue->cls;
3573 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3575 if (queue->type != type)
3577 d = (struct GNUNET_MESH_Data *) queue->cls;
3584 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3585 if (NULL == dst->core_transmit)
3587 GNUNET_PEER_resolve (dst->id, &id);
3588 dst->core_transmit =
3589 GNUNET_CORE_notify_transmit_ready (core_handle,
3592 GNUNET_TIME_UNIT_FOREVER_REL,
3598 t->pending_messages++;
3602 /******************************************************************************/
3603 /******************** MESH NETWORK HANDLERS **************************/
3604 /******************************************************************************/
3608 * Core handler for path creation
3610 * @param cls closure
3611 * @param message message
3612 * @param peer peer identity this notification is about
3614 * @return GNUNET_OK to keep the connection open,
3615 * GNUNET_SYSERR to close it (signal serious error)
3618 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3619 const struct GNUNET_MessageHeader *message)
3621 unsigned int own_pos;
3624 MESH_TunnelNumber tid;
3625 struct GNUNET_MESH_CreateTunnel *msg;
3626 struct GNUNET_PeerIdentity *pi;
3627 struct MeshPeerPath *path;
3628 struct MeshPeerInfo *dest_peer_info;
3629 struct MeshPeerInfo *orig_peer_info;
3630 struct MeshTunnel *t;
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3633 "Received a path create msg [%s]\n",
3634 GNUNET_i2s (&my_full_id));
3635 size = ntohs (message->size);
3636 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3638 GNUNET_break_op (0);
3642 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3643 if (size % sizeof (struct GNUNET_PeerIdentity))
3645 GNUNET_break_op (0);
3648 size /= sizeof (struct GNUNET_PeerIdentity);
3651 GNUNET_break_op (0);
3654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3655 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3657 tid = ntohl (msg->tid);
3658 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3660 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3661 t = tunnel_get (pi, tid);
3662 if (NULL == t) /* might be a local tunnel */
3666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3667 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3673 t->port = ntohl (msg->port);
3674 opt = ntohl (msg->opt);
3675 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3677 t->nobuffer = GNUNET_YES;
3680 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3682 t->reliable = GNUNET_YES;
3684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3686 tunnel_reset_timeout (t);
3688 t->state = MESH_TUNNEL_WAITING;
3690 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3691 if (NULL == dest_peer_info)
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3694 " Creating PeerInfo for destination.\n");
3695 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3696 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3697 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3699 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3701 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3702 if (NULL == orig_peer_info)
3704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3705 " Creating PeerInfo for origin.\n");
3706 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3707 orig_peer_info->id = GNUNET_PEER_intern (pi);
3708 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3709 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3712 path = path_new (size);
3714 for (i = 0; i < size; i++)
3716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3717 GNUNET_i2s (&pi[i]));
3718 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3719 if (path->peers[i] == myid)
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3723 if (own_pos == 0 && path->peers[own_pos] != myid)
3725 /* create path: self not found in path through self */
3726 GNUNET_break_op (0);
3727 path_destroy (path);
3731 path_add_to_peers (path, GNUNET_NO);
3732 tunnel_use_path (t, path);
3734 peer_info_add_tunnel (dest_peer_info, t);
3736 if (own_pos == size - 1)
3738 struct MeshClient *c;
3740 /* Find target client */
3741 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3744 /* TODO send reject */
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3749 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3751 /* Assign local tid */
3752 while (NULL != tunnel_get_incoming (next_local_tid))
3753 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3754 t->local_tid_dest = next_local_tid++;
3755 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3757 if (GNUNET_YES == t->reliable)
3759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3760 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
3762 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3765 tunnel_add_client (t, c);
3766 send_client_tunnel_create (t);
3771 struct MeshPeerPath *path2;
3773 t->next_hop = path->peers[own_pos + 1];
3774 GNUNET_PEER_change_rc(t->next_hop, 1);
3776 /* It's for somebody else! Retransmit. */
3777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3778 path2 = path_duplicate (path);
3779 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3780 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3781 send_create_path (t);
3789 * Core handler for path ACKs
3791 * @param cls closure
3792 * @param message message
3793 * @param peer peer identity this notification is about
3795 * @return GNUNET_OK to keep the connection open,
3796 * GNUNET_SYSERR to close it (signal serious error)
3799 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3800 const struct GNUNET_MessageHeader *message)
3802 struct GNUNET_MESH_PathACK *msg;
3803 struct MeshPeerInfo *peer_info;
3804 struct MeshPeerPath *p;
3805 struct MeshTunnel *t;
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3808 GNUNET_i2s (&my_full_id));
3809 msg = (struct GNUNET_MESH_PathACK *) message;
3810 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3813 /* TODO notify that we don't know the tunnel */
3814 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3816 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3820 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3822 peer_info = peer_get (&msg->peer_id);
3823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3824 GNUNET_i2s (&msg->peer_id));
3825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3828 /* Add path to peers? */
3832 path_add_to_peers (p, GNUNET_YES);
3838 t->state = MESH_TUNNEL_READY;
3839 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3840 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3842 /* Message for us? */
3843 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3846 if (NULL == t->owner)
3848 GNUNET_break_op (0);
3851 if (NULL != peer_info->dhtget)
3853 GNUNET_DHT_get_stop (peer_info->dhtget);
3854 peer_info->dhtget = NULL;
3856 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3857 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3862 " not for us, retransmitting...\n");
3863 peer_info = peer_get (&msg->oid);
3864 send_prebuilt_message (message, t->prev_hop, t);
3870 * Core handler for notifications of broken paths
3872 * @param cls closure
3873 * @param message message
3874 * @param peer peer identity this notification is about
3876 * @return GNUNET_OK to keep the connection open,
3877 * GNUNET_SYSERR to close it (signal serious error)
3880 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3881 const struct GNUNET_MessageHeader *message)
3883 struct GNUNET_MESH_PathBroken *msg;
3884 struct MeshTunnel *t;
3886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3887 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3888 msg = (struct GNUNET_MESH_PathBroken *) message;
3889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3890 GNUNET_i2s (&msg->peer1));
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3892 GNUNET_i2s (&msg->peer2));
3893 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3896 GNUNET_break_op (0);
3899 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3900 GNUNET_PEER_search (&msg->peer2));
3907 * Core handler for tunnel destruction
3909 * @param cls closure
3910 * @param message message
3911 * @param peer peer identity this notification is about
3913 * @return GNUNET_OK to keep the connection open,
3914 * GNUNET_SYSERR to close it (signal serious error)
3917 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3918 const struct GNUNET_MessageHeader *message)
3920 struct GNUNET_MESH_TunnelDestroy *msg;
3921 struct MeshTunnel *t;
3923 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3925 "Got a TUNNEL DESTROY packet from %s\n",
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3928 " for tunnel %s [%u]\n",
3929 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3930 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3933 /* Probably already got the message from another path,
3934 * destroyed the tunnel and retransmitted to children.
3937 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3941 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3944 t->local_tid, t->local_tid_dest);
3946 if (GNUNET_PEER_search (peer) == t->prev_hop)
3948 // TODO check owner's signature
3949 // TODO add owner's signatue to tunnel for retransmission
3950 peer_cancel_queues (t->prev_hop, t);
3951 GNUNET_PEER_change_rc (t->prev_hop, -1);
3954 else if (GNUNET_PEER_search (peer) == t->next_hop)
3956 // TODO check dest's signature
3957 // TODO add dest's signatue to tunnel for retransmission
3958 peer_cancel_queues (t->next_hop, t);
3959 GNUNET_PEER_change_rc (t->next_hop, -1);
3964 GNUNET_break_op (0);
3965 // TODO check both owner AND destination's signature to see which matches
3966 // TODO restransmit in appropriate direction
3969 tunnel_destroy_empty (t);
3971 // TODO: add timeout to destroy the tunnel anyway
3977 * Core handler for mesh network traffic going from the origin to a peer
3979 * @param cls closure
3980 * @param peer peer identity this notification is about
3981 * @param message message
3982 * @return GNUNET_OK to keep the connection open,
3983 * GNUNET_SYSERR to close it (signal serious error)
3986 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3987 const struct GNUNET_MessageHeader *message)
3989 struct GNUNET_MESH_Data *msg;
3990 struct MeshTunnel *t;
3995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3998 size = ntohs (message->size);
4000 sizeof (struct GNUNET_MESH_Data) +
4001 sizeof (struct GNUNET_MessageHeader))
4006 msg = (struct GNUNET_MESH_Data *) message;
4007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4008 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4010 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4013 /* TODO notify back: we don't know this tunnel */
4014 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4015 GNUNET_break_op (0);
4018 pid = ntohl (msg->pid);
4019 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
4021 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4022 GNUNET_break_op (0);
4023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4024 "Received PID %u, (prev %u), ACK %u\n",
4025 pid, t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
4029 tunnel_reset_timeout (t);
4030 if (t->dest == myid)
4032 /* TODO signature verification */
4033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4034 " it's for us! sending to clients...\n");
4035 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4036 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
4037 if (GMC_is_pid_bigger (pid, t->prev_fc.last_pid_recv))
4041 mid = GNUNET_ntohll (msg->mid);
4042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4043 " pid %u (%llu) not seen yet\n", pid, mid);
4044 t->prev_fc.last_pid_recv = pid;
4046 if (GNUNET_NO == t->reliable ||
4047 (mid >= t->bck_rel->mid_recv && mid <= t->bck_rel->mid_recv + 64))
4049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4050 "!!! RECV %llu\n", GNUNET_ntohll(msg->mid));
4051 if (GNUNET_YES == t->reliable)
4053 /* Is this the exact next expected messasge? */
4054 if (mid == t->bck_rel->mid_recv)
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4057 t->bck_rel->mid_recv++;
4058 tunnel_send_client_ucast (t, msg);
4059 tunnel_send_client_buffered_ucast (t);
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4064 tunnel_add_buffer_ucast (t, msg);
4067 else /* Tunnel unreliable, send to clients directly */
4069 tunnel_send_client_ucast (t, msg);
4074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4075 " MID %llu not expected (%llu - %llu), dropping!\n",
4076 GNUNET_ntohll (msg->mid),
4077 t->bck_rel->mid_recv, t->bck_rel->mid_recv + 64LL);
4082 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4084 " Pid %u not expected (%u), dropping!\n",
4085 pid, t->prev_fc.last_pid_recv + 1);
4087 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4090 t->prev_fc.last_pid_recv = pid;
4091 if (0 == t->next_hop)
4096 ttl = ntohl (msg->ttl);
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4100 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4102 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4106 " not for us, retransmitting...\n");
4108 send_prebuilt_message (message, t->next_hop, t);
4109 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4115 * Core handler for mesh network traffic toward the owner of a tunnel
4117 * @param cls closure
4118 * @param message message
4119 * @param peer peer identity this notification is about
4121 * @return GNUNET_OK to keep the connection open,
4122 * GNUNET_SYSERR to close it (signal serious error)
4125 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4126 const struct GNUNET_MessageHeader *message)
4128 struct GNUNET_MESH_Data *msg;
4129 struct MeshTunnel *t;
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4136 size = ntohs (message->size);
4137 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
4138 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4140 GNUNET_break_op (0);
4143 msg = (struct GNUNET_MESH_Data *) message;
4144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4145 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4146 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4147 pid = ntohl (msg->pid);
4150 /* TODO notify that we dont know this tunnel (whom)? */
4151 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4153 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4154 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4158 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
4160 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
4161 GNUNET_break_op (0);
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4163 "Received PID %u, ACK %u\n",
4164 pid, t->next_fc.last_ack_sent);
4165 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4169 if (myid == t->id.oid)
4171 /* TODO signature verification */
4172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4173 " it's for us! sending to clients...\n");
4174 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4175 if ( (GNUNET_NO == t->reliable &&
4176 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
4178 (GNUNET_YES == t->reliable &&
4179 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
4181 t->next_fc.last_pid_recv = pid;
4182 tunnel_send_client_to_orig (t, msg);
4186 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4188 " Pid %u not expected, sending FWD ACK!\n", pid);
4190 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4194 " not for us, retransmitting...\n");
4195 t->next_fc.last_pid_recv = pid;
4196 if (0 == t->prev_hop) /* No owner AND no prev hop */
4198 if (GNUNET_YES == t->destroy)
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4201 "to orig received on a dying tunnel %s [%X]\n",
4202 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4206 "unknown to origin at %s\n",
4207 GNUNET_i2s (&my_full_id));
4208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4212 "on tunnel %s [%X]\n",
4213 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4216 ttl = ntohl (msg->ttl);
4217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4220 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4221 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4222 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4225 send_prebuilt_message (message, t->prev_hop, t);
4226 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4233 * Core handler for mesh network traffic end-to-end ACKs.
4235 * @param cls Closure.
4236 * @param message Message.
4237 * @param peer Peer identity this notification is about.
4239 * @return GNUNET_OK to keep the connection open,
4240 * GNUNET_SYSERR to close it (signal serious error)
4243 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4244 const struct GNUNET_MessageHeader *message)
4246 struct GNUNET_MESH_DataACK *msg;
4247 struct MeshTunnelReliability *rel;
4248 struct MeshReliableMessage *copy;
4249 struct MeshReliableMessage *next;
4250 struct MeshTunnel *t;
4256 type = ntohs (message->type);
4257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4258 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4259 msg = (struct GNUNET_MESH_DataACK *) message;
4261 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4264 /* TODO notify that we dont know this tunnel (whom)? */
4265 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4268 ack = GNUNET_ntohll (msg->mid);
4269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %llu\n", ack);
4271 /* Is this a forward or backward ACK? */
4272 id = GNUNET_PEER_search (peer);
4273 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4276 if (NULL == t->owner)
4278 send_prebuilt_message (message, t->prev_hop, t);
4282 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4284 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4287 if (NULL == t->client)
4289 send_prebuilt_message (message, t->next_hop, t);
4293 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4297 GNUNET_break_op (0);
4301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %llu\n", ack);
4302 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4304 if (copy->mid > ack)
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %llu, out!\n", copy->mid);
4307 tunnel_free_buffer_ucast (t, msg);
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %llu\n", copy->mid);
4313 tunnel_free_reliable_message (copy);
4316 if (GNUNET_YES == work)
4318 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4320 GNUNET_SCHEDULER_cancel (rel->retry_task);
4321 if (NULL == rel->head_sent)
4323 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4327 struct GNUNET_TIME_Absolute new_target;
4328 struct GNUNET_TIME_Relative delay;
4330 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4331 MESH_RETRANSMIT_MARGIN);
4332 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4334 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4336 GNUNET_SCHEDULER_add_delayed (delay,
4337 &tunnel_retransmit_message,
4343 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
4349 * Core handler for mesh network traffic point-to-point acks.
4351 * @param cls closure
4352 * @param message message
4353 * @param peer peer identity this notification is about
4355 * @return GNUNET_OK to keep the connection open,
4356 * GNUNET_SYSERR to close it (signal serious error)
4359 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4360 const struct GNUNET_MessageHeader *message)
4362 struct GNUNET_MESH_ACK *msg;
4363 struct MeshTunnel *t;
4364 struct MeshFlowControl *fc;
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4370 msg = (struct GNUNET_MESH_ACK *) message;
4372 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4376 /* TODO notify that we dont know this tunnel (whom)? */
4377 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4380 ack = ntohl (msg->pid);
4381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4383 /* Is this a forward or backward ACK? */
4384 id = GNUNET_PEER_search (peer);
4385 if (t->next_hop == id)
4388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4391 else if (t->prev_hop == id)
4394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4399 GNUNET_break_op (0);
4403 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4404 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4406 GNUNET_SCHEDULER_cancel (fc->poll_task);
4407 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4408 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4410 fc->last_ack_recv = ack;
4411 peer_unlock_queue (id);
4413 if (t->next_hop == id)
4414 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4416 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4423 * Core handler for mesh network traffic point-to-point ack polls.
4425 * @param cls closure
4426 * @param message message
4427 * @param peer peer identity this notification is about
4429 * @return GNUNET_OK to keep the connection open,
4430 * GNUNET_SYSERR to close it (signal serious error)
4433 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4434 const struct GNUNET_MessageHeader *message)
4436 struct GNUNET_MESH_Poll *msg;
4437 struct MeshTunnel *t;
4438 struct MeshFlowControl *fc;
4443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4446 msg = (struct GNUNET_MESH_Poll *) message;
4448 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4452 /* TODO notify that we dont know this tunnel (whom)? */
4453 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4454 GNUNET_break_op (0);
4458 /* Is this a forward or backward ACK? */
4459 id = GNUNET_PEER_search(peer);
4460 pid = ntohl (msg->pid);
4461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4462 if (t->next_hop == id)
4464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->next_fc.last_pid_recv);
4467 old = fc->last_pid_recv;
4468 fc->last_pid_recv = pid;
4469 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4471 else if (t->prev_hop == id)
4473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->prev_fc.last_pid_recv);
4476 old = fc->last_pid_recv;
4477 fc->last_pid_recv = pid;
4478 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4483 if (GNUNET_YES == t->reliable)
4484 fc->last_pid_recv = old;
4491 * Core handler for mesh keepalives.
4493 * @param cls closure
4494 * @param message message
4495 * @param peer peer identity this notification is about
4496 * @return GNUNET_OK to keep the connection open,
4497 * GNUNET_SYSERR to close it (signal serious error)
4499 * TODO: Check who we got this from, to validate route.
4502 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4503 const struct GNUNET_MessageHeader *message)
4505 struct GNUNET_MESH_TunnelKeepAlive *msg;
4506 struct MeshTunnel *t;
4508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4511 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4512 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4516 /* TODO notify that we dont know that tunnel */
4517 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4522 tunnel_reset_timeout (t);
4523 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4526 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4527 send_prebuilt_message (message, t->next_hop, t);
4534 * Functions to handle messages from core
4536 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4537 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4538 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4539 sizeof (struct GNUNET_MESH_PathBroken)},
4540 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4541 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4542 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4543 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4544 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4545 sizeof (struct GNUNET_MESH_DataACK)},
4546 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4547 sizeof (struct GNUNET_MESH_DataACK)},
4548 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4549 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4550 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4551 sizeof (struct GNUNET_MESH_ACK)},
4552 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4553 sizeof (struct GNUNET_MESH_Poll)},
4554 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4555 sizeof (struct GNUNET_MESH_PathACK)},
4561 /******************************************************************************/
4562 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4563 /******************************************************************************/
4568 * notify_client_connection_failure: notify a client that the connection to the
4569 * requested remote peer is not possible (for instance, no route found)
4570 * Function called when the socket is ready to queue more data. "buf" will be
4571 * NULL and "size" zero if the socket was closed for writing in the meantime.
4573 * @param cls closure
4574 * @param size number of bytes available in buf
4575 * @param buf where the callee should write the message
4576 * @return number of bytes written to buf
4579 notify_client_connection_failure (void *cls, size_t size, void *buf)
4582 struct MeshPeerInfo *peer_info;
4583 struct GNUNET_MESH_PeerControl *msg;
4584 struct GNUNET_PeerIdentity id;
4586 if (0 == size && NULL == buf)
4588 // TODO retry? cancel?
4592 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4593 peer_info = (struct MeshPeerInfo *) cls;
4594 msg = (struct GNUNET_MESH_PeerControl *) buf;
4595 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4596 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4597 // msg->tunnel_id = htonl(peer_info->t->tid);
4598 GNUNET_PEER_resolve (peer_info->id, &id);
4599 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4607 * Send keepalive packets for a tunnel.
4609 * @param cls Closure (tunnel for which to send the keepalive).
4610 * @param tc Notification context.
4612 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4615 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4617 struct MeshTunnel *t = cls;
4618 struct GNUNET_MESH_TunnelKeepAlive *msg;
4619 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4622 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4623 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4624 NULL == t->owner || 0 == t->local_tid)
4629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4630 "sending keepalive for tunnel %d\n", t->id.tid);
4632 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4633 msg->header.size = htons (size);
4634 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4635 msg->oid = my_full_id;
4636 msg->tid = htonl (t->id.tid);
4637 send_prebuilt_message (&msg->header, t->next_hop, t);
4639 t->maintenance_task =
4640 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4645 * Function to process paths received for a new peer addition. The recorded
4646 * paths form the initial tunnel, which can be optimized later.
4647 * Called on each result obtained for the DHT search.
4649 * @param cls closure
4650 * @param exp when will this value expire
4651 * @param key key of the result
4652 * @param get_path path of the get request
4653 * @param get_path_length lenght of get_path
4654 * @param put_path path of the put request
4655 * @param put_path_length length of the put_path
4656 * @param type type of the result
4657 * @param size number of bytes in data
4658 * @param data pointer to the result data
4661 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4662 const struct GNUNET_HashCode * key,
4663 const struct GNUNET_PeerIdentity *get_path,
4664 unsigned int get_path_length,
4665 const struct GNUNET_PeerIdentity *put_path,
4666 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4667 size_t size, const void *data)
4669 struct MeshPeerInfo *peer = cls;
4670 struct MeshPeerPath *p;
4671 struct GNUNET_PeerIdentity pi;
4674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4675 GNUNET_PEER_resolve (peer->id, &pi);
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4678 p = path_build_from_dht (get_path, get_path_length,
4679 put_path, put_path_length);
4680 path_add_to_peers (p, GNUNET_NO);
4682 for (i = 0; i < peer->ntunnels; i++)
4684 struct GNUNET_PeerIdentity id;
4686 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4688 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4689 peer->tunnels[i]->local_tid,
4690 peer->tunnels[i]->local_tid_dest);
4691 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4694 peer_connect (peer, peer->tunnels[i]);
4702 /******************************************************************************/
4703 /********************* MESH LOCAL HANDLES **************************/
4704 /******************************************************************************/
4708 * Handler for client connection.
4710 * @param cls Closure (unused).
4711 * @param client Client handler.
4714 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4716 struct MeshClient *c;
4720 c = GNUNET_malloc (sizeof (struct MeshClient));
4722 GNUNET_SERVER_client_keep (client);
4723 GNUNET_SERVER_client_set_user_context (client, c);
4724 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4729 * Handler for client disconnection
4731 * @param cls closure
4732 * @param client identification of the client; NULL
4733 * for the last call when the server is destroyed
4736 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4738 struct MeshClient *c;
4739 struct MeshClient *next;
4741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4748 c = client_get (client);
4751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4753 GNUNET_SERVER_client_drop (c->handle);
4754 c->shutting_down = GNUNET_YES;
4755 if (NULL != c->own_tunnels)
4757 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4758 &tunnel_destroy_iterator, c);
4759 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4762 if (NULL != c->incoming_tunnels)
4764 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4765 &tunnel_destroy_iterator, c);
4766 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4769 if (NULL != c->ports)
4770 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4772 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4775 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4780 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4788 * Handler for new clients
4790 * @param cls closure
4791 * @param client identification of the client
4792 * @param message the actual message, which includes messages the client wants
4795 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4796 const struct GNUNET_MessageHeader *message)
4798 struct GNUNET_MESH_ClientConnect *cc_msg;
4799 struct MeshClient *c;
4804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4806 /* Check data sanity */
4807 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4808 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4809 if (0 != (size % sizeof (uint32_t)))
4812 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4815 size /= sizeof (uint32_t);
4817 /* Initialize new client structure */
4818 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4819 c->id = next_client_id++; /* overflow not important: just for debug */
4820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4826 p = (uint32_t *) &cc_msg[1];
4827 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4828 for (i = 0; i < size; i++)
4831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4833 /* store in client's hashmap */
4834 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4835 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4836 /* store in global hashmap */
4837 /* FIXME only allow one client to have the port open,
4838 * have a backup hashmap with waiting clients */
4839 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4840 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4844 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4845 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4846 GNUNET_SERVER_notification_context_add (nc, client);
4847 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4849 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4855 * Handler for requests of new tunnels
4857 * @param cls Closure.
4858 * @param client Identification of the client.
4859 * @param message The actual message.
4862 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4863 const struct GNUNET_MessageHeader *message)
4865 struct GNUNET_MESH_TunnelMessage *t_msg;
4866 struct MeshPeerInfo *peer_info;
4867 struct MeshTunnel *t;
4868 struct MeshClient *c;
4869 MESH_TunnelNumber tid;
4871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4873 /* Sanity check for client registration */
4874 if (NULL == (c = client_get (client)))
4877 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4882 /* Message size sanity check */
4883 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4890 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4892 GNUNET_i2s (&t_msg->peer));
4893 /* Sanity check for tunnel numbering */
4894 tid = ntohl (t_msg->tunnel_id);
4895 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4898 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4901 /* Sanity check for duplicate tunnel IDs */
4902 if (NULL != tunnel_get_by_local_id (c, tid))
4905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4910 while (NULL != tunnel_get_by_pi (myid, next_tid))
4911 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4912 t = tunnel_new (myid, next_tid, c, tid);
4913 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4920 t->port = ntohl (t_msg->port);
4921 tunnel_set_options (t, ntohl (t_msg->opt));
4922 if (GNUNET_YES == t->reliable)
4924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4925 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4927 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4931 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4933 peer_info = peer_get (&t_msg->peer);
4934 peer_info_add_tunnel (peer_info, t);
4935 peer_connect (peer_info, t);
4936 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4942 * Handler for requests of deleting tunnels
4944 * @param cls closure
4945 * @param client identification of the client
4946 * @param message the actual message
4949 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4950 const struct GNUNET_MessageHeader *message)
4952 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4953 struct MeshClient *c;
4954 struct MeshTunnel *t;
4955 MESH_TunnelNumber tid;
4957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4958 "Got a DESTROY TUNNEL from client!\n");
4960 /* Sanity check for client registration */
4961 if (NULL == (c = client_get (client)))
4964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4969 /* Message sanity check */
4970 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4977 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4979 /* Retrieve tunnel */
4980 tid = ntohl (tunnel_msg->tunnel_id);
4981 t = tunnel_get_by_local_id(c, tid);
4984 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4986 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4990 /* Cleanup after the tunnel */
4991 client_delete_tunnel (c, t);
4998 peer_info_remove_tunnel (peer_get_short (t->dest), t);
5002 /* The tunnel will be destroyed when the last message is transmitted. */
5003 tunnel_destroy_empty (t);
5005 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5011 * Handler for client traffic
5013 * @param cls closure
5014 * @param client identification of the client
5015 * @param message the actual message
5018 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
5019 const struct GNUNET_MessageHeader *message)
5021 struct GNUNET_MESH_LocalData *data_msg;
5022 struct MeshClient *c;
5023 struct MeshTunnel *t;
5024 struct MeshFlowControl *fc;
5025 MESH_TunnelNumber tid;
5028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5029 "Got data from a client!\n");
5031 /* Sanity check for client registration */
5032 if (NULL == (c = client_get (client)))
5035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5040 data_msg = (struct GNUNET_MESH_LocalData *) message;
5042 /* Sanity check for message size */
5043 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
5044 if (size < sizeof (struct GNUNET_MessageHeader))
5047 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5051 /* Tunnel exists? */
5052 tid = ntohl (data_msg->tid);
5053 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
5056 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5059 t = tunnel_get_by_local_id (c, tid);
5063 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5067 /* Is the client in the tunnel? */
5068 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5070 t->owner->handle == client)
5072 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5074 t->client->handle == client) ) )
5077 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5081 /* Ok, everything is correct, send the message
5082 * (pretend we got it from a mesh peer)
5085 struct GNUNET_MESH_Data *payload;
5086 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5088 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5089 if (GNUNET_YES == t->reliable)
5091 struct MeshTunnelReliability *rel;
5092 struct MeshReliableMessage *copy;
5094 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5095 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5096 + sizeof(struct GNUNET_MESH_Data)
5098 copy->mid = rel->mid_sent++;
5099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %llu\n", copy->mid);
5100 copy->timestamp = GNUNET_TIME_absolute_get ();
5103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
5104 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5105 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5108 GNUNET_TIME_relative_multiply (rel->expected_delay,
5109 MESH_RETRANSMIT_MARGIN);
5111 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5112 &tunnel_retransmit_message,
5115 payload = (struct GNUNET_MESH_Data *) ©[1];
5116 payload->mid = GNUNET_htonll (copy->mid);
5120 payload = (struct GNUNET_MESH_Data *) cbuf;
5122 // FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
5123 // use different struct for unreliable traffic, save 8 bytes
5125 memcpy (&payload[1], &data_msg[1], size);
5126 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5127 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5128 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5129 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5130 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5131 payload->tid = htonl (t->id.tid);
5132 payload->ttl = htonl (default_ttl);
5133 payload->pid = htonl (fc->last_pid_recv + 1);
5134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5135 " calling generic handler...\n");
5136 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5137 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5139 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5142 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5149 * Handler for client's ACKs for payload traffic.
5151 * @param cls Closure (unused).
5152 * @param client Identification of the client.
5153 * @param message The actual message.
5156 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5157 const struct GNUNET_MessageHeader *message)
5159 struct GNUNET_MESH_LocalAck *msg;
5160 struct MeshTunnel *t;
5161 struct MeshClient *c;
5162 MESH_TunnelNumber tid;
5164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5165 /* Sanity check for client registration */
5166 if (NULL == (c = client_get (client)))
5169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5174 msg = (struct GNUNET_MESH_LocalAck *) message;
5176 /* Tunnel exists? */
5177 tid = ntohl (msg->tunnel_id);
5178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5179 t = tunnel_get_by_local_id (c, tid);
5183 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5184 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5185 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5189 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5192 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5193 t->prev_fc.last_ack_recv++;
5194 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5198 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5199 t->next_fc.last_ack_recv++;
5200 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5203 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5211 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5213 * @param cls Closure (client handle).
5214 * @param key Key (hashed tunnel ID, unused).
5215 * @param value Tunnel info.
5217 * @return GNUNET_YES, to keep iterating.
5220 monitor_all_tunnels_iterator (void *cls,
5221 const struct GNUNET_HashCode * key,
5224 struct GNUNET_SERVER_Client *client = cls;
5225 struct MeshTunnel *t = value;
5226 struct GNUNET_MESH_LocalMonitor *msg;
5228 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5229 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5230 msg->tunnel_id = htonl (t->id.tid);
5231 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5232 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5233 GNUNET_PEER_resolve (t->dest, &msg->destination);
5235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5236 "* sending info about tunnel %s [%u]\n",
5237 GNUNET_i2s (&msg->owner), t->id.tid);
5239 GNUNET_SERVER_notification_context_unicast (nc, client,
5240 &msg->header, GNUNET_NO);
5246 * Handler for client's MONITOR request.
5248 * @param cls Closure (unused).
5249 * @param client Identification of the client.
5250 * @param message The actual message.
5253 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5254 const struct GNUNET_MessageHeader *message)
5256 struct MeshClient *c;
5258 /* Sanity check for client registration */
5259 if (NULL == (c = client_get (client)))
5262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5266 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5267 "Received get tunnels request from client %u\n",
5269 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5270 monitor_all_tunnels_iterator,
5272 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5273 "Get tunnels request from client %u completed\n",
5275 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5280 * Handler for client's MONITOR_TUNNEL request.
5282 * @param cls Closure (unused).
5283 * @param client Identification of the client.
5284 * @param message The actual message.
5287 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5288 const struct GNUNET_MessageHeader *message)
5290 const struct GNUNET_MESH_LocalMonitor *msg;
5291 struct GNUNET_MESH_LocalMonitor *resp;
5292 struct MeshClient *c;
5293 struct MeshTunnel *t;
5295 /* Sanity check for client registration */
5296 if (NULL == (c = client_get (client)))
5299 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5303 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5304 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5305 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5308 ntohl (msg->tunnel_id));
5309 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5312 /* We don't know the tunnel FIXME */
5313 struct GNUNET_MESH_LocalMonitor warn;
5316 GNUNET_SERVER_notification_context_unicast (nc, client,
5319 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5323 /* Initialize context */
5324 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5326 GNUNET_PEER_resolve (t->dest, &resp->destination);
5327 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5328 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5329 &resp->header, GNUNET_NO);
5332 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5333 "Monitor tunnel request from client %u completed\n",
5335 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5340 * Functions to handle messages from clients
5342 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5343 {&handle_local_new_client, NULL,
5344 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5345 {&handle_local_tunnel_create, NULL,
5346 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5347 sizeof (struct GNUNET_MESH_TunnelMessage)},
5348 {&handle_local_tunnel_destroy, NULL,
5349 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5350 sizeof (struct GNUNET_MESH_TunnelMessage)},
5351 {&handle_local_data, NULL,
5352 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5353 {&handle_local_ack, NULL,
5354 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5355 sizeof (struct GNUNET_MESH_LocalAck)},
5356 {&handle_local_get_tunnels, NULL,
5357 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5358 sizeof (struct GNUNET_MessageHeader)},
5359 {&handle_local_show_tunnel, NULL,
5360 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5361 sizeof (struct GNUNET_MESH_LocalMonitor)},
5367 * Method called whenever a given peer connects.
5369 * @param cls closure
5370 * @param peer peer identity this notification is about
5373 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5375 struct MeshPeerInfo *peer_info;
5376 struct MeshPeerPath *path;
5378 DEBUG_CONN ("Peer connected\n");
5379 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5380 peer_info = peer_get (peer);
5381 if (myid == peer_info->id)
5383 DEBUG_CONN (" (self)\n");
5384 path = path_new (1);
5388 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5389 path = path_new (2);
5390 path->peers[1] = peer_info->id;
5391 GNUNET_PEER_change_rc (peer_info->id, 1);
5392 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5394 path->peers[0] = myid;
5395 GNUNET_PEER_change_rc (myid, 1);
5396 peer_info_add_path (peer_info, path, GNUNET_YES);
5402 * Method called whenever a peer disconnects.
5404 * @param cls closure
5405 * @param peer peer identity this notification is about
5408 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5410 struct MeshPeerInfo *pi;
5411 struct MeshPeerQueue *q;
5412 struct MeshPeerQueue *n;
5414 DEBUG_CONN ("Peer disconnected\n");
5415 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5425 /* TODO try to reroute this traffic instead */
5426 queue_destroy(q, GNUNET_YES);
5429 if (NULL != pi->core_transmit)
5431 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5432 pi->core_transmit = NULL;
5434 peer_remove_path (pi, pi->id, myid);
5437 DEBUG_CONN (" (self)\n");
5439 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5445 * Install server (service) handlers and start listening to clients.
5450 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5451 GNUNET_SERVER_connect_notify (server_handle,
5452 &handle_local_client_connect, NULL);
5453 GNUNET_SERVER_disconnect_notify (server_handle,
5454 &handle_local_client_disconnect, NULL);
5455 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5457 clients_head = NULL;
5458 clients_tail = NULL;
5460 GNUNET_SERVER_resume (server_handle);
5465 * To be called on core init/fail.
5467 * @param cls Closure (config)
5468 * @param server handle to the server for this service
5469 * @param identity the public identity of this peer
5472 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5473 const struct GNUNET_PeerIdentity *identity)
5475 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5479 GNUNET_break (core_handle == server);
5480 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5486 GNUNET_i2s (identity));
5487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5489 GNUNET_i2s (&my_full_id));
5490 GNUNET_CORE_disconnect (core_handle);
5491 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5492 NULL, /* Closure passed to MESH functions */
5493 &core_init, /* Call core_init once connected */
5494 &core_connect, /* Handle connects */
5495 &core_disconnect, /* remove peers on disconnects */
5496 NULL, /* Don't notify about all incoming messages */
5497 GNUNET_NO, /* For header only in notification */
5498 NULL, /* Don't notify about all outbound messages */
5499 GNUNET_NO, /* For header-only out notification */
5500 core_handlers); /* Register these handlers */
5509 /******************************************************************************/
5510 /************************ MAIN FUNCTIONS ****************************/
5511 /******************************************************************************/
5514 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5516 * @param cls closure
5517 * @param key current key code
5518 * @param value value in the hash map
5519 * @return GNUNET_YES if we should continue to iterate,
5523 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5525 struct MeshTunnel *t = value;
5532 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5534 * @param cls closure
5535 * @param key current key code
5536 * @param value value in the hash map
5537 * @return GNUNET_YES if we should continue to iterate,
5541 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5543 struct MeshPeerInfo *p = value;
5544 struct MeshPeerQueue *q;
5545 struct MeshPeerQueue *n;
5553 queue_destroy(q, GNUNET_YES);
5557 peer_info_destroy (p);
5563 * Task run during shutdown.
5569 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5573 if (core_handle != NULL)
5575 GNUNET_CORE_disconnect (core_handle);
5578 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5579 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5580 if (dht_handle != NULL)
5582 GNUNET_DHT_disconnect (dht_handle);
5587 GNUNET_SERVER_notification_context_destroy (nc);
5590 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5592 GNUNET_SCHEDULER_cancel (announce_id_task);
5593 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5600 * Process mesh requests.
5602 * @param cls closure
5603 * @param server the initialized server
5604 * @param c configuration to use
5607 run (void *cls, struct GNUNET_SERVER_Handle *server,
5608 const struct GNUNET_CONFIGURATION_Handle *c)
5611 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5614 server_handle = server;
5615 GNUNET_SERVER_suspend (server_handle);
5618 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5623 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5624 "mesh", "peer/privatekey");
5625 GNUNET_SCHEDULER_shutdown ();
5630 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5631 &refresh_path_time))
5633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5635 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5636 "mesh", "refresh path time");
5637 GNUNET_SCHEDULER_shutdown ();
5642 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5647 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5648 "mesh", "id announce time");
5649 GNUNET_SCHEDULER_shutdown ();
5654 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5659 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5660 "mesh", "connect timeout");
5661 GNUNET_SCHEDULER_shutdown ();
5666 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5669 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5671 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5672 "mesh", "max msgs queue");
5673 GNUNET_SCHEDULER_shutdown ();
5678 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5683 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5684 "mesh", "max tunnels");
5685 GNUNET_SCHEDULER_shutdown ();
5690 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5693 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5695 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5696 "mesh", "default ttl", 64);
5701 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5704 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5705 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5706 "mesh", "max peers", 1000);
5711 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5718 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5719 "Mesh is running with drop mode enabled. "
5720 "This is NOT a good idea! "
5721 "Remove the DROP_PERCENT option from your configuration.\n");
5725 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5726 &dht_replication_level))
5728 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5730 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5731 "mesh", "dht replication level", 3);
5732 dht_replication_level = 3;
5735 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5736 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5737 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5738 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5740 dht_handle = GNUNET_DHT_connect (c, 64);
5741 if (NULL == dht_handle)
5745 stats = GNUNET_STATISTICS_create ("mesh", c);
5747 /* Scheduled the task to clean up when shutdown is called */
5748 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5750 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5751 GNUNET_free (keyfile);
5752 GNUNET_assert (NULL != pk);
5753 my_private_key = pk;
5754 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5755 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5756 &my_full_id.hashPubKey);
5757 myid = GNUNET_PEER_intern (&my_full_id);
5758 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5759 "Mesh for peer [%s] starting\n",
5760 GNUNET_i2s(&my_full_id));
5762 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5763 NULL, /* Closure passed to MESH functions */
5764 &core_init, /* Call core_init once connected */
5765 &core_connect, /* Handle connects */
5766 &core_disconnect, /* remove peers on disconnects */
5767 NULL, /* Don't notify about all incoming messages */
5768 GNUNET_NO, /* For header only in notification */
5769 NULL, /* Don't notify about all outbound messages */
5770 GNUNET_NO, /* For header-only out notification */
5771 core_handlers); /* Register these handlers */
5772 if (NULL == core_handle)
5775 GNUNET_SCHEDULER_shutdown ();
5779 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5780 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5786 * The main function for the mesh service.
5788 * @param argc number of arguments from the command line
5789 * @param argv command line arguments
5790 * @return 0 ok, 1 on error
5793 main (int argc, char *const *argv)
5798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5799 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5801 ret = (GNUNET_OK == r) ? 0 : 1;
5802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5806 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5807 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5808 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);